Пример #1
0
 private void EnqueueInit(byte[] data)
 {
     if (this.DoFraming)
     {
         StreamBuffer streamBuffer = new StreamBuffer(data.Length + 32);
         byte[]       array        = new byte[7]
         {
             251,
             0,
             0,
             0,
             0,
             0,
             1
         };
         int num = 1;
         Protocol.Serialize(data.Length + array.Length, array, ref num);
         streamBuffer.Write(array, 0, array.Length);
         streamBuffer.Write(data, 0, data.Length);
         if (base.TrafficStatsEnabled)
         {
             base.TrafficStatsOutgoing.TotalPacketCount++;
             base.TrafficStatsOutgoing.TotalCommandsInPackets++;
             base.TrafficStatsOutgoing.CountControlCommand(streamBuffer.IntLength);
         }
         this.EnqueueMessageAsPayload(DeliveryMode.Reliable, streamBuffer, 0);
     }
 }
Пример #2
0
        internal override StreamBuffer SerializeOperationToMessage(byte opCode, Dictionary <byte, object> parameters, EgMessageType messageType, bool encrypt)
        {
            StreamBuffer streamBuffer = PeerBase.MessageBufferPoolGet();

            streamBuffer.SetLength(0L);
            if (!encrypt)
            {
                streamBuffer.Write(this.messageHeader, 0, this.messageHeader.Length);
            }
            base.SerializationProtocol.SerializeOperationRequest(streamBuffer, opCode, parameters, false);
            if (encrypt)
            {
                byte[] array = base.CryptoProvider.Encrypt(streamBuffer.GetBuffer(), 0, streamBuffer.IntLength);
                streamBuffer.SetLength(0L);
                streamBuffer.Write(this.messageHeader, 0, this.messageHeader.Length);
                streamBuffer.Write(array, 0, array.Length);
            }
            byte[] buffer = streamBuffer.GetBuffer();
            if (messageType != EgMessageType.Operation)
            {
                buffer[this.messageHeader.Length - 1] = (byte)messageType;
            }
            if (encrypt)
            {
                buffer[this.messageHeader.Length - 1] = (byte)(buffer[this.messageHeader.Length - 1] | 0x80);
            }
            if (this.DoFraming)
            {
                int num = 1;
                Protocol.Serialize(streamBuffer.IntLength, buffer, ref num);
            }
            return(streamBuffer);
        }
Пример #3
0
        private bool SerializeCustom(StreamBuffer dout, object serObject)
        {
            CustomType customType = default(CustomType);

            if (Protocol.TypeDict.TryGetValue(serObject.GetType(), out customType))
            {
                if (customType.SerializeStreamFunction == null)
                {
                    byte[] array = customType.SerializeFunction(serObject);
                    dout.WriteByte(99);
                    dout.WriteByte(customType.Code);
                    this.SerializeShort(dout, (short)array.Length, false);
                    dout.Write(array, 0, array.Length);
                    return(true);
                }
                dout.WriteByte(99);
                dout.WriteByte(customType.Code);
                int position = dout.Position;
                dout.Position += 2;
                short num  = customType.SerializeStreamFunction(dout, serObject);
                long  num2 = dout.Position;
                dout.Position = position;
                this.SerializeShort(dout, num, false);
                dout.Position += num;
                if (dout.Position != num2)
                {
                    throw new Exception("Serialization failed. Stream position corrupted. Should be " + num2 + " is now: " + dout.Position + " serializedLength: " + num);
                }
                return(true);
            }
            return(false);
        }
Пример #4
0
 private void SerializeDouble(StreamBuffer dout, double serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(100);
     }
     byte[] obj = this.memDoubleBlockBytes;
     lock (obj)
     {
         this.memDoubleBlock[0] = serObject;
         Buffer.BlockCopy(this.memDoubleBlock, 0, this.memDoubleBlockBytes, 0, 8);
         byte[] array = this.memDoubleBlockBytes;
         if (BitConverter.IsLittleEndian)
         {
             byte b  = array[0];
             byte b2 = array[1];
             byte b3 = array[2];
             byte b4 = array[3];
             array[0] = array[7];
             array[1] = array[6];
             array[2] = array[5];
             array[3] = array[4];
             array[4] = b4;
             array[5] = b3;
             array[6] = b2;
             array[7] = b;
         }
         dout.Write(array, 0, 8);
     }
 }
Пример #5
0
 private void SerializeByteArraySegment(StreamBuffer dout, byte[] serObject, int offset, int count, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(120);
     }
     this.SerializeInteger(dout, count, false);
     dout.Write(serObject, offset, count);
 }
Пример #6
0
 private void SerializeByteArray(StreamBuffer dout, byte[] serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(120);
     }
     this.SerializeInteger(dout, serObject.Length, false);
     dout.Write(serObject, 0, serObject.Length);
 }
Пример #7
0
        internal StreamBuffer SerializeMessageToMessage(object message, bool encrypt, byte[] messageHeader, bool writeLength = true)
        {
            StreamBuffer streamBuffer = PeerBase.MessageBufferPoolGet();

            streamBuffer.SetLength(0L);
            if (!encrypt)
            {
                streamBuffer.Write(messageHeader, 0, messageHeader.Length);
            }
            if (message is byte[])
            {
                byte[] array = message as byte[];
                streamBuffer.Write(array, 0, array.Length);
            }
            else
            {
                this.SerializationProtocol.SerializeMessage(streamBuffer, message);
            }
            if (encrypt)
            {
                byte[] array2 = this.CryptoProvider.Encrypt(streamBuffer.GetBuffer(), 0, streamBuffer.IntLength);
                streamBuffer.SetLength(0L);
                streamBuffer.Write(messageHeader, 0, messageHeader.Length);
                streamBuffer.Write(array2, 0, array2.Length);
            }
            byte[] buffer = streamBuffer.GetBuffer();
            buffer[messageHeader.Length - 1] = (byte)((message is byte[]) ? 9 : 8);
            if (encrypt)
            {
                buffer[messageHeader.Length - 1] = (byte)(buffer[messageHeader.Length - 1] | 0x80);
            }
            if (writeLength)
            {
                int num = 1;
                Protocol.Serialize(streamBuffer.IntLength, buffer, ref num);
            }
            return(streamBuffer);
        }
Пример #8
0
 public override void SerializeString(StreamBuffer dout, string serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(115);
     }
     byte[] bytes = Encoding.UTF8.GetBytes(serObject);
     if (bytes.Length > 32767)
     {
         throw new NotSupportedException("Strings that exceed a UTF8-encoded byte-length of 32767 (short.MaxValue) are not supported. Yours is: " + bytes.Length);
     }
     this.SerializeShort(dout, (short)bytes.Length, false);
     dout.Write(bytes, 0, bytes.Length);
 }
Пример #9
0
 public override void SerializeShort(StreamBuffer dout, short serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(107);
     }
     byte[] obj = this.memShort;
     lock (obj)
     {
         byte[] array = this.memShort;
         array[0] = (byte)(serObject >> 8);
         array[1] = (byte)serObject;
         dout.Write(array, 0, 2);
     }
 }
Пример #10
0
 private void SerializeInteger(StreamBuffer dout, int serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(105);
     }
     byte[] obj = this.memInteger;
     lock (obj)
     {
         byte[] array = this.memInteger;
         array[0] = (byte)(serObject >> 24);
         array[1] = (byte)(serObject >> 16);
         array[2] = (byte)(serObject >> 8);
         array[3] = (byte)serObject;
         dout.Write(array, 0, 4);
     }
 }
