/* * SendMessage appends the message queue with an inMessage */ public void SendMessage(string inMessage) { lock (MessageQ) { MessageQ.Enqueue(inMessage); } SendMessageCallback(null); }
public override void Worker() { try { while (KeepRunning) { var item = _produce(); MessageQ.TryAdd(new Message{Data = item}); } } catch (ThreadInterruptedException) { WasInterrupted = true; } }
/* * SendMessageCallback transfer data to the destination through the NetworkStream */ private void SendMessageCallback(IAsyncResult result) { // result wont be null if current call to the SendMessageCallback is a continuation of BeginWrite execution // result will be null if current call to the SendMessageCallback is a continuation of SendMessage execution if (result != null && mNetworkStream != null) { mNetworkStream.EndWrite(result); lock (MessageQ){ sending = false; } } string CurrentMessage = null; lock (MessageQ) { //There is a possibility, that call to SendMessageCallback was made through SendMessage, //while another writing is in progress, so the call just returns, as inMessage was appended to the queue, and will be delivered in its turn if (sending) { return; } if (MessageQ.Count != 0) { sending = true; CurrentMessage = MessageQ.Dequeue(); //Console.WriteLine(CurrentMessage); } } //CurrentMessage wont be null if the queue was not empty //CurrentMessage will be null if the queue was empty if (CurrentMessage != null && mNetworkStream != null) { try { if (mTcpClient.Connected) { mNetworkStream.BeginWrite(Encoding.UTF8.GetBytes(CurrentMessage), 0, Encoding.UTF8.GetBytes(CurrentMessage).Length, new AsyncCallback(SendMessageCallback), null); } else { EmergencyDisc(); return; } } catch (NullReferenceException ex) { return; } } }
public override void Worker() { try { while (KeepRunning) { Message message; if (MessageQ.TryTake(out message, TimeSpan.FromMilliseconds(100))) { _consume(message.Data); } else { //There's nothing in the Q so I have some spare time... //Excellent moment to update my statisics or update some history to logfiles //for now we sleep: Thread.Sleep(TimeSpan.FromMilliseconds(100)); } } } catch (ThreadInterruptedException) { WasInterrupted = true; } }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// \fn public override void ArrangeMessageQ(MessageQ messageQueue) /// /// \brief Arrange the order of processing of the messages /// /// \par Description. /// This method is activated before retrieving a message from the message queue /// It gives a chance for the processor to determine the order of processing /// of the messages /// /// \par Algorithm. /// /// \par Usage Notes. /// /// \author Ilan Hindy /// \date 26/01/2017 /// /// \param destProcessId Identifier for the destination process. /// \param messageQueue The message queue. //////////////////////////////////////////////////////////////////////////////////////////////////// public override void ArrangeMessageQ(MessageQ messageQueue) { base.ArrangeMessageQ(messageQueue); }