summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2023-04-21 11:36:47 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2023-05-05 16:03:23 +0000
commitee4624e7904f37c411fc7215d7c41d7a1b6b7d4f (patch)
tree0af36a4da03b7372dc34f9b08f7ee5ed958da90b /src/net
parentd9ae7419ab16d6e91469fcdc6a89dbc3afc5a030 (diff)
downloadmanaserv-ee4624e7904f37c411fc7215d7c41d7a1b6b7d4f.tar.gz
manaserv-ee4624e7904f37c411fc7215d7c41d7a1b6b7d4f.tar.bz2
manaserv-ee4624e7904f37c411fc7215d7c41d7a1b6b7d4f.tar.xz
manaserv-ee4624e7904f37c411fc7215d7c41d7a1b6b7d4f.zip
Added support for logging in with Stellar
* Added PAMSG_STELLAR_LOGIN / PAMSG_STELLAR_LOGIN_RESPONSE, which is used by the client to request a login token that can be signed using a Stellar wallet. * Added uWebSockets dependency, used to listen for a separate server that verifies signed tokens (the Stellar Bridge). When a token is verified, it is sent to manaserv-account, which then sends a APMSG_LOGIN_RESPONSE to the client matching the token. * Added RapidJSON dependency to parse the JSON WebSocket messages. * To keep everything in a single thread, uWebSockets is now driving the event loop. Processing of ENet hosts, writing stats and expired bans have been moved to uSocket timers. * C++ standard updated to C++17, as required by uWebSockets. When Stellar is used to login, the public key is used as the username. It might be better to introduce an explicit field for this, especially when we want to enable an account to feature both Stellar login as well as login with username / password.
Diffstat (limited to 'src/net')
-rw-r--r--src/net/connectionhandler.cpp21
-rw-r--r--src/net/connectionhandler.h23
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:
/**