public Message Deserialize(Stream stream, out SerializationContext serializationContext)
        {
            serializationContext = null;
            var reader = new StreamReader(stream) { Position = 0 };
            var subTypeInfo = this.packetInspector.FindSubType(reader);

            if (subTypeInfo == null)
            {
                return null;
            }

            var serializer = this.serializerResolver.GetSerializer(subTypeInfo.Type);
            if (serializer == null)
            {
                return null;
            }

            reader.Position = 0;
            serializationContext = new SerializationContext(this.serializerResolver);
            var message = new Message
                              {
                                  Header = (Header)this.headerSerializer.Deserialize(reader, serializationContext), 
                                  Body = (MessageBody)serializer.Deserialize(reader, serializationContext)
                              };
            return message;
        }
 /// <summary>
 /// </summary>
 /// <param name="message">
 /// </param>
 /// <returns>
 /// </returns>
 public byte[] Serialize(Message message)
 {
     using (var stream = new MemoryStream())
     {
         this.serializer.Serialize(stream, message);
         return stream.ToArray();
     }
 }
        public byte[] Serialize(Message message, out SerializationContext serializationContext)
        {
            Contract.Requires<ArgumentNullException>(message != null);
            Contract.Requires<ArgumentNullException>(message.Header != null);
            Contract.Requires<ArgumentNullException>(message.Body != null);
            Contract.Ensures(Contract.Result<byte[]>() != null);
            Contract.Ensures(Contract.Result<byte[]>().Length >= 16);

            throw new NotImplementedException();
        }
        public byte[] Serialize(Message message, out SerializationContext serializationContext)
        {
            using (var memoryStream = new MemoryStream())
            {
                this.messageSerializer.Serialize(memoryStream, message, out serializationContext);
                var buffer = memoryStream.GetBuffer();
                if (buffer == null || buffer.Length < 16)
                {
                    throw new InvalidOperationException();
                }

                return buffer;
            }
        }
Beispiel #5
0
        /// <summary>
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <param name="stat">
        /// </param>
        /// <param name="value">
        /// </param>
        /// <param name="announce">
        /// </param>
        public static void Send(IZoneClient client, int stat, uint value, bool announce)
        {
            var statMessage = new StatMessage
                              {
                                  Identity = client.Character.Identity,
                                  Stats =
                                      new[]
                                      {
                                          new GameTuple<CharacterStat, uint>
                                          {
                                              Value1 =
                                                  (CharacterStat)stat,
                                              Value2 = value
                                          }
                                      }
                              };
            var statM = new Message { Body = statMessage };
            if (!client.Character.DoNotDoTimers)
            {
                client.Character.Playfield.Publish(
                    new IMSendAOtomationMessageToClient { client = client, message = statM });
            }

            /* announce to playfield? */
            if (announce)
            {
                client.Character.Playfield.AnnounceOthers(statMessage, client.Character.Identity);
            }
        }
 public byte[] Serialize(Message message)
 {
     SerializationContext ignore;
     return this.Serialize(message, out ignore);
 }
 /// <summary>
 /// </summary>
 /// <param name="sender">
 /// </param>
 /// <param name="message">
 /// </param>
 public MessageReceivedEvent(object sender, Message message)
 {
     this.sender = sender;
     this.message = message;
 }
        /// <summary>
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="message">
        /// </param>
        public void Publish(object sender, Message message)
        {
            IList<IHandleMessage> handlers;
            if (this.messageHandlers.TryGetValue(message.Body.GetType(), out handlers) == false)
            {
                return;
            }

            foreach (IHandleMessage handler in handlers)
            {
                handler.Handle(sender, message);
            }
        }
Beispiel #9
0
        static void savePacket(int srcPort, Message message, Stream data)
        {
            if (message != null) {
                Console.WriteLine(srcPort+" "+message.Body.GetType().Name+" size="+message.Header.Size);

                //What packet do you get when you literally walk away from a conversation?  Change playfield?
                //Can you have multiple conversations simultaneously?
                //You still need to fix your TCP parser to reassemble packets (erp)
                //What about timing information (for animations and reponses)? (probably also requires refactoring)

                if (message.Body is KnuBotOpenChatWindowMessage){
                    KnuBotOpenChatWindowMessage body = (KnuBotOpenChatWindowMessage)message.Body;
                    string conversationIdentity =  body.Identity.Instance.ToString();
                    Console.WriteLine("    Begin conversation with " +conversationIdentity);
                }else if(message.Body is  KnuBotAnswerListMessage) {
                    KnuBotAnswerListMessage body = (KnuBotAnswerListMessage)message.Body;
                    Console.WriteLine("    Dialog Options: ");
                    foreach(var option in body.DialogOptions){
                        Console.WriteLine("    - "+option.Text);
                    }
                }else if(message.Body is KnuBotAnswerMessage){
                    KnuBotAnswerMessage body = (KnuBotAnswerMessage)message.Body;
                    string conversationIdentity = body.Identity.Instance.ToString();
                    Console.WriteLine("Answered with option: " + body.Answer);
                    Console.WriteLine("ok?");
                }else if(message.Body is KnuBotAppendTextMessage){
                    KnuBotAppendTextMessage body = (KnuBotAppendTextMessage)message.Body;
                    string conversationIdentity = body.Identity.Instance.ToString();
                    Console.WriteLine(conversationIdentity + " says: " + body.Text);
                    Console.WriteLine("ok?");
                }else if(message.Body is CharacterActionMessage){
                    CharacterActionMessage body = (CharacterActionMessage)message.Body;
                    string identity = body.Identity.Instance.ToString();
                    Console.WriteLine("    Character " + identity + " performs action "+body.Action.ToString());
                }
            } else {
                data.Position = 0;
                byte[] buffer = new byte[20];
                int countRead = data.Read(buffer, 0, buffer.Length);

                bool allZero = true;
                foreach (byte b in buffer) {
                    if (b != 0) {
                        allZero = false;
                        break;
                    }
                }

                if (countRead <= 0) {
                    Console.WriteLine(srcPort + " Could not deserialize: EMPTY PACKET");
                } else if (allZero) {
                    Console.WriteLine(srcPort + " Could not deserialize: ZEROES EVERYWHERE");
                } else if (countRead > 0) {
                    Console.WriteLine(srcPort + " Could not deserialize, unknown packet: " + BitConverter.ToString(buffer));
                } else {
                    Console.WriteLine(srcPort + " Could not deserialize, empty packet");
                }
            }
        }
Beispiel #10
0
 /// <summary>
 /// </summary>
 /// <param name="message">
 /// </param>
 /// <exception cref="NotImplementedException">
 /// </exception>
 public void Announce(Message message)
 {
     Announce(message.Body);
 }
Beispiel #11
0
        private void OnSendCallback(Message message, byte[] packet, Action resumeHook)
        {
            Contract.Requires(packet != null);
            Contract.Requires(resumeHook != null);
            Contract.Requires(this.client != null);
            Contract.Requires(this.actionExecutionContext != null);

            if (message == null || message.Body == null)
            {
                this.bus.Publish(new PacketSentEvent(this.id, packet));
                return;
            }

            this.triggerHandler.ExecuteTriggerActions(message.Body, resumeHook, this.actionExecutionContext);
            this.bus.Publish(new PacketSentEvent(this.id, packet));
        }
 public void Serialize(Stream stream, Message message)
 {
     Contract.Requires<ArgumentNullException>(stream != null);
     Contract.Requires<ArgumentNullException>(message != null);
     throw new NotImplementedException();
 }
 public void Serialize(Stream stream, Message message)
 {
     this.messageSerializer.Serialize(stream, message);
 }
        public void Serialize(Stream stream, Message message, out SerializationContext serializationContext)
        {
            serializationContext = null;
            var serializer = this.serializerResolver.GetSerializer(message.Body.GetType());
            if (serializer == null)
            {
                return;
            }

            serializationContext = new SerializationContext(this.serializerResolver);
            var writer = new StreamWriter(stream) { Position = 0 };
            this.headerSerializer.Serialize(writer, serializationContext, message.Header);
            serializer.Serialize(writer, serializationContext, message.Body);
            var length = writer.Position;
            writer.Position = 6;
            writer.WriteInt16((short)length);
        }
 public void Serialize(Stream stream, Message message)
 {
     SerializationContext ignore;
     this.Serialize(stream, message, out ignore);
 }