Beispiel #1
0
 private int UnpackID(UnPacker unpacker)
 {
     if (this.nextID < sbyte.MaxValue)
     {
         return unpacker.UnpackByte();
     }
     else if (this.nextID < short.MaxValue)
     {
         return unpacker.UnpackShort();
     }
     else
     {
         return unpacker.UnpackInteger();
     }
 }
Beispiel #2
0
        public TouchEvent DecodeTouch(UnPacker unpacker)
        {
            int length = unpacker.UnpackByte();
            Touch[] touches = new Touch[length];
            for (int i = 0; i < length; i++)
            {
                Touch touch;
                touch.Location.X = unpacker.UnpackShort();
                touch.Location.Y = unpacker.UnpackShort();
                touch.Pressure = unpacker.UnpackByte() / 256.0f;
                touches[i] = touch;
            }

            return new TouchEvent(touches);
        }
Beispiel #3
0
 public object Deserialize(UnPacker unpacker)
 {
     int id = this.UnpackID(unpacker);
     ISerializer serializer = this.serializerByType[this.idToType[id]];
     return serializer.Deserialize(unpacker);
 }
Beispiel #4
0
 private object DeserializeMessage(UnPacker unpacker)
 {
     return this.serializeManager.Deserialize(unpacker);
 }
Beispiel #5
0
        internal void OnRecived(int connectionID, byte[] bytes)
        {
            if (IsHeartbeat(bytes))
            {
                ConsoleColor c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Server got a heartbeat from {0}", connectionID);
                Console.ForegroundColor = c;
                return;
            }

            UnPacker unPacker = new UnPacker(bytes);
            while (unPacker.remaining() > 0)
            {
                RecivedEventArgs eventArgs = new RecivedEventArgs(connectionID, this.DeserializeMessage(unPacker));
                if (this.Recived != null)
                    this.Recived(this, eventArgs);
            }
        }
Beispiel #6
0
 protected override byte[] processMessage(UnPacker unPacker)
 {
     unPacker.UnpackByte(); //Remove id byte from message.
     return unPacker.UnpackByteArray(unPacker.remaining());
 }
Beispiel #7
0
            private byte[] processRecivedMessage(UnPacker unPacker, int accnum)
            {
                if (accnum > this.lastRecivedMessageAckNum)
                {
                    addMissingMessages(accnum);
                    this.lastRecivedMessageAckNum = accnum;

                    return unPacker.UnpackByteArray(unPacker.remaining());
                }
                else
                {
                    if (this.missingMessages.Contains(accnum))
                    {
                        this.missingMessages.Remove(accnum);
                        return unPacker.UnpackByteArray(unPacker.remaining());
                    }

                    return null;
                }
            }
Beispiel #8
0
            protected override byte[] processMessage(UnPacker unPacker)
            {
                int flags = unPacker.UnpackByte();
                int accnum = unPacker.UnpackInteger();

                if ((flags & ACK_BIT) == ACK_BIT)
                {
                    connection.reliableTimer.removeResender(accnum);
                    return null;
                }
                else
                {
                    sendAckMessage(accnum);
                    return processRecivedMessage(unPacker, accnum);
                }
            }
Beispiel #9
0
 protected abstract byte[] processMessage(UnPacker unPacker);
Beispiel #10
0
            private byte[] processRecivedMessage(UnPacker unPacker, int accnum)
            {
                if (this.lastOrderedMessage + 1 == accnum)
                {
                    this.lastOrderedMessage++;
                    if (this.outOfOrderMessages.Count == 0)
                        return unPacker.UnpackByteArray(unPacker.remaining());

                    return this.buildOrderedMessages(unPacker);
                }
                else if (this.lastOrderedMessage >= accnum)
                {
                    return null;
                }
                else
                {
                    if (!this.outOfOrderMessages.ContainsKey(accnum))
                    {
                        Console.WriteLine("Message out of order! {0}", accnum);
                        this.outOfOrderMessages.Add(accnum, unPacker.UnpackByteArray(unPacker.remaining()));
                    }
                    return null;
                }
            }
Beispiel #11
0
            private byte[] buildOrderedMessages(UnPacker unPacker)
            {
                List<int> keysToRemove = new List<int>();
                Packer packer = new Packer(unPacker.remaining());
                packer.Pack(unPacker.UnpackByteArray(unPacker.remaining()));

                foreach (var item in this.outOfOrderMessages.Keys)
                {
                    if (this.lastOrderedMessage + 1 == item)
                    {
                        keysToRemove.Add(item);
                        this.lastOrderedMessage++;
                        packer.Pack(this.outOfOrderMessages[item]);
                    }
                    else
                    {
                        break;
                    }
                }

                keysToRemove.ForEach((x) => this.outOfOrderMessages.Remove(x));

                return packer.GetPackedData();
            }
Beispiel #12
0
 protected override byte[] processMessage(UnPacker unPacker)
 {
     unPacker.UnpackByte();
     int seqNum = unPacker.UnpackInteger();
     if (seqNum < lastRecivedSequenceNumber)
     {
         return null; //Discard the message if it is old.
     }
     else
     {
         lastRecivedSequenceNumber = seqNum;
         return unPacker.UnpackByteArray(unPacker.remaining());
     }
 }