private void ProcessReceivedData(IPEndPoint remoteEndPoint, byte[] receivedData) { TAppSession appSession = AppServer.GetAppSessionByIndentityKey(remoteEndPoint.ToString()); if (appSession == null) //New session { if (m_LiveConnectionCount >= AppServer.Config.MaxConnectionNumber) { AppServer.Logger.LogError(string.Format("Cannot accept a new UDP connection from {0}, the max connection number {1} has been exceed!", remoteEndPoint.ToString(), AppServer.Config.MaxConnectionNumber)); return; } var session = RegisterSession(new UdpSocketSession <TAppSession, TCommandInfo>(m_ListenSocket, remoteEndPoint, Protocol.CreateCommandReader(AppServer))); if (session == null) { return; } Interlocked.Increment(ref m_LiveConnectionCount); session.Closed += new EventHandler <SocketSessionClosedEventArgs>(session_Closed); session.Start(); Async.Run(() => session.ProcessData(receivedData), (x) => AppServer.Logger.LogError(x)); } else //Existing session { var session = appSession.SocketSession as UdpSocketSession <TAppSession, TCommandInfo>; Async.Run(() => session.ProcessData(receivedData), (x) => AppServer.Logger.LogError(x)); } }
private void ProcessReceivedDataWithSessionKey(IPEndPoint remoteEndPoint, byte[] receivedData) { TCommandInfo commandInfo; string sessionKey; try { int left; commandInfo = m_UdpCommandInfoReader.FindCommandInfo(null, receivedData, 0, receivedData.Length, false, out left); var udpCommandInfo = commandInfo as UdpCommandInfo; if (left > 0) { AppServer.Logger.LogError("The output parameter left must be zero in this case!"); return; } if (udpCommandInfo == null) { AppServer.Logger.LogError("Invalid UDP package format!"); return; } if (string.IsNullOrEmpty(udpCommandInfo.SessionKey)) { AppServer.Logger.LogError("Failed to get session key from UDP package!"); return; } sessionKey = udpCommandInfo.SessionKey; } catch (Exception exc) { AppServer.Logger.LogError("Failed to parse UDP package!", exc); return; } TAppSession appSession = AppServer.GetAppSessionByIndentityKey(sessionKey); if (appSession == null) { var socketSession = RegisterSession(new UdpSocketSession <TAppSession, TCommandInfo>(m_ListenSocket, remoteEndPoint, sessionKey)); if (socketSession == null) { return; } appSession = socketSession.AppSession; Interlocked.Increment(ref m_LiveConnectionCount); socketSession.Closed += new EventHandler <SocketSessionClosedEventArgs>(session_Closed); socketSession.Start(); } else { var socketSession = appSession.SocketSession as UdpSocketSession <TAppSession, TCommandInfo>; //Client remote endpoint may change, so update session to ensure the server can find client correctly socketSession.UpdateRemoteEndPoint(remoteEndPoint); } Async.Run(() => appSession.ExecuteCommand(appSession, commandInfo)); }