#include <SocketMuxer.h++>


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 Thread * | currentThread () |
| 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 Connection * | connectionReady (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. | |
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.
| SocketMuxer | ( | uint_t | maxConnections = 64, |
|
| uint_t | defaultIdleLimit = 0, |
|||
| uint_t | sleepInterval = 100 | |||
| ) |
Construct a new SocketMuxer.
| 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.
| 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.
| 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.
| address | The address of the remote peer. |
| 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.
| 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.
| 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.
| 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.
| 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.
| 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.
| socket | The socket to listen on for new connections. The socket must have already been initialized and put 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.
| void run | ( | ) | [virtual] |
| 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.
| msec | The number of milliseconds to sleep. |
| void start | ( | ) | throw () [virtual, inherited] |
| 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.
| 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().
| msec | The number of milliseconds to sleep. |
Write a block of data to all active connections.
| buf | The buffer containing the data to be sent. | |
| count | The number of elements to write. |
| void yield | ( | ) | throw () [protected, inherited] |
Yield the CPU to (potentially) another thread.
ConnectionList* _connections [protected] |
The list of active connections.
1.5.9