1
0
mirror of https://github.com/deavmi/birchwood synced 2024-09-20 09:03:29 +02:00

- Calling quit() will now shutdown the whole client

This commit is contained in:
Tristan B. Velloza Kildaire 2022-11-05 12:37:27 +02:00
parent fbf9ef9eb8
commit 924ccbaacc

View File

@ -162,6 +162,8 @@ public final class Client : Thread
/* Event engine */ /* Event engine */
private Engine engine; private Engine engine;
private bool running = false;
this(ConnectionInfo connInfo) this(ConnectionInfo connInfo)
{ {
super(&loop); super(&loop);
@ -415,10 +417,11 @@ public final class Client : Thread
this.sendHandler = new Thread(&sendHandlerFunc); this.sendHandler = new Thread(&sendHandlerFunc);
this.sendHandler.start(); this.sendHandler.start();
/* Set running sttaus to true */
running = true;
/* Start socket loop */ /* Start socket loop */
this.start(); this.start();
} }
catch(SocketOSException e) catch(SocketOSException e)
{ {
@ -471,7 +474,7 @@ public final class Client : Thread
*/ */
private void recvHandlerFunc() private void recvHandlerFunc()
{ {
while(true) while(running)
{ {
/* Lock the receieve queue */ /* Lock the receieve queue */
recvQueueLock.lock(); recvQueueLock.lock();
@ -581,7 +584,7 @@ public final class Client : Thread
/* TODO: Hoist up into ConnInfo */ /* TODO: Hoist up into ConnInfo */
ulong fakeLagInBetween = 1; ulong fakeLagInBetween = 1;
while(true) while(running)
{ {
/* TODO: handle normal messages (xCount with fakeLagInBetween) */ /* TODO: handle normal messages (xCount with fakeLagInBetween) */
@ -633,6 +636,39 @@ public final class Client : Thread
/* Generate the quit command using the custom quit message */ /* Generate the quit command using the custom quit message */
Message quitCommand = new Message("", "QUIT", connInfo.quitMessage); Message quitCommand = new Message("", "QUIT", connInfo.quitMessage);
sendMessage(quitCommand.encode()); sendMessage(quitCommand.encode());
/* TODO: I don't know how long we should wait here */
Thread.sleep(dur!("seconds")(1));
/* TODO: Tare down */
disconnect();
}
/* Attempt to tare down everything */
private void disconnect()
{
/* Set the state of running to false */
running = false;
logger.log("disconnect() begin");
/* TODO: Should we do this here? */
// FIXME: Catch any errors
socket.close();
logger.log("disconnect() socket closed");
/* Wait for reeceive handler to realise it needs to stop */
recvHandler.join();
logger.log("disconnect() recvHandler stopped");
/* Wait for the send handler to realise it needs to stop */
sendHandler.join();
logger.log("disconnect() sendHandler stopped");
/* TODO: Stop eventy (FIXME: I don't know if this is implemented in Eventy yet, do this!) */
engine.shutdown();
logger.log("disconnect() eventy stopped");
logger.log("disconnect() end");
} }
private void processMessage(ubyte[] message) private void processMessage(ubyte[] message)
@ -670,8 +706,11 @@ public final class Client : Thread
/** /**
* Message loop * Message loop
*
* FIXME: We need to find a way to tare down this socket, we don't
* want to block forever after running quit
*/ */
while(true) while(running)
{ {
/* Receieve at most 512 bytes (as per RFC) */ /* Receieve at most 512 bytes (as per RFC) */
ptrdiff_t bytesRead = socket.receive(currentData, SocketFlags.PEEK); ptrdiff_t bytesRead = socket.receive(currentData, SocketFlags.PEEK);
@ -793,6 +832,9 @@ public final class Client : Thread
// } // }
Thread.sleep(dur!("seconds")(15));
client.quit();
} }