Added listener architecture

This commit is contained in:
Tristan B. Kildaire 2020-07-27 13:42:09 +02:00
parent ad5ee5a9ae
commit 0326b61e34
3 changed files with 58 additions and 27 deletions

22
source/server/listener.d Normal file
View File

@ -0,0 +1,22 @@
module server.listener;
import core.thread : Thread;
import server.server : ButterflyServer;
public abstract class ButterflyListener : Thread
{
private ButterflyServer server;
this()
{
super(&run);
}
public abstract void run();
public void setServer(ButterflyServer server)
{
this.server = server;
}
}

27
source/server/listeners.d Normal file
View File

@ -0,0 +1,27 @@
module server.listeners;
import core.thread : Thread;
import server.listener : ButterflyListener;
import std.socket : Socket, Address, SocketType, ProtocolType;
public class IPv4Listener : ButterflyListener
{
private Socket serverSocket;
this(Address bindAddress)
{
/**
* Instantiate a new Socket for the given Address
* `bindAddress` of which it will bind to.
*/
serverSocket = new Socket(bindAddress.addressFamily, SocketType.STREAM, ProtocolType.TCP);
serverSocket.bind(bindAddress);
}
public override void run()
{
}
}

View File

@ -3,6 +3,7 @@ module server.server;
import std.socket : Socket, Address, SocketType, ProtocolType;
import client.client : ButterflyClient;
import std.file : mkdir, exists, isDir;
import server.listener : ButterflyListener;
public final class ButterflyServer
{
@ -10,6 +11,7 @@ public final class ButterflyServer
* TODO: Later implement listeners so that we can
* bind to multiple sockets.
*/
private ButterflyListener[] listeners;
/**
* Socket to listen for incoming connections on
@ -24,7 +26,7 @@ public final class ButterflyServer
/* TODO: Server domain */
public string domain;
this(Address bindAddress, string domain)
this(ButterflyListener[] listeners, string domain)
{
/**
* Create the needed directories (if not already present)
@ -32,20 +34,15 @@ public final class ButterflyServer
directoryCheck();
/**
* Instantiate a new Socket for the given Address
* `bindAddress` of which it will bind to.
* Set all the listeners
*/
serverSocket = new Socket(bindAddress.addressFamily, SocketType.STREAM, ProtocolType.TCP);
serverSocket.bind(bindAddress);
this.listeners = listeners;
/* Set the domain of the server */
this.domain = domain;
/* Start accepting connections */
run();
/* Close the socket */
serverSocket.close();
}
private void directoryCheck()
@ -93,26 +90,11 @@ public final class ButterflyServer
private void run()
{
/* TODO: backlog */
/* Start accepting incoming connections */
serverSocket.listen(1);
/* TODO: Loop here */
while(active)
/* Start the listeners */
foreach(ButterflyListener listener; listeners)
{
/* Block for an incoming queued connection */
Socket clientSocket = serverSocket.accept();
/**
* Create a new ButterflyClient to represent the
* client connection.
*/
ButterflyClient client = new ButterflyClient(this, clientSocket);
/* Start the client thread */
client.start();
/* TODO: Add to array */
listener.start();
}
}
}