Exemplo n.º 1
0
        /// <summary>
        /// Helper method that parses received data, looking for newline characters
        /// Keeps going until queue of received callbacks and payloads, as well as data
        /// has been sent.
        /// </summary>
        private void MessageReceived()
        {           
            int index;
            // If there are callbacks / payloads pending, check data for new line chars
            if (_messagesReceived.Count > 0)
            {
                // get the index of the first new line char and use that to construct a new string
                // that will be returned.
                if ((index = _textReceivedSoFar.IndexOf('\n')) >= 0)
                {
                    _textToReturn = _textReceivedSoFar.Substring(0, index);
                    if (_textToReturn.EndsWith("\r"))
                    {
                        _textToReturn = _textToReturn.Substring(0, index - 1);
                    }

                    _textReceivedSoFar = _textReceivedSoFar.Substring(index + 1);
                    _messageReceived = _messagesReceived.Dequeue();
                    ThreadPool.QueueUserWorkItem(x => { _messageReceived.ReCallback(_textToReturn, null, _messageReceived.Payload); });
                }
                // If there is still a request... then call BeginReceive
                byte[] buffer = new byte[1024];
                _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, MessageReceivedCallback, buffer);
            }
            else
            {
                _receiveParseIsOngoing = false;
            }
        }