This commit is contained in:
Tristan B. Kildaire 2020-05-09 16:14:55 +02:00
parent 4d21754fff
commit b862ba7270
3 changed files with 64 additions and 29 deletions

View File

@ -6,7 +6,7 @@ import std.socket : SocketOSException, parseAddress, UnixAddress;
import utils.debugging : dprint;
import std.stdio : File, writeln;
import std.json : parseJSON, JSONValue;
import listeners.listener : BesterListener;
import listeners.listener : BesterListener, BesterListenerException;
import listeners.types : TCP4Listener, TCP6Listener, UNIXListener;
import std.file : exists;
@ -67,28 +67,36 @@ private BesterListener[] getListeners(BesterServer server, JSONValue networkBloc
/* TODO: Error handling and get keys and clean up for formality */
/* Look for IPv4 TCP block */
JSONValue inet4TCPBlock = networkBlock["tcp4"];
dprint("<<< IPv4 TCP Block >>>\n" ~ inet4TCPBlock.toPrettyString());
string inet4Address = inet4TCPBlock["address"].str();
ushort inet4Port = to!(ushort)(inet4TCPBlock["port"].str());
TCP4Listener tcp4Listener = new TCP4Listener(server, parseAddress(inet4Address, inet4Port));
listeners ~= tcp4Listener;
try
{
/* Look for IPv4 TCP block */
JSONValue inet4TCPBlock = networkBlock["tcp4"];
dprint("<<< IPv4 TCP Block >>>\n" ~ inet4TCPBlock.toPrettyString());
string inet4Address = inet4TCPBlock["address"].str();
ushort inet4Port = to!(ushort)(inet4TCPBlock["port"].str());
TCP4Listener tcp4Listener = new TCP4Listener(server, parseAddress(inet4Address, inet4Port));
listeners ~= tcp4Listener;
/* Look for IPv6 TCP block */
JSONValue inet6TCPBlock = networkBlock["tcp6"];
dprint("<<< IPv6 TCP Block >>>\n" ~ inet6TCPBlock.toPrettyString());
string inet6Address = inet6TCPBlock["address"].str();
ushort inet6Port = to!(ushort)(inet6TCPBlock["port"].str());
TCP6Listener tcp6Listener = new TCP6Listener(server, parseAddress(inet6Address, inet6Port));
listeners ~= tcp6Listener;
/* Look for IPv6 TCP block */
JSONValue inet6TCPBlock = networkBlock["tcp6"];
dprint("<<< IPv6 TCP Block >>>\n" ~ inet6TCPBlock.toPrettyString());
string inet6Address = inet6TCPBlock["address"].str();
ushort inet6Port = to!(ushort)(inet6TCPBlock["port"].str());
TCP6Listener tcp6Listener = new TCP6Listener(server, parseAddress(inet6Address, inet6Port));
listeners ~= tcp6Listener;
/* Look for UNIX Domain block */
JSONValue unixDomainBlock = networkBlock["unix"];
dprint("<<< UNIX Domain Block >>>\n" ~ unixDomainBlock.toPrettyString());
string unixAddress = unixDomainBlock["address"].str();
// UNIXListener unixListener = new UNIXListener(server, new UnixAddress(unixAddress));
// listeners ~= unixListener;
/* Look for UNIX Domain block */
JSONValue unixDomainBlock = networkBlock["unix"];
dprint("<<< UNIX Domain Block >>>\n" ~ unixDomainBlock.toPrettyString());
string unixAddress = unixDomainBlock["address"].str();
// UNIXListener unixListener = new UNIXListener(server, new UnixAddress(unixAddress));
// listeners ~= unixListener;
}
catch(BesterListenerException e)
{
}
return listeners;
}

View File

@ -10,6 +10,7 @@ import std.string : cmp;
import handlers.handler;
import server.server;
import connection.connection;
import base.types : BesterException;
/**
* Represents a server listener which is a method
@ -46,9 +47,6 @@ public class BesterListener : Thread
{
/* Set the server socket */
this.serverSocket = serverSocket;
/* Set the address */
address = serverSocket.localAddress();
}
/**
@ -90,3 +88,11 @@ public class BesterListener : Thread
active = false;
}
}
public final class BesterListenerException : BesterException
{
this(BesterListener e)
{
super("Could not bind to: " ~ e.toString());
}
}

View File

@ -1,8 +1,8 @@
module listeners.types;
import listeners.listener : BesterListener;
import listeners.listener : BesterListener, BesterListenerException;
import server.server : BesterServer;
import std.socket : Socket, Address, AddressFamily, SocketType;
import std.socket : Socket, Address, AddressFamily, SocketType, SocketException;
/**
* Represents a stream socket listener over UNIX
@ -13,7 +13,14 @@ public final class UNIXListener : BesterListener
this(BesterServer besterServer, Address address)
{
super(besterServer);
setServerSocket(setupUNIXSocket(address));
try
{
setServerSocket(setupUNIXSocket(address));
}
catch(SocketException e)
{
throw new BesterListenerException(this);
}
}
private Socket setupUNIXSocket(Address address)
@ -39,7 +46,14 @@ public final class TCP4Listener : BesterListener
this(BesterServer besterServer, Address address)
{
super(besterServer);
setServerSocket(setupTCP4Socket(address));
try
{
setServerSocket(setupTCP4Socket(address));
}
catch(SocketException e)
{
throw new BesterListenerException(this);
}
}
private Socket setupTCP4Socket(Address address)
@ -65,7 +79,14 @@ public final class TCP6Listener : BesterListener
this(BesterServer besterServer, Address address)
{
super(besterServer);
setServerSocket(setupTCP6Socket(address));
try
{
setServerSocket(setupTCP6Socket(address));
}
catch(SocketException e)
{
throw new BesterListenerException(this);
}
}
private Socket setupTCP6Socket(Address address)