Beispiel #1
0
        protected override void DecodePackage()
        {
            Messages = new List <Package>();

            _buffer.Position = 0;

            _datagramHeader         = new DatagramHeader(ReadByte());
            _datagramSequenceNumber = ReadLittle();

            while (_buffer.Position < _buffer.Length)
            {
                byte flags = ReadByte();
                _reliability = (Reliability)((flags & Convert.ToByte("011100000", 2)) >> 5);
                bool isSplitPacket = ((flags & Convert.ToByte("00010000", 2)) > 0);

                short dataBitLength = ReadShort();

                if (_reliability == Reliability.Reliable ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered
                    )
                {
                    _reliableMessageNumber = ReadLittle();
                }
                else
                {
                    _reliableMessageNumber = -1;
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced
                    )
                {
                    _sequencingIndex = ReadLittle();
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered ||
                    _reliability == Reliability.ReliableOrderedWithAckReceipt
                    )
                {
                    _orderingIndex   = ReadLittle();
                    _orderingChannel = ReadByte();                     // flags
                }
                else
                {
                    _orderingIndex   = 0;
                    _orderingChannel = 0;
                }

                if (isSplitPacket)
                {
                    _splitPacketCount = ReadInt();
                    _splitPacketId    = ReadShort();
                    _splitPacketIndex = ReadInt();
                }
                else
                {
                    _splitPacketCount = -1;
                    _splitPacketId    = -1;
                    _splitPacketIndex = -1;
                }

                // Slurp the payload
                MessageLength = (int)Math.Ceiling((((double)dataBitLength) / 8));

                byte[] internalBuffer = ReadBytes(MessageLength);

                if (isSplitPacket)
                {
                    SplitPartPackage splitPartPackage = SplitPartPackage.CreateObject();
                    splitPartPackage.Id      = internalBuffer[0];
                    splitPartPackage.Message = internalBuffer;
                    Messages.Add(splitPartPackage);
                    return;
                }

                Package package = PackageFactory.CreatePackage(internalBuffer[0], internalBuffer) ?? new UnknownPackage(internalBuffer[0], internalBuffer);
                Messages.Add(package);
                if (MessageLength != internalBuffer.Length)
                {
                    Debug.WriteLine("Missmatch of requested lenght, and actual read lenght");
                }
            }
        }
Beispiel #2
0
        protected override void DecodePackage()
        {
            Messages = new List <Package>();

            _buffer.Position = 0;

            _datagramHeader         = new DatagramHeader(ReadByte());
            _datagramSequenceNumber = ReadLittle();
            _datagramHeader.datagramSequenceNumber = _datagramSequenceNumber;

            while (_buffer.Position < _buffer.Length)
            {
                byte flags = ReadByte();
                _reliability = (Reliability)((flags & Convert.ToByte("011100000", 2)) >> 5);
                _hasSplit    = ((flags & Convert.ToByte("00010000", 2)) > 0);

                short dataBitLength = ReadShort();

                if (_reliability == Reliability.Reliable ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered
                    )
                {
                    _reliableMessageNumber = ReadLittle();
                }
                else
                {
                    _reliableMessageNumber = -1;
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced
                    )
                {
                    _sequencingIndex = ReadLittle();
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered ||
                    _reliability == Reliability.ReliableOrderedWithAckReceipt
                    )
                {
                    _orderingIndex   = ReadLittle();
                    _orderingChannel = ReadByte();                     // flags
                }
                else
                {
                    _orderingIndex   = 0;
                    _orderingChannel = 0;
                }

                if (_hasSplit)
                {
                    _splitPacketCount = ReadInt();
                    _splitPacketId    = ReadShort();
                    _splitPacketIndex = ReadInt();
                }
                else
                {
                    _splitPacketCount = -1;
                    _splitPacketId    = -1;
                    _splitPacketIndex = -1;
                }

                // Slurp the payload
                MessageLength = (int)Math.Ceiling((((double)dataBitLength) / 8));

                byte[] internalBuffer = ReadBytes(MessageLength);

                if (_hasSplit)
                {
                    //Log.Debug("Recieve split\n" + HexDump(internalBuffer));
                    SplitPartPackage splitPartPackage = SplitPartPackage.CreateObject();
                    splitPartPackage.Id      = internalBuffer[0];
                    splitPartPackage.Message = internalBuffer;
                    Messages.Add(splitPartPackage);
                    return;
                }

                byte id = internalBuffer[0];
                if (id == 0x8e)
                {
                    id = internalBuffer[1];
                }

                Package package = PackageFactory.CreatePackage(id, internalBuffer) ?? new UnknownPackage(id, internalBuffer);
                package.DatagramSequenceNumber = _datagramSequenceNumber;
                package.ReliableMessageNumber  = _reliableMessageNumber;
                package.OrderingChannel        = _orderingChannel;
                package.OrderingIndex          = _orderingIndex;

                if (!(package is McpeBatch))
                {
                    Log.Debug($"Raw: {package.DatagramSequenceNumber} {package.ReliableMessageNumber} {package.OrderingIndex} {package.GetType().Name} 0x{package.Id:x2} \n{HexDump(internalBuffer)}");
                }

                Messages.Add(package);
                if (MessageLength != internalBuffer.Length)
                {
                    Debug.WriteLine("Missmatch of requested lenght, and actual read lenght");
                }
            }
        }
