Sleipnir
|
Provide a basic multithreaded TCP/IP server for simple communication tasks. More...
#include <server.h>
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. |
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.
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.
iPort | TCP/IP port on which the server will listen. |
iTimeout | Timeout interval for socket listening. |
pServerClient | Pointer to a template server client object from which new clients will be created to handle incoming requests. |
Prepares the server to listen for connection requests on the given port. No actual socket manipulation happens until the server is started.
Definition at line 59 of file server.cpp.
bool Sleipnir::CServer::Start | ( | ) |
Opens a server socket and blocks, listening for incoming connections to which server client threads are attached.
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.
Definition at line 96 of file server.cpp.
void Sleipnir::CServer::Stop | ( | ) | [inline] |