Sleipnir
|
Provide a simple interface for objects handling network requests on a server thread. More...
#include <serverclient.h>
Public Member Functions | |
virtual IServerClient * | NewInstance (SOCKET iSocket, uint32_t iHost, uint16_t iPort)=0 |
Requests a clone of the current server client object to handle an incoming connection on the given socket. | |
virtual bool | ProcessMessage (const std::vector< unsigned char > &vecbMessage)=0 |
Called when a new message has been received from the client and should be processed by the server. | |
virtual void | Destroy ()=0 |
Indicates that the server client object should delete itself. |
Provide a simple interface for objects handling network requests on a server thread.
When a CServer detects an incoming connection, it creates a new thread and passes control of that thread to an IServerClient to communicate with the incoming client. By implementing IServerClient, a Sleipnir user can quickly and easily create multithreaded network servers.
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 ProcessMessage - but it is good practice and can greatly simplify TCP/IP communication.
For example, to create an echo server client, one might implement:
class CEchoServerClient : public IServerClient { public: SOCKET m_iSocket; CEchoServerClient( SOCKET iSocket ) : m_iSocket(iSocket) { } IServerClient* NewInstance( SOCKET iSocket, uint32_t iHost, uint16_t iPort ) { return new CEchoServerClient( iSocket ); } void Destroy( ) { delete this; } bool ProcessMessage( const vector<unsigned char>& vecbMessage ) { size_t i; uint32_t iSize; iSize = vecbMessage.size( ); send( m_iSocket, &iSize, sizeof(iSize), 0 ); for( i = 0; i < vecbMessage.size( ); ++i ) if( vecbMessage[ i ] == EOF ) return false; else send( m_iSocket, &vecbMessage[ i ], 1, 0 ); return true; } }
See CServer for an example server setup using this client object.
Definition at line 84 of file serverclient.h.
virtual void Sleipnir::IServerClient::Destroy | ( | ) | [pure virtual] |
Indicates that the server client object should delete itself.
virtual IServerClient* Sleipnir::IServerClient::NewInstance | ( | SOCKET | iSocket, |
uint32_t | iHost, | ||
uint16_t | iPort | ||
) | [pure virtual] |
Requests a clone of the current server client object to handle an incoming connection on the given socket.
iSocket | Socket to be used for communication with the incoming request. |
iHost | Host byte-ordered encoding of the incoming client's IP address. |
iPort | Host byte-ordered encoding of the incoming client's IP port. |
CServer calls NewInstance whenever an incoming connection request is detected. The returned new server client object should be configured to store iSocket and, optionally, the host and port information so as to communicate with the new client during ProcessMessage.
virtual bool Sleipnir::IServerClient::ProcessMessage | ( | const std::vector< unsigned char > & | vecbMessage | ) | [pure virtual] |
Called when a new message has been received from the client and should be processed by the server.
vecbMessage | Contents of the message sent by the clients (without the preceding byte size indicator). |