Exemple #1
0
        public Listener(IPEndPoint ipep)
        {
            m_Accepted         = new Queue <Socket>();
            m_AcceptedSyncRoot = ((ICollection)m_Accepted).SyncRoot;

            m_Listener = Bind(ipep);

            if (m_Listener == null)
            {
                return;
            }

            DisplayListener();

#if Framework_4_0
            m_EventArgs            = new SocketAsyncEventArgs();
            m_EventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(Accept_Completion);
            Accept_Start();
#else
            m_OnAccept = new AsyncCallback(OnAccept);
            try
            {
                IAsyncResult res = m_Listener.BeginAccept(m_OnAccept, m_Listener);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
            catch (ObjectDisposedException)
            {
            }
#endif
        }
Exemple #2
0
        private void Accept_Start()
        {
            bool result = false;

            do
            {
                try
                {
                    result = !m_Listener.AcceptAsync(m_EventArgs);
                }
                catch (SocketException ex)
                {
                    NetState.TraceException(ex);
                    break;
                }
                catch (ObjectDisposedException)
                {
                    break;
                }

                if (result)
                {
                    Accept_Process(m_EventArgs);
                }
            } while (result);
        }
Exemple #3
0
        private bool VerifySocket(Socket socket)
        {
            try
            {
                SocketConnectEventArgs args = new SocketConnectEventArgs(socket);

                EventSink.InvokeSocketConnect(args);

                return(args.AllowConnection);
            }
            catch (Exception ex)
            {
                NetState.TraceException(ex);

                return(false);
            }
        }
Exemple #4
0
        private void OnAccept(IAsyncResult asyncResult)
        {
            Socket listener = (Socket)asyncResult.AsyncState;

            Socket accepted = null;

            try
            {
                accepted = listener.EndAccept(asyncResult);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            if (accepted != null)
            {
                if (VerifySocket(accepted))
                {
                    Enqueue(accepted);
                }
                else
                {
                    Release(accepted);
                }
            }

            try
            {
                listener.BeginAccept(m_OnAccept, listener);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
            catch (ObjectDisposedException)
            {
            }
        }
Exemple #5
0
        private void Release(Socket socket)
        {
            try
            {
                socket.Shutdown(SocketShutdown.Both);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }

            try
            {
                socket.Close();
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
        }