Пример #1
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);
        }
Пример #2
0
        public static void MessageBufferPoolPut(StreamBuffer buff)
        {
            Queue <StreamBuffer> messageBufferPool = PeerBase.MessageBufferPool;

            lock (messageBufferPool)
            {
                buff.IntPosition = 0;
                buff.SetLength(0L);
                PeerBase.MessageBufferPool.Enqueue(buff);
            }
        }
Пример #3
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);
        }
        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();
        }