Esempio n. 1
0
        public void Write(ByteArrayReaderWriter stream)
        {
            stream.Write(ClientID);
            stream.Write((uint)ConnectServers.Length);
            foreach (var server in ConnectServers)
            {
                server.WriteData(stream);
            }

            stream.Write(ClientToServerKey);
            stream.Write(ServerToClientKey);
            stream.Write(UserData);
        }
        private void writeVariableLengthUShort(ushort val, ByteArrayReaderWriter writer)
        {
            if (val > 32767)
            {
                throw new ArgumentOutOfRangeException();
            }
            byte b  = (byte)(val & 0x7F);
            byte b2 = (byte)(val >> 7);

            if (b2 != 0)
            {
                b = (byte)(b | 0x80);
            }
            writer.Write(b);
            if (b2 != 0)
            {
                writer.Write(b2);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Writes packet header to the stream
        /// </summary>
        public void Write(ByteArrayReaderWriter stream)
        {
            if (this.PacketType == NetcodePacketType.ConnectionRequest)
            {
                stream.Write((byte)0);
            }
            else
            {
                byte prefixByte = this.GetPrefixByte();

                // now write prefix byte and sequence number bytes
                stream.Write(prefixByte);

                int   sequenceBytes      = prefixByte >> 4;
                ulong tempSequenceNumber = this.SequenceNumber;
                for (int i = 0; i < sequenceBytes; i++)
                {
                    stream.Write((byte)(tempSequenceNumber & 0xFF));
                    tempSequenceNumber >>= 8;
                }
            }
        }
        private void writeVariableLengthUShort(ushort val, ByteArrayReaderWriter writer)
        {
            if (val > 0x7fff)
            {
                throw new ArgumentOutOfRangeException();
            }

            byte b1 = (byte)(val & 0x007F); // write the lowest 7 bits
            byte b2 = (byte)(val >> 7);     // write remaining 8 bits

            // if there's a second byte to write, set the continue flag
            if (b2 != 0)
            {
                b1 |= 0x80;
            }

            // write bytes
            writer.Write(b1);
            if (b2 != 0)
            {
                writer.Write(b2);
            }
        }
Esempio n. 5
0
        public void Write(ByteArrayReaderWriter writer)
        {
            writer.WriteASCII(Defines.NETCODE_VERSION_INFO_STR);
            writer.Write(ProtocolID);

            writer.Write(CreateTimestamp);
            writer.Write(ExpireTimestamp);
            writer.Write(ConnectTokenSequence);
            writer.Write(PrivateConnectTokenBytes);

            writer.Write((uint)ConnectServers.Length);
            for (int i = 0; i < ConnectServers.Length; i++)
            {
                ConnectServers[i].WriteData(writer);
            }

            writer.Write(ClientToServerKey);
            writer.Write(ServerToClientKey);
            writer.Write(TimeoutSeconds);
        }
        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;
            }
        }
Esempio n. 7
0
 public void Write(ByteArrayReaderWriter stream)
 {
     stream.Write(ChallengeTokenSequence);
     stream.Write(ChallengeTokenBytes);
 }
Esempio n. 8
0
 public void Write(ByteArrayReaderWriter stream)
 {
     stream.Write(ClientID);
     stream.Write(UserData);
 }
Esempio n. 9
0
 public void Write(ByteArrayReaderWriter stream)
 {
     stream.Write(ClientIndex);
     stream.Write(MaxSlots);
 }
Esempio n. 10
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);
        }
Esempio n. 11
0
 public void WriteData(ByteArrayReaderWriter stream)
 {
     stream.Write((byte)this.AddressType);
     stream.Write(this.Endpoint.Address.GetAddressBytes());
     stream.Write((ushort)this.Endpoint.Port);
 }