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

185 lines
3.4 KiB
D
Raw Normal View History

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 13:14:37 +02:00
import core.sync.mutex : Mutex;
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;
2020-09-23 13:14:37 +02:00
/* 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;
2020-09-23 10:33:10 +02:00
/* 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 13:14:37 +02:00
/* Initialize locks */
initLocks();
2020-09-23 10:33:10 +02:00
2020-09-23 09:37:18 +02:00
/* Start the connection handler */
start();
}
2020-09-23 10:33:10 +02:00
/**
2020-09-23 13:14:37 +02:00
* Initializes mutexes
2020-09-23 10:33:10 +02:00
*/
2020-09-23 13:14:37 +02:00
private void initLocks()
2020-09-23 10:33:10 +02:00
{
2020-09-23 13:14:37 +02:00
/* Initialie the socket write lock */
writeLock = new Mutex();
2020-09-23 10:33:10 +02:00
}
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);
2020-09-23 13:14:37 +02:00
/* Process the message */
process(receivedMessage);
2020-09-23 10:33:10 +02:00
/* TODO: Tristanable needs reserved-tag support (client-side concern) */
}
}
2020-09-23 13:14:37 +02:00
/* 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();
}
2020-09-23 10:33:10 +02:00
/**
* Process the received message
*/
private void process(DataMessage message)
{
/* Get the tag */
long tag = message.tag;
2020-09-23 10:33:10 +02:00
/* Get the command byte */
byte commandByte = message.data[0];
/* If `auth` command (requires: unauthed) */
2020-09-23 10:33:10 +02:00
if(commandByte == 0 && !hasAuthed)
{
/* Get the length of the username */
byte usernameLength = message.data[1];
/* Get the username and password */
string username = cast(string)message.data[2..usernameLength];
2020-09-23 10:57:52 +02:00
string password = cast(string)message.data[cast(ulong)2+usernameLength..message.data.length];
2020-09-23 13:14:37 +02:00
/* Authenticate */
bool status = authenticate(username, password);
/* Encode the reply */
byte[] reply = [1, status];
/* TODO: Implement me */
writeSocket(tag, reply);
2020-09-23 10:33:10 +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
{
/* TODO: Check plugins */
bool isPlugin = false;
if(isPlugin)
{
}
else
{
}
2020-09-23 09:37:18 +02:00
}
}
2020-09-23 10:33:10 +02:00
/**
2020-09-23 13:14:37 +02:00
* Authenticate
2020-09-23 10:33:10 +02:00
*
2020-09-23 13:14:37 +02:00
* Login as a user with the given credentials
2020-09-23 10:33:10 +02:00
*/
2020-09-23 13:14:37 +02:00
private bool authenticate(string username, string password)
{
/* TODO: Implement me */
return true;
}
2020-09-23 09:37:18 +02:00
}