示例#1
0
 public int GetSessionCount()
 {
     if (m_SessionGroup == null)
     {
         return(0);
     }
     else
     {
         return(m_SessionGroup.GetSessionCount());
     }
 }
示例#2
0
        public void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.

            Socket socket = null;

            lock (m_ListenWatcher)
            {
                try
                {
                    // Get the socket that handles the client request.
                    Socket listener = ar == null ? null : ar.AsyncState as Socket;
                    if (listener != null)
                    {
                        socket = listener.EndAccept(ar);
                    }
                }
                catch { socket = null; }
            }

            // after unlock the watcher, re-activate it ...
            m_ListenWatcher.Set();

            if (socket == null)
            {
                return;
            }

            if (!m_Acceptable || m_State <= 0)
            {
                try { socket.Close(); }
                catch { }
                return;
            }

            if (m_MaxClientCount > 0 && m_SessionGroup.GetSessionCount() >= m_MaxClientCount)
            {
                try { socket.Close(); }
                catch { }
                return;
            }

            // set every new session's io buffer size to a global value by default
            // but you still can change it in every session's OnConnect event function
            socket.ReceiveBufferSize = m_IoBufferSize;
            socket.SendBufferSize    = m_IoBufferSize;

            // Create the session object.

            Session session = null;

            lock (m_SessionGroup)
            {
                m_NextSessionID++;
                session = new Session(m_NextSessionID, socket, m_IoHandler, m_IoFilter, m_Cert != null);
                session.SetSessionGroup(m_SessionGroup);
            }

            try
            {
                if (session != null)
                {
                    Stream stream = session.GetStream();
                    if (stream != null)
                    {
                        if (stream is SslStream && m_Cert != null)
                        {
                            var result = (stream as SslStream).BeginAuthenticateAsServer(m_Cert, new AsyncCallback(AuthenticateCallback), session);
                            if (result == null)
                            {
                                throw new Exception("Failed to run BeginAuthenticateAsServer()");
                            }
                            else
                            {
                                return;
                            }
                        }
                        else if (stream is NetworkStream)
                        {
                            if (session != null)
                            {
                                session.Open();
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (m_IoHandler != null)
                {
                    try
                    {
                        m_IoHandler.OnError(null, Session.ERROR_CONNECT, ex);
                    }
                    catch { }
                }
            }

            if (session != null)
            {
                session.Close();
            }
        }