void ProcessPackageWithoutSessionID(Socket listenSocket, IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            var sessionID = remoteEndPoint.ToString();
            var appSession = AppServer.GetAppSessionByID(sessionID);

            if (appSession == null) //New session
            {
                if (!DetectConnectionNumber(remoteEndPoint))
                    return;

                var socketSession = new UdpSocketSession(listenSocket, remoteEndPoint, sessionID);

                appSession = AppServer.CreateAppSession(socketSession);

                if (appSession == null)
                    return;

                Interlocked.Increment(ref m_ConnectionCount);
                socketSession.Closed += OnSocketSessionClosed;
                socketSession.Start();

                appSession.ProcessRequest(receivedData, 0, receivedData.Length, false);
            }
            else //Existing session
            {
                appSession.ProcessRequest(receivedData, 0, receivedData.Length, false);
            }
        }
Example #2
0
        private void ProcessReceivedData(IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            TAppSession appSession = AppServer.GetAppSessionByID(remoteEndPoint.ToString());

            if (appSession == null) //New session
            {
                if (m_LiveConnectionCount >= AppServer.Config.MaxConnectionNumber)
                {
                    if (AppServer.Logger.IsErrorEnabled)
                    {
                        AppServer.Logger.ErrorFormat("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, TRequestInfo>(m_ListenSocket, remoteEndPoint, this.RequestFilterFactory.CreateFilter(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.Error(x));
            }
            else //Existing session
            {
                var session = appSession.SocketSession as UdpSocketSession <TAppSession, TRequestInfo>;
                Async.Run(() => session.ProcessData(receivedData), (x) => AppServer.Logger.Error(x));
            }
        }
        void ProcessPackageWithSessionID(Socket listenSocket, IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            TRequestInfo requestInfo;
            
            string sessionID;

            int rest;

            try
            {
                requestInfo = this.m_UdpRequestFilter.Filter(receivedData, 0, receivedData.Length, false, out rest);
            }
            catch (Exception exc)
            {
                if(AppServer.Logger.IsErrorEnabled)
                    AppServer.Logger.Error("Failed to parse UDP package!", exc);
                return;
            }

            var udpRequestInfo = requestInfo as UdpRequestInfo;

            if (rest > 0)
            {
                if (AppServer.Logger.IsErrorEnabled)
                    AppServer.Logger.Error("The output parameter rest must be zero in this case!");
                return;
            }

            if (udpRequestInfo == null)
            {
                if (AppServer.Logger.IsErrorEnabled)
                    AppServer.Logger.Error("Invalid UDP package format!");
                return;
            }

            if (string.IsNullOrEmpty(udpRequestInfo.SessionID))
            {
                if (AppServer.Logger.IsErrorEnabled)
                    AppServer.Logger.Error("Failed to get session key from UDP package!");
                return;
            }

            sessionID = udpRequestInfo.SessionID;

            var appSession = AppServer.GetAppSessionByID(sessionID);

            if (appSession == null)
            {
                if (!DetectConnectionNumber(remoteEndPoint))
                    return;

                var socketSession = new UdpSocketSession(listenSocket, remoteEndPoint, sessionID);
                appSession = AppServer.CreateAppSession(socketSession);

                if (appSession == null)
                    return;

                Interlocked.Increment(ref m_ConnectionCount);

                socketSession.Closed += OnSocketSessionClosed;
                socketSession.Start();
            }
            else
            {
                var socketSession = appSession.SocketSession as UdpSocketSession;
                //Client remote endpoint may change, so update session to ensure the server can find client correctly
                socketSession.UpdateRemoteEndPoint(remoteEndPoint);
            }

            m_RequestHandler.ExecuteCommand(appSession, requestInfo);
        }
Example #4
0
        private void ProcessReceivedDataWithSessionID(IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            TRequestInfo requestInfo;
            string       sessionID;

            try
            {
                int left;
                requestInfo = this.m_UdpRequestFilter.Filter(null, receivedData, 0, receivedData.Length, false, out left);

                var udpRequestInfo = requestInfo as UdpRequestInfo;

                if (left > 0)
                {
                    if (AppServer.Logger.IsErrorEnabled)
                    {
                        AppServer.Logger.Error("The output parameter left must be zero in this case!");
                    }
                    return;
                }

                if (udpRequestInfo == null)
                {
                    if (AppServer.Logger.IsErrorEnabled)
                    {
                        AppServer.Logger.Error("Invalid UDP package format!");
                    }
                    return;
                }

                if (string.IsNullOrEmpty(udpRequestInfo.SessionID))
                {
                    if (AppServer.Logger.IsErrorEnabled)
                    {
                        AppServer.Logger.Error("Failed to get session key from UDP package!");
                    }
                    return;
                }

                sessionID = udpRequestInfo.SessionID;
            }
            catch (Exception exc)
            {
                if (AppServer.Logger.IsErrorEnabled)
                {
                    AppServer.Logger.Error("Failed to parse UDP package!", exc);
                }
                return;
            }

            var appSession = AppServer.GetAppSessionByID(sessionID);

            if (appSession == null)
            {
                var socketSession = RegisterSession(new UdpSocketSession(m_ListenSocket, remoteEndPoint, sessionID));
                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, TRequestInfo>;
                //Client remote endpoint may change, so update session to ensure the server can find client correctly
                socketSession.UpdateRemoteEndPoint(remoteEndPoint);
            }

            Async.Run(() => appSession.(appSession, requestInfo));
        }