From d4384c439eca306a96280cc21daaba254fd3f208 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Sun, 27 Sep 2020 18:31:34 +0200 Subject: [PATCH] Added function to get list of connections, added sendUserMessage function (WIP) to send a message to a given user --- source/dnetd/dconnection.d | 40 +++++++++++++++++++++++++++- source/dnetd/dserver.d | 54 +++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/source/dnetd/dconnection.d b/source/dnetd/dconnection.d index 7818f06..6091103 100644 --- a/source/dnetd/dconnection.d +++ b/source/dnetd/dconnection.d @@ -186,6 +186,10 @@ public class DConnection : Thread { command = Command.PART; } + else if(commandByte == cast(ulong)5) + { + command = Command.MSG; + } else if(commandByte == cast(ulong)6) { command = Command.LIST; @@ -372,7 +376,9 @@ public class DConnection : Thread /* If we are sending to a user */ if(messageType == cast(byte)0) { - /* TODO Implemet me */ + /* Send the message to the user */ + bool sendStatus = sendUserMessage(destination, msg); + reply = [sendStatus]; } /* If we are sending to a channel */ else if(messageType == cast(ubyte)1) @@ -438,11 +444,43 @@ public class DConnection : Thread return true; } + /** + * Send user a message + * + * Sends the provided user the specified message + */ + private bool sendUserMessage(string username, string message) + { + /* Find the user to send to */ + DConnection user = server.findUser(username); + + /* If the user was found */ + if(user) + { + /* Send the messge */ + /* TODO: Implement me */ + + + /* TODO: Return value should be based off message send success */ + return true; + } + /* If the user was not found */ + else + { + return false; + } + } + public string getUsername() { return username; } + public ConnectionType getConnectionType() + { + return connType; + } + public override string toString() { string toStr = "["~to!(string)(connType)~"]: "; diff --git a/source/dnetd/dserver.d b/source/dnetd/dserver.d index 897176c..c95277b 100644 --- a/source/dnetd/dserver.d +++ b/source/dnetd/dserver.d @@ -31,6 +31,7 @@ public class DServer : Thread * Connection queue */ private DConnection[] connectionQueue; + private Mutex connectionLock; /** * Channels @@ -89,7 +90,10 @@ public class DServer : Thread private void initLocks() { - /* Initialioze the channel lock */ + /* Initialize the connection lock */ + connectionLock = new Mutex(); + + /* Initialize the channel lock */ channelLock = new Mutex(); } @@ -112,8 +116,14 @@ public class DServer : Thread /* Spawn a connection handler */ DConnection connection = new DConnection(this, socket); + /* Lock the connections list */ + connectionLock.lock(); + /* Add to the connection queue */ connectionQueue ~= connection; + + /* Unlock the connections list */ + connectionLock.unlock(); } } @@ -153,6 +163,48 @@ public class DServer : Thread return channel; } + /** + * Returns the DConnection with the matching + * username, null if not found + */ + public DConnection findUser(string username) + { + /* Get all the current connections */ + DConnection[] connections = getConnections(); + + /* Find the user with the matching user name */ + foreach(DConnection connection; connections) + { + /* The connection must be a user (not unspec or server) */ + if(connection.getConnectionType() == DConnection.ConnectionType.CLIENT) + { + /* Match the username */ + if(cmp(connection.getUsername(), username) == 0) + { + return connection; + } + } + } + + return null; + } + + public DConnection[] getConnections() + { + /* The current connections list */ + DConnection[] currentConnections; + + /* Lock the connections list */ + connectionLock.lock(); + + currentConnections = connectionQueue; + + /* Unlock the connections list */ + connectionLock.unlock(); + + return currentConnections; + } + public DChannel[] getChannels() { /* The current channels list */