Пример #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
        internal void SendPing()
        {
            int num = this.lastPingResult = SupportClass.GetTickCount();

            if (!this.DoFraming)
            {
                SendOptions sendOptions = new SendOptions()
                {
                    DeliveryMode = DeliveryMode.Reliable
                };
                StreamBuffer streamBuffer = this.SerializeOperationToMessage(PhotonCodes.Ping, new Dictionary <byte, object>
                {
                    {
                        (byte)1,
                        (object)num
                    }
                }, EgMessageType.InternalOperationRequest, sendOptions.Encrypt);
                if (base.TrafficStatsEnabled)
                {
                    base.TrafficStatsOutgoing.CountControlCommand(streamBuffer.IntLength);
                }
                this.SendData(streamBuffer.GetBuffer(), streamBuffer.IntLength);
                PeerBase.MessageBufferPoolPut(streamBuffer);
            }
            else
            {
                int num2 = 1;
                Protocol.Serialize(num, this.pingRequest, ref num2);
                if (base.TrafficStatsEnabled)
                {
                    base.TrafficStatsOutgoing.CountControlCommand(this.pingRequest.Length);
                }
                this.SendData(this.pingRequest, this.pingRequest.Length);
            }
        }
Пример #3
0
        public bool EnqueueMessageAsPayload(DeliveryMode deliveryMode, StreamBuffer opMessage, byte channelId)
        {
            if (opMessage == null)
            {
                return(false);
            }
            if (this.DoFraming)
            {
                byte[] buffer = opMessage.GetBuffer();
                buffer[5] = channelId;
                switch (deliveryMode)
                {
                case DeliveryMode.Unreliable:
                    buffer[6] = 0;
                    break;

                case DeliveryMode.Reliable:
                    buffer[6] = 1;
                    break;

                case DeliveryMode.UnreliableUnsequenced:
                    buffer[6] = 2;
                    break;

                case DeliveryMode.ReliableUnsequenced:
                    buffer[6] = 3;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("DeliveryMode", deliveryMode, null);
                }
            }
            List <StreamBuffer> obj = this.outgoingStream;

            lock (obj)
            {
                this.outgoingStream.Add(opMessage);
                base.outgoingCommandsInStream++;
            }
            int num = base.ByteCountLastOperation = opMessage.IntLength;

            if (base.TrafficStatsEnabled)
            {
                switch (deliveryMode)
                {
                case DeliveryMode.Unreliable:
                    base.TrafficStatsOutgoing.CountUnreliableOpCommand(num);
                    break;

                case DeliveryMode.Reliable:
                    base.TrafficStatsOutgoing.CountReliableOpCommand(num);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("deliveryMode", deliveryMode, null);
                }
                base.TrafficStatsGameLevel.CountOperation(num);
            }
            return(true);
        }
Пример #4
0
        internal override bool SendOutgoingCommands()
        {
            if (base.peerConnectionState == ConnectionStateValue.Disconnected)
            {
                return(false);
            }
            if (!base.PhotonSocket.Connected)
            {
                return(false);
            }
            base.timeInt = SupportClass.GetTickCount() - base.timeBase;
            base.timeLastSendOutgoing = base.timeInt;
            if (base.peerConnectionState == ConnectionStateValue.Connected && Math.Abs(SupportClass.GetTickCount() - this.lastPingResult) > base.timePingInterval)
            {
                this.SendPing();
            }
            List <StreamBuffer> obj = this.outgoingStream;

            lock (obj)
            {
                for (int i = 0; i < this.outgoingStream.Count; i++)
                {
                    StreamBuffer streamBuffer = this.outgoingStream[i];
                    this.SendData(streamBuffer.GetBuffer(), streamBuffer.IntLength);
                    PeerBase.MessageBufferPoolPut(streamBuffer);
                }
                this.outgoingStream.Clear();
                base.outgoingCommandsInStream = 0;
            }
            return(false);
        }
Пример #5
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);
        }
Пример #6
0
        public static string StreamBufferToString(StreamBuffer buff)
        {
            StringBuilder bld = new StringBuilder();

            bld.Append("StreamBuffer with length: " + buff.IntLength + "\n");
            int length = buff.IntLength;

            byte[] buffer = buff.GetBuffer();
            for (int i = 0; i < length; i++)
            {
                bld.Append(buffer[i].ToString() + "\n");
            }
            return(bld.ToString());
        }
        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();
        }
Пример #8
0
        internal virtual bool DeserializeMessageAndCallback(StreamBuffer stream)
        {
            if (stream.IntLength < 2)
            {
                if ((int)this.debugOut >= 1)
                {
                    this.Listener.DebugReturn(DebugLevel.ERROR, "Incoming UDP data too short! " + stream.IntLength);
                }
                return(false);
            }
            byte b = stream.ReadByteAsByte();

            if (b != 243 && b != 253)
            {
                if ((int)this.debugOut >= 1)
                {
                    this.Listener.DebugReturn(DebugLevel.ALL, "No regular operation UDP message: " + b);
                }
                return(false);
            }
            byte b2   = stream.ReadByteAsByte();
            byte b3   = (byte)(b2 & 0x7F);
            bool flag = (b2 & 0x80) > 0;

            if (b3 != 1)
            {
                try
                {
                    if (flag)
                    {
                        byte[] buf = this.CryptoProvider.Decrypt(stream.GetBuffer(), 2, stream.IntLength - 2);
                        stream = new StreamBuffer(buf);
                    }
                    else
                    {
                        stream.Seek(2L, SeekOrigin.Begin);
                    }
                }
                catch (Exception ex)
                {
                    if ((int)this.debugOut >= 1)
                    {
                        this.Listener.DebugReturn(DebugLevel.ERROR, "msgType: " + b3 + " exception: " + ex.ToString());
                    }
                    SupportClass.WriteStackTrace(ex);
                    return(false);
                }
            }
            if (FullLogging)
            {
                Listener.DebugReturn(DebugLevel.ERROR, SupportClass.StreamBufferToString(stream));
            }
            int num = 0;

            switch (b3)
            {
            case 3:
            {
                OperationResponse operationResponse = this.SerializationProtocol.DeserializeOperationResponse(stream);
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.CountResult(this.ByteCountCurrentDispatch);
                    num = SupportClass.GetTickCount();
                }
                this.Listener.OnOperationResponse(operationResponse);
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.TimeForResponseCallback(operationResponse.OperationCode, SupportClass.GetTickCount() - num);
                }
                break;
            }

            case 4:
            {
                try
                {
                    EventData eventData = this.SerializationProtocol.DeserializeEventData(stream, this.reusableEventData);
                    if (this.TrafficStatsEnabled)
                    {
                        this.TrafficStatsGameLevel.CountEvent(this.ByteCountCurrentDispatch);
                        num = SupportClass.GetTickCount();
                    }
                    this.Listener.OnEvent(eventData);
                    if (this.TrafficStatsEnabled)
                    {
                        this.TrafficStatsGameLevel.TimeForEventCallback(eventData.Code, SupportClass.GetTickCount() - num);
                    }
                    if (this.photonPeer.ReuseEventInstance)
                    {
                        this.reusableEventData = eventData;
                    }
                }
                catch (Exception e)
                {
                    if (debugOut >= DebugLevel.INFO)
                    {
                        this.EnqueueDebugReturn(DebugLevel.ERROR, "Received abnormal EventData: " + e.Message + "\nException: " + e.StackTrace);
                    }
                    return(true);
                }
                break;
            }

            case 1:
                this.InitCallback();
                break;

            case 7:
            {
                OperationResponse operationResponse = this.SerializationProtocol.DeserializeOperationResponse(stream);
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.CountResult(this.ByteCountCurrentDispatch);
                    num = SupportClass.GetTickCount();
                }
                if (operationResponse.OperationCode == PhotonCodes.InitEncryption)
                {
                    this.DeriveSharedKey(operationResponse);
                }
                else if (operationResponse.OperationCode == PhotonCodes.Ping)
                {
                    TPeer tPeer = this as TPeer;
                    if (tPeer != null)
                    {
                        tPeer.ReadPingResult(operationResponse);
                    }
                }
                else
                {
                    this.EnqueueDebugReturn(DebugLevel.ERROR, "Received unknown internal operation. " + operationResponse.ToStringFull());
                }
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.TimeForResponseCallback(operationResponse.OperationCode, SupportClass.GetTickCount() - num);
                }
                break;
            }

            case 8:
            {
                object obj = this.SerializationProtocol.DeserializeMessage(stream);
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.CountEvent(this.ByteCountCurrentDispatch);
                    num = SupportClass.GetTickCount();
                }
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.TimeForMessageCallback(SupportClass.GetTickCount() - num);
                }
                break;
            }

            case 9:
            {
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.CountEvent(this.ByteCountCurrentDispatch);
                    num = SupportClass.GetTickCount();
                }
                byte[] array = stream.ToArrayFromPos();
                if (this.TrafficStatsEnabled)
                {
                    this.TrafficStatsGameLevel.TimeForRawMessageCallback(SupportClass.GetTickCount() - num);
                }
                break;
            }

            default:
                this.EnqueueDebugReturn(DebugLevel.ERROR, "unexpected msgType " + b3);
                break;
            }
            return(true);
        }
Пример #9
0
        public override object Deserialize(StreamBuffer din, byte type)
        {
            switch (type)
            {
            case 105:
                return(this.DeserializeInteger(din));

            case 115:
                return(this.DeserializeString(din));

            case 97:
                return(this.DeserializeStringArray(din));

            case 120:
                return(this.DeserializeByteArray(din, -1));

            case 110:
                return(this.DeserializeIntArray(din, -1));

            case 104:
                return(this.DeserializeHashTable(din));

            case 68:
                return(this.DeserializeDictionary(din));

            case 111:
                return(this.DeserializeBoolean(din));

            case 107:
                return(this.DeserializeShort(din));

            case 108:
                return(this.DeserializeLong(din));

            case 98:
                return(this.DeserializeByte(din));

            case 102:
                return(this.DeserializeFloat(din));

            case 100:
                return(this.DeserializeDouble(din));

            case 121:
                return(this.DeserializeArray(din));

            case 99:
            {
                byte customTypeCode = din.ReadByte();
                return(this.DeserializeCustom(din, customTypeCode));
            }

            case 122:
                return(this.DeserializeObjectArray(din));

            case 101:
                return(this.DeserializeEventData(din, null));

            case 113:
                return(this.DeserializeOperationRequest(din));

            case 112:
                return(this.DeserializeOperationResponse(din));

            case 0:
            case 42:
                return(null);

            default:
                throw new Exception("Deserialize(): " + type + " pos: " + din.Position + " bytes: " + din.Length + ". " + SupportClass.ByteArrayToString(din.GetBuffer()));
            }
        }