2020-04-24 17:48:22 +02:00
|
|
|
module listeners.listener;
|
2020-04-16 15:02:34 +02:00
|
|
|
|
2020-04-16 16:35:07 +02:00
|
|
|
import utils.debugging : debugPrint;
|
|
|
|
import std.conv : to;
|
2020-04-20 18:48:06 +02:00
|
|
|
import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress, SocketFlags, Address;
|
2020-04-16 17:05:56 +02:00
|
|
|
import core.thread : Thread;
|
2020-04-17 18:47:05 +02:00
|
|
|
import std.stdio : writeln, File;
|
2020-04-18 15:25:22 +02:00
|
|
|
import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON;
|
2020-04-17 14:50:12 +02:00
|
|
|
import std.string : cmp;
|
2020-04-24 17:53:07 +02:00
|
|
|
import handlers.handler;
|
2020-04-24 17:48:22 +02:00
|
|
|
import server.server;
|
|
|
|
import connection.connection;
|
2020-04-16 16:35:07 +02:00
|
|
|
|
2020-05-07 21:22:10 +02:00
|
|
|
/**
|
|
|
|
* Represents a server listener which is a method
|
|
|
|
* by which conections to the server (client or server)
|
|
|
|
* can be made.
|
|
|
|
*/
|
2020-04-20 18:48:06 +02:00
|
|
|
public class BesterListener : Thread
|
2020-04-20 17:18:40 +02:00
|
|
|
{
|
2020-04-20 18:48:06 +02:00
|
|
|
/* The associated BesterServer */
|
|
|
|
private BesterServer server;
|
|
|
|
|
|
|
|
/* The server's socket */
|
|
|
|
private Socket serverSocket;
|
2020-05-07 19:08:19 +02:00
|
|
|
|
|
|
|
/* Whether or not the listener is active */
|
|
|
|
private bool active = true;
|
2020-04-20 18:48:06 +02:00
|
|
|
|
2020-05-09 15:19:57 +02:00
|
|
|
/* the address of this listener */
|
|
|
|
protected Address address;
|
|
|
|
|
2020-04-20 18:48:06 +02:00
|
|
|
this(BesterServer besterServer)
|
|
|
|
{
|
2020-04-20 18:50:00 +02:00
|
|
|
/* Set the function address to be called as the worker function */
|
2020-04-20 18:55:48 +02:00
|
|
|
super(&run);
|
2020-04-20 18:50:00 +02:00
|
|
|
|
|
|
|
/* Set this listener's BesterServer */
|
2020-04-20 18:48:06 +02:00
|
|
|
this.server = besterServer;
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:34:29 +02:00
|
|
|
/**
|
|
|
|
* Set the server socket.
|
|
|
|
*/
|
2020-04-20 18:48:06 +02:00
|
|
|
public void setServerSocket(Socket serverSocket)
|
|
|
|
{
|
|
|
|
/* Set the server socket */
|
|
|
|
this.serverSocket = serverSocket;
|
2020-05-09 15:19:57 +02:00
|
|
|
|
|
|
|
/* Set the address */
|
|
|
|
address = serverSocket.localAddress();
|
2020-04-20 18:48:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 14:37:24 +02:00
|
|
|
/**
|
|
|
|
* Get the server socket.
|
|
|
|
*/
|
|
|
|
public Socket getServerSocket()
|
|
|
|
{
|
|
|
|
return serverSocket;
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:34:29 +02:00
|
|
|
/**
|
|
|
|
* Start listen loop.
|
|
|
|
*/
|
2020-04-20 18:48:06 +02:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
serverSocket.listen(1); /* TODO: This value */
|
|
|
|
debugPrint("Server listen loop started");
|
2020-05-07 22:34:29 +02:00
|
|
|
|
|
|
|
/* Loop receive and dispatch connections whilst active */
|
2020-05-07 19:08:19 +02:00
|
|
|
while(active)
|
2020-04-20 18:48:06 +02:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
2020-05-07 19:08:19 +02:00
|
|
|
|
|
|
|
/* Close the socket */
|
|
|
|
serverSocket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown()
|
|
|
|
{
|
|
|
|
active = false;
|
2020-04-20 18:48:06 +02:00
|
|
|
}
|
2020-04-20 17:18:40 +02:00
|
|
|
}
|