Example #1
0
 private void CreatePeerInternal(object state)
 {
     try
     {
         InitRequest initRequest = (InitRequest)state;
         PeerBase    pObj        = this.CreatePeer(initRequest);
         if (pObj == null)
         {
             initRequest.PhotonPeer.DisconnectClient();
         }
         else
         {
             initRequest.PhotonPeer.SetUserData(pObj);
             this.IncrementPeerCounter();
             byte[]     data   = initRequest.Protocol.SerializeInitResponse();
             SendResult result = (SendResult)initRequest.PhotonPeer.Send(data, MessageReliablity.Flush | MessageReliablity.Reliable, 0, pObj.MessageContentType);
             if (log.IsDebugEnabled)
             {
                 log.DebugFormat("OnInit - response sent to ConnId {0} with SendResult {1}", new object[] { initRequest.PhotonPeer.GetConnectionID(), result });
             }
             if (operationDataLogger.IsDebugEnabled)
             {
                 operationDataLogger.DebugFormat("OnInit - ConnID={0}, send data=({1} bytes) {2}", new object[] { initRequest.PhotonPeer.GetConnectionID(), data.Length, BitConverter.ToString(data) });
             }
         }
     }
     catch (Exception exception)
     {
         log.Error(exception);
     }
 }
Example #2
0
        internal static bool TryParseWebSocketInitRequest(byte[] data, string applicationName, Version clientVersion, out InitRequest initRequest)
        {
            initRequest = null;
            IRpcProtocol json  = Json;
            string       str   = Encoding.UTF8.GetString(data);
            int          index = str.IndexOf("Sec-WebSocket-Protocol: ", StringComparison.OrdinalIgnoreCase);

            if (index > 0)
            {
                int length = str.IndexOfAny(new char[] { '\n', '\r' }, index);
                if (length < 0)
                {
                    length = str.Length;
                }
                string str2 = str.Substring(index, length - index).Replace("Sec-WebSocket-Protocol: ", string.Empty);
                if (!Enum.IsDefined(typeof(ProtocolType), str2))
                {
                    log.WarnFormat("Subprotocol {0} is unknown", new object[] { str2 });
                    return(false);
                }
                switch (((ProtocolType)Enum.Parse(typeof(ProtocolType), str2)))
                {
                case ProtocolType.GpBinaryV162:
                    json = GpBinaryV162;
                    goto Label_0101;

                case ProtocolType.Json:
                    json = Json;
                    goto Label_0101;
                }
                log.WarnFormat("Subprotocol {0} not supported", new object[] { str2 });
                return(false);
            }
Label_0101:
            initRequest = new InitRequest(applicationName, clientVersion, json);
            return(true);
        }
 protected abstract PeerBase CreatePeer(InitRequest initRequest);
Example #4
0
 /// <summary>
 /// Called by the unmanaged socket server when a peer intializes the connection.
 /// This method determines the used rpc protocol and then calls <see cref="M:Photon.SocketServer.ApplicationBase.CreatePeer(Photon.SocketServer.InitRequest)">CreatePeer</see>.
 /// </summary>
 /// <param name="nativePeer">The photon peer.</param>
 /// <param name="data"> The data.</param>
 /// <param name="channelCount">The number of channels that will be used by this peer. </param>
 void IPhotonApplication.OnInit(IPhotonPeer nativePeer, byte[] data, byte channelCount)
 {
     try
     {
         InitRequest request;
         PhotonCounter.InitPerSec.Increment();
         if (operationDataLogger.IsDebugEnabled)
         {
             operationDataLogger.DebugFormat("OnInit - ConnID={0}, data=({1} bytes) {2}", new object[] { nativePeer.GetConnectionID(), data.Length, BitConverter.ToString(data) });
         }
         if (log.IsDebugEnabled)
         {
             log.DebugFormat("OnInit - ConnID={0}, IP {1} on port {2}, type = {3}", new object[] { nativePeer.GetConnectionID(), nativePeer.GetRemoteIP(), nativePeer.GetLocalPort(), nativePeer.GetListenerType() });
         }
         if (nativePeer.GetListenerType() == ListenerType.HTTPListener)
         {
             string str = "";
             if (data.Length > 7)
             {
                 str = Encoding.ASCII.GetString(data, 7, data.Length - 7);
                 log.DebugFormat("OnInit - {0}", new object[] { str });
             }
             log.DebugFormat("OnInit - HttpPeer cnnected. " + str, new object[0]);
             if (str.Contains("json"))
             {
                 request = new InitRequest("LiteLobby", new Version(0, 3, 6), JsonProtocol.Instance);
             }
             else
             {
                 request = new InitRequest("LiteLobby", new Version(0, 3, 6), GpBinaryByteProtocolV16.HeaderV2Instance);
             }
         }
         else if (nativePeer.GetListenerType() == ListenerType.WebSocketListener)
         {
             if (!Protocol.TryParseWebSocketInitRequest(data, this.ApplicationName, Versions.DefaultWebsocketPeerVersion, out request))
             {
                 log.Warn("OnInit - Failed to parse init request for WebSocket peer: {0}" + data);
                 nativePeer.DisconnectClient();
                 return;
             }
         }
         else if (!Protocol.TryParseInitRequest(data, out request))
         {
             if (log.IsDebugEnabled)
             {
                 if (data.Length == 0)
                 {
                     log.Debug("OnInit - Data must contain at least one byte.");
                 }
                 else
                 {
                     log.DebugFormat("OnInit - Failed to parse init request with protocol {0}", new object[] { data[0] });
                 }
             }
             nativePeer.DisconnectClient();
             return;
         }
         request.PhotonPeer = nativePeer;
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.CreatePeerInternal), request);
     }
     catch (Exception exception)
     {
         log.Error(exception);
         throw;
     }
 }
Example #5
0
        /// <summary>
        /// The try parse init request.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="initRequest">The init request.</param>
        /// <returns>True if init request had correct format.</returns>
        internal static bool TryParseInitRequest(byte[] data, out InitRequest initRequest)
        {
            RtsMessageHeader header;
            RtsInitMessage   message;

            if ((data == null) || (data.Length == 0))
            {
                initRequest = null;
                return(false);
            }
            RtsMagicByte num = (RtsMagicByte)data[0];

            switch (num)
            {
            case RtsMagicByte.GpBinaryV2:
                if (RtsMessageHeaderConverterBinaryV2.Instance.TryParseHeader(data, out header))
                {
                    goto Label_0050;
                }
                break;

            case RtsMagicByte.Amf3V2:
                if (RtsMessageHeaderConverterAmf3V2.Instance.TryParseHeader(data, out header))
                {
                    goto Label_0050;
                }
                break;
            }
            initRequest = null;
            return(false);

Label_0050:
            if (RtsInitMessage.TryParse(data, header.SizeInBytes, out message))
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Parsed init message for application {0}, client version {1}.{2}.{3}, protocol {4} version {5}.{6}", new object[] { message.ApplicationId, message.ClientVersion[0], message.ClientVersion[1], message.ClientVersion[2], num, message.ProtocolVersion[0], message.ProtocolVersion[1] });
                }
                Version version       = new Version(message.ProtocolVersion[0], message.ProtocolVersion[1]);
                Version clientVersion = new Version(message.ClientVersion[0], message.ClientVersion[1], message.ClientVersion[2]);
                if (version.Major == 1)
                {
                    switch (version.Minor)
                    {
                    case 2:
                    {
                        IRpcProtocol protocol3 = GpBinaryByteProtocolV16Flash.FlashHeaderV2Instance;
                        initRequest = new InitRequest(message.ApplicationId, clientVersion, protocol3);
                        return(true);
                    }

                    case 5:
                        IRpcProtocol protocol;
                        if (!protocols15.TryGetValue(header.MagicByte, out protocol))
                        {
                            break;
                        }
                        initRequest = new InitRequest(message.ApplicationId, clientVersion, protocol);
                        return(true);

                    case 6:
                        IRpcProtocol protocol2;
                        if (!protocols16.TryGetValue(header.MagicByte, out protocol2))
                        {
                            break;
                        }
                        initRequest = new InitRequest(message.ApplicationId, clientVersion, protocol2);
                        return(true);

                    case 7:
                        if (header.MagicByte != RtsMagicByte.GpBinaryV2)
                        {
                            break;
                        }
                        initRequest = new InitRequest(message.ApplicationId, clientVersion, GpBinaryV17);
                        return(true);
                    }
                }
            }
            initRequest = null;
            return(false);
        }
Example #6
0
 // Methods
 protected PeerBase(InitRequest initRequest)
     : this(initRequest.Protocol, initRequest.PhotonPeer)
 {
 }