Ejemplo n.º 1
0
 /// <summary>
 /// Called by the unmanaged socket server when a peer's send buffer reaches it's limit or is freed again.
 /// </summary>
 /// <param name="photonPeer"> The unmanaged peer.</param>
 /// <param name="userData">The user data.</param>
 /// <param name="flowControlEvent">The flow control event.</param>
 void IPhotonApplication.OnFlowControlEvent(IPhotonPeer photonPeer, object userData, FlowControlEvent flowControlEvent)
 {
     try
     {
         IManagedPeer peer = userData as IManagedPeer;
         if (peer == null)
         {
             log.ErrorFormat("OnFlowControlEvent - Peer {0}'s user data is of wrong type or null: {1}; event {2}", new object[] { photonPeer.GetConnectionID(), userData, flowControlEvent });
             photonPeer.DisconnectClient();
         }
         else
         {
             if (log.IsDebugEnabled)
             {
                 log.DebugFormat("OnFlowControlEvent: Peer={0}; event={1}", new object[] { photonPeer.GetConnectionID(), flowControlEvent });
             }
             if (flowControlEvent == FlowControlEvent.FlowControlAllOk)
             {
                 peer.Application_OnSendBufferEmpty();
             }
         }
     }
     catch (Exception exception)
     {
         log.Error(exception);
         throw;
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Called by the unmanaged socket server when new data was received.
 /// </summary>
 /// <param name="photonPeer">The peer who sent the operation.</param>
 /// <param name="userData">The user data.</param>
 /// <param name="data">The data for the operation.</param>
 /// <param name="reliability">Message reliable flags for the operation.</param>
 /// <param name="channelId">The channel ID.</param>
 /// <param name="messageContentType">The Message Content Type.</param>
 /// <param name="rtt"> The round trip time.</param>
 /// <param name="rttVariance">The round trip time variance.</param>
 /// <param name="numFailures"> The number of failures.</param>
 void IPhotonApplication.OnReceive(IPhotonPeer photonPeer, object userData, byte[] data, MessageReliablity reliability, byte channelId, MessageContentType messageContentType, int rtt, int rttVariance, int numFailures)
 {
     try
     {
         PhotonCounter.OperationReceiveCount.Increment();
         PhotonCounter.OperationReceivePerSec.Increment();
         if (operationDataLogger.IsDebugEnabled)
         {
             operationDataLogger.DebugFormat("OnReceive - ConnID={0}, data=({1} bytes) {2}", new object[] { photonPeer.GetConnectionID(), data.Length, BitConverter.ToString(data) });
         }
         IManagedPeer peer = userData as IManagedPeer;
         if (peer == null)
         {
             log.ErrorFormat("OnReceive - Peer {0}'s user data is of wrong type or null: {1}", new object[] { photonPeer.GetConnectionID(), userData });
             photonPeer.DisconnectClient();
         }
         else
         {
             SendParameters sendParameters = new SendParameters
             {
                 Unreliable = reliability == MessageReliablity.UnReliable,
                 ChannelId  = channelId
             };
             peer.Application_OnReceive(data, sendParameters, rtt, rttVariance, numFailures);
         }
     }
     catch (Exception exception)
     {
         log.Error(exception);
         throw;
     }
 }
Ejemplo n.º 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, 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;
            }
        }
Ejemplo n.º 4
0
        private void Disconnect()
        {
            IPhotonPeer photonPeer;

            lock (this.syncRoot)
            {
                photonPeer = this.photonPeer;
            }
            if (!this.aborted)
            {
                photonPeer.DisconnectClient();
                this.aborted = true;
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Callback for established outbound connections.
 /// </summary>
 /// <param name="photonPeer">The photon peer.</param>
 /// <param name="data">Contains the response headers from the handshake negotiation for outbound websocket connections; empty for all other connection types.</param>
 /// <param name="userData">The user data.</param>
 void IPhotonApplication.OnOutboundConnectionEstablished(IPhotonPeer photonPeer, byte[] data, object userData)
 {
     try
     {
         TemporaryServerPeer peer = userData as TemporaryServerPeer;
         if (peer != null)
         {
             peer.Application_OnOutboundConnectionEstablished(photonPeer);
         }
         else
         {
             log.ErrorFormat("Outbound connection established for peer {0} but user data contained unexpected data: {1}; aborting connection.", new object[] { photonPeer.GetConnectionID(), userData });
             photonPeer.DisconnectClient();
         }
     }
     catch (Exception exception)
     {
         log.Error(exception);
         throw;
     }
 }
Ejemplo n.º 6
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;
     }
 }
Ejemplo n.º 7
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;
            }
        }