Implementing different listeners for supporting Unix, INET4 and INET6 tcp

This commit is contained in:
Tristan B. Kildaire 2020-04-20 18:48:06 +02:00
parent 1c35bcd793
commit 8978f057a3
2 changed files with 61 additions and 3 deletions

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

@ -0,0 +1,20 @@
module server.listeners;
import server.types;
import std.socket : Socket;
public class UNIXListener : BesterListener
{
this(BesterServer besterServer, string path)
{
super(besterServer);
setServerSocket(setupUNIXSocket(path));
}
private Socket setupUNIXSocket(string path)
{
Socket unixSocket;
return unixSocket;
}
}

View File

@ -2,20 +2,58 @@ module server.types;
import utils.debugging : debugPrint;
import std.conv : to;
import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress, SocketFlags;
import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress, SocketFlags, Address;
import core.thread : Thread;
import std.stdio : writeln, File;
import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON;
import std.string : cmp;
import server.handler;
import server.listeners;
/* TODO: Implement me */
/* All this will do is accept incoming connections
* but they will be pooled in the BesterServer.
*/
public class BesterListener
public class BesterListener : Thread
{
/* The associated BesterServer */
private BesterServer server;
/* The server's socket */
private Socket serverSocket;
this(BesterServer besterServer)
{
this.server = besterServer;
}
public void setServerSocket(Socket serverSocket)
{
/* Set the server socket */
this.serverSocket = serverSocket;
}
/* Start listen loop */
public void run()
{
serverSocket.listen(1); /* TODO: This value */
debugPrint("Server listen loop started");
while(true)
{
/* Wait for an incoming connection */
Socket clientConnection = serverSocket.accept();
/* Create a new client connection handler and start its thread */
BesterConnection besterConnection = new BesterConnection(clientConnection, server);
besterConnection.start();
/* Add this client to the list of connected clients */
server.clients ~= besterConnection;
}
}
}
@ -34,7 +72,7 @@ public class BesterServer
private Socket serverSocket;
/* Connected clients */
private BesterConnection[] clients;
public BesterConnection[] clients;
this(JSONValue config)
{