コード例 #1
0
        protected virtual void OnSessionAccepted(Listener listener, Socket socket)
        {
            if (Stopped.IsTrue())
                return;

            // TODO 여기서 동접 체크?

            SocketAsyncEventArgs recvSAEA;
            if (!m_RecvSAEAPool.TryPop(out recvSAEA))
            {
                Task.Run(() => { socket.CloseEx(); });
                Logger.Error(string.Format("Max connection number {0} was reached!", m_ServerConfig.MaxConnectionNumber));
                return;
            }

            SocketAsyncEventArgs sendSAEA;
            if (!m_SendSAEAPool.TryPop(out sendSAEA))
            {
                Task.Run(() => { socket.CloseEx(); });
                Logger.Error(string.Format("Max connection number {0} was reached!", m_ServerConfig.MaxConnectionNumber));
                return;
            }

            Session session = CreateSession(socket, recvSAEA, sendSAEA);
            if (session == null)
            {
                recvSAEA.UserToken = null;
                sendSAEA.UserToken = null;
                m_RecvSAEAPool.Push(recvSAEA);
                m_SendSAEAPool.Push(sendSAEA);
                Task.Run(() => { socket.CloseEx(); });
                return;
            }

            session.Connected += OnSessionConnected;
            session.Disconnected += OnSessionDisconnected;
            session.Received += OnSessionReceived;
            session.Error += OnSessionError;

            long sessionID = GenerateSessionID();
            session.SetSessionID(sessionID);
            m_Sessions.TryAdd(sessionID, session);

            session.OnConnected();

            // TODO
            session.StartReceive();

            Logger.Debug(string.Format("OnSessionAccepted:{0}", session.m_ID));
        }