GClasses
GClasses::GPackageServer Class Reference

Detailed Description

This class abstracts a server that speaks a home-made protocol that guarantees packages will arrive in the same order and size as when they were sent. This protocol is a simple layer on top of TCP.

#include <GSocket.h>

Inheritance diagram for GClasses::GPackageServer:
GClasses::GDomServer

Public Member Functions

 GPackageServer (unsigned short port)
 
virtual ~GPackageServer ()
 
void checkForNewConnections ()
 Accept any new incoming connections. More...
 
std::set< GPackageConnection * > & connections ()
 Returns a reference to the current set of connections. More...
 
void disconnect (GPackageConnection *pConn)
 Disconnect from the specified client. More...
 
virtual void pump (GPackageConnection *pConn)
 Receives any pending messages into an internal buffer (to unblock the client, in case its send buffer is full.) More...
 
char * receive (size_t *pOutLen, GPackageConnection **pOutConn)
 Receives the next available package. (The order and size is guaranteed to arrive the same as when it was sent.) NULL is returned if no package is ready in its entirety. The returned value is a pointer to an internal buffer, the contents of which is only valid until the next time receive is called. More...
 
void send (const char *buf, size_t len, GPackageConnection *pConn)
 Send a package, which guarantees to arrive in the same order and size as it was sent. More...
 
void setMaxBufferSizes (size_t a, size_t b)
 Sets some internal values that guide how it reallocates the internal buffer. 'a' is the maximum buffer size to keep around. 'b' is the maximum size for the buffer ever. If a package bigger than 'b' is sent, an exception will be thrown. If a package bigger than 'a' is sent, then the buffer will be grown to that size, but it will be made small again the next time a package is received. More...
 

Static Public Member Functions

static void test ()
 Performs unit tests for this class. Throws an exception if there is a failure. More...
 

Protected Member Functions

virtual GPackageConnectionmakeConnection (SOCKET s)
 This is called just before a new connection is accepted. It returns a pointer to a new GPackageConnection object to associate with this connection. (The connection, however, isn't yet fully established, so it might cause an error if you send something to the client in an overload of this method.) If you want to associate some data with each connection, you can overload this method to create a custom object. More...
 
virtual void onDisconnect (GTCPConnection *pConn)
 This is called when a connection is first known to have disconnected. More...
 
virtual void onReceiveBadData (const char *message)
 This is called when a client sends some bad data. More...
 

Protected Attributes

unsigned int m_maxBufSize
 
unsigned int m_maxPackageSize
 
SOCKET m_sock
 
std::set< GPackageConnection * > m_socks
 

Constructor & Destructor Documentation

GClasses::GPackageServer::GPackageServer ( unsigned short  port)
virtual GClasses::GPackageServer::~GPackageServer ( )
virtual

Member Function Documentation

void GClasses::GPackageServer::checkForNewConnections ( )

Accept any new incoming connections.

std::set<GPackageConnection*>& GClasses::GPackageServer::connections ( )
inline

Returns a reference to the current set of connections.

void GClasses::GPackageServer::disconnect ( GPackageConnection pConn)

Disconnect from the specified client.

virtual GPackageConnection* GClasses::GPackageServer::makeConnection ( SOCKET  s)
inlineprotectedvirtual

This is called just before a new connection is accepted. It returns a pointer to a new GPackageConnection object to associate with this connection. (The connection, however, isn't yet fully established, so it might cause an error if you send something to the client in an overload of this method.) If you want to associate some data with each connection, you can overload this method to create a custom object.

virtual void GClasses::GPackageServer::onDisconnect ( GTCPConnection pConn)
inlineprotectedvirtual

This is called when a connection is first known to have disconnected.

virtual void GClasses::GPackageServer::onReceiveBadData ( const char *  message)
inlineprotectedvirtual

This is called when a client sends some bad data.

virtual void GClasses::GPackageServer::pump ( GPackageConnection pConn)
virtual

Receives any pending messages into an internal buffer (to unblock the client, in case its send buffer is full.)

char* GClasses::GPackageServer::receive ( size_t *  pOutLen,
GPackageConnection **  pOutConn 
)

Receives the next available package. (The order and size is guaranteed to arrive the same as when it was sent.) NULL is returned if no package is ready in its entirety. The returned value is a pointer to an internal buffer, the contents of which is only valid until the next time receive is called.

void GClasses::GPackageServer::send ( const char *  buf,
size_t  len,
GPackageConnection pConn 
)

Send a package, which guarantees to arrive in the same order and size as it was sent.

void GClasses::GPackageServer::setMaxBufferSizes ( size_t  a,
size_t  b 
)
inline

Sets some internal values that guide how it reallocates the internal buffer. 'a' is the maximum buffer size to keep around. 'b' is the maximum size for the buffer ever. If a package bigger than 'b' is sent, an exception will be thrown. If a package bigger than 'a' is sent, then the buffer will be grown to that size, but it will be made small again the next time a package is received.

static void GClasses::GPackageServer::test ( )
static

Performs unit tests for this class. Throws an exception if there is a failure.

Member Data Documentation

unsigned int GClasses::GPackageServer::m_maxBufSize
protected
unsigned int GClasses::GPackageServer::m_maxPackageSize
protected
SOCKET GClasses::GPackageServer::m_sock
protected
std::set<GPackageConnection*> GClasses::GPackageServer::m_socks
protected