// usually the Photon.SocketServer.Application provides the path but Policy is not inheriting that /// <summary> /// OnDisconnect callback. /// </summary> /// <param name = "peer"> /// The peer. /// </param> /// <param name = "userData"> /// The user data. /// </param> /// <param name = "reasonCode">Disconnect reason code.</param> /// <param name = "reasonDetail">Disconnect reason details.</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> public void OnDisconnect( IPhotonPeer peer, object userData, DisconnectReason reasonCode, string reasonDetail, int rtt, int rttVariance, int numFailures) { if (log.IsInfoEnabled && reasonCode != DisconnectReason.ClientDisconnect && reasonCode != DisconnectReason.ManagedDisconnect) { log.InfoFormat( "OnDisconnect: PID {0}, {1} ({2}), RTT: {3}, Variance: {4}, Failures:{5}", peer.GetConnectionID(), reasonCode, reasonDetail, rtt, rttVariance, numFailures); } else if (log.IsDebugEnabled) { log.DebugFormat( "OnDisconnect: PID {0}, {1} ({2}), RTT: {3}, Variance: {4}, Failures:{5}", peer.GetConnectionID(), reasonCode, reasonDetail, rtt, rttVariance, numFailures); } }
/// <summary> /// Called by the unmanaged socket server if a peer disconnects (or is disconnected). /// </summary> /// <param name="photonPeer">The peer which disconnected.</param> /// <param name="userData"> The user data.</param> /// <param name="reasonCode">The disconnect reason code.</param> /// <param name="reasonDetail">The disconnect reason detail.</param> /// <param name="rtt">The round trip time.</param> /// <param name="rttVariance">The round trip time variance.</param> /// <param name="numFailures"> The amount of failures. </param> void IPhotonApplication.OnDisconnect(IPhotonPeer photonPeer, object userData, DisconnectReason reasonCode, string reasonDetail, int rtt, int rttVariance, int numFailures) { try { IManagedPeer peer = userData as IManagedPeer; if (peer != null) { if (log.IsDebugEnabled) { log.DebugFormat("OnDisconnect - ConnID={0}", new object[] { photonPeer.GetConnectionID() }); } if (peer.Application_OnDisconnect(reasonCode, reasonDetail, rtt, rttVariance, numFailures)) { this.DecrementPeerCounter(); } } else if (log.IsDebugEnabled) { log.DebugFormat("OnDisconnect - Peer not found: {0}", new object[] { photonPeer.GetConnectionID() }); } } catch (Exception exception) { log.Error(exception); throw; } finally { photonPeer.SetUserData(null); } }
/// <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; } }
/// <summary> /// Callback for failed outbound connections. /// </summary> /// <param name="photonPeer"> The photon peer.</param> /// <param name="userData">The user data.</param> /// <param name="errorCode">The error Code.</param> /// <param name="errorMessage">The error message</param> void IPhotonApplication.OnOutboundConnectionFailed(IPhotonPeer photonPeer, object userData, int errorCode, string errorMessage) { try { TemporaryServerPeer peer = userData as TemporaryServerPeer; if (peer != null) { this.OnServerConnectionFailed(errorCode, errorMessage, peer.State); } else { log.ErrorFormat("Outbound connection failed for peer {0}: {1} - {2}; user data contained unexpected data: {3}", new object[] { photonPeer.GetConnectionID(), errorCode, errorMessage, userData }); } } catch (Exception exception) { log.Error(exception); throw; } finally { photonPeer.SetUserData(null); } }
/// <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; } }
/// <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; } }
/// <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; } }
/// <summary> /// Logs the init request. /// </summary> /// <param name="peer">The peer.</param> /// <param name="channelId">The channel id.</param> /// <param name="data">The data.</param> /// <param name="sendResult">The send result.</param> private static void LogInitRequest(IPhotonPeer peer, int channelId, byte[] data, SendResult sendResult) { if (operationDataLogger.IsDebugEnabled) { operationDataLogger.DebugFormat("SentInitRequest: ConnID={0}, ChannelId={1}, result={2}, data=({3} bytes) {4}", new object[] { peer.GetConnectionID(), channelId, sendResult, data.Length, BitConverter.ToString(data) }); } else if (log.IsDebugEnabled) { log.DebugFormat("SentInitRequest: ConnID={0}, ChannelId={1}, result={2} size={3} bytes", new object[] { peer.GetConnectionID(), channelId, sendResult, data.Length }); } }