예제 #1
0
파일: Policy.cs 프로젝트: xubingyue/def
        /// <summary>
        ///   Called when [init].
        /// </summary>
        /// <param name = "peer">The peer.</param>
        /// <param name = "data">The data.</param>
        public void OnInit(IPhotonPeer peer, byte[] data, byte channelCount)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    Encoding utf8Encoding = Encoding.UTF8;
                    log.DebugFormat("OnInit - {0}", utf8Encoding.GetString(data));
                }

                if (data[0] == '<' && data[21] == '>')
                {
                    byte[] bytes = peer.GetLocalPort() == 943 ? this.silverlightPolicyBytesUtf8 : this.policyBytesUtf8;

                    // in case the policy app ever serves a websocket port...
                    MessageContentType contentType = peer.GetListenerType() == ListenerType.WebSocketListener
                                                         ? MessageContentType.Text
                                                         : MessageContentType.Binary;

                    peer.Send(bytes, MessageReliablity.Reliable, 0, contentType);

                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Policy sent.");
                    }
                }

                peer.DisconnectClient(); // silverlight does not disconnect by itself
            }
            catch (Exception e)
            {
                log.Error(e);
                throw;
            }
        }
예제 #2
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;
     }
 }
예제 #3
0
        /// <summary>
        ///   Called when [init].
        /// </summary>
        /// <param name = "peer">The peer.</param>
        /// <param name = "data">The data.</param>
        public void OnInit(IPhotonPeer peer, byte[] data)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    Encoding utf8Encoding = Encoding.UTF8;
                    log.DebugFormat("OnInit - {0}", utf8Encoding.GetString(data));
                }

                if (data[0] == '<' && data[21] == '>')
                {
                    byte[] bytes = peer.GetLocalPort() == 943 ? this.silverlightPolicyBytesUtf8 : this.policyBytesUtf8;
                    peer.Send(bytes, MessageReliablity.Reliable, 0);

                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Policy sent.");
                    }
                }

                peer.DisconnectClient(); // silverlight does not disconnect by itself
            }
            catch (Exception e)
            {
                log.Error(e);
                throw;
            }
        }