Beispiel #3
0
        protected override void DecodePackage()
        {
            Messages = new List <Package>();

            _buffer.Position = 0;

            _datagramHeader         = new DatagramHeader(ReadByte());
            _datagramSequenceNumber = ReadLittle();
            _datagramHeader.datagramSequenceNumber = _datagramSequenceNumber;

            _hasSplit = false;
            while (_buffer.Position < _buffer.Length)
            {
                if (_hasSplit)
                {
                    Log.Warn("Reading second split message");
                }

                byte flags = ReadByte();
                _reliability = (Reliability)((flags & Convert.ToByte("011100000", 2)) >> 5);
                _hasSplit    = ((flags & Convert.ToByte("00010000", 2)) > 0);

                short dataBitLength = ReadShort();

                if (_reliability == Reliability.Reliable ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered
                    )
                {
                    _reliableMessageNumber = ReadLittle();
                }
                else
                {
                    _reliableMessageNumber = -1;
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced
                    )
                {
                    _sequencingIndex = ReadLittle();
                }
                else
                {
                    _sequencingIndex = -1;
                }

                if (_reliability == Reliability.UnreliableSequenced ||
                    _reliability == Reliability.ReliableSequenced ||
                    _reliability == Reliability.ReliableOrdered ||
                    _reliability == Reliability.ReliableOrderedWithAckReceipt
                    )
                {
                    _orderingIndex   = ReadLittle();
                    _orderingChannel = ReadByte();                     // flags
                }
                else
                {
                    _orderingIndex   = 0;
                    _orderingChannel = 0;
                }

                if (_hasSplit)
                {
                    _splitPacketCount = ReadInt();
                    _splitPacketId    = ReadShort();
                    _splitPacketIndex = ReadInt();
                }
                else
                {
                    _splitPacketCount = -1;
                    _splitPacketId    = -1;
                    _splitPacketIndex = -1;
                }

                // Slurp the payload
                MessageLength = (int)Math.Ceiling((((double)dataBitLength) / 8));

                byte[] internalBuffer = ReadBytes(MessageLength);

                if (_hasSplit)
                {
                    SplitPartPackage splitPartPackage = SplitPartPackage.CreateObject();
                    splitPartPackage.DatagramSequenceNumber = _datagramSequenceNumber;
                    splitPartPackage.Reliability            = _reliability;
                    splitPartPackage.ReliableMessageNumber  = _reliableMessageNumber;
                    splitPartPackage.OrderingChannel        = _orderingChannel;
                    splitPartPackage.OrderingIndex          = _orderingIndex;
                    splitPartPackage.SplitId    = _splitPacketId;
                    splitPartPackage.SplitCount = _splitPacketCount;
                    splitPartPackage.SplitIdx   = _splitPacketIndex;
                    splitPartPackage.Id         = internalBuffer[0];
                    splitPartPackage.Message    = internalBuffer;
                    Messages.Add(splitPartPackage);

                    if (Log.IsDebugEnabled && _buffer.Position < _buffer.Length)
                    {
                        Log.Debug($"Got split message, but more to read {_buffer.Length - _buffer.Position}");
                    }
                    continue;
                }

                byte    id      = internalBuffer[0];
                Package package = PackageFactory.CreatePackage(id, internalBuffer, "raknet") ?? new UnknownPackage(id, internalBuffer);
                package.DatagramSequenceNumber = _datagramSequenceNumber;
                package.Reliability            = _reliability;
                package.ReliableMessageNumber  = _reliableMessageNumber;
                package.OrderingChannel        = _orderingChannel;
                package.OrderingIndex          = _orderingIndex;

                //if (!(package is McpeBatch)) Log.Debug($"Raw: {package.DatagramSequenceNumber} {package.ReliableMessageNumber} {package.OrderingIndex} {package.GetType().Name} 0x{package.Id:x2} \n{HexDump(internalBuffer)}");

                Messages.Add(package);
                if (Log.IsDebugEnabled && MessageLength != internalBuffer.Length)
                {
                    Log.Debug("Missmatch of requested lenght, and actual read lenght");
                }
            }
        }