Пример #1
0
        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;
                }

                socketSession.Initialize(appSession);

                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);
            }
        }
Пример #2
0
        protected UdpSocketSession <TAppSession, TCommandInfo> RegisterSession(UdpSocketSession <TAppSession, TCommandInfo> socketSession)
        {
            TAppSession appSession = this.AppServer.CreateAppSession(socketSession);

            if (appSession == null)
            {
                return(null);
            }

            socketSession.Initialize(this.AppServer, appSession);
            return(socketSession);
        }
Пример #3
0
        void ProcessPackageWithSessionID(Socket listenSocket, IPEndPoint remoteEndPoint, byte[] receivedData)
        {
            TRequestInfo requestInfo;

            string sessionID;

            int left;

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

            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;

            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;
                }

                socketSession.Initialize(appSession);

                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);
        }