From 26f24f12c8fddd3ca56b661388a5a8cc6bd12262 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 24 Sep 2020 13:54:01 +0200 Subject: [PATCH] On 'link' and 'auth' commands set the respective connection types to SERVER and CLIENT --- source/dnetd/dchannel.d | 7 ++++++- source/dnetd/dconnection.d | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/source/dnetd/dchannel.d b/source/dnetd/dchannel.d index af66a0c..394c275 100644 --- a/source/dnetd/dchannel.d +++ b/source/dnetd/dchannel.d @@ -11,6 +11,7 @@ module dnetd.dchannel; import dnetd.dconnection : DConnection; import core.sync.mutex : Mutex; import std.conv : to; +import std.stdio : writeln; public class DChannel { @@ -98,7 +99,11 @@ public class DChannel if(!(member is sender)) { /* Send the message */ - member.writeSocket(0, msg); + writeln("Delivering message for channel '"~name~"' to user '"~member.getUsername()~"'..."); + bool status = member.writeSocket(0, msg); + writeln("Delivered message for channel '"~name~"' to user '"~member.getUsername()~"'!"); + + /* TODO: Errors from status */ } } } diff --git a/source/dnetd/dconnection.d b/source/dnetd/dconnection.d index 51609f2..906a756 100644 --- a/source/dnetd/dconnection.d +++ b/source/dnetd/dconnection.d @@ -21,12 +21,20 @@ import dnetd.dchannel : DChannel; public class DConnection : Thread { + /* The connection type */ + public enum ConnectionType + { + CLIENT, SERVER + } + /** * Connection information */ private DServer server; private Socket socket; private bool hasAuthed; + private ConnectionType connType; + private string username; /* Write lock for socket */ /* TODO: Forgot how bmessage works, might need, might not, if multipel calls @@ -138,6 +146,11 @@ public class DConnection : Thread private void process(DataMessage message) { /* Get the tag */ + /** + * TODO: Client side will always do 1, because we don't have + * multi-thread job processing, only need this to differentiate + * between commands and async notifications + */ long tag = message.tag; /* Get the command byte */ @@ -156,6 +169,14 @@ public class DConnection : Thread /* Authenticate */ bool status = authenticate(username, password); + /* TODO: What to do on bad authetication? */ + + /* Set the username */ + this.username = username; + + /* Set the type of this connection to `client` */ + connType = ConnectionType.CLIENT; + /* Encode the reply */ byte[] reply = [status]; @@ -166,6 +187,10 @@ public class DConnection : Thread else if(commandByte == 1 && !hasAuthed) { /* TODO: Implement me later */ + + + /* Set the type of this connection to `server` */ + connType = ConnectionType.SERVER; } /* If `register` command (requires: unauthed) */ else if(commandByte == 2 && !hasAuthed) @@ -349,4 +374,9 @@ public class DConnection : Thread /* TODO: Implement me */ return true; } + + public string getUsername() + { + return username; + } }