예제 #1
0
        public void ReceivePacket(byte[] packetData, int bufferLength)
        {
            if (bufferLength > config.MaxPacketSize)
            {
                throw new ArgumentOutOfRangeException("Packet is larger than max packet size");
            }
            if (packetData == null)
            {
                throw new InvalidOperationException("Tried to receive null packet!");
            }
            if (bufferLength > packetData.Length)
            {
                throw new InvalidOperationException("Buffer length exceeds actual packet length!");
            }
            byte b = packetData[0];

            if ((b & 1) == 0)
            {
                byte   channelID;
                ushort arg;
                ushort ack;
                uint   ackBits;
                int    num = PacketIO.ReadPacketHeader(packetData, 0, bufferLength, out channelID, out arg, out ack, out ackBits);
                bool   flag;
                lock (receivedPackets)
                {
                    flag = !receivedPackets.TestInsert(arg);
                }
                if (!flag && (b & 0x80) == 0)
                {
                    if (num >= bufferLength)
                    {
                        throw new FormatException("Buffer too small for packet data!");
                    }
                    ByteBuffer byteBuffer = ObjPool <ByteBuffer> .Get();

                    byteBuffer.SetSize(bufferLength - num);
                    byteBuffer.BufferCopy(packetData, num, 0, byteBuffer.Length);
                    config.ProcessPacketCallback(arg, byteBuffer.InternalBuffer, byteBuffer.Length);
                    lock (receivedPackets)
                    {
                        ReceivedPacketData receivedPacketData = receivedPackets.Insert(arg);
                        if (receivedPacketData == null)
                        {
                            throw new InvalidOperationException("Failed to insert received packet!");
                        }
                        receivedPacketData.time        = time;
                        receivedPacketData.packetBytes = (uint)(config.PacketHeaderSize + bufferLength);
                    }
                    ObjPool <ByteBuffer> .Return(byteBuffer);
                }
                if (flag && (b & 0x80) == 0)
                {
                    return;
                }
                for (int i = 0; i < 32; i++)
                {
                    if ((ackBits & 1) != 0)
                    {
                        ushort         obj            = (ushort)(ack - i);
                        SentPacketData sentPacketData = sentPackets.Find(obj);
                        if (sentPacketData != null && !sentPacketData.acked)
                        {
                            sentPacketData.acked = true;
                            if (config.AckPacketCallback != null)
                            {
                                config.AckPacketCallback(obj);
                            }
                            float num2 = (float)(time - sentPacketData.time) * 1000f;
                            if ((rtt == 0f && num2 > 0f) || Math.Abs(rtt - num2) < 1E-05f)
                            {
                                rtt = num2;
                            }
                            else
                            {
                                rtt += (num2 - rtt) * config.RTTSmoothFactor;
                            }
                        }
                    }
                    ackBits >>= 1;
                }
                return;
            }
            int    fragmentID;
            int    numFragments;
            int    fragmentBytes;
            ushort num3;
            ushort ack2;
            uint   ackBits2;
            byte   channelID2;
            int    num4 = PacketIO.ReadFragmentHeader(packetData, 0, bufferLength, config.MaxFragments, config.FragmentSize, out fragmentID, out numFragments, out fragmentBytes, out num3, out ack2, out ackBits2, out channelID2);
            FragmentReassemblyData fragmentReassemblyData = fragmentReassembly.Find(num3);

            if (fragmentReassemblyData == null)
            {
                fragmentReassemblyData = fragmentReassembly.Insert(num3);
                if (fragmentReassemblyData == null)
                {
                    return;
                }
                fragmentReassemblyData.Sequence             = num3;
                fragmentReassemblyData.Ack                  = 0;
                fragmentReassemblyData.AckBits              = 0u;
                fragmentReassemblyData.NumFragmentsReceived = 0;
                fragmentReassemblyData.NumFragmentsTotal    = numFragments;
                fragmentReassemblyData.PacketBytes          = 0;
                Array.Clear(fragmentReassemblyData.FragmentReceived, 0, fragmentReassemblyData.FragmentReceived.Length);
            }
            if (numFragments == fragmentReassemblyData.NumFragmentsTotal && !fragmentReassemblyData.FragmentReceived[fragmentID])
            {
                fragmentReassemblyData.NumFragmentsReceived++;
                fragmentReassemblyData.FragmentReceived[fragmentID] = true;
                byte[] buffer = BufferPool.GetBuffer(2048);
                Buffer.BlockCopy(packetData, num4, buffer, 0, bufferLength - num4);
                fragmentReassemblyData.StoreFragmentData(channelID2, num3, ack2, ackBits2, fragmentID, config.FragmentSize, buffer, bufferLength - num4);
                BufferPool.ReturnBuffer(buffer);
                if (fragmentReassemblyData.NumFragmentsReceived == fragmentReassemblyData.NumFragmentsTotal)
                {
                    ByteBuffer byteBuffer2 = ObjPool <ByteBuffer> .Get();

                    byteBuffer2.SetSize(fragmentReassemblyData.PacketDataBuffer.Length - fragmentReassemblyData.HeaderOffset);
                    Buffer.BlockCopy(fragmentReassemblyData.PacketDataBuffer.InternalBuffer, fragmentReassemblyData.HeaderOffset, byteBuffer2.InternalBuffer, 0, byteBuffer2.Length);
                    ReceivePacket(byteBuffer2.InternalBuffer, byteBuffer2.Length);
                    ObjPool <ByteBuffer> .Return(byteBuffer2);

                    fragmentReassemblyData.PacketDataBuffer.SetSize(0);
                    fragmentReassembly.Remove(num3);
                }
            }
        }
        public void ReceivePacket(byte[] packetData, int bufferLength)
        {
            if (bufferLength > config.MaxPacketSize)
            {
                throw new ArgumentOutOfRangeException("Packet is larger than max packet size");
            }

            if (packetData == null)
            {
                throw new InvalidOperationException("Tried to receive null packet!");
            }

            if (bufferLength > packetData.Length)
            {
                throw new InvalidOperationException("Buffer length exceeds actual packet length!");
            }

            byte prefixByte = packetData[0];

            if ((prefixByte & 1) == 0)
            {
                // regular packet

                ushort sequence;
                ushort ack;
                uint   ackBits;

                byte channelID;
                bool compressed;

                int packetHeaderBytes = PacketIO.ReadPacketHeader(packetData, 0, bufferLength, out channelID, out sequence, out ack, out ackBits, out compressed);

                bool isStale;
                lock (receivedPackets)
                    isStale = !receivedPackets.TestInsert(sequence);

                if (!isStale && (prefixByte & 0x80) == 0)
                {
                    if (packetHeaderBytes >= bufferLength)
                    {
                        throw new FormatException($"Buffer too small for packet data! {packetHeaderBytes} packetHeaderBytes > {bufferLength} bufferLength");
                    }

                    ByteBuffer tempBuffer = ObjPool <ByteBuffer> .Get();

                    if (compressed)
                    {
                        var decompressedBytes = LZ4Codec.Unwrap(packetData, packetHeaderBytes);
                        tempBuffer.SetSize(decompressedBytes.Length);
                        tempBuffer.BufferCopy(decompressedBytes, 0, 0, decompressedBytes.Length);
                        bufferLength = decompressedBytes.Length + packetHeaderBytes;
                    }
                    else
                    {
                        tempBuffer.SetSize(bufferLength - packetHeaderBytes);
                        tempBuffer.BufferCopy(packetData, packetHeaderBytes, 0, tempBuffer.Length);
                    }

                    // process packet
                    config.ProcessPacketCallback(sequence, tempBuffer.InternalBuffer, tempBuffer.Length);

                    // add to received buffer
                    lock (receivedPackets) {
                        ReceivedPacketData receivedPacketData = receivedPackets.Insert(sequence);

                        if (receivedPacketData == null)
                        {
                            throw new InvalidOperationException("Failed to insert received packet!");
                        }

                        receivedPacketData.time        = this.time;
                        receivedPacketData.packetBytes = (uint)(bufferLength);
                    }

                    ObjPool <ByteBuffer> .Return(tempBuffer);
                }

                if (!isStale || (prefixByte & 0x80) != 0)
                {
                    for (int i = 0; i < 32; i++)
                    {
                        if ((ackBits & 1) != 0)
                        {
                            ushort         ack_sequence   = (ushort)(ack - i);
                            SentPacketData sentPacketData = sentPackets.Find(ack_sequence);

                            if (sentPacketData != null && !sentPacketData.acked)
                            {
                                sentPacketData.acked = true;

                                if (config.AckPacketCallback != null)
                                {
                                    config.AckPacketCallback(ack_sequence);
                                }

                                float rtt = (float)(this.time - sentPacketData.time) * 1000.0f;
                                if ((this.rtt == 0f && rtt > 0f) || Math.Abs(this.rtt - rtt) < 0.00001f)
                                {
                                    this.rtt = rtt;
                                }
                                else
                                {
                                    this.rtt += (rtt - this.rtt) * config.RTTSmoothFactor;
                                }
                            }
                        }

                        ackBits >>= 1;
                    }
                }
            }
            else
            {
                // fragment packet

                int fragmentID;
                int numFragments;
                int fragmentBytes;

                ushort sequence;
                ushort ack;
                uint   ackBits;

                byte fragmentChannelID;

                bool compressed;
                int  fragmentHeaderBytes = PacketIO.ReadFragmentHeader(packetData, 0, bufferLength, config.MaxFragments, config.FragmentSize,
                                                                       out fragmentID, out numFragments, out fragmentBytes, out sequence, out ack, out ackBits, out fragmentChannelID, out compressed);

                FragmentReassemblyData reassemblyData = fragmentReassembly.Find(sequence);
                if (reassemblyData == null)
                {
                    reassemblyData = fragmentReassembly.Insert(sequence);

                    // failed to insert into buffer (stale)
                    if (reassemblyData == null)
                    {
                        return;
                    }

                    reassemblyData.Sequence             = sequence;
                    reassemblyData.Ack                  = 0;
                    reassemblyData.AckBits              = 0;
                    reassemblyData.NumFragmentsReceived = 0;
                    reassemblyData.NumFragmentsTotal    = numFragments;
                    reassemblyData.PacketBytes          = 0;
                    reassemblyData.PacketDataBuffer.SetSize(0);
                    Array.Clear(reassemblyData.FragmentReceived, 0, reassemblyData.FragmentReceived.Length);
                }

                if (numFragments != reassemblyData.NumFragmentsTotal)
                {
                    return;
                }

                if (reassemblyData.FragmentReceived[fragmentID])
                {
                    return;
                }

                reassemblyData.NumFragmentsReceived++;
                reassemblyData.FragmentReceived[fragmentID] = true;

                byte[] tempFragmentData = BufferPool.GetBuffer(2048);
                Buffer.BlockCopy(packetData, fragmentHeaderBytes, tempFragmentData, 0, bufferLength - fragmentHeaderBytes);

                reassemblyData.StoreFragmentData(fragmentChannelID, sequence, ack, ackBits, fragmentID, config.FragmentSize, tempFragmentData, bufferLength - fragmentHeaderBytes, compressed);
                BufferPool.ReturnBuffer(tempFragmentData);

                if (reassemblyData.NumFragmentsReceived == reassemblyData.NumFragmentsTotal)
                {
                    // grab internal buffer and pass it to ReceivePacket. Internal buffer will be packet marked as normal packet, so it will go through normal packet path

                    // copy into new buffer to remove preceding offset (used to simplify variable length header handling)
                    ByteBuffer temp = ObjPool <ByteBuffer> .Get();

                    temp.SetSize(reassemblyData.PacketDataBuffer.Length - reassemblyData.HeaderOffset);
                    Buffer.BlockCopy(reassemblyData.PacketDataBuffer.InternalBuffer, reassemblyData.HeaderOffset, temp.InternalBuffer, 0, temp.Length);

                    // receive packet
                    this.ReceivePacket(temp.InternalBuffer, temp.Length);

                    // return temp buffer
                    ObjPool <ByteBuffer> .Return(temp);

                    // clear reassembly
                    reassemblyData.PacketDataBuffer.SetSize(0);
                    fragmentReassembly.Remove(sequence);
                }
            }
        }
        public void ReceivePacket(byte[] packetData, int bufferLength)
        {
            if (bufferLength > config.MaxPacketSize)
            {
                throw new ArgumentOutOfRangeException("Packet is larger than max packet size");
            }

            byte prefixByte = packetData[0];

            if ((prefixByte & 1) == 0)
            {
                // regular packet

                ushort sequence;
                ushort ack;
                uint   ackBits;

                byte channelID;

                int  packetHeaderBytes = PacketIO.ReadPacketHeader(packetData, 0, bufferLength, out channelID, out sequence, out ack, out ackBits);
                bool isStale           = !receivedPackets.TestInsert(sequence);

                if (!isStale && (prefixByte & 0x80) == 0)
                {
                    ByteBuffer tempBuffer = ObjPool <ByteBuffer> .Get();

                    tempBuffer.SetSize(bufferLength - packetHeaderBytes);
                    tempBuffer.BufferCopy(packetData, packetHeaderBytes, 0, tempBuffer.Length);

                    // process packet
                    config.ProcessPacketCallback(sequence, tempBuffer.InternalBuffer, tempBuffer.Length);

                    // add to received buffer
                    ReceivedPacketData receivedPacketData = receivedPackets.Insert(sequence);
                    receivedPacketData.time        = this.time;
                    receivedPacketData.packetBytes = (uint)(config.PacketHeaderSize + bufferLength);

                    ObjPool <ByteBuffer> .Return(tempBuffer);
                }

                if (!isStale || (prefixByte & 0x80) != 0)
                {
                    for (int i = 0; i < 32; i++)
                    {
                        if ((ackBits & 1) != 0)
                        {
                            ushort         ack_sequence   = (ushort)(ack - i);
                            SentPacketData sentPacketData = sentPackets.Find(ack_sequence);

                            if (sentPacketData != null && !sentPacketData.acked)
                            {
                                sentPacketData.acked = true;

                                if (config.AckPacketCallback != null)
                                {
                                    config.AckPacketCallback(ack_sequence);
                                }

                                float rtt = (float)(this.time - sentPacketData.time) * 1000.0f;
                                if ((this.rtt == 0f && rtt > 0f) || Math.Abs(this.rtt - rtt) < 0.00001f)
                                {
                                    this.rtt = rtt;
                                }
                                else
                                {
                                    this.rtt += (rtt - this.rtt) * config.RTTSmoothFactor;
                                }
                            }
                        }

                        ackBits >>= 1;
                    }
                }
            }
            else
            {
                // fragment packet

                int fragmentID;
                int numFragments;
                int fragmentBytes;

                ushort sequence;
                ushort ack;
                uint   ackBits;

                byte fragmentChannelID;

                int fragmentHeaderBytes = PacketIO.ReadFragmentHeader(packetData, 0, bufferLength, config.MaxFragments, config.FragmentSize,
                                                                      out fragmentID, out numFragments, out fragmentBytes, out sequence, out ack, out ackBits, out fragmentChannelID);

                FragmentReassemblyData reassemblyData = fragmentReassembly.Find(sequence);
                if (reassemblyData == null)
                {
                    reassemblyData = fragmentReassembly.Insert(sequence);

                    // failed to insert into buffer (stale)
                    if (reassemblyData == null)
                    {
                        return;
                    }

                    reassemblyData.Sequence             = sequence;
                    reassemblyData.Ack                  = 0;
                    reassemblyData.AckBits              = 0;
                    reassemblyData.NumFragmentsReceived = 0;
                    reassemblyData.NumFragmentsTotal    = numFragments;
                    reassemblyData.PacketBytes          = 0;
                    Array.Clear(reassemblyData.FragmentReceived, 0, reassemblyData.FragmentReceived.Length);
                }

                if (numFragments != reassemblyData.NumFragmentsTotal)
                {
                    return;
                }

                if (reassemblyData.FragmentReceived[fragmentID])
                {
                    return;
                }

                reassemblyData.NumFragmentsReceived++;
                reassemblyData.FragmentReceived[fragmentID] = true;

                byte[] tempFragmentData = BufferPool.GetBuffer(2048);
                Buffer.BlockCopy(packetData, fragmentHeaderBytes, tempFragmentData, 0, bufferLength - fragmentHeaderBytes);
                reassemblyData.StoreFragmentData(fragmentChannelID, sequence, ack, ackBits, fragmentID, config.FragmentSize, tempFragmentData, bufferLength - fragmentHeaderBytes);
                BufferPool.ReturnBuffer(tempFragmentData);

                if (reassemblyData.NumFragmentsReceived == reassemblyData.NumFragmentsTotal)
                {
                    // grab internal buffer and pass it to ReceivePacket. Internal buffer will be packet marked as normal packet, so it will go through normal packet path
                    this.ReceivePacket(reassemblyData.PacketDataBuffer.InternalBuffer, reassemblyData.PacketDataBuffer.Length);
                    reassemblyData.PacketDataBuffer.SetSize(0);
                    fragmentReassembly.Remove(sequence);
                }
            }
        }