/// <summary> /// Receive and return a Command from the command-pipe. /// </summary> /// <param name="timeout">how long to wait for a command (in milliseconds) before returning</param> /// <param name="command"></param> public bool TryRecv(int timeout, out Command command) { // Try to get the command straight away. if (m_active) { if (m_commandPipe.TryRead(out command)) { return(true); } // If there are no more commands available, switch into passive state. m_active = false; m_signaler.Recv(); } // Wait for signal from the command sender. if (!m_signaler.WaitEvent(timeout)) { command = default(Command); return(false); } // We've got the signal. Now we can switch into active state. m_active = true; // Get a command. var ok = m_commandPipe.TryRead(out command); Debug.Assert(ok); return(ok); }
/// <summary> /// Checks if there is at least one message to read in the pipe. /// </summary> /// <returns> Returns <c>true</c> if there is at least one message to read in the pipe; <c>false</c> otherwise. </returns> public bool CheckRead() { if (!m_inActive || (m_state != State.Active && m_state != State.Pending)) { return(false); } // Check if there's an item in the pipe. if (!m_inboundPipe.CheckRead()) { m_inActive = false; return(false); } // If the next item in the pipe is message delimiter, // initiate termination process. if (m_inboundPipe.Probe().IsDelimiter) { var msg = new Msg(); bool ok = m_inboundPipe.TryRead(out msg); Debug.Assert(ok); Delimit(); return(false); } return(true); }
/// <summary> /// Create a new Mailbox with the given name. /// </summary> /// <param name="name">the name to give this new Mailbox</param> public Mailbox([NotNull] string name) { // Get the pipe into passive state. That way, if the users starts by // polling on the associated file descriptor it will get woken up when // new command is posted. Command cmd; bool ok = m_commandPipe.TryRead(out cmd); Debug.Assert(!ok); m_active = false; #if DEBUG m_name = name; #endif }
/// <summary> /// This method is called to assign the specified pipe as a replacement for the outbound pipe that was being used. /// </summary> /// <param name="pipe">the pipe to use for writing</param> /// <remarks> /// A "Hiccup" occurs when an outbound pipe experiences something like a transient disconnect or for whatever other reason /// is no longer available for writing to. /// </remarks> protected override void ProcessHiccup(object pipe) { // Destroy old out-pipe. Note that the read end of the pipe was already // migrated to this thread. Assumes.NotNull(m_outboundPipe); m_outboundPipe.Flush(); var msg = new Msg(); while (m_outboundPipe.TryRead(out msg)) { msg.Close(); } // Plug in the new out-pipe. Assumes.NotNull(pipe); m_outboundPipe = (YPipe <Msg>)pipe; m_outActive = true; // If appropriate, notify the user about the hiccup. if (m_state == State.Active) { Assumes.NotNull(m_sink); m_sink.Hiccuped(this); } }
public IOThreadMailbox([NotNull] string name, [NotNull] Proactor proactor, [NotNull] IMailboxEvent mailboxEvent) { m_proactor = proactor; m_mailboxEvent = mailboxEvent; // Get the pipe into passive state. That way, if the users starts by // polling on the associated file descriptor it will get woken up when // new command is posted. Command cmd; bool ok = m_commandPipe.TryRead(out cmd); Debug.Assert(!ok); #if DEBUG m_name = name; #endif }
public bool TryRecv(int timeout, out Command command) { // Try to get the command straight away. if (m_commandPipe.TryRead(out command)) { return(true); } // If the timeout is zero, it will be quicker to release the lock, giving other a chance to send a command // and immediately relock it. if (timeout == 0) { Monitor.Exit(m_sync); Monitor.Enter(m_sync); } else { // Wait for signal from the command sender. Monitor.Wait(m_sync, timeout); } // Another thread may already fetch the command return(m_commandPipe.TryRead(out command)); }
public bool TryRecv(out Command command) { return(m_commandPipe.TryRead(out command)); }