mirror of https://github.com/deavminet/dnetd.git
Initial code
This commit is contained in:
parent
ac251073b6
commit
9ad7a4b313
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"authors": [
|
||||
"Tristan B. Kildaire"
|
||||
],
|
||||
"copyright": "Copyright © 2020, Tristan B. Kildaire",
|
||||
"description": "dnet server",
|
||||
"license": "AGPLv3",
|
||||
"name": "dnet"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
dnet protocol specification
|
||||
===========================
|
||||
|
||||
-- Command listing --
|
||||
|
||||
1. `auth`
|
||||
2. `link`
|
|
@ -0,0 +1,13 @@
|
|||
import std.stdio;
|
||||
import std.socket : parseAddress;
|
||||
import dnetd.dserver : DServer;
|
||||
|
||||
void main()
|
||||
{
|
||||
/* TODO: Args for bind */
|
||||
|
||||
|
||||
DServer dserver = new DServer(parseAddress("0.0.0.0", 7777));
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
public class DConnection : Thread
|
||||
{
|
||||
/* The client's socket */
|
||||
private Socket socket;
|
||||
|
||||
this(Socket socket)
|
||||
{
|
||||
/* Set the function to be called on thread start */
|
||||
super(&worker);
|
||||
|
||||
/* Set the socket */
|
||||
this.socket = socket;
|
||||
|
||||
/* Start the connection handler */
|
||||
start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Byte dequeue loop
|
||||
*/
|
||||
private void worker()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* DServer
|
||||
*
|
||||
* Represents a server instance.
|
||||
*
|
||||
* Holds a list of DConnections,
|
||||
* configuration parameters and
|
||||
* more.
|
||||
*/
|
||||
|
||||
module dnetd.dserver;
|
||||
|
||||
import core.thread : Thread;
|
||||
import std.socket : Address, Socket;
|
||||
import dnetd.dconnection;
|
||||
|
||||
public class DServer : Thread
|
||||
{
|
||||
/* The server's socket to bind, listen and accept connections from */
|
||||
private Socket serverSocket;
|
||||
|
||||
/* Bind address */
|
||||
private Address sockAddress;
|
||||
|
||||
|
||||
/* Connection queue */
|
||||
private DConnection[] connectionQueue;
|
||||
|
||||
this(Address sockAddress)
|
||||
{
|
||||
/* Set the function to be called on thread start */
|
||||
super(&dequeueLoop);
|
||||
|
||||
/* Set the listening address */
|
||||
this.sockAddress = sockAddress;
|
||||
|
||||
/* Initialize the server */
|
||||
init();
|
||||
|
||||
/* Start the server */
|
||||
startServer();
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
/* Setup socket */
|
||||
initNetwork();
|
||||
|
||||
/* Setup queues */
|
||||
initQueues();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the socket, binds it
|
||||
* to the given address
|
||||
*/
|
||||
private void initNetwork()
|
||||
{
|
||||
/* Create the socket */
|
||||
serverSocket = new Socket();
|
||||
|
||||
/* Bind the socket to the given address */
|
||||
serverSocket.bind(sockAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates all needed queues
|
||||
* and their mutexes
|
||||
*/
|
||||
private void initQueues()
|
||||
{
|
||||
/* TODO: Implement me */
|
||||
}
|
||||
|
||||
private void startServer()
|
||||
{
|
||||
/* Start the connection dequeue thread */
|
||||
start();
|
||||
}
|
||||
|
||||
private void dequeueLoop()
|
||||
{
|
||||
/* Start accepting-and-enqueuing connections */
|
||||
serverSocket.listen(0); /* TODO: Linux be lile, hehahahhahahah who gives one - I give zero */
|
||||
|
||||
while(true)
|
||||
{
|
||||
/* Dequeue a connection */
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
/* Spawn a connection handler */
|
||||
DConnection connection = new DConnection(socket);
|
||||
|
||||
/* Add to the connection queue */
|
||||
connectionQueue ~= connection;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue