1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 09:43:37 +02:00

Added writeSocket, initialized locks

This commit is contained in:
Tristan 🅱. Kildaire 2020-09-23 13:14:37 +02:00
parent e10a7d5b3e
commit fdea191041

View File

@ -14,7 +14,7 @@ import core.thread : Thread;
import std.socket : Socket; import std.socket : Socket;
import bmessage; import bmessage;
import tristanable.encoding : DataMessage; import tristanable.encoding : DataMessage;
import core.sync.mutex : Mutex;
public class DConnection : Thread public class DConnection : Thread
{ {
@ -24,6 +24,13 @@ public class DConnection : Thread
private Socket socket; private Socket socket;
private bool hasAuthed; private bool hasAuthed;
/* Write lock for socket */
/* TODO: Forgot how bmessage works, might need, might not, if multipel calls
* then yes, if single then no as it is based off (well glibc's write)
* thread safe code
*/
private Mutex writeLock;
/* Reserved tag for push notifications */ /* Reserved tag for push notifications */
private long notificationTag = 0; private long notificationTag = 0;
@ -35,20 +42,20 @@ public class DConnection : Thread
/* Set the socket */ /* Set the socket */
this.socket = socket; this.socket = socket;
/* Initialize the tagging facility */ /* Initialize locks */
initTagger(); initLocks();
/* Start the connection handler */ /* Start the connection handler */
start(); start();
} }
/** /**
* Initializes tristanable * Initializes mutexes
* TODO: Implemet me (also tristanable needs reserved tags first)
*/ */
private void initTagger() private void initLocks()
{ {
/* Initialie the socket write lock */
writeLock = new Mutex();
} }
/** /**
@ -76,10 +83,42 @@ public class DConnection : Thread
/* Decode the tristanable message (tagged message) */ /* Decode the tristanable message (tagged message) */
receivedMessage = DataMessage.decode(receivedBytes); receivedMessage = DataMessage.decode(receivedBytes);
/* Process the message */
process(receivedMessage);
/* TODO: Tristanable needs reserved-tag support (client-side concern) */ /* TODO: Tristanable needs reserved-tag support (client-side concern) */
} }
} }
/* TODO: add mutex for writing with message and funciton for doing so */
/**
* Write to socket
*/
private void writeSocket(long tag, byte[] data)
{
/* TODO: Implement me */
/* Create the tagged message */
DataMessage message = new DataMessage(tag, data);
/* Lock the write mutex */
writeLock.lock();
/* TODO: Do send */
bool status = sendMessage(socket, message.encode());
/* TODO: use status */
/* Unlock the write mutex */
writeLock.unlock();
}
/** /**
* Process the received message * Process the received message
*/ */
@ -100,6 +139,15 @@ public class DConnection : Thread
/* Get the username and password */ /* Get the username and password */
string username = cast(string)message.data[2..usernameLength]; string username = cast(string)message.data[2..usernameLength];
string password = cast(string)message.data[cast(ulong)2+usernameLength..message.data.length]; string password = cast(string)message.data[cast(ulong)2+usernameLength..message.data.length];
/* Authenticate */
bool status = authenticate(username, password);
/* Encode the reply */
byte[] reply = [1, status];
/* TODO: Implement me */
writeSocket(tag, reply);
} }
/* If `link` command (requires: unauthed) */ /* If `link` command (requires: unauthed) */
else if(commandByte == 1 && !hasAuthed) else if(commandByte == 1 && !hasAuthed)
@ -124,6 +172,13 @@ public class DConnection : Thread
} }
/** /**
* Authenticate
* *
* Login as a user with the given credentials
*/ */
private bool authenticate(string username, string password)
{
/* TODO: Implement me */
return true;
}
} }