33 m_address(peer_info.m_address) ,
34 m_socket(peer_info.m_socket) ,
35 m_sp(*this,*this,*m_socket.get(),0U) ,
36 m_handle(peer_info.m_handle) ,
37 m_delete_timer(*this,&
ServerPeer::onTimeout,*this)
39 G_ASSERT( m_socket.get() != nullptr ) ;
40 G_DEBUG(
"GNet::ServerPeer::ctor: [" <<
this <<
"]: " <<
logId() ) ;
42 m_socket->addReadHandler( *
this ) ;
43 m_socket->addExceptionHandler( *
this ) ;
49 G_DEBUG(
"GNet::ServerPeer::dtor: [" <<
this <<
"]: fd " << logId() ) ;
52 m_socket->dropReadHandler() ;
53 m_socket->dropExceptionHandler() ;
58 return m_address.displayString() +
"@" + m_socket->asString() ;
68 G_ASSERT( m_socket.get() != nullptr ) ;
69 return *m_socket.get() ;
79 G_DEBUG(
"ServerPeer::onException: exception: " << e.what() ) ;
80 doDelete( e.what() ) ;
86 m_delete_timer.startTimer( 0U ) ;
91 G_ASSERT( m_socket.get() != nullptr ) ;
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 ) ;
122 return m_sp.send( segments ) ;
129 if( m_sp.writeEvent() )
132 catch( std::exception & e )
134 G_DEBUG(
"GNet::ServerPeer::writeEvent: exception: " << e.what() ) ;
135 doDelete( e.what() ) ;
143 init( listening_address ) ;
153 m_cleaned_up = false ;
155 G_DEBUG(
"GNet::Server::init: listening on socket " << m_socket->asString() <<
" with address " << listening_address.
displayString() ) ;
158 m_socket->bind( listening_address ) ;
160 m_socket->listen( G::limits::net_listen_queue ) ;
161 m_socket->addReadHandler( *
this ) ;
170 ok = socket.canBindHint( address ) ;
172 if( !ok && do_throw )
188 m_cleaned_up = true ;
189 serverCleanupCore() ;
197 void GNet::Server::serverCleanupCore()
199 for( PeerList::iterator p = m_peer_list.begin() ; p != m_peer_list.end() ; ++p )
202 if( handle.
peer() != nullptr )
204 G_DEBUG(
"GNet::Server::serverCleanupCore: deleting peer: [" << handle.
peer() <<
"]" ) ;
213 if( m_socket.get() != nullptr )
214 result = m_socket->getLocalAddress() ;
222 G_DEBUG(
"GNet::Server::readEvent: " <<
this ) ;
223 G_ASSERT( m_socket.get() != nullptr ) ;
230 accept( peer_info ) ;
231 G_DEBUG(
"GNet::Server::readEvent: new connection from " << peer_info.m_address.
displayString()
232 <<
" on " << peer_info.m_socket->asString() ) ;
236 peer_info.m_handle = &m_peer_list.back() ;
244 if( peer ==
nullptr )
246 m_peer_list.pop_back() ;
247 G_DEBUG(
"GNet::Server::readEvent: connection rejected from " << peer_info.m_address.
displayString() ) ;
251 m_peer_list.back().set( peer ) ;
252 G_DEBUG(
"GNet::Server::readEvent: new connection accepted: " << peer->
logId() ) ;
256 void GNet::Server::accept( PeerInfo & peer_info )
261 peer_info.m_socket = accept_pair.first ;
262 peer_info.m_address = accept_pair.second ;
264 G_ASSERT( peer_info.m_socket.get() != nullptr ) ;
267 void GNet::Server::collectGarbage()
270 G_DEBUG(
"GNet::Server::collectGarbage" ) ;
271 for( PeerList::iterator p = m_peer_list.begin() ; p != m_peer_list.end() ; )
273 ServerPeerHandle handle = *p ;
274 if( handle.peer() == nullptr )
276 G_DEBUG(
"GNet::Server::collectGarbage: [" << handle.old() <<
"]" ) ;
277 p = m_peer_list.erase( p ) ;
288 G_DEBUG(
"GNet::Server::writeEvent" ) ;
293 GNet::Server::PeerInfo::PeerInfo() :
294 m_address(
Address::defaultAddress()) ,
void removeServerPeer(const Connection &server_peer)
Removes a server connection.
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.
ServerPeer(Server::PeerInfo)
Constructor.
The GNet::Address class encapsulates a TCP/UDP transport address.
A class which acquires the process's special privileges on construction and releases them on destruct...
void set(ServerPeer *p)
Sets the pointer.
virtual void readEvent() override
Override from GNet::EventHandler.
virtual void writeEvent() override
Override from GNet::EventHandler.
A structure used in the implementation of GNet::Server.
A derivation of GNet::Socket for a stream socket.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
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 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...
int domain() const
Returns the address 'domain', eg. PF_INET.
virtual ~Server()
Destructor.
void doDeleteThis(int)
Does delete this. Should only be used by the GNet::Server class.
virtual std::string peerCertificate() const override
Returns the peer's TLS certificate.
void addServerPeer(const Connection &server_peer)
Adds a server connection.
void reset()
Resets the pointer.
virtual std::pair< bool, Address > peerAddress() const override
Returns the peer address.
virtual void onException(std::exception &) g__final override
Override from GNet::EventHandler.
ServerPeer * old()
Returns the pointer value before it was reset().
virtual void writeEvent() override
Override from GNet::EventHandler.
std::string logId() const
Returns an identification string for logging purposes.
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.
void init(const Address &listening_address)
Initilisation after default construction.
virtual ~ServerPeer()
Destructor.
A structure used in GNet::Server::newPeer().
ServerPeerHandle()
Default constructor.
virtual void readEvent() override
Override from GNet::EventHandler.
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
std::pair< bool, Address > address() const
Returns the listening address.
virtual std::pair< bool, Address > localAddress() const override
Returns the local address.
ServerPeer * peer()
Returns the pointer.