GClasses
|
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>
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 GPackageConnection * | makeConnection (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 |
GClasses::GPackageServer::GPackageServer | ( | unsigned short | port | ) |
|
virtual |
void GClasses::GPackageServer::checkForNewConnections | ( | ) |
Accept any new incoming connections.
|
inline |
Returns a reference to the current set of connections.
void GClasses::GPackageServer::disconnect | ( | GPackageConnection * | pConn | ) |
Disconnect from the specified client.
|
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.
|
inlineprotectedvirtual |
This is called when a connection is first known to have disconnected.
|
inlineprotectedvirtual |
This is called when a client sends some bad data.
|
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.
|
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 |
Performs unit tests for this class. Throws an exception if there is a failure.
|
protected |
|
protected |
|
protected |
|
protected |