- Fixed runtime error with initialization by placing the `Connection` type within the `server` module
This commit is contained in:
Tristan B. Velloza Kildaire 2023-02-27 18:34:33 +02:00
parent eed104df64
commit 0559f092de
2 changed files with 135 additions and 115 deletions

View File

@ -1,108 +0,0 @@
module nostril.connection;
import std.stdio;
import vibe.vibe;
import vibe.http.dist;
import std.json;
import gogga;
import gogga;
// TODO: Investigate if we need the belowe (I copied it from Birchwood)
__gshared GoggaLogger logger;
__gshared static this()
{
logger = new GoggaLogger();
}
import vibe.vibe : WebSocket, WebSocketException;
import core.thread.fiber : Fiber;
import std.json;
// TODO: This won't work with Thread, must be a fiber
// ... because of how vibe.d works
public class Connection : Fiber
{
/* Client socket */
private WebSocket socket;
/* Request information */
private HTTPServerRequest httpRequest;
this(WebSocket ws)
{
super(&worker);
this.socket = ws;
this.httpRequest = cast(HTTPServerRequest)socket.request();
}
private void worker()
{
logger.print("Handling web socket: "~to!(string)(socket)~"\n",DebugType.INFO);
logger.print("New connection from: "~to!(string)(httpRequest.peer)~"\n",DebugType.INFO);
while(socket.waitForData())
{
string data;
try
{
import std.stdio;
writeln("Ha");
data = socket.receiveText();
writeln("Hello receve done");
}
catch(WebSocketException e)
{
logger.print("Error in receive text\n", DebugType.ERROR);
}
try
{
handler(data);
}
catch(Exception e)
{
logger.print("Error in handler\n", DebugType.ERROR);
}
}
logger.print("Web socket connection closing...\n", DebugType.WARNING);
}
/**
* Handles received data
*
* Params:
* text = received data
*/
private void handler(string text)
{
string receivedText = text;
logger.print(receivedText~"\n", DebugType.INFO);
JSONValue jsonReceived;
try
{
jsonReceived = parseJSON(receivedText);
logger.print(jsonReceived.toPrettyString()~"\n", DebugType.INFO);
// TODO: Add handling here
}
catch(JSONException e)
{
logger.print("There was an error parsing the client's JSON\n", DebugType.ERROR);
}
}
}

View File

@ -11,18 +11,22 @@ __gshared static this()
import core.thread : Thread; import core.thread : Thread;
import std.json;
import vibe.vibe : URLRouter, HTTPServerSettings; import vibe.vibe : URLRouter, HTTPServerSettings;
import vibe.vibe : WebSocket, handleWebSockets; import vibe.vibe : WebSocket, handleWebSockets;
import vibe.vibe; import vibe.vibe;
import nostril.connection; import vibe.vibe : WebSocket, WebSocketException;
import core.thread.fiber : Fiber;
/** /**
* Server * Server
* *
* A thread which manages all of the vibe.d fibers * A BRUHTODO which manages all of the vibe.d fibers
* for running a server which accepts client connections * for running a server which accepts client connections
*
* TODO: Make this a thread (so seperate FIberSchedulr associated with it I would hope)
*/ */
public class Server public class Server
{ {
@ -33,8 +37,18 @@ public class Server
private URLRouter router; private URLRouter router;
private @safe void delegate(scope HTTPServerRequest, HTTPServerResponse) websocketNegotiater; private @safe void delegate(scope HTTPServerRequest, HTTPServerResponse) websocketNegotiater;
/**
* Connection queue
*/
private Connection[Connection] connections;
/**
* TODO
*
* Params:
* bindAddresses =
* bindPort =
*/
this(string[] bindAddresses, ushort bindPort) this(string[] bindAddresses, ushort bindPort)
{ {
// Setup where to listen // Setup where to listen
@ -50,6 +64,9 @@ public class Server
router.get("/", this.websocketNegotiater); router.get("/", this.websocketNegotiater);
} }
/**
* TODO
*/
public void startServer() public void startServer()
{ {
// Bind the router to the server // Bind the router to the server
@ -79,17 +96,128 @@ public class Server
void websocketHandler(scope WebSocket socket) void websocketHandler(scope WebSocket socket)
{ {
/* Create a new connection to handle this client */ /* Create a new connection to handle this client */
Connection connection = new Connection(socket); Connection connection = new Connection(this, socket);
/* Add it to the queue */
// TODO: Add this
/* Call the fiber and let it start */ /* Call the fiber and let it start */
connection.call(); connection.call();
} }
/**
* Adds the given Connection to the connection queue
* even if it already exists in it (it won't duplicate)
*
* Params:
* newConnection = the connection to add
*/
public final void addConnection(Connection newConnection)
{
connections[newConnection] = newConnection;
}
public final void delConnection(Connection existingConnection)
{
connections.remove(existingConnection);
}
}
// TODO: This won't work with Thread, must be a fiber
// ... because of how vibe.d works
public class Connection : Fiber
{
/* Client socket */
private WebSocket socket;
/* Request information */
private HTTPServerRequest httpRequest;
/* The server instance associated with */
private Server server;
this(Server server, WebSocket ws)
{
super(&worker);
this.server = server;
this.socket = ws;
this.httpRequest = cast(HTTPServerRequest)socket.request();
}
private void worker()
{
/* Add it to the queue */
server.addConnection(this);
logger.print("Handling web socket: "~to!(string)(socket)~"\n",DebugType.INFO);
logger.print("New connection from: "~to!(string)(httpRequest.peer)~"\n",DebugType.INFO);
while(socket.waitForData())
{
string data;
try
{
import std.stdio;
writeln("Ha");
data = socket.receiveText();
writeln("Hello receve done");
}
catch(WebSocketException e)
{
logger.print("Error in receive text\n", DebugType.ERROR);
}
try
{
handler(data);
}
catch(Exception e)
{
logger.print("Error in handler\n", DebugType.ERROR);
}
}
/* Remove it from the queue */
server.delConnection(this);
logger.print("Web socket connection closing...\n", DebugType.WARNING);
}
/**
* Handles received data
*
* Params:
* text = received data
*/
private void handler(string text)
{
string receivedText = text;
logger.print(receivedText~"\n", DebugType.INFO);
JSONValue jsonReceived;
try
{
jsonReceived = parseJSON(receivedText);
logger.print(jsonReceived.toPrettyString()~"\n", DebugType.INFO);
// TODO: Add handling here
}
catch(JSONException e)
{
logger.print("There was an error parsing the client's JSON\n", DebugType.ERROR);
}
}
} }
/** /**