Ejemplo n.º 1
0
 protected override void ReceiveEvent_Completed(object sender, SocketAsyncEventArgs e)
 {
     if (!IsConnected())
     {
         return;
     }
     _reader.Add(e.Buffer, e.Offset, e.BytesTransferred);
     while (_reader.BytesAvailable > 0)
     {
         if (_currentMessage == null)
         {
             _currentMessage = new MessagePart();
         }
         if (_currentMessage.Build(_reader))
         {
             var messageDataReader = new CustomDataReader(_currentMessage.Data);
             var message           = MessageReceiver.BuildMessage((uint)_currentMessage.MessageId, messageDataReader);
             if (message == null)
             {
                 return;
             }
             _dispatcher.Dispatch(message);
             _currentMessage = null;
         }
         else
         {
             break;
         }
     }
     base.ReceiveEvent_Completed(sender, e);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Build a messagePart and call the ConstructMessage(); method.
        /// </summary>
        /// <param name="buffer">data received</param>
        /// <returns>Message of your protocol, builted</returns>
        public static Message BuildMessage(byte[] buffer)
        {
            var reader      = new CustomDataReader(buffer);
            var messagePart = new MessagePart(false);

            if (messagePart.Build(reader))
            {
                Message message;
                try
                {
                    message = ConstructMessage((ushort)messagePart.MessageId.Value, reader);
                    return(message);
                }
                catch (Exception ex)
                {
                    logger.Alert("Error while building Message :" + ex.Message);
                    return(null);
                }
                finally
                {
                    reader.Dispose();
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        private void Receive()
        {
            if (m_buffer.BytesAvailable <= 0)
            {
                return;
            }

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

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

                    if (message.MessageId == 5632)
                    {
                        Debug.Print("Message = {0}", m_currentMessage.Data);
                    }

                    if (OnNewMessage != null)
                    {
                        OnNewMessage(message, m_name);
                    }
                }
                catch (Exception ex)
                {
                    if (m_currentMessage.MessageId == 5632)
                    {
                        Debug.Print("Message = {0}", m_currentMessage.Data);
                    }

                    var fullData = new List <byte>
                    {
                        (byte)((m_currentMessage.Header & 0xFF00) >> 8),
                        (byte)(m_currentMessage.Header & 0xFF)
                    };
                    fullData.AddRange(m_currentMessage.Data);
                    message = new ErrorMessage(fullData.ToArray(), ex.Message);

                    if (OnNewMessage != null)
                    {
                        OnNewMessage(message, m_name);
                    }
                }

                m_currentMessage = null;

                Receive(); // there is maybe a second message in the buffer
            }
        }
Ejemplo n.º 4
0
        protected virtual bool BuildMessage(BufferSegment buffer)
        {
            if (m_currentMessage == null)
            {
                m_currentMessage = new MessagePart(false);
            }

            var reader = new FastBigEndianReader(buffer)
            {
                Position    = buffer.Offset + m_readOffset,
                MaxPosition = buffer.Offset + m_readOffset + m_remainingLength,
            };

            // if message is complete
            if (m_currentMessage.Build(reader))
            {
                var dataPos = reader.Position;
                // prevent to read above
                reader.MaxPosition = dataPos + m_currentMessage.Length.Value;

                Message message;
                try
                {
                    message = MessageReceiver.BuildMessage((uint)m_currentMessage.MessageId.Value, reader);
                }
                catch (Exception)
                {
                    if (m_currentMessage.ReadData)
                    {
                        logger.Debug("Message = {0}", m_currentMessage.Data.ToString(" "));
                    }
                    else
                    {
                        reader.Seek(dataPos, SeekOrigin.Begin);
                        logger.Debug("Message = {0}", reader.ReadBytes(m_currentMessage.Length.Value).ToString(" "));
                    }
                    throw;
                }


                OnMessageReceived(message);

                m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
                m_writeOffset      = m_readOffset = (int)reader.Position - buffer.Offset;
                m_currentMessage   = null;

                return(m_remainingLength <= 0 || BuildMessage(buffer));
            }

            m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
            m_readOffset       = (int)reader.Position - buffer.Offset;
            m_writeOffset      = m_readOffset + m_remainingLength;

            EnsureBuffer(m_currentMessage.Length.HasValue ? m_currentMessage.Length.Value : 3);

            return(false);
        }
Ejemplo n.º 5
0
 public override void OnReceive(byte[] data)
 {
     using (BigEndianReader reader = new BigEndianReader(data))
     {
         MessagePart messagePart = new MessagePart(false);
         if (messagePart.Build(reader))
         {
             dynamic message = MessageReceiver.BuildMessage((uint)messagePart.Id, reader);
             ConsoleUtils.Write(ConsoleUtils.Type.RECEIV, $"{message} Id {messagePart.Id} Length {messagePart.Length} ...");
             MessageHandlerManager <LoginClient> .InvokeHandler(this, message);
         }
     }
 }
Ejemplo n.º 6
0
        public static void Dispatch(AbstractClient client, byte[] data)
        {
            if (!IsInitialized)
            {
                return;
            }

            if (client == null)
            {
                return;
            }

            BigEndianReader reader = new BigEndianReader(data);

            while (reader.BytesAvailable >= 2)
            {
                short header        = reader.ReadShort();
                uint  messageId     = (uint)header >> 2;
                int   m_lenghtcount = header & 3;

                if (reader.BytesAvailable >= m_lenghtcount)
                {
                    if ((m_lenghtcount < 0) || (m_lenghtcount > 3))
                    {
                        throw new Exception("Malformated Message Header, invalid bytes number to read messages");
                    }
                    else
                    {
                        if (messageId != 0)
                        {
                            if (MessageList.ContainsKey(messageId))
                            {
                                NetworkMessage message = (NetworkMessage)Activator.CreateInstance(MessageList[messageId]);
                                reader = new BigEndianReader(data);

                                var messagePart = new MessagePart();
                                messagePart.Build(reader, true);

                                reader = new BigEndianReader(messagePart.Data);

                                Console.WriteLine("Received: {0}:{1}.", message.MessageId, message.GetType().Name);

                                message.Deserialize(reader);
                                Build(client, message);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
 private void GameClient_OnClientReceivedData(byte[] data)
 {
     using (BigEndianReader reader = new BigEndianReader(data))
     {
         MessagePart messagePart = new MessagePart(false);
         if (messagePart.Build(reader))
         {
             dynamic message = MessageReceiver.BuildMessage((uint)messagePart.Id, reader);
             if (Config.Debug)
             {
                 ConsoleUtils.Write(ConsoleUtils.Type.RECEIV, $"{message} Id {messagePart.Id} Length {messagePart.Length} ...");
             }
             MessageHandlerManager <Client> .InvokeHandler(this, message);
         }
     }
 }
Ejemplo n.º 8
0
        protected virtual bool BuildMessage(BufferSegment buffer)
        {
            if (m_semaphore.CurrentCount == 0)
            {
                Console.WriteLine("Client Busy");
            }

            m_semaphore.Wait();

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

            var reader = new FastBigEndianReader(buffer)
            {
                Position    = buffer.Offset + m_readOffset,
                MaxPosition = buffer.Offset + m_readOffset + m_remainingLength,
            };

            // if message is complete
            if (m_currentMessage.Build(reader))
            {
                var dataPos = reader.Position;
                // prevent to read above
                reader.MaxPosition = dataPos + m_currentMessage.Length.Value;

                Message message;
                try
                {
                    message = MessageReceiver.BuildMessage((uint)m_currentMessage.MessageId.Value, reader);
                }
                catch (Exception)
                {
                    if (m_currentMessage.ReadData)
                    {
                        logger.Debug("Message = {0}", m_currentMessage.Data.ToString(" "));
                    }
                    else
                    {
                        reader.Seek(dataPos, SeekOrigin.Begin);
                        logger.Debug("Message = {0}", reader.ReadBytes(m_currentMessage.Length.Value).ToString(" "));
                    }
                    throw;
                }


                if (LogPackets)
                {
                    Console.WriteLine($"(RECV) {this} : {message}");
                }

                OnMessageReceived(message);

                m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
                m_writeOffset      = m_readOffset = (int)reader.Position - buffer.Offset;
                m_currentMessage   = null;

                return(m_remainingLength <= 0 || BuildMessage(buffer));
            }

            logger.Debug("Message truncated, ensure buffer is big enough ...");

            m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
            m_readOffset       = (int)reader.Position - buffer.Offset;
            m_writeOffset      = m_readOffset + m_remainingLength;

            EnsureBuffer(m_currentMessage.Length.HasValue ? m_currentMessage.Length.Value : 5); // 5 is the max header size

            m_semaphore.Release();

            return(false);
        }