Home C-Bits Package Documentation Project Page

Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

cbits::ServerSocket Class Reference

This class represents a server-side socket endpoint that remote clients connect to in order to setup a network link to a server. More...

#include <ServerSocket.h>

Inheritance diagram for cbits::ServerSocket:

[legend]
Collaboration diagram for cbits::ServerSocket:
[legend]
List of all members.

Public Methods

 ServerSocket (const int port, const int qdepth=15) throw ( SocketException )
 Construct a cbits::ServerSocket instance that can accept client network connections on a specified local port. More...

virtual ~ServerSocket ()
 Destructor. More...

virtual Socketaccept (const long int msec=0) throw ( SocketException )
 Accept the next connection on this server socket. More...

void close ()
 Close the server socket. More...

socket_t getHandle () const
 Access the BSD socket descriptor. More...

const bool isError () const
 Determine if the server socket is an error condition. More...

const std::string & getError () const
 Get human readable description of the last error, if any. More...

const int getSockOpt (const int level, const int op_name, char *result, int *result_len) const
 Set a socket option on the server socket. More...

std::ostream * getLogger () const
 Access this ServerSocket's debug logging stream. More...

std::ostream * getLocalLogger () const
 Access this ServerSocket's individual logging stream, set via cbits::ServerSocket::setLocalLogger, if any. More...

void setLocalLogger (std::ostream *os) const
 Set the instance debug logging stream for this ServerSocket. More...

void closeLocalLogger () const
 Close this ServerSocket's individually set debug log stream, set via cbits::ServerSocket::setLocalLogger, if any. More...


Static Public Methods

std::ostream * getGlobalLogger ()
 Access the global debug logging stream for all ServerSocket instances that don't have an individual logging streams. More...

void setGlobalLogger (std::ostream *os)
 Set a global debug logging stream to be used by all ServerSocket instances. More...

void closeGlobalLogger ()
 Close the globally set debug log stream, set via cbits::ServerSocket::setGlobalLogger, if any. More...


Protected Methods

virtual const socket_t accept_h (const long int msec=0) throw ( SocketException )
 Low level method to accept the next client network connection. More...

Socketget_tcp_socket () const
 Get a reference to the cbits::Socket instance used by this ServerSocket to bind to the server port. More...


Protected Attributes

bool _error
 errno value of the last error that occurred. More...

std::string _errstr
 A human readable description of _error, if any. More...

std::ostream * _llogr
 Debug logging sream for this instance, if set. More...


Static Protected Attributes

std::ostream * _glogr = 0
 Global, default logging stream for all cbits::ServerSocket instances, if set. More...


Private Attributes

Socket_ssock
 The cbits::Socket instance used to bind to the local server port. More...

CriticalSection _read_csect
 Critical section object used to synchronize access to the error status of this ServerSocket. More...

CriticalSection _csect
 Critical section object used to prevent multiple threads from simultaneously calling cbits::ServerSocket::accept. More...


Detailed Description

This class represents a server-side socket endpoint that remote clients connect to in order to setup a network link to a server.

In practice, cbits::ServerSocket acts like a cbits::Socket factory, generating a separate instance of cbits::Socket (via cbits::ServerSocket::accept) for each client network connection it accepts.


Constructor & Destructor Documentation

ServerSocket::ServerSocket const int    port,
const int    qdepth = 15
throw ( SocketException )
 

Construct a cbits::ServerSocket instance that can accept client network connections on a specified local port.

Exceptions:
SocketException  if the server socket can not be created.
Parameters:
port  Port that clients will connect to.
qdepth  Max number of pending client connections. In most cases the default is adequate.

ServerSocket::~ServerSocket   [virtual]
 

Destructor.

Releases the server port and destroys this cbits::ServerSocket instance.


Member Function Documentation

Socket * ServerSocket::accept const long int    msec = 0 throw ( SocketException ) [virtual]
 

Accept the next connection on this server socket.

Returns:
A Socket instance representing a new client connection, or 0 if the call timed out.
Exceptions:
SocketException  if an error occurs on the underlying socket.
Parameters:
msec  The maximum number of milliseconds to wait for a client connection. A zero or negative number is interpreted to mean 'wait forever'.

Reimplemented in cbits::SSLServerSocket.

const socket_t ServerSocket::accept_h const long int    msec = 0 throw ( SocketException ) [protected, virtual]
 

Low level method to accept the next client network connection.

This method could be reimplemented in a derived class that does this differently (on a different platform, say).

Returns:
A socket handle representing a new client connection, or 0 if the maximum wait time elapsed.
Exceptions:
SocketException  if an error occurs on the socket.
Parameters:
mmsec  Maximum number of milliseconds to wait for a client connection if one is not immediately available. A negative or zero value is interpreted to mean 'wait forever'.

void ServerSocket::close  
 

Close the server socket.

void ServerSocket::closeGlobalLogger   [static]
 

Close the globally set debug log stream, set via cbits::ServerSocket::setGlobalLogger, if any.

Since the output stream supplied to cbits::ServerSocket::setGlobalLogger is externally created, the output stream itself is not closed by this action.

This action does not affect any cbits::Socket instances created by this ServerSocket.

void ServerSocket::closeLocalLogger   const
 

Close this ServerSocket's individually set debug log stream, set via cbits::ServerSocket::setLocalLogger, if any.

Since the output stream supplied to cbits::ServerSocket::setLocalLogger is externally created, the output stream itself is not closed by this action.

This action does not affect any cbits::Socket instances created by this ServerSocket.

Socket* cbits::ServerSocket::get_tcp_socket   const [inline, protected]
 

Get a reference to the cbits::Socket instance used by this ServerSocket to bind to the server port.

Returns:
A pointer to a cbits::Socket instance.

const std::string& cbits::ServerSocket::getError   const [inline]
 

Get human readable description of the last error, if any.

std::ostream * ServerSocket::getGlobalLogger   [static]
 

Access the global debug logging stream for all ServerSocket instances that don't have an individual logging streams.

This only accesses the logging stream that is used by ServerSocket instances that haven't been individually configured with their own logging stream via cbits::ServerSocket::setLocalLogger.

Returns:
pointer to std::ostream instance or 0 if no global logger has been set.

socket_t ServerSocket::getHandle   const
 

Access the BSD socket descriptor.

Returns:
BSD socket descriptor for the server socket.

std::ostream * ServerSocket::getLocalLogger   const
 

Access this ServerSocket's individual logging stream, set via cbits::ServerSocket::setLocalLogger, if any.

Returns:
a pointer to a std::ostream instance, or 0 if not set.

std::ostream * ServerSocket::getLogger   const
 

Access this ServerSocket's debug logging stream.

A pointer to a stream is always returned. Logging statements that use this method should perform runtime checks to determine if application debug logging is enabled.

Returns:
Pointer to std::ostream instance.

const int ServerSocket::getSockOpt const int    level,
const int    op_name,
char *    result,
int *    result_len
const
 

Set a socket option on the server socket.

Returns:
See the getsockopt manpage for a description of the return values on your platform.
Parameters:
level  See getsockopt documentation.
op_name  See getsockopt documentation.
buffer  See getsockopt documentation.
buflen  See getsockopt documentation.

const bool ServerSocket::isError   const
 

Determine if the server socket is an error condition.

Returns:
true if the socket is in an error state; otherwise, false.

void ServerSocket::setGlobalLogger std::ostream *    os [static]
 

Set a global debug logging stream to be used by all ServerSocket instances.

This does not override a ServerSocket instance's individual logging stream, if any, set via cbits::ServerSocket::setLocalLogger.

This method is a useful way to provide a default logging stream to all ServerSocket instances, without having to configure each instance separately, but still be able to set the logging stream for specific ServerSocket instances.

If this ServerSocket doesn't have an individual logging stream configured, the global logging stream (if set) is used to configure the logging stream for all cbits::Socket instances created by this ServerSocket.

Parameters:
os  Pointer to global debug logging stream

void ServerSocket::setLocalLogger std::ostream *    os const
 

Set the instance debug logging stream for this ServerSocket.

This logger is also configured for all cbits::Socket instances that are created by this ServerSocket.


Member Data Documentation

CriticalSection cbits::ServerSocket::_csect [private]
 

Critical section object used to prevent multiple threads from simultaneously calling cbits::ServerSocket::accept.

Reimplemented in cbits::SSLServerSocket.

bool cbits::ServerSocket::_error [protected]
 

errno value of the last error that occurred.

std::string cbits::ServerSocket::_errstr [protected]
 

A human readable description of _error, if any.

std::ostream * ServerSocket::_glogr = 0 [static, protected]
 

Global, default logging stream for all cbits::ServerSocket instances, if set.

std::ostream* cbits::ServerSocket::_llogr [protected]
 

Debug logging sream for this instance, if set.

CriticalSection cbits::ServerSocket::_read_csect [private]
 

Critical section object used to synchronize access to the error status of this ServerSocket.

Socket* cbits::ServerSocket::_ssock [private]
 

The cbits::Socket instance used to bind to the local server port.


The documentation for this class was generated from the following files:
Generated by
doxygen
Hosted by
SourceForge