Sleipnir
Public Member Functions
Sleipnir::CServer Class Reference

Provide a basic multithreaded TCP/IP server for simple communication tasks. More...

#include <server.h>

Inheritance diagram for Sleipnir::CServer:
Sleipnir::CServerImpl

Public Member Functions

bool Initialize (size_t iPort, size_t iTimeout, IServerClient *pServerClient)
 Prepare a server object to listen for incoming connections on the specified port.
bool Start ()
 Opens a server socket and blocks, listening for incoming connections to which server client threads are attached.
void Stop ()
 Signals that the server should stop listening for incoming connections.

Detailed Description

Provide a basic multithreaded TCP/IP server for simple communication tasks.

A server object listens for incoming connections on a specified port. When a connection is made, a new thread is spawned and control is given to an IServerClient object, a handler that knows how to respond to incoming messages. This means that by implementing the simple IServerClient interface, a server program can be created quickly and easily to service network requests in a robust, multithreaded manner.

Sleipnir servers always assume that TCP/IP messages are passed preceded by a four-byte unsigned integer indicating the size of the message. For example, to send the string, "Hello, world!" to a CServer object, a client should send the integer 13 to the server, followed by the 13 bytes of the message (assuming ASCII encoding, of course). It is not necessary for Sleipnir servers to respond to their clients in the same manner - this is left up to the implementation of IServerClient::ProcessMessage - but it is good practice and can greatly simplify TCP/IP communication.

Example usage for a server on port 1234 might resemble:

 CServer            Server;
 CEchoServerClient  ESC;
 
 Server.Initialize( 1234, 100, &ESC );
 Server.Start( );

See IServerClient for an example implementation of an echo server client.

Remarks:
CServer will call IServerClient::NewInstance for each new incoming request, creating a new client object to handle that thread. When the connection is closed by IServerClient::ProcessMessage returning false, the server will call IServerClient::Destroy to clean up that object. The original server client object (ESC in the example above) is only used to create additional new instances.

Definition at line 61 of file server.h.


Member Function Documentation

bool Sleipnir::CServer::Initialize ( size_t  iPort,
size_t  iTimeout,
IServerClient pServerClient 
)

Prepare a server object to listen for incoming connections on the specified port.

Parameters:
iPortTCP/IP port on which the server will listen.
iTimeoutTimeout interval for socket listening.
pServerClientPointer to a template server client object from which new clients will be created to handle incoming requests.
Returns:
True if the server was initialized successfully.

Prepares the server to listen for connection requests on the given port. No actual socket manipulation happens until the server is started.

Remarks:
On platforms where listening on a socket must be interrupted periodically, the interrupt will occur with the given timeout frequency. This is usually a nonissue, and a value of ~100ms is adequate.
See also:
Start

Definition at line 59 of file server.cpp.

Opens a server socket and blocks, listening for incoming connections to which server client threads are attached.

Returns:
True if the server was bound and closed cleanly, false otherwise.

Begins listening on a TCP/IP server socket for incoming connections. When such a connection is detected, a new IServerClient object as provided to Initialize is created and given control of a new thread in which to handle the request. The server object then continues to listen for additional connections on the main thread. This method will block and not return until the server is closed, e.g. by Stop.

Remarks:
Initialize must be called before the server is started.

Definition at line 96 of file server.cpp.

void Sleipnir::CServer::Stop ( ) [inline]

Signals that the server should stop listening for incoming connections.

Remarks:
Results in an exit from Start at the next timeout interval.

Definition at line 73 of file server.h.


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