On 'link' and 'auth' commands set the respective connection types to SERVER and CLIENT

This commit is contained in:
Tristan B. Kildaire 2020-09-24 13:54:01 +02:00
parent 95978f8637
commit 26f24f12c8
2 changed files with 36 additions and 1 deletions

View File

@ -11,6 +11,7 @@ module dnetd.dchannel;
import dnetd.dconnection : DConnection; import dnetd.dconnection : DConnection;
import core.sync.mutex : Mutex; import core.sync.mutex : Mutex;
import std.conv : to; import std.conv : to;
import std.stdio : writeln;
public class DChannel public class DChannel
{ {
@ -98,7 +99,11 @@ public class DChannel
if(!(member is sender)) if(!(member is sender))
{ {
/* Send the message */ /* 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 */
} }
} }
} }

View File

@ -21,12 +21,20 @@ import dnetd.dchannel : DChannel;
public class DConnection : Thread public class DConnection : Thread
{ {
/* The connection type */
public enum ConnectionType
{
CLIENT, SERVER
}
/** /**
* Connection information * Connection information
*/ */
private DServer server; private DServer server;
private Socket socket; private Socket socket;
private bool hasAuthed; private bool hasAuthed;
private ConnectionType connType;
private string username;
/* Write lock for socket */ /* Write lock for socket */
/* TODO: Forgot how bmessage works, might need, might not, if multipel calls /* 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) private void process(DataMessage message)
{ {
/* Get the tag */ /* 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; long tag = message.tag;
/* Get the command byte */ /* Get the command byte */
@ -156,6 +169,14 @@ public class DConnection : Thread
/* Authenticate */ /* Authenticate */
bool status = authenticate(username, password); 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 */ /* Encode the reply */
byte[] reply = [status]; byte[] reply = [status];
@ -166,6 +187,10 @@ public class DConnection : Thread
else if(commandByte == 1 && !hasAuthed) else if(commandByte == 1 && !hasAuthed)
{ {
/* TODO: Implement me later */ /* TODO: Implement me later */
/* Set the type of this connection to `server` */
connType = ConnectionType.SERVER;
} }
/* If `register` command (requires: unauthed) */ /* If `register` command (requires: unauthed) */
else if(commandByte == 2 && !hasAuthed) else if(commandByte == 2 && !hasAuthed)
@ -349,4 +374,9 @@ public class DConnection : Thread
/* TODO: Implement me */ /* TODO: Implement me */
return true; return true;
} }
public string getUsername()
{
return username;
}
} }