Example #1
0
        private void BuildMessage()
        {
            if (m_buffer.BytesAvailable <= 0)
            {
                return;
            }

            if (m_currentMessage == null)
            {
                m_currentMessage = new MessagePart();
            }

            // if message is complete
            if (m_currentMessage.Build(m_buffer))
            {
                var messageDataReader = new BigEndianReader(m_currentMessage.Data);
                var message           = MessageBuilder.BuildMessage((uint)m_currentMessage.MessageId.Value, messageDataReader);

                LastActivity = DateTime.Now;

                OnMessageReceived(message);

                m_currentMessage = null;
                BuildMessage(); // there is maybe a second message in the buffer
            }
        }
        public virtual void Receive(byte[] data, int offset, int count)
        {
            lock (m_recvLocker)
            {
                m_buffer.Add(data, offset, count);

                while (m_buffer.BytesAvailable > 0)
                {
                    if (m_currentMessage == null)
                    {
                        m_currentMessage = new MessagePart();
                    }

                    // if message is complete
                    if (m_currentMessage.Build(m_buffer))
                    {
                        var            messageDataReader = new BigEndianReader(m_currentMessage.Data);
                        NetworkMessage message           = MessageBuilder.BuildMessage((uint)m_currentMessage.MessageId.Value, messageDataReader);

                        LastActivity = DateTime.Now;
                        OnMessageReceived(message);

                        m_currentMessage = null;
                    }
                }
            }
        }