34 m_address(peer_info.m_address) ,
35 m_socket(peer_info.m_socket) ,
36 m_sp(*this,*this,*m_socket.get(),0U) ,
37 m_handle(peer_info.m_handle) ,
38 m_delete_timer(*this,&
ServerPeer::onTimeout,*this)
41 G_DEBUG(
"GNet::ServerPeer::ctor: [" <<
this <<
"]: " <<
logId() ) ;
43 m_socket->addReadHandler( *
this ) ;
44 m_socket->addExceptionHandler( *
this ) ;
50 G_DEBUG(
"GNet::ServerPeer::dtor: [" <<
this <<
"]: fd " << logId() ) ;
53 m_socket->dropReadHandler() ;
58 return m_address.displayString() +
"@" + m_socket->asString() ;
69 return *m_socket.get() ;
79 G_DEBUG(
"ServerPeer::onException: exception: " << e.what() ) ;
80 doDelete( e.what() ) ;
86 m_delete_timer.startTimer( 0U ) ;
92 return m_socket->getLocalAddress() ;
97 return std::pair<bool,Address>( true , m_address ) ;
102 return m_sp.peerCertificate() ;
105 void GNet::ServerPeer::onTimeout()
117 return m_sp.send( data , offset ) ;
124 if( m_sp.writeEvent() )
127 catch( std::exception & e )
129 G_WARNING(
"GNet::ServerPeer::writeEvent: exception: " << e.what() ) ;
137 m_connection_lookup(connection_lookup)
139 init( listening_port ) ;
143 m_connection_lookup(connection_lookup)
145 init( listening_address ) ;
149 m_connection_lookup(NULL) ,
156 init(
Address(listening_port) ) ;
161 m_cleaned_up = false ;
165 if( ! m_socket->bind( listening_address ) )
168 throw CannotListen() ;
169 m_socket->addReadHandler( *
this ) ;
177 if( !ok && do_throw )
193 m_cleaned_up = true ;
194 serverCleanupCore() ;
202 void GNet::Server::serverCleanupCore()
204 for( PeerList::iterator p = m_peer_list.begin() ; p != m_peer_list.end() ; ++p )
207 if( handle.
peer() != NULL )
209 G_DEBUG(
"GNet::Server::serverCleanupCore: deleting peer: [" << handle.
peer() <<
"]" ) ;
217 G_ERROR(
"Server::onException: exception: " << e.what() ) ;
224 if( m_socket.get() != NULL )
225 result = m_socket->getLocalAddress() ;
233 G_DEBUG(
"GNet::Server::readEvent: " <<
this ) ;
234 G_ASSERT( m_socket.get() != NULL ) ;
241 accept( peer_info ) ;
243 <<
" (" << peer_info.
m_name <<
")" ) ;
247 peer_info.
m_handle = &m_peer_list.back() ;
257 m_peer_list.pop_back() ;
262 m_peer_list.back().set( peer ) ;
263 G_DEBUG(
"GNet::Server::readEvent: new connection accepted: " << peer->
logId() ) ;
267 void GNet::Server::accept( PeerInfo & peer_info )
272 peer_info.m_socket = accept_pair.
first ;
273 peer_info.m_address = accept_pair.
second ;
277 throw AcceptError() ;
278 if( peer_info.m_socket.get() == NULL )
279 throw AcceptError() ;
282 if( m_connection_lookup != NULL )
284 std::pair<bool,Address> local = peer_info.m_socket->getLocalAddress() ;
287 ConnectionLookup::Connection c = m_connection_lookup->find( local.second , peer_info.m_address ) ;
289 peer_info.m_name = c.peerName() ;
294 void GNet::Server::collectGarbage()
297 G_DEBUG(
"GNet::Server::collectGarbage" ) ;
298 for( PeerList::iterator p = m_peer_list.begin() ; p != m_peer_list.end() ; )
300 ServerPeerHandle handle = *p ;
301 if( handle.peer() == NULL )
303 G_DEBUG(
"GNet::Server::collectGarbage: [" << handle.old() <<
"]" ) ;
304 p = m_peer_list.erase( p ) ;
315 G_DEBUG(
"GNet::Server::writeEvent" ) ;
321 m_address(
Address::invalidAddress()) ,
virtual void onException(std::exception &)
Final override from GNet::EventHandler.
void removeServerPeer(const Connection &server_peer)
Removes a server connection.
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
An abstract base class for the GNet::Server's connection to a remote client.
static bool canBind(const Address &listening_address, bool do_throw)
Checks that the specified address can be bound.
virtual std::pair< bool, Address > localAddress() const
Returns the local address.
virtual std::pair< bool, Address > peerAddress() const
Returns the peer address.
virtual void writeEvent()
Final override from GNet::EventHandler.
ServerPeer(Server::PeerInfo)
Constructor.
The Address class encapsulates an IP transport address.
A class which acquires the process's special privileges on construction and releases them on destruct...
std::string::size_type size_type
A std::size_t type.
static Address invalidAddress()
Returns an invalid address.
ServerPeerHandle * m_handle
void set(ServerPeer *p)
Sets the pointer.
A class for getting more information about a connection from the operating system.
A structure used in the implementation of GNet::Server.
A derivation of Socket for a stream socket.
virtual void readEvent()
Final override from GNet::EventHandler.
std::string displayString(bool with_port=true, bool with_scope_id=false) const
Returns a string which represents the address for debugging and diagnostics purposes.
bool send(const std::string &data, std::string::size_type offset=0U)
Sends data down the socket to the peer.
Server()
Default constructor. Initialise with init().
StreamSocket & socket()
Returns a reference to the client-server connection socket.
static bool enabled()
Returns true if test features are enabled.
static Monitor * instance()
Returns the singleton pointer. Returns null if none.
void doDelete(const std::string &=std::string())
Does "onDelete(); delete this".
A class which is used to return a new()ed socket to calling code, together with associated informatio...
virtual ~Server()
Destructor.
void doDeleteThis(int)
Does delete this. Should only be used by the GNet::Server class.
virtual void readEvent()
Final override from GNet::EventHandler.
void addServerPeer(const Connection &server_peer)
Adds a server connection.
virtual std::string peerCertificate() const
Returns the peer's TLS certificate.
void reset()
Resets the pointer.
virtual void writeEvent()
Final override from GNet::EventHandler.
ServerPeer * old()
Returns the pointer value before it was reset().
std::string logId() const
Returns an identification string for logging purposes.
void init(unsigned int listening_port)
Initilisation after default construction.
void serverCleanup()
May be called from the derived class destructor in order to trigger early deletion of peer objects...
void sslAccept()
Waits for the peer to start a secure session.
virtual ~ServerPeer()
Destructor.
A structure used in GNet::Server::newPeer().
ServerPeerHandle()
Default constructor.
std::pair< bool, Address > address() const
Returns the listening address.
ServerPeer * peer()
Returns the pointer.
virtual void onException(std::exception &e)
Final override from GNet::EventHandler.