Wt::WSocketNotifier Class Reference

A utility class for asynchronous notification of socket activity. More...

#include <Wt/WSocketNotifier>

Inheritance diagram for Wt::WSocketNotifier:

Inheritance graph
[legend]

List of all members.

Public Types

enum  Type { Read, Write, Exception }
 Enumeration that event type. More...

Public Member Functions

 WSocketNotifier (int socket, Type type, WObject *parent=0)
 Create a new socket notifier.
 ~WSocketNotifier ()
 Delete a socket notifier.
int socket () const
 Return the socket.
Type type () const
 Return the event type.
void setEnabled (bool enabled)
 Enable or disable the notifier.
bool isEnabled () const
 Return if the notifier is enabled.
Signal< int > & activated ()
 Signal indicating an event.


Detailed Description

A utility class for asynchronous notification of socket activity.

Use a aocket notifier to integrate listening for socket events into the Wt event loop. In this way, you do not need a separate thread to listen for socket activity. Socket activity is either the availability of data to be read (Read event), possibility to write data (Write event), or an exception that occurred (Exception event).

When an event on a socket is available, the notifier emits the activated() signal. As in the case of a user interface event (like for example WInteractWidget::clicked()), you will typically modify the widget tree in response to the event. But, unless you use a timer (WTimer) or use server-initiated updates (see WApplication::triggerUpdates()), these changes are not propagated to the user interface, until the next user interface event.

Like other events, socket notification events are serial (not simultaneous), and there are no thread safety issues (you don't need to call WApplication::getUpdateLock() to get an update lock).

 Wt::WSocketNotifier *notifier_;

 void init() {
   ...
   int sock = ...
   notifier_ = new Wt::WSocketNotifier(sock, Wt::WSocketNotifier::Read, this);
   notifier_->activated().connect(SLOT(this, HelloApplication::readData));
 }

 void readData() {
   // data is available on socket, or socket was closed by peer
   char buf[100];
   int s = read(notifier_->socket(), buf, 99);

   if (s > 0) {
     ...
   } else {
     // closed by peer
     notifier_->setEnabled(false);
     close(notifier_->socket());
   }
 }

Note: At present, WSocketNotifier works only in conjunction with the wthttpd connector, and not when using the FastCGI connector.


Member Enumeration Documentation

Enumeration that event type.

Enumerator:
Read  Ready to read.
Write  Ready to write.
Exception  Exception.


Constructor & Destructor Documentation

Wt::WSocketNotifier::WSocketNotifier ( int  socket,
Type  type,
WObject parent = 0 
)

Create a new socket notifier.

Create a new socket listener to listen for events of given type on a socket with file descriptor socket.


Member Function Documentation

void Wt::WSocketNotifier::setEnabled ( bool  enabled  ) 

Enable or disable the notifier.

By default, the socket notifier is enabled to receive events. When disabled, no events will be notified (as if the socket notifier didn't exist).

Signal<int>& Wt::WSocketNotifier::activated (  )  [inline]

Signal indicating an event.

The signal is emitted when an event that was waited for is available. The signal argument is socket().


Generated on Tue Nov 3 15:34:33 2009 for Wt by doxygen 1.5.6