2020-09-23 09:37:18 +02:00
|
|
|
/**
|
|
|
|
* dconnection
|
|
|
|
*
|
|
|
|
* Client/server connection handler spawned
|
|
|
|
* by socket connection dequeue loop.
|
|
|
|
*
|
|
|
|
* Handles all interactions between
|
|
|
|
* the server and the specific client/server.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module dnetd.dconnection;
|
|
|
|
|
|
|
|
import core.thread : Thread;
|
|
|
|
import std.socket : Socket;
|
2020-09-23 10:33:10 +02:00
|
|
|
import bmessage;
|
|
|
|
import tristanable.encoding : DataMessage;
|
|
|
|
|
2020-09-23 09:37:18 +02:00
|
|
|
|
|
|
|
public class DConnection : Thread
|
|
|
|
{
|
2020-09-23 10:33:10 +02:00
|
|
|
/**
|
|
|
|
* Connection information
|
|
|
|
*/
|
2020-09-23 09:37:18 +02:00
|
|
|
private Socket socket;
|
2020-09-23 10:33:10 +02:00
|
|
|
private bool hasAuthed;
|
|
|
|
|
|
|
|
/* Reserved tag for push notifications */
|
|
|
|
private long notificationTag = 0;
|
2020-09-23 09:37:18 +02:00
|
|
|
|
|
|
|
this(Socket socket)
|
|
|
|
{
|
|
|
|
/* Set the function to be called on thread start */
|
|
|
|
super(&worker);
|
|
|
|
|
|
|
|
/* Set the socket */
|
|
|
|
this.socket = socket;
|
|
|
|
|
2020-09-23 10:33:10 +02:00
|
|
|
/* Initialize the tagging facility */
|
|
|
|
initTagger();
|
|
|
|
|
2020-09-23 09:37:18 +02:00
|
|
|
/* Start the connection handler */
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2020-09-23 10:33:10 +02:00
|
|
|
/**
|
|
|
|
* Initializes tristanable
|
|
|
|
* TODO: Implemet me (also tristanable needs reserved tags first)
|
|
|
|
*/
|
|
|
|
private void initTagger()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:37:18 +02:00
|
|
|
/**
|
|
|
|
* Byte dequeue loop
|
|
|
|
*/
|
|
|
|
private void worker()
|
|
|
|
{
|
2020-09-23 10:33:10 +02:00
|
|
|
/* Received bytes (for bformat) */
|
|
|
|
byte[] receivedBytes;
|
|
|
|
|
|
|
|
/* Received message */
|
|
|
|
DataMessage receivedMessage;
|
|
|
|
|
2020-09-23 09:37:18 +02:00
|
|
|
while(true)
|
2020-09-23 10:33:10 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Block to receive a bformat message
|
|
|
|
*
|
|
|
|
* (Does decoding for bformat too)
|
|
|
|
*/
|
|
|
|
bool status = receiveMessage(socket, receivedBytes);
|
|
|
|
|
|
|
|
/* TODO: Check status */
|
|
|
|
|
|
|
|
/* Decode the tristanable message (tagged message) */
|
|
|
|
receivedMessage = DataMessage.decode(receivedBytes);
|
|
|
|
|
|
|
|
/* TODO: Tristanable needs reserved-tag support (client-side concern) */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the received message
|
|
|
|
*/
|
|
|
|
private void process(DataMessage message)
|
|
|
|
{
|
|
|
|
/* Get the command byte */
|
|
|
|
byte commandByte = message.data[0];
|
|
|
|
|
2020-09-23 10:52:33 +02:00
|
|
|
/* If `auth` command (requires: unauthed) */
|
2020-09-23 10:33:10 +02:00
|
|
|
if(commandByte == 0 && !hasAuthed)
|
|
|
|
{
|
2020-09-23 10:52:33 +02:00
|
|
|
/* Get the length of the username */
|
|
|
|
byte usernameLength = message.data[1];
|
|
|
|
|
|
|
|
/* Get the username and password */
|
|
|
|
string username = cast(string)message.data[2..usernameLength];
|
|
|
|
string password = cast(string)message.data[cast(ubyte)2+usernameLength..message.data.length];
|
2020-09-23 10:33:10 +02:00
|
|
|
}
|
2020-09-23 10:52:33 +02:00
|
|
|
/* If `link` command (requires: unauthed) */
|
2020-09-23 10:33:10 +02:00
|
|
|
else if(commandByte == 1 && !hasAuthed)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
/* TODO: Handle this case */
|
|
|
|
else
|
2020-09-23 09:37:18 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 10:33:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-09-23 09:37:18 +02:00
|
|
|
}
|