diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/connectionhandler.cpp | 21 | ||||
-rw-r--r-- | src/net/connectionhandler.h | 23 |
2 files changed, 33 insertions, 11 deletions
diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp index 4edb1fd2..ee98a18b 100644 --- a/src/net/connectionhandler.cpp +++ b/src/net/connectionhandler.cpp @@ -36,16 +36,20 @@ #endif bool ConnectionHandler::startListen(enet_uint16 port, - const std::string &listenHost) + const std::string &hostname) { + // Remember unresolved host for debugging purposes. + this->hostname = hostname; + // Bind the server to the default localhost. + ENetAddress address; address.host = ENET_HOST_ANY; address.port = port; - if (!listenHost.empty()) - enet_address_set_host(&address, listenHost.c_str()); + if (!hostname.empty()) + enet_address_set_host(&address, hostname.c_str()); - LOG_INFO("Listening on " << listenHost << ":" << port << "..."); + LOG_INFO("Listening on " << hostname << ":" << port << "..."); #if defined(ENET_VERSION) && ENET_VERSION >= ENET_CUTOFF host = enet_host_create( &address /* the address to bind the server host to */, @@ -86,6 +90,11 @@ void ConnectionHandler::stopListen() // FIXME: memory leak on NetComputers } +enet_uint16 ConnectionHandler::getPort() const +{ + return host ? host->address.port : 0; +} + void ConnectionHandler::flush() { enet_host_flush(host); @@ -93,9 +102,11 @@ void ConnectionHandler::flush() void ConnectionHandler::process(enet_uint32 timeout) { + enet_host_service(host, nullptr, timeout); + ENetEvent event; // Process Enet events and do not block. - while (enet_host_service(host, &event, timeout) > 0) { + while (enet_host_check_events(host, &event) > 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: { diff --git a/src/net/connectionhandler.h b/src/net/connectionhandler.h index e49d49a9..2d0a2a6a 100644 --- a/src/net/connectionhandler.h +++ b/src/net/connectionhandler.h @@ -41,11 +41,12 @@ class ConnectionHandler /** * Open the server socket. - * @param port the port to listen to - * @host the host IP to listen on, defaults to the default localhost + * + * @param port the port to listen to + * @param hostname the host IP to listen on, defaults to the default localhost */ bool startListen(enet_uint16 port, - const std::string &host = std::string()); + const std::string &hostname = std::string()); /** * Disconnect all the clients and close the server socket. @@ -53,13 +54,23 @@ class ConnectionHandler void stopListen(); /** + * Return the port the handler is listening on, or 0 if not listening. + */ + enet_uint16 getPort() const; + + /** + * Return the host the handler was asked to listen on. + */ + const std::string &getHostname() const { return hostname; } + + /** * Process outgoing messages and listen to the server socket for * incoming messages and new connections. * * @timeout an optional timeout in milliseconds to wait for something * to happen when there is nothing to do */ - virtual void process(enet_uint32 timeout = 0); + void process(enet_uint32 timeout = 0); /** * Process outgoing messages. @@ -82,8 +93,8 @@ class ConnectionHandler unsigned getClientCount() const; private: - ENetAddress address; /**< Includes the port to listen to. */ - ENetHost *host; /**< The host that listen for connections. */ + std::string hostname; /**< The unresolved host the handler was asked to listen on. */ + ENetHost *host; /**< The ENet host that listen for connections. */ protected: /** |