Beispiel #1
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(ENetClient client, Channel channel, byte[] buffer)
        {
            var reader = new LittleEndianReader(buffer);

            PacketCmd messageId = (PacketCmd)reader.ReadByte();

            Message message;

            try
            {
                message = ConstructMessage(messageId, channel, reader);

                if (message == null)
                {
                    logger.Write("Message: " + messageId + " not registered in protocol", MessageState.WARNING);
                }
                return(message);
            }
            catch (Exception ex)
            {
                logger.Write("Exception while building Message : (" + messageId + ") =>" + ex.Message, MessageState.WARNING);
                return(null);
            }
            finally
            {
                reader.Dispose();
            }
        }
Beispiel #2
0
        public static bool HandleMessage(Message message, ENetClient client)
        {
            if (message == null)
            {
                // client.Disconnect();
                return(false);
            }

            var handler = Handlers.FirstOrDefault(x => x.Key == message.Cmd);

            if (handler.Value != null)
            {
                {
                    if (ShowProtocolMessage)
                    {
                        logger.Write("Receive " + message.ToString(), MessageState.INFO);
                    }

                    try
                    {
                        client.OnMessageHandle(message, handler.Value);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        logger.Write(string.Format("Unable to handle message {0} {1} : '{2}'", message.ToString(), handler.Value.Method.Name, ex.InnerException.ToString()), MessageState.WARNING);
                        return(false);
                    }
                }
            }
            else
            {
                if (ShowProtocolMessage)
                {
                    logger.Write(string.Format("No Handler: ({0}) {1}", message.Cmd.ToString(), message.ToString()), MessageState.IMPORTANT_INFO);
                }
                return(true);
            }
        }