39 m_config(server_config) ,
40 m_server_peer_config(server_peer_config) ,
43 G_DEBUG(
"GNet::Server::ctor: listening on socket " << m_socket.
asString()
53 m_config(server_config) ,
54 m_server_peer_config(server_peer_config) ,
57 G_DEBUG(
"GNet::Server::ctor: listening on socket " << m_socket.
asString()
60 bool uds = listening_address.
family() == Address::Family::local ;
63 bool open = m_config.uds_open_permissions ;
64 using Mode = G::Process::Umask::Mode ;
67 m_socket.
bind( listening_address ) ;
72 m_socket.
bind( listening_address ) ;
82 if( path.size() > 1U && path.at(0U) ==
'/' )
96 bool with_scope = true ;
97 Address result = m_socket.getLocalAddress() ;
99 result.
setScopeId( m_socket.getBoundScopeId() ) ;
103void GNet::Server::readEvent()
106 G_DEBUG(
"GNet::Server::readEvent: " <<
this ) ;
111 accept( peer_info ) ;
112 Address peer_address = peer_info.m_address ;
113 G_DEBUG(
"GNet::Server::readEvent: new connection from " << peer_address.
displayString()
114 <<
" on " << peer_info.m_socket->asString() ) ;
120 ServerPeer::eventLoggingString(peer_address,m_server_peer_config) ) ;
129 std::unique_ptr<ServerPeer> peer = newPeer( m_es.eh(
this).unbound() , std::move(peer_info) ) ;
132 if( peer ==
nullptr )
134 G_WARNING(
"GNet::Server::readEvent: connection rejected from " << peer_address.
displayString() ) ;
138 G_DEBUG(
"GNet::Server::readEvent: new connection accepted" ) ;
139 m_peer_list.push_back( std::shared_ptr<ServerPeer>(peer.release()) ) ;
143void GNet::Server::accept( ServerPeerInfo & peer_info )
145 AcceptInfo accept_info ;
148 accept_info = m_socket.accept() ;
150 peer_info.m_address = accept_info.address ;
151 peer_info.m_socket = std::move( accept_info.socket_ptr ) ;
154void GNet::Server::onException( ExceptionSource * esrc , std::exception & e ,
bool done )
156 G_DEBUG(
"GNet::Server::onException: exception=[" << e.what() <<
"] esrc=[" <<
static_cast<void*
>(esrc) <<
"]" ) ;
157 bool handled = false ;
158 if( esrc !=
nullptr )
160 for(
auto list_p = m_peer_list.begin() ; list_p != m_peer_list.end() ; ++list_p )
162 if( (*list_p).get() == esrc )
164 std::shared_ptr<ServerPeer> peer_p = *list_p ;
165 m_peer_list.erase( list_p ) ;
166 (*peer_p).doOnDelete( e.what() , done ) ;
174 G_WARNING(
"GNet::Server::onException: unhandled exception: " << e.what() ) ;
181 m_peer_list.clear() ;
186 return !m_peer_list.empty() ;
191 using Peers = std::vector<std::weak_ptr<ServerPeer>> ;
193 result.reserve( m_peer_list.size() ) ;
194 for(
auto & peer : m_peer_list )
195 result.push_back( std::weak_ptr<ServerPeer>(peer) ) ;
199void GNet::Server::writeEvent()
201 G_DEBUG(
"GNet::Server::writeEvent" ) ;
206GNet::ServerPeerInfo::ServerPeerInfo( Server * server , ServerPeer::Config server_peer_config ) :
207 m_address(Address::defaultAddress()) ,
208 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 lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
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.
bool hasPeers() const
Returns true if peers() is not empty.
Server(EventState, const Address &listening_address, const ServerPeer::Config &, const Config &)
Constructor.
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 &, EventState)
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 Arg arg(const char *)
Duplicates a c-string for add().
static void add(Fn, Arg arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
static bool cleanup(const Cleanup::Arg &path_arg) noexcept
Deletes the file.
A Path object represents a file system path.
Used to temporarily modify the process umask.
A class which acquires the process's special privileges on construction and releases them on destruct...
A configuration structure for GNet::ServerPeer.
A configuration structure for GNet::Server.