/// <summary> /// Run one pass of the main loop. /// </summary> private void RunPass() { // Wait for a command. while (m_commandQueue.Count == 0) Block(new SelectSockets()); // Get the next command. KmodThreadCommand command = m_commandQueue.Dequeue(); // Send the command. m_transport.sendMsg(command.Msg); while (true) { m_transport.doXfer(); if (!m_transport.isSending) break; SelectSockets select = new SelectSockets(); select.AddWrite(m_kmodSock); Block(select); } // We're done. if (!command.HaveResultFlag) return; // Wait for the results or the next command. bool firstFlag = true; while (m_commandQueue.Count == 0) { if (!m_transport.isReceiving) m_transport.beginRecv(); m_transport.doXfer(); // Push the next result. if (m_transport.doneReceiving) { lock (Mon) { m_elementQueue.Enqueue(m_transport.getRecv()); Monitor.Pulse(Mon); } // Send the notification that results are ready for this command. if (firstFlag) { firstFlag = false; PostToUI(new KmodThreadNotif(m_broker, command)); } } // Wait for the next result or the next command. else { SelectSockets select = new SelectSockets(); select.AddRead(m_kmodSock); Block(select); } } }
/// <summary> /// Check if we are connected. /// </summary> public override void BeforeSelectConnecting(SelectSockets selectSockets) { // The socket is connected. if (Sock.Poll(0, SelectMode.SelectWrite)) { Status = EAnpThreadChannelStatus.Handshake; selectSockets.LowerTimeout(0); } // The connection has failed. else if (Sock.Poll(0, SelectMode.SelectError)) { throw new Exception("the connection to the KWM could not be established"); } // Add the socket in the write set and wait forever. else { selectSockets.AddWrite(Sock); } }
/// <summary> /// Prepare to send handshake data. /// </summary> public override void BeforeSelectHandshake(SelectSockets selectSockets) { selectSockets.AddWrite(Sock); }
/// <summary> /// Update the select set specified with the socket of the transport. /// </summary> public void UpdateSelect(SelectSockets selectSocket) { Debug.Assert(IsReceiving || DoneReceiving); if (IsSending) selectSocket.AddWrite(sock); if (IsReceiving && !DoneReceiving) selectSocket.AddRead(sock); }