diff --git a/source/birchwood/client/client.d b/source/birchwood/client/client.d index 26739b1..c6888b1 100644 --- a/source/birchwood/client/client.d +++ b/source/birchwood/client/client.d @@ -11,7 +11,7 @@ import eventy : EventyEvent = Event, Engine, EventType, Signal; import birchwood.config : ConnectionInfo; import birchwood.client.exceptions : BirchwoodException; import birchwood.protocol.messages : Message, encodeMessage, decodeMessage, isValidText; -// import birchwood.protocol.constants : ReplyType; + import birchwood.client.receiver : ReceiverThread; import birchwood.client.sender : SenderThread; import birchwood.client.events; @@ -570,13 +570,19 @@ public class Client : Thread /* Register default handler */ initEvents(); - /** - * Start the receive and send queue manager - */ + // /** + // * Initialize the ready events for both the + // * receive and send queue managers, then after + // * doing so start both managers and spin for + // * both of them to enter a ready state (i.e. + // * they have ensured a waiting-pipe pair for + // * libsnooze exists) + // */ this.receiver.start(); this.sender.start(); + // while(!receiver.isReady() || !sender.isReady()) {} - /* Set running sttaus to true */ + /* Set running status to true */ running = true; /* Start socket loop */ @@ -587,6 +593,7 @@ public class Client : Thread throw new BirchwoodException(BirchwoodException.ErrorType.CONNECT_ERROR); } } + // TODO: Do actual liveliness check here else { throw new BirchwoodException(BirchwoodException.ErrorType.ALREADY_CONNECTED); diff --git a/source/birchwood/client/receiver.d b/source/birchwood/client/receiver.d index bfca735..94b3bc2 100644 --- a/source/birchwood/client/receiver.d +++ b/source/birchwood/client/receiver.d @@ -30,6 +30,7 @@ public final class ReceiverThread : Thread * to be processed and received */ private Event receiveEvent; + // private bool hasEnsured; /** * The associated IRC client @@ -48,7 +49,7 @@ public final class ReceiverThread : Thread super(&recvHandlerFunc); this.client = client; this.receiveEvent = new Event(); // TODO: Catch any libsnooze error here - this.recvQueueLock = new Mutex(); + this.recvQueueLock = new Mutex(); } // TODO: Rename to `receiveQ` @@ -92,17 +93,27 @@ public final class ReceiverThread : Thread { while(client.running) { - // TODO: Insert libsnooze wait here - - // TODO: Add a for-loop here which one can configure which is - // ... a "per iteration" how much to process and act on - // TODO: We could look at libsnooze wait starvation or mutex racing (future thought) + + // // Do a once-off call to `ensure()` here which then only runs once and + // // ... sets a `ready` flag for the Client to spin on. This ensures that + // // ... when the first received messages will be able to cause a wait + // // ... to immediately unblock rather than letting wait() register itself + // // ... and then require another receiveQ call to wake it up and process + // // ... the initial n messages + m new ones resulting in the second call + // if(hasEnsured == false) + // { + // receiveEvent.ensure(); + // hasEnsured = true; + // } + // TODO: See above notes about libsnooze behaviour due // ... to usage in our context receiveEvent.wait(); // TODO: Catch any exceptions from libsnooze + + /* Lock the receieve queue */ recvQueueLock.lock(); @@ -208,4 +219,9 @@ public final class ReceiverThread : Thread // ... to usage in our context receiveEvent.notifyAll(); } + + // public bool isReady() + // { + // return hasEnsured; + // } } \ No newline at end of file diff --git a/source/birchwood/client/sender.d b/source/birchwood/client/sender.d index 3bd9bb9..2db0b65 100644 --- a/source/birchwood/client/sender.d +++ b/source/birchwood/client/sender.d @@ -25,6 +25,7 @@ public final class SenderThread : Thread * to be processed and sent */ private Event sendEvent; + // private bool hasEnsured; /** * The associated IRC client @@ -83,10 +84,17 @@ public final class SenderThread : Thread while(client.running) { - // TODO: Insert libsnooze wait here - - // TODO: Add a for-loop here which one can configure which is - // ... a "per iteration" how much to process and act on + // // Do a once-off call to `ensure()` here which then only runs once and + // // ... sets a `ready` flag for the Client to spin on. This ensures that + // // ... when the first sent messages will be able to cause a wait + // // ... to immediately unblock rather than letting wait() register itself + // // ... and then require another sendQ call to wake it up and process + // // ... the initial n messages + m new ones resulting in the second call + // if(hasEnsured == false) + // { + // sendEvent.ensure(); + // hasEnsured = true; + // } // TODO: We could look at libsnooze wait starvation or mutex racing (future thought) @@ -96,6 +104,10 @@ public final class SenderThread : Thread // ... to usage in our context sendEvent.wait(); // TODO: Catch any exceptions from libsnooze + // TODO: After the above call have a once-off call to `ensure()` here + // ... which then only runs once and sets a `ready` flag for the Client + // ... to spin on + /* Lock queue */ sendQueueLock.lock(); @@ -120,4 +132,9 @@ public final class SenderThread : Thread // ... to usage in our context sendEvent.notifyAll(); } + + // public bool isReady() + // { + // return hasEnsured; + // } } \ No newline at end of file