Working on connection handler sub-system

This commit is contained in:
Tristan B. Velloza Kildaire 2021-12-26 11:57:17 +02:00
parent 2ff697bc3e
commit 713d3a6a39
4 changed files with 159 additions and 13 deletions

View File

@ -0,0 +1,52 @@
/**
*
*/
module dnetd.connection.connection;
import core.thread : Thread;
/**
* Represents a client's/server's connection to
* this server
*
* These are normally spawned by the `serviceLoop`
* of Listener sub-classes
*/
public abstract class Connection : Thread
{
this()
{
super(&handler);
}
/**
* Connection handler
*
* This is to be implemented by sub-classes
*/
public abstract void handler();
}
import std.socket;
/**
* FIXME: When we do anything so far, I am assuming
* a streaming socket so we shouldn't let the
* SocketListener use any SocketType that isn't STREAM
*/
public final class SocketConnection : Connection
{
private Socket clientSock;
this(Socket socket)
{
clientSock = socket;
}
public override void handler()
{
while(true)
{
}
}
}

View File

@ -1,13 +0,0 @@
/**
* Module for listeners
*
* These include the base listener class responsible
* for managing a connection with peers and some
* concrete classes implementing this for various
* network protocols
*/
module dnetd.listeners;
public abstract class Listener
{
}

View File

@ -0,0 +1,42 @@
/**
* Module for listeners
*
* These include the base listener class responsible
* for managing a connection with peers and some
* concrete classes implementing this for various
* network protocols
*/
module dnetd.listeners.listeners;
import dnetd.server : Server;
import dnetd.exceptions : GeneralException;
import std.exception;
import core.thread : Thread;
public abstract class Listener : Thread
{
private Server server;
this(Server server)
{
super(&serviceLoop);
this.server = server;
}
/**
* The connection accepting loop of which
* is provided as the "worker" function to
* the thread
*/
public abstract void serviceLoop();
}
public abstract class ListenerException : GeneralException
{
/* TODO: Potentially remove `listener` */
this(Listener listener, string msg)
{
/* TODO: Set message here */
super(msg);
}
}

View File

@ -0,0 +1,65 @@
/**
* Socket-based listener for supporting any
* network protocol that Linux supports, such as
* TCP (STREAM-based) on IPv4 and IPv6 and UNIX
* domain sockets... to name a few.
*/
module dnetd.listeners.socket;
import dnetd.listeners.listeners;
import dnetd.server : Server;
import std.socket;
import std.conv : to;
public final class SocketListenerException : ListenerException
{
this(SocketListener e)
{
string msg = to!(string)(e.getAddress().addressFamily)
~
"-type socket listener error";
super(e, msg);
}
}
public final class SocketListener : Listener
{
private Socket socket;
private Address address;
this(Server server, Address address, SocketType type, ProtocolType proto)
{
super(server);
this.address = address;
try
{
socket = new Socket(address.addressFamily, type, proto);
socket.bind(address);
}
catch(SocketOSException e)
{
throw new SocketListenerException(this);
}
}
/**
* Socket accept-and-connection spawner
* loop
*/
public override void serviceLoop()
{
while(true)
{
Socket clientSock = socket.accept();
//TODO
}
}
public Address getAddress()
{
return address;
}
}