SocketMuxer Class Reference

A socket I/O multiplexer. More...

#include <SocketMuxer.h++>

Inheritance diagram for SocketMuxer:

Inheritance graph
[legend]
Collaboration diagram for SocketMuxer:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 SocketMuxer (uint_t maxConnections=64, uint_t defaultIdleLimit=0, uint_t sleepInterval=100)
 Construct a new SocketMuxer.
virtual ~SocketMuxer () throw ()
 Destructor.
void run ()
 Main function.
void cleanup ()
 Cleanup function.
size_t getConnectionCount () const
 Get the count of currently active connections.
virtual bool init (ServerSocket *socket)
 Initialize the muxer with the given server socket.
uint_t writeAll (const byte_t *buf, size_t count)
 Write a block of data to all active connections.
virtual void start () throw ()
 Start executing the thread.
virtual void stop () throw ()
 Stop execution of the thread.
bool join () throw ()
 Wait for the thread to terminate.
bool isRunning () const throw ()
 Test if the thread is currently running.
bool isDetached () const throw ()
 Test if the thread is detached.
void setPriority (Priority priority) throw ()
 Set the thread priority.
Priority getPriority () const throw ()
 Get the thread priority.
void setName (const String &name) throw ()
 Set the name of this thread.
String getName () const throw ()
 Get the name of this thread.

Static Public Member Functions

static void sleep (uint_t msec) throw ()
 Suspend the calling thread for the given time interval.
static ThreadcurrentThread ()
 Obtain a pointer to the Thread object for the calling thread.
static ThreadID currentThreadID () throw ()
 Get the calling thread's thread ID.

Protected Member Functions

virtual ConnectionconnectionReady (const SocketAddress &address)=0
 This method is called when a new connection is accepted.
virtual void dataReceived (Connection *connection)=0
 This method is called when data has been received on the connection.
virtual void dataSent (Connection *connection)
 This method is called when the amount of data that is queued to be sent on the connection is less than or equal to the write low-water mark for the connection.
virtual void dataReceivedOOB (Connection *connection)
 This method is called when an out-of-band data byte has been received on the connection.
virtual void connectionClosed (Connection *connection)=0
 This method is called when a connection is closed, either by request or because the remote peer disconnected.
virtual void connectionTimedOut (Connection *connection)=0
 This method is called when a connection is closed because it timed out due to inactivity.
virtual void exceptionOccurred (Connection *connection, const IOException &ex)
 This method is called when an exception occurs during I/O.
bool trySleep (uint_t msec) throw ()
 Suspend the thread for the given time interval, returning early if the thread was cancelled via a call to stop().
bool testCancel () throw ()
 Test for asynchronous cancellation requests.
void yield () throw ()
 Yield the CPU to (potentially) another thread.

Static Protected Member Functions

static void exit () throw ()
 Exit (terminate) the calling thread.

Protected Attributes

ConnectionList * _connections
 The list of active connections.


Detailed Description

A socket I/O multiplexer.

This class is built around the Reactor pattern; the various connection event handlers are called when the corresponding I/O events occur on the sockets being managed by the muxer. A muxer must run in its own thread.

Author:
Mark Lindner

Constructor & Destructor Documentation

SocketMuxer ( uint_t  maxConnections = 64,
uint_t  defaultIdleLimit = 0,
uint_t  sleepInterval = 100 
)

Construct a new SocketMuxer.

Parameters:
maxConnections The maximum number of connections that the muxer should manage.
defaultIdleLimit The default idle limit for connections, in milliseconds. Connections that exceed their idle limit will be closed automatically. A value of 0 indicates no idle limit.
sleepInterval The maximum amount of time, in milliseconds, to wait in the I/O loop for one or more connections to become ready for reading or writing.

~SocketMuxer (  )  throw () [virtual]

Destructor.

Closes and destroys all active connections.


Member Function Documentation

void cleanup (  )  [virtual]

Cleanup function.

This function will be executed by the thread just prior to termination. The default implementation is a no-op.

Reimplemented from Thread.

virtual void connectionClosed ( Connection connection  )  [protected, pure virtual]

This method is called when a connection is closed, either by request or because the remote peer disconnected.

The method should delete the connection object before returning.

Parameters:
connection The connection.

virtual Connection* connectionReady ( const SocketAddress address  )  [protected, pure virtual]

This method is called when a new connection is accepted.

It must construct and return a new Connection object representing the connection, or NULL if the connection should be denied.

Parameters:
address The address of the remote peer.
Returns:
A new Connection object for this connection, or NULL if the connection should be closed (rejected).

virtual void connectionTimedOut ( Connection connection  )  [protected, pure virtual]

This method is called when a connection is closed because it timed out due to inactivity.

The method should delete the connection object before returning.

Parameters:
connection The connection.

Thread * currentThread (  )  [static, inherited]

Obtain a pointer to the Thread object for the calling thread.

If the calling thread is the main thread, or some other thread that was not created via commonc++, a NULL pointer is returned. Never delete the object returned by this method.

ccxx::ThreadID currentThreadID (  )  throw () [static, inherited]

Get the calling thread's thread ID.

virtual void dataReceived ( Connection connection  )  [protected, pure virtual]

This method is called when data has been received on the connection.

It is called only when the total number of bytes available to read is at least equal to the value of the read low-water mark for the connection.

Parameters:
connection The connection.

void dataReceivedOOB ( Connection connection  )  [protected, virtual]

This method is called when an out-of-band data byte has been received on the connection.

The default implementation does nothing.

Parameters:
connection The connection.

void dataSent ( Connection connection  )  [protected, virtual]

This method is called when the amount of data that is queued to be sent on the connection is less than or equal to the write low-water mark for the connection.

This is an indicator to the application that it can now send more data. The default implementation does nothing.

Parameters:
connection The connection.

void exceptionOccurred ( Connection connection,
const IOException ex 
) [protected, virtual]

This method is called when an exception occurs during I/O.

The default implementation closes the connection.

Parameters:
connection The connection.
ex The exception that occurred.

void exit (  )  throw () [static, protected, inherited]

Exit (terminate) the calling thread.

size_t getConnectionCount (  )  const

Get the count of currently active connections.

String getName (  )  const throw () [inline, inherited]

Get the name of this thread.

Priority getPriority (  )  const throw () [inherited]

Get the thread priority.

bool init ( ServerSocket socket  )  [virtual]

Initialize the muxer with the given server socket.

The muxer will accept new connections on the server socket and add them to its list of managed connections.

Parameters:
socket The socket to listen on for new connections. The socket must have already been initialized and put in a listening state.
Returns:
true on success, false if the socket is not in a listening state.

bool isDetached (  )  const throw () [inline, inherited]

Test if the thread is detached.

bool isRunning (  )  const throw () [inherited]

Test if the thread is currently running.

bool join (  )  throw () [inherited]

Wait for the thread to terminate.

Returns:
true if the join was successful, false otherwise. Note that threads which were created in a detached state are not joinable.

void run (  )  [virtual]

Main function.

Thread execution begins in this function. The default implementation does nothing.

Reimplemented from Thread.

void setName ( const String name  )  throw () [inline, inherited]

Set the name of this thread.

void setPriority ( Priority  priority  )  throw () [inherited]

Set the thread priority.

void sleep ( uint_t  msec  )  throw () [static, inherited]

Suspend the calling thread for the given time interval.

Parameters:
msec The number of milliseconds to sleep.

void start (  )  throw () [virtual, inherited]

Start executing the thread.

If a Runnable object was supplied in the constructor, its run() method is invoked; otherwise the thread object's run() method is called. If the thread is already running, the call has no effect.

void stop (  )  throw () [virtual, inherited]

Stop execution of the thread.

The thread is not forcibly killed; instead, an asynchronous cancellation request is posted to the thread. If the thread is not running, the call has no effect.

bool testCancel (  )  throw () [protected, inherited]

Test for asynchronous cancellation requests.

Returns:
true If a cancellation request has been posted to the calling thread.

bool trySleep ( uint_t  msec  )  throw () [protected, inherited]

Suspend the thread for the given time interval, returning early if the thread was cancelled via a call to stop().

Parameters:
msec The number of milliseconds to sleep.
Returns:
true if the full interval elapsed, false if the thread was cancelled during the sleep.

uint_t writeAll ( const byte_t buf,
size_t  count 
)

Write a block of data to all active connections.

Parameters:
buf The buffer containing the data to be sent.
count The number of elements to write.
Returns:
The number of connections to which the data was successfully queued.

void yield (  )  throw () [protected, inherited]

Yield the CPU to (potentially) another thread.


Member Data Documentation

ConnectionList* _connections [protected]

The list of active connections.


The documentation for this class was generated from the following files:

Generated on Sat Apr 17 23:03:10 2010 for libcommonc++ by  doxygen 1.5.9