/// <summary> /// Getting IORB requests from IORequests queue, sending it to Simulator; /// Receiving Simulator answers, enqueing it to IOAnswers queue. /// </summary> public void ProcessRequestsFromQueue(TimeSpan timeout, CancellationToken token) { try { Console.WriteLine("Process Request form queue thread id={0}", Thread.CurrentThread.ManagedThreadId); while (!token.IsCancellationRequested) { bool isSuccessful; IORequestBlock forProcess = IORequests.DequeueRequest(out isSuccessful, timeout); if (isSuccessful) { CommunicationObject commObj; if (CommunicationManager.CommunicationObjects.TryGetValue(forProcess.ProcessControllerName, out commObj)) { // to do: napraviti taskove i asinhrono if (commObj.ProcessRequest(forProcess)) { } else { } } } } } catch (Exception e) { } Console.WriteLine("Process requests Action Finished."); }
/// <summary> /// Getting IORB requests from IORequests queue, sending it to Simulator; /// Receiving Simulator answers, enqueing it to IOAnswers queue. /// </summary> public void ProcessRequestsFromQueue() { while (!isShutdown) { bool isSuccessful; IORequestBlock forProcess = IORequests.DequeueRequest(out isSuccessful); if (isSuccessful) { TcpClient client; if (TcpChannels.TryGetValue(forProcess.ProcessControllerName, out client)) { try { // to do: test this case...connection lasts forever? if (!client.Connected) { processControllers.TryGetValue(forProcess.ProcessControllerName, out ProcessController rtu); client.Connect(rtu.HostName, rtu.HostPort); } NetworkStream stream = client.GetStream(); int offset = 0; stream.Write(forProcess.SendBuff, offset, forProcess.SendMsgLength); // to do: processing big messages. whole, or in parts? // ... forProcess.RcvBuff = new byte[client.ReceiveBufferSize]; var length = stream.Read(forProcess.RcvBuff, offset, client.ReceiveBufferSize); forProcess.RcvMsgLength = length; IORequests.EnqueueAnswer(forProcess); } catch (Exception e) { // to do: handle this... Console.WriteLine(e.Message); //if (client.Connected) // client.Close(); // TcpChannels.Remove(toProcess.RtuName); } } else { Console.WriteLine("\nThere is no communication link with {0} rtu. Request is disposed.", forProcess.ProcessControllerName); } } Thread.Sleep(millisecondsTimeout: timerMsc); } }