A socket I/O selector.
More...
#include <SocketSelector.h++>
|
static void | exit () |
| Exit (terminate) the calling thread. More...
|
|
A socket I/O selector.
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 selector. A selector must run in its own thread.
- Author
- Mark Lindner
◆ SocketSelector()
Construct a new SocketSelector.
- Parameters
-
maxConnections | The maximum number of connections that the selector 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. |
◆ ~SocketSelector()
Destructor.
Closes and destroys all active connections.
◆ cleanup()
Cleanup function.
This function will be executed by the thread just prior to termination. The default implementation is a no-op.
Reimplemented from Thread.
◆ connectionClosed()
virtual void connectionClosed |
( |
Connection * |
connection | ) |
|
|
protectedpure 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. |
◆ connectionReady()
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).
◆ connectionTimedOut()
virtual void connectionTimedOut |
( |
Connection * |
connection | ) |
|
|
protectedpure 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. |
◆ currentThread()
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.
◆ currentThreadID()
Get the calling thread's thread ID.
◆ dataReceived()
virtual void dataReceived |
( |
Connection * |
connection | ) |
|
|
protectedpure 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. |
◆ dataReceivedOOB()
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. |
◆ dataSent()
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. |
◆ exceptionOccurred()
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. |
◆ exit()
Exit (terminate) the calling thread.
◆ getConnectionCount()
size_t getConnectionCount |
( |
| ) |
const |
Get the count of currently active connections.
◆ getName()
Get the name of this thread.
◆ getPriority()
◆ init()
Initialize the selector with the given server socket.
The selector 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.
◆ isDetached()
bool isDetached |
( |
| ) |
const |
|
inlineinherited |
Test if the thread is detached.
◆ isRunning()
Test if the thread is currently running.
◆ join()
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.
◆ run()
Main function.
Thread execution begins in this function. The default implementation does nothing.
Reimplemented from Thread.
◆ setName()
void setName |
( |
const String & |
name | ) |
|
|
inlineinherited |
Set the name of this thread.
◆ setPriority()
◆ sleep()
Suspend the calling thread for the given time interval.
- Parameters
-
msec | The number of milliseconds to sleep. |
◆ start()
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.
◆ stop()
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.
◆ testCancel()
Test for asynchronous cancellation requests.
- Returns
- true If a cancellation request has been posted to the calling thread.
◆ trySleep()
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.
◆ wakeup()
Wake up the selector.
This should be called if new data has been enqueued for sending on one of the selector's connections.
◆ writeAll()
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.
◆ yield()
Yield the CPU to (potentially) another thread.
◆ _connections
ConnectionList* _connections |
|
protected |
The list of active connections.
The documentation for this class was generated from the following files: