libcommonc++  0.7
SocketSelector Class Referenceabstract

A socket I/O selector. More...

#include <SocketSelector.h++>

Inheritance diagram for SocketSelector:
Collaboration diagram for SocketSelector:

Public Member Functions

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

Static Public Member Functions

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

Protected Member Functions

virtual ConnectionconnectionReady (const SocketAddress &address)=0
 This method is called when a new connection is accepted. More...
 
virtual void dataReceived (Connection *connection)=0
 This method is called when data has been received on the connection. More...
 
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. More...
 
virtual void dataReceivedOOB (Connection *connection)
 This method is called when an out-of-band data byte has been received on the connection. More...
 
virtual void connectionClosed (Connection *connection)=0
 This method is called when a connection is closed, either by request or because the remote peer disconnected. More...
 
virtual void connectionTimedOut (Connection *connection)=0
 This method is called when a connection is closed because it timed out due to inactivity. More...
 
virtual void exceptionOccurred (Connection *connection, const IOException &ex)
 This method is called when an exception occurs during I/O. More...
 
bool trySleep (timespan_ms_t msec)
 Suspend the thread for the given time interval, returning early if the thread was cancelled via a call to stop(). More...
 
bool testCancel ()
 Test for asynchronous cancellation requests. More...
 
void yield ()
 Yield the CPU to (potentially) another thread. More...
 

Static Protected Member Functions

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

Protected Attributes

ConnectionList * _connections
 The list of active connections. More...
 

Detailed Description

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

Constructor & Destructor Documentation

◆ SocketSelector()

SocketSelector ( uint_t  maxConnections = 64,
timespan_ms_t  defaultIdleLimit = 0 
)

Construct a new SocketSelector.

Parameters
maxConnectionsThe maximum number of connections that the selector should manage.
defaultIdleLimitThe 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()

~SocketSelector ( )
virtual

Destructor.

Closes and destroys all active connections.

Member Function Documentation

◆ cleanup()

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.

◆ 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
connectionThe connection.

◆ connectionReady()

virtual Connection* connectionReady ( const SocketAddress address)
protectedpure 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
addressThe 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
connectionThe connection.

◆ currentThread()

Thread * currentThread ( )
staticinherited

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()

ccxx::ThreadID currentThreadID ( )
staticinherited

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
connectionThe connection.

◆ dataReceivedOOB()

void dataReceivedOOB ( Connection connection)
protectedvirtual

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

The default implementation does nothing.

Parameters
connectionThe connection.

◆ dataSent()

void dataSent ( Connection connection)
protectedvirtual

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
connectionThe connection.

◆ exceptionOccurred()

void exceptionOccurred ( Connection connection,
const IOException ex 
)
protectedvirtual

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

The default implementation closes the connection.

Parameters
connectionThe connection.
exThe exception that occurred.

◆ exit()

void exit ( )
staticprotectedinherited

Exit (terminate) the calling thread.

◆ getConnectionCount()

size_t getConnectionCount ( ) const

Get the count of currently active connections.

◆ getName()

String getName ( ) const
inlineinherited

Get the name of this thread.

◆ getPriority()

Priority getPriority ( ) const
inherited

Get the thread priority.

◆ init()

bool init ( ServerSocket socket)
virtual

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
socketThe 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()

bool isRunning ( ) const
inherited

Test if the thread is currently running.

◆ join()

bool join ( )
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.

◆ run()

void run ( )
virtual

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()

void setPriority ( Priority  priority)
inherited

Set the thread priority.

◆ sleep()

void sleep ( timespan_ms_t  msec)
staticinherited

Suspend the calling thread for the given time interval.

Parameters
msecThe number of milliseconds to sleep.

◆ start()

void start ( )
virtualinherited

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()

void stop ( )
virtualinherited

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()

bool testCancel ( )
protectedinherited

Test for asynchronous cancellation requests.

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

◆ trySleep()

bool trySleep ( timespan_ms_t  msec)
protectedinherited

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

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

◆ wakeup()

void 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()

uint_t writeAll ( const byte_t buf,
size_t  count 
)

Write a block of data to all active connections.

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

◆ yield()

void yield ( )
protectedinherited

Yield the CPU to (potentially) another thread.

Member Data Documentation

◆ _connections

ConnectionList* _connections
protected

The list of active connections.


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