private void ProcessReceivedData(ReceivedDataStateObject stateObject)
        {
            if (stateObject == null || stateObject.Data == null)
            {
                return;
            }

            var receivedData = XDocument.Parse(stateObject.Data);
            var root         = receivedData.Root;

            if (root == null)
            {
                return;
            }

            if (_pendingCommand != null && root.Name.LocalName != "CornerstoneMessage")
            {
                var sender = _pendingCommand.Sender;
                if (sender != null)
                {
                    //Send the received data back to the object that issued the command so it can process the response.
                    sender.ProcessResponse(receivedData);
                }

                //We no longer have a pending command to keep track of.
                _pendingCommand = null;
            }
            else
            {
                EventAggregatorContext.Current.GetEvent <MessageDataEvent>().Publish(receivedData);
            }
        }
Exemple #2
0
        private void ProcessReceivedData(ReceivedDataStateObject stateObject)
        {
            if (stateObject == null || stateObject.Data == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(stateObject.Data) && stateObject.Data.TrimStart().StartsWith("<"))
            {
                var receivedData = XDocument.Parse(stateObject.Data);
                var root         = receivedData.Root;
                if (root == null)
                {
                    return;
                }

                if (_pendingCommand != null && root.Name.LocalName != "CornerstoneMessage")
                {
                    var sender = _pendingCommand.Sender;
                    //Send the received data back to the object that issued the command so it can process the response.
                    sender?.ProcessResponse(receivedData);

                    //We no longer have a pending command to keep track of.
                    _pendingCommand = null;
                }
                else
                {
                    EventAggregatorContext.Current.GetEvent <MessageDataEvent>().Publish(receivedData);
                }
            }
            else if (!string.IsNullOrEmpty(stateObject.Data) &&
                     (stateObject.Data.TrimStart().StartsWith("{") || stateObject.Data.TrimStart().StartsWith("[")))
            {
                var sender = _pendingCommand.Sender;
                //Send the received data back to the object that issued the command so it can process the response.
                sender?.ProcessResponse(stateObject.Data);
                _pendingCommand = null;
            }
            else
            {
                var sender = _pendingCommand.Sender;
                if (sender != null)
                {
                    //Send the received data back to the object that issued the command so it can process the response.
                    sender.ProcessResponse(stateObject.Data);
                }
                EventAggregatorContext.Current.GetEvent <MessageData2Event>().Publish(stateObject.Data);
            }

            /*var receivedData = XDocument.Parse(stateObject.Data);
             * var root = receivedData.Root;
             * if (root == null)
             * {
             *  return;
             * }
             *
             * if (_pendingCommand != null && root.Name.LocalName != "CornerstoneMessage")
             * {
             *  var sender = _pendingCommand.Sender;
             *  if (sender != null)
             *  {
             *      //Send the received data back to the object that issued the command so it can process the response.
             *      sender.ProcessResponse(receivedData);
             *  }
             *
             *  //We no longer have a pending command to keep track of.
             *  _pendingCommand = null;
             * }
             * else
             * {
             *  EventAggregatorContext.Current.GetEvent<MessageDataEvent>().Publish(receivedData);
             * }*/
        }
Exemple #3
0
        /// <summary>
        /// Called when data is received from Cornerstone. This method will be called multiple
        /// times until all of the data in response to a command is received. When Cornerstone
        /// responds to a command, it will first send the number of bytes contained in the command
        /// response, followed by the command response itself. This method uses the ReceivedDataStateObject
        /// to keep a running compilation of the data received in the case where Cornerstone's response
        /// does not arrive all at once.
        /// </summary>
        /// <param name="ar">Received data.</param>
        private void DataReceived(IAsyncResult ar)
        {
            try
            {
                var stateObject = ar.AsyncState as ReceivedDataStateObject;
                if (stateObject == null)
                {
                    //The state object is null which indicates that this is a new response and not a continuation
                    //of a previous response.
                    var bytes = _tcpClient.GetStream().EndRead(ar);

                    // Detect the stream ended.
                    if (bytes == 0)
                    {
                        return;
                    }

                    //Get the number of bytes contained in the command response.
                    int length = BitConverter.ToInt32(_receiveBuffer, 0);
                    //Create a state object to hold onto the response.
                    stateObject = new ReceivedDataStateObject {
                        Length = length
                    };

                    EventAggregatorContext.Current.GetEvent <RecordDataTrafficEvent>().Publish(new DataTrafficSentReceivedViewModel(_receiveBuffer, false, bytes));

                    //check if we have received more than 4 bytes. The 4 bytes contains the length of the following
                    //data. If we have received more than the 4 bytes, we need to add that to our buffer.
                    if (bytes > 4)
                    {
                        stateObject.Length -= (bytes - 4);
                        stateObject.Data   += EncodingToUse.GetString(_receiveBuffer, 4, bytes - 4);

                        //check if we have received it all.
                        if (stateObject.Length <= 0)
                        {
                            //process the data we have received.
                            ProcessReceivedData(stateObject);
                        }
                    }
                    //Set ourselves up to be notified when more data arrives.
                    _tcpClient.GetStream().BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, DataReceived, stateObject);
                }
                else
                {
                    //Find out how many bytes are in the response.
                    var bytes = _tcpClient.GetStream().EndRead(ar);
                    //Subtract them from the total that we are expecting.
                    stateObject.Length -= bytes;
                    //Append the data to our running compilation.
                    stateObject.Data += EncodingToUse.GetString(_receiveBuffer, 0, bytes);

                    EventAggregatorContext.Current.GetEvent <RecordDataTrafficEvent>().Publish(new DataTrafficSentReceivedViewModel(_receiveBuffer, false, bytes));

                    if (stateObject.Length > 0)
                    {
                        //We are still waiting for more of the response, so starting waiting....
                        _tcpClient.GetStream().BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, DataReceived, stateObject);
                    }
                    else
                    {
                        //process the data we have received.
                        ProcessReceivedData(stateObject);

                        //Start waiting for the next response.
                        _tcpClient.GetStream().BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, DataReceived, null);
                    }
                }
            }
            catch
            {
                Disconnect();
            }
        }