36 m_config(server_config) ,
37 m_server_peer_config(server_peer_config) ,
40 G_DEBUG(
"GNet::Server::ctor: listening on socket " << m_socket.
asString()
50 m_config(server_config) ,
51 m_server_peer_config(server_peer_config) ,
54 G_DEBUG(
"GNet::Server::ctor: listening on socket " << m_socket.
asString()
57 bool uds = listening_address.
family() == Address::Family::local ;
60 bool open = m_config.uds_open_permissions ;
61 using Mode = G::Process::Umask::Mode ;
64 m_socket.
bind( listening_address ) ;
69 m_socket.
bind( listening_address ) ;
79 if( path.size() > 1U && path.at(0U) ==
'/' )
93 bool with_scope = true ;
94 Address result = m_socket.getLocalAddress() ;
96 result.
setScopeId( m_socket.getBoundScopeId() ) ;
100void GNet::Server::readEvent()
103 G_DEBUG(
"GNet::Server::readEvent: " <<
this ) ;
108 accept( peer_info ) ;
109 Address peer_address = peer_info.m_address ;
117 G_DEBUG(
"GNet::Server::readEvent: new connection from " << peer_address.
displayString()
118 <<
" on " << peer_info.m_socket->asString() ) ;
127 std::unique_ptr<ServerPeer> peer = newPeer(
ExceptionSinkUnbound(
this) , std::move(peer_info) ) ;
130 if( peer ==
nullptr )
132 G_WARNING(
"GNet::Server::readEvent: connection rejected from " << peer_address.
displayString() ) ;
136 G_DEBUG(
"GNet::Server::readEvent: new connection accepted" ) ;
137 m_peer_list.push_back( std::shared_ptr<ServerPeer>(peer.release()) ) ;
141void GNet::Server::accept( ServerPeerInfo & peer_info )
143 AcceptInfo accept_info ;
146 accept_info = m_socket.accept() ;
148 peer_info.m_address = accept_info.address ;
149 peer_info.m_socket = std::move( accept_info.socket_ptr ) ;
152void GNet::Server::onException( ExceptionSource * esrc , std::exception & e ,
bool done )
154 G_DEBUG(
"GNet::Server::onException: exception=[" << e.what() <<
"] esrc=[" <<
static_cast<void*
>(esrc) <<
"]" ) ;
155 bool handled = false ;
156 if( esrc !=
nullptr )
158 for(
auto list_p = m_peer_list.begin() ; list_p != m_peer_list.end() ; ++list_p )
160 if( (*list_p).get() == esrc )
162 std::shared_ptr<ServerPeer> peer_p = *list_p ;
163 m_peer_list.erase( list_p ) ;
164 (*peer_p).doOnDelete( e.what() , done ) ;
172 G_WARNING(
"GNet::Server::onException: unhandled exception: " << e.what() ) ;
179 m_peer_list.clear() ;
184 return !m_peer_list.empty() ;
189 using Peers = std::vector<std::weak_ptr<ServerPeer>> ;
191 result.reserve( m_peer_list.size() ) ;
192 for(
auto & peer : m_peer_list )
193 result.push_back( std::weak_ptr<ServerPeer>(peer) ) ;
197void GNet::Server::writeEvent()
199 G_DEBUG(
"GNet::Server::writeEvent" ) ;
202bool GNet::Server::unlink(
G::SignalSafe ,
const char * path )
noexcept
204 return path ? ( std::remove(path) == 0 ) :
true ;
209GNet::ServerPeerInfo::ServerPeerInfo( Server * server , ServerPeer::Config server_peer_config ) :
210 m_address(Address::defaultAddress()) ,
211 m_server_peer_config(server_peer_config) ,
The GNet::Address class encapsulates a TCP/UDP transport address.
Address & setScopeId(unsigned long)
Sets the scope-id.
std::string displayString(bool with_scope_id=false) const
Returns a printable string that represents the transport address.
std::string hostPartString() const
Returns a printable string that represents the network address.
Family family() const noexcept
Returns the address family enumeration.
A class that encapsulates a network socket file descriptor and an associated windows event handle.
A class that sets the G::LogOuput::context() while in scope.
A potential ExceptionSink that is realised by bind()ing an exception source pointer.
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
An interface for a network listener.
static void addServer(const Listener &server)
Adds a server.
static void removeServer(const Listener &server) noexcept
Removes a server.
A move-only structure used in GNet::Server::newPeer() and containing the new socket.
~Server() override
Destructor.
Address address() const override
Returns the listening address.
Server(ExceptionSink, const Address &listening_address, const ServerPeer::Config &, const Config &)
Constructor.
bool hasPeers() const
Returns true if peers() is not empty.
std::vector< std::weak_ptr< GNet::ServerPeer > > peers()
Returns the list of ServerPeer objects.
void serverCleanup()
Should be called by the most-derived class's destructor in order to trigger early deletion of peer ob...
std::string asString() const
Returns the socket handle as a string.
void addReadHandler(EventHandler &, ExceptionSink)
Adds this socket to the event source list so that the given handler receives read events.
void bind(const Address &)
Binds the socket with the given address.
void listen()
Starts the socket listening on the bound address for incoming connections or incoming datagrams.
Address getLocalAddress() const
Retrieves the local address of the socket.
A derivation of GNet::Socket for a stream socket.
static const char * strdup(const char *)
A strdup() function that makes it clear in the stack trace that leaks are expected.
static void add(bool(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
Used to temporarily modify the process umask.
A class which acquires the process's special privileges on construction and releases them on destruct...
An empty structure that is used to indicate a signal-safe, reentrant implementation.
A configuration structure for GNet::ServerPeer.
A configuration structure for GNet::Server.