public override void SendMessage(byte[] buffer, int bufferLength)
        {
            int    num  = 0;
            ushort num2 = oldestUnacked;

            while (PacketIO.SequenceLessThan(num2, sequence))
            {
                if (sendBuffer.Exists(num2))
                {
                    num++;
                }
                num2 = (ushort)(num2 + 1);
            }
            if (num == sendBuffer.Size)
            {
                ByteBuffer byteBuffer = ObjPool <ByteBuffer> .Get();

                byteBuffer.SetSize(bufferLength);
                byteBuffer.BufferCopy(buffer, 0, 0, bufferLength);
                messageQueue.Enqueue(byteBuffer);
            }
            else
            {
                ushort         val            = sequence++;
                BufferedPacket bufferedPacket = sendBuffer.Insert(val);
                bufferedPacket.time = -1.0;
                int variableLengthBytes = getVariableLengthBytes((ushort)bufferLength);
                bufferedPacket.buffer.SetSize(bufferLength + 2 + variableLengthBytes);
                using (ByteArrayReaderWriter byteArrayReaderWriter = ByteArrayReaderWriter.Get(bufferedPacket.buffer.InternalBuffer))
                {
                    byteArrayReaderWriter.Write(val);
                    writeVariableLengthUShort((ushort)bufferLength, byteArrayReaderWriter);
                    byteArrayReaderWriter.WriteBuffer(buffer, bufferLength);
                }
                bufferedPacket.writeLock = false;
            }
        }
Example #2
0
        public ushort SendPacket(byte[] packetData, int length, byte channelID)
        {
            if (length > config.MaxPacketSize)
            {
                throw new ArgumentOutOfRangeException("Packet is too large to send, max packet size is " + config.MaxPacketSize + " bytes");
            }
            ushort num = sequence++;
            ushort ack;
            uint   ackBits;

            lock (receivedPackets)
            {
                receivedPackets.GenerateAckBits(out ack, out ackBits);
            }
            SentPacketData sentPacketData = sentPackets.Insert(num);

            sentPacketData.time        = time;
            sentPacketData.packetBytes = (uint)(config.PacketHeaderSize + length);
            sentPacketData.acked       = false;
            if (length <= config.FragmentThreshold)
            {
                byte[] buffer = BufferPool.GetBuffer(2048);
                int    num2   = PacketIO.WritePacketHeader(buffer, channelID, num, ack, ackBits);
                int    arg    = length + num2;
                Buffer.BlockCopy(packetData, 0, buffer, num2, length);
                config.TransmitPacketCallback(buffer, arg);
                BufferPool.ReturnBuffer(buffer);
            }
            else
            {
                byte[] buffer2 = BufferPool.GetBuffer(10);
                int    num3    = 0;
                try
                {
                    num3 = PacketIO.WritePacketHeader(buffer2, channelID, num, ack, ackBits);
                }
                catch
                {
                    throw;
                }
                int    num4    = length / config.FragmentSize + ((length % config.FragmentSize != 0) ? 1 : 0);
                byte[] buffer3 = BufferPool.GetBuffer(2048);
                int    num5    = 0;
                byte   b       = 1;
                b = (byte)(b | (byte)((channelID & 3) << 6));
                for (int i = 0; i < num4; i++)
                {
                    using (ByteArrayReaderWriter byteArrayReaderWriter = ByteArrayReaderWriter.Get(buffer3))
                    {
                        byteArrayReaderWriter.Write(b);
                        byteArrayReaderWriter.Write(channelID);
                        byteArrayReaderWriter.Write(num);
                        byteArrayReaderWriter.Write((byte)i);
                        byteArrayReaderWriter.Write((byte)(num4 - 1));
                        if (i == 0)
                        {
                            byteArrayReaderWriter.WriteBuffer(buffer2, num3);
                        }
                        int num6 = config.FragmentSize;
                        if (num5 + num6 > length)
                        {
                            num6 = length - num5;
                        }
                        for (int j = 0; j < num6; j++)
                        {
                            byteArrayReaderWriter.Write(packetData[num5++]);
                        }
                        int arg2 = (int)byteArrayReaderWriter.WritePosition;
                        config.TransmitPacketCallback(buffer3, arg2);
                    }
                }
                BufferPool.ReturnBuffer(buffer2);
                BufferPool.ReturnBuffer(buffer3);
            }
            return(num);
        }