Пример #11
0
        private void SerializeIntArrayOptimized(StreamBuffer inWriter, int[] serObject, bool setType)
        {
            if (setType)
            {
                inWriter.WriteByte(121);
            }
            this.SerializeShort(inWriter, (short)serObject.Length, false);
            inWriter.WriteByte(105);
            byte[] array = new byte[serObject.Length * 4];
            int    num   = 0;

            for (int i = 0; i < serObject.Length; i++)
            {
                array[num++] = (byte)(serObject[i] >> 24);
                array[num++] = (byte)(serObject[i] >> 16);
                array[num++] = (byte)(serObject[i] >> 8);
                array[num++] = (byte)serObject[i];
            }
            inWriter.Write(array, 0, array.Length);
        }
Пример #12
0
 private void SerializeFloat(StreamBuffer dout, float serObject, bool setType)
 {
     if (setType)
     {
         dout.WriteByte(102);
     }
     lock (memFloatBlockBytes)
     {
         memFloatBlock[0] = serObject;
         Buffer.BlockCopy(memFloatBlock, 0, memFloatBlockBytes, 0, 4);
         if (BitConverter.IsLittleEndian)
         {
             byte b  = memFloatBlockBytes[0];
             byte b2 = memFloatBlockBytes[1];
             memFloatBlockBytes[0] = memFloatBlockBytes[3];
             memFloatBlockBytes[1] = memFloatBlockBytes[2];
             memFloatBlockBytes[2] = b2;
             memFloatBlockBytes[3] = b;
         }
         dout.Write(memFloatBlockBytes, 0, 4);
     }
 }
Пример #13
0
        internal NCommand(EnetPeer peer, byte[] inBuff, ref int readingOffset)
        {
            this.commandType      = inBuff[readingOffset++];
            this.commandChannelID = inBuff[readingOffset++];
            this.commandFlags     = inBuff[readingOffset++];
            this.reservedByte     = inBuff[readingOffset++];
            Protocol.Deserialize(out this.Size, inBuff, ref readingOffset);
            Protocol.Deserialize(out this.reliableSequenceNumber, inBuff, ref readingOffset);
            peer.bytesIn += this.Size;
            int num = 0;

            switch (this.commandType)
            {
            case 1:
            case 16:
                Protocol.Deserialize(out this.ackReceivedReliableSequenceNumber, inBuff, ref readingOffset);
                Protocol.Deserialize(out this.ackReceivedSentTime, inBuff, ref readingOffset);
                break;

            case 6:
            case 14:
                num = this.Size - 12;
                break;

            case 7:
                Protocol.Deserialize(out this.unreliableSequenceNumber, inBuff, ref readingOffset);
                num = this.Size - 16;
                break;

            case 11:
                Protocol.Deserialize(out this.unsequencedGroupNumber, inBuff, ref readingOffset);
                num = this.Size - 16;
                break;

            case 8:
            case 15:
                Protocol.Deserialize(out this.startSequenceNumber, inBuff, ref readingOffset);
                Protocol.Deserialize(out this.fragmentCount, inBuff, ref readingOffset);
                Protocol.Deserialize(out this.fragmentNumber, inBuff, ref readingOffset);
                Protocol.Deserialize(out this.totalLength, inBuff, ref readingOffset);
                Protocol.Deserialize(out this.fragmentOffset, inBuff, ref readingOffset);
                num = this.Size - 32;
                this.fragmentsRemaining = this.fragmentCount;
                break;

            case 3:
            {
                short peerID = default(short);
                Protocol.Deserialize(out peerID, inBuff, ref readingOffset);
                readingOffset += 30;
                if (peer.peerID == -1 || peer.peerID == -2)
                {
                    peer.peerID = peerID;
                }
                break;
            }
            }
            if (num != 0)
            {
                StreamBuffer streamBuffer = PeerBase.MessageBufferPoolGet();
                streamBuffer.Write(inBuff, readingOffset, num);
                this.Payload             = streamBuffer;
                this.Payload.IntPosition = 0;
                readingOffset           += num;
            }
        }
        public void ReceiveLoop()
        {
            StreamBuffer streamBuffer = new StreamBuffer(base.MTU);

            byte[] array = new byte[9];
            while (base.State == PhotonSocketState.Connected)
            {
                streamBuffer.SetLength(0L);
                try
                {
                    int i   = 0;
                    int num = 0;
                    while (i < 9)
                    {
                        try
                        {
                            num = this.sock.Receive(array, i, 9 - i, SocketFlags.None);
                        }
                        catch (SocketException ex)
                        {
                            bool flag = base.State != PhotonSocketState.Disconnecting && base.State > PhotonSocketState.Disconnected && ex.SocketErrorCode == SocketError.WouldBlock;
                            if (flag)
                            {
                                bool flag2 = base.ReportDebugOfLevel(DebugLevel.ALL);
                                if (flag2)
                                {
                                    base.EnqueueDebugReturn(DebugLevel.ALL, "ReceiveLoop() got a WouldBlock exception. This is non-fatal. Going to continue.");
                                }
                                continue;
                            }
                            throw;
                        }
                        i += num;
                        bool flag3 = num == 0;
                        if (flag3)
                        {
                            throw new SocketException(10054);
                        }
                    }
                    bool flag4 = array[0] == 240;
                    if (flag4)
                    {
                        base.HandleReceivedDatagram(array, array.Length, true);
                    }
                    else
                    {
                        int  num2 = (int)array[1] << 24 | (int)array[2] << 16 | (int)array[3] << 8 | (int)array[4];
                        bool trafficStatsEnabled = this.peerBase.TrafficStatsEnabled;
                        if (trafficStatsEnabled)
                        {
                            bool flag5 = array[5] == 0;
                            bool flag6 = flag5;
                            if (flag6)
                            {
                                this.peerBase.TrafficStatsIncoming.CountReliableOpCommand(num2);
                            }
                            else
                            {
                                this.peerBase.TrafficStatsIncoming.CountUnreliableOpCommand(num2);
                            }
                        }
                        bool flag7 = base.ReportDebugOfLevel(DebugLevel.ALL);
                        if (flag7)
                        {
                            base.EnqueueDebugReturn(DebugLevel.ALL, "message length: " + num2);
                        }
                        streamBuffer.SetCapacityMinimum(num2 - 7);
                        streamBuffer.Write(array, 7, i - 7);
                        i     = 0;
                        num2 -= 9;
                        while (i < num2)
                        {
                            try
                            {
                                num = this.sock.Receive(streamBuffer.GetBuffer(), (int)streamBuffer.Position, num2 - i, SocketFlags.None);
                            }
                            catch (SocketException ex2)
                            {
                                bool flag8 = base.State != PhotonSocketState.Disconnecting && base.State > PhotonSocketState.Disconnected && ex2.SocketErrorCode == SocketError.WouldBlock;
                                if (flag8)
                                {
                                    bool flag9 = base.ReportDebugOfLevel(DebugLevel.ALL);
                                    if (flag9)
                                    {
                                        base.EnqueueDebugReturn(DebugLevel.ALL, "ReceiveLoop() got a WouldBlock exception. This is non-fatal. Going to continue.");
                                    }
                                    continue;
                                }
                                throw;
                            }
                            streamBuffer.Position += (long)num;
                            i += num;
                            bool flag10 = num == 0;
                            if (flag10)
                            {
                                throw new SocketException(10054);
                            }
                        }
                        base.HandleReceivedDatagram(streamBuffer.ToArray(), (int)streamBuffer.Length, false);
                        bool flag11 = base.ReportDebugOfLevel(DebugLevel.ALL);
                        if (flag11)
                        {
                            base.EnqueueDebugReturn(DebugLevel.ALL, "TCP < " + streamBuffer.Length + ((streamBuffer.Length == (long)(num2 + 2)) ? " OK" : " BAD"));
                        }
                    }
                }
                catch (SocketException ex3)
                {
                    bool flag12 = base.State != PhotonSocketState.Disconnecting && base.State > PhotonSocketState.Disconnected;
                    if (flag12)
                    {
                        bool flag13 = base.ReportDebugOfLevel(DebugLevel.ERROR);
                        if (flag13)
                        {
                            base.EnqueueDebugReturn(DebugLevel.ERROR, "Receiving failed. SocketException: " + ex3.SocketErrorCode);
                        }
                        bool flag14 = ex3.SocketErrorCode == SocketError.ConnectionReset || ex3.SocketErrorCode == SocketError.ConnectionAborted;
                        if (flag14)
                        {
                            base.HandleException(StatusCode.DisconnectByServer);
                        }
                        else
                        {
                            base.HandleException(StatusCode.ExceptionOnReceive);
                        }
                    }
                }
                catch (Exception ex4)
                {
                    bool flag15 = base.State != PhotonSocketState.Disconnecting && base.State > PhotonSocketState.Disconnected;
                    if (flag15)
                    {
                        bool flag16 = base.ReportDebugOfLevel(DebugLevel.ERROR);
                        if (flag16)
                        {
                            base.EnqueueDebugReturn(DebugLevel.ERROR, string.Concat(new object[]
                            {
                                "Receive issue. State: ",
                                base.State,
                                ". Server: '",
                                base.ServerAddress,
                                "' Exception: ",
                                ex4
                            }));
                        }
                        base.HandleException(StatusCode.ExceptionOnReceive);
                    }
                }
            }
            this.Disconnect();
        }
Пример #15
0
        private void SerializeArray(StreamBuffer dout, Array serObject, bool setType)
        {
            if (setType)
            {
                dout.WriteByte(121);
            }
            if (serObject.Length > 32767)
            {
                throw new NotSupportedException("String[] that exceed 32767 (short.MaxValue) entries are not supported. Yours is: " + serObject.Length);
            }
            this.SerializeShort(dout, (short)serObject.Length, false);
            Type   elementType = serObject.GetType().GetElementType();
            GpType codeOfType  = this.GetCodeOfType(elementType);

            if (codeOfType != 0)
            {
                dout.WriteByte((byte)codeOfType);
                if (codeOfType == GpType.Dictionary)
                {
                    bool setKeyType   = default(bool);
                    bool setValueType = default(bool);
                    this.SerializeDictionaryHeader(dout, (object)serObject, out setKeyType, out setValueType);
                    for (int i = 0; i < serObject.Length; i++)
                    {
                        object value = serObject.GetValue(i);
                        this.SerializeDictionaryElements(dout, value, setKeyType, setValueType);
                    }
                }
                else
                {
                    for (int j = 0; j < serObject.Length; j++)
                    {
                        object value2 = serObject.GetValue(j);
                        this.Serialize(dout, value2, false);
                    }
                }
                return;
            }
            CustomType customType = default(CustomType);

            if (Protocol.TypeDict.TryGetValue(elementType, out customType))
            {
                dout.WriteByte(99);
                dout.WriteByte(customType.Code);
                int   num = 0;
                short num2;
                long  num3;
                while (true)
                {
                    if (num < serObject.Length)
                    {
                        object value3 = serObject.GetValue(num);
                        if (customType.SerializeStreamFunction == null)
                        {
                            byte[] array = customType.SerializeFunction(value3);
                            this.SerializeShort(dout, (short)array.Length, false);
                            dout.Write(array, 0, array.Length);
                        }
                        else
                        {
                            int position = dout.Position;
                            dout.Position += 2;
                            num2           = customType.SerializeStreamFunction(dout, value3);
                            num3           = dout.Position;
                            dout.Position  = position;
                            this.SerializeShort(dout, num2, false);
                            dout.Position += num2;
                            if (dout.Position != num3)
                            {
                                break;
                            }
                        }
                        num++;
                        continue;
                    }
                    return;
                }
                throw new Exception("Serialization failed. Stream position corrupted. Should be " + num3 + " is now: " + dout.Position + " serializedLength: " + num2);
            }
            throw new NotSupportedException("cannot serialize array of type " + elementType);
        }