Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        protected override void OnBytesRead(byte[] buffer, int offset, int length)
        {
            for (var i = offset; i < offset + length; i++)
            {
                m_messageData.WriteByte(buffer[i]);
            }

            do
            {
                if (m_messageLength == -1 && m_messageData.Count > 3)
                {
                    m_messageLength = m_messageData.ReadInt();
                }
                if (m_messageLength != -1 && m_messageId == -1 && m_messageData.Count > 3)
                {
                    m_messageId = m_messageData.ReadInt();
                }
                if (m_messageLength != -1 && m_messageId != -1 && m_messageData.Count >= m_messageLength)
                {
                    var message = MessageBuilder.BuildMessage(m_messageId, m_messageData.ReadBytes(m_messageLength));

                    OnMessageEvent?.Invoke(message);

                    m_messageId     = -1;
                    m_messageLength = -1;
                }
            }while ((m_messageLength == -1 || m_messageId == -1) && m_messageData.Count > 3);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public IEnumerable <AbstractRcpMessage> GetMessages(byte[] buffer, int offset, int length)
        {
            for (int i = offset; i < offset + length; i++)
            {
                m_messageData.WriteByte(buffer[i]);
            }

            do
            {
                if (m_messageLength == -1 && m_messageData.Count > 3)
                {
                    m_messageLength = m_messageData.ReadInt();
                }
                if (m_messageLength != -1 && m_messageId == -1 && m_messageData.Count > 3)
                {
                    m_messageId = m_messageData.ReadInt();
                }
                if (m_messageLength != -1 && m_messageId != -1 && m_messageData.Count >= m_messageLength)
                {
                    yield return(MessageBuilder.BuildMessage(m_messageId, m_messageData.ReadBytes(m_messageLength)));

                    m_messageId     = -1;
                    m_messageLength = -1;
                }
            }while ((m_messageLength == -1 || m_messageId == -1) && m_messageData.Count > 3);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IEnumerable <string> Receive(byte[] buffer, int offset, int count)
        {
            for (int i = offset; i < offset + count; i++)
            {
                if (buffer[i] == 0x00)
                {
                    if (Environment.TickCount - LastPacketTime < 1000)
                    {
                        CumulatedPacketInOneSecond++;
                        if (CumulatedPacketInOneSecond > 25)
                        {
                            Logger.Warn("Client kicked due to packet spam : " + Ip);
                            Disconnect();
                            break;
                        }
                    }
                    else
                    {
                        CumulatedPacketInOneSecond = 1;
                        LastPacketTime             = Environment.TickCount;
                    }

                    if (Cypher)
                    {
                        yield return(Crypt.UnprepareData(Encoding.UTF8.GetString(m_messageQueue.ReadBytes(m_messageQueue.Count))));
                    }
                    else
                    {
                        yield return(Encoding.UTF8.GetString(m_messageQueue.ReadBytes(m_messageQueue.Count)));
                    }
                }
                else
                {
                    if (buffer[i] != '\n')
                    {
                        m_messageQueue.WriteByte(buffer[i]);
                    }
                }
            }
        }