A network server class which listens on a specific port and spins off ServerPeer objects for each incoming connection. More...
#include <gserver.h>
Classes | |
struct | PeerInfo |
A structure used in GNet::Server::newPeer(). More... | |
Public Member Functions | |
Server (unsigned int listening_port, ConnectionLookup *=NULL) | |
Constructor taking a port number. More... | |
Server (const Address &listening_address, ConnectionLookup *=NULL) | |
Constructor. More... | |
Server () | |
Default constructor. Initialise with init(). More... | |
void | init (unsigned int listening_port) |
Initilisation after default construction. More... | |
void | init (const Address &listening_address) |
Initilisation after default construction. More... | |
virtual | ~Server () |
Destructor. More... | |
std::pair< bool, Address > | address () const |
Returns the listening address. More... | |
virtual void | readEvent () |
Final override from GNet::EventHandler. More... | |
virtual void | writeEvent () |
Final override from GNet::EventHandler. More... | |
virtual void | onException (std::exception &e) |
Final override from GNet::EventHandler. More... | |
Public Member Functions inherited from GNet::EventHandler | |
virtual | ~EventHandler () |
Destructor. More... | |
virtual void | exceptionEvent () |
Called for an exception event. More... | |
Static Public Member Functions | |
static bool | canBind (const Address &listening_address, bool do_throw) |
Checks that the specified address can be bound. More... | |
Protected Member Functions | |
virtual ServerPeer * | newPeer (PeerInfo)=0 |
A factory method which new()s a ServerPeer-derived object. More... | |
void | serverCleanup () |
May be called from the derived class destructor in order to trigger early deletion of peer objects, before the derived part of the server disappears. More... | |
A network server class which listens on a specific port and spins off ServerPeer objects for each incoming connection.
|
explicit |
Constructor taking a port number.
The server listens on all local interfaces.
Definition at line 136 of file gserver.cpp.
References init().
|
explicit |
Constructor.
The server listens only on the specific (local) interface.
Definition at line 142 of file gserver.cpp.
References init().
GNet::Server::Server | ( | ) |
Default constructor. Initialise with init().
Definition at line 148 of file gserver.cpp.
|
virtual |
Destructor.
Definition at line 182 of file gserver.cpp.
std::pair< bool, GNet::Address > GNet::Server::address | ( | ) | const |
Returns the listening address.
Pair.first is false if not properly init()ialised.
Definition at line 221 of file gserver.cpp.
References GNet::Address::invalidAddress().
Referenced by GNet::MultiServer::serverReport().
|
static |
Checks that the specified address can be bound.
Throws CannotBind if the address cannot be bound and 'do_throw' is true.
Definition at line 172 of file gserver.cpp.
References GNet::Socket::canBindHint(), and GNet::Address::displayString().
Referenced by GNet::MultiServer::canBind().
void GNet::Server::init | ( | unsigned int | listening_port | ) |
Initilisation after default construction.
Definition at line 154 of file gserver.cpp.
Referenced by Server().
void GNet::Server::init | ( | const Address & | listening_address | ) |
Initilisation after default construction.
Definition at line 159 of file gserver.cpp.
References GNet::Address::displayString(), G_DEBUG, and G::limits::net_listen_queue.
|
protectedpure virtual |
A factory method which new()s a ServerPeer-derived object.
This method is called when a new connection comes into this server. The new ServerPeer object is used to represent the state of the client/server connection.
The new ServerPeer object manages its own lifetime doing a "delete this" when the network connection disappears. But the Server also deletes remaining peers during its destruction.
The implementation should pass the 'PeerInfo' parameter through to the ServerPeer base-class constructor.
Should return NULL for non-fatal errors. It is expected that a typical server process will terminate if newPeer() throws, so most implementations will catch any exceptions and return NULL.
Implemented in GNet::MultiServerImp.
|
virtual |
Final override from GNet::EventHandler.
Implements GNet::EventHandler.
Definition at line 215 of file gserver.cpp.
References G_ERROR.
|
virtual |
Final override from GNet::EventHandler.
Reimplemented from GNet::EventHandler.
Definition at line 229 of file gserver.cpp.
References GNet::Address::displayString(), G_ASSERT, G_DEBUG, GNet::ServerPeer::logId(), GNet::Server::PeerInfo::m_address, GNet::Server::PeerInfo::m_handle, and GNet::Server::PeerInfo::m_name.
|
protected |
May be called from the derived class destructor in order to trigger early deletion of peer objects, before the derived part of the server disappears.
If this is called from the most-derived Server class then it allows ServerPeer objects to use their Server object safely during destruction.
Definition at line 187 of file gserver.cpp.
|
virtual |
Final override from GNet::EventHandler.
Reimplemented from GNet::EventHandler.
Definition at line 313 of file gserver.cpp.
References G_DEBUG.