示例#1
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(SocketPool.AcquireSocket(), 0, m_OnAccept, listener);
            } catch (SocketException ex) {
                NetState.TraceException(ex);
            } catch (ObjectDisposedException) {
            }
        }
示例#2
0
        public virtual void Dispose(bool flush)
        {
            if (m_Socket == null || m_Disposing)
            {
                return;
            }

            m_Disposing = true;

            if (flush)
            {
                flush = Flush();
            }

            try
            {
                m_Socket.Shutdown(SocketShutdown.Both);
            }
            catch (SocketException ex)
            {
                TraceException(ex);
            }

            try
            {
                m_Socket.Close();

                SocketPool.ReleaseSocket(m_Socket);
            }
            catch (SocketException ex)
            {
                TraceException(ex);
            }

            if (m_RecvBuffer != null)
            {
                m_ReceiveBufferPool.ReleaseBuffer(m_RecvBuffer);
            }

            m_Socket = null;

            m_Buffer     = null;
            m_RecvBuffer = null;
            m_OnReceive  = null;
            m_OnSend     = null;
            m_Running    = false;

            m_Disposed.Enqueue(this);

            if (/*!flush &&*/ !m_SendQueue.IsEmpty)
            {
                lock (m_SendQueue)
                    m_SendQueue.Clear();
            }
        }
示例#3
0
        public virtual void Dispose(bool flush)
        {
            // Genova: Usuário desconectando do servidor...
            if (m_Mobile is Mobile)
            {
                ControladorODBC.ODBCDispose(m_Mobile);
            }

            if (m_Socket == null || m_Disposing)
            {
                return;
            }

            m_Disposing = true;

            if (flush)
            {
                flush = Flush();
            }

            try {
                m_Socket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                TraceException(ex);
            }

            try {
                m_Socket.Close();

                SocketPool.ReleaseSocket(m_Socket);
            } catch (SocketException ex) {
                TraceException(ex);
            }

            if (m_RecvBuffer != null)
            {
                m_ReceiveBufferPool.ReleaseBuffer(m_RecvBuffer);
            }

            m_Socket = null;

            m_Buffer     = null;
            m_RecvBuffer = null;
            m_OnReceive  = null;
            m_OnSend     = null;
            m_Running    = false;

            m_Disposed.Enqueue(this);

            if (/*!flush &&*/ !m_SendQueue.IsEmpty)
            {
                lock (m_SendQueue)
                    m_SendQueue.Clear();
            }
        }
示例#4
0
        private void Release(Socket socket)
        {
            try {
                socket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                NetState.TraceException(ex);
            }

            try {
                socket.Close();

                SocketPool.ReleaseSocket(socket);
            } catch (SocketException ex) {
                NetState.TraceException(ex);
            }
        }
示例#5
0
        private Socket Bind(IPEndPoint ipep)
        {
            Socket s = SocketPool.AcquireSocket();

            try
            {
                s.LingerState.Enabled = false;
#if !MONO
                s.ExclusiveAddressUse = false;
#endif

                s.Bind(ipep);
                s.Listen(8);

                if (ipep.Address.Equals(IPAddress.Any))
                {
                    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

                    foreach (NetworkInterface adapter in adapters)
                    {
                        IPInterfaceProperties properties = adapter.GetIPProperties();

                        foreach (IPAddressInformation unicast in properties.UnicastAddresses)
                        {
                            Console.WriteLine("Listening: {0}:{1}", unicast.Address, ipep.Port);
                        }
                    }

                    /*
                     * try {
                     *      Console.WriteLine( "Listening: {0}:{1}", IPAddress.Loopback, ipep.Port );
                     *
                     *      IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() );
                     *
                     *      IPAddress[] ip = iphe.AddressList;
                     *
                     *      for ( int i = 0; i < ip.Length; ++i )
                     *              Console.WriteLine( "Listening: {0}:{1}", ip[i], ipep.Port );
                     * }
                     * catch { }
                     */
                }
                else
                {
                    Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
                }

                IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, m_OnAccept, s);

                return(s);
            }
            catch (Exception e)
            {
                if (e is SocketException)
                {
                    SocketException se = (SocketException)e;

                    if (se.ErrorCode == 10048)                         // WSAEADDRINUSE
                    {
                        Console.WriteLine("Listener Failed: {0}:{1} (In Use)", ipep.Address, ipep.Port);
                    }
                    else if (se.ErrorCode == 10049)                         // WSAEADDRNOTAVAIL
                    {
                        Console.WriteLine("Listener Failed: {0}:{1} (Unavailable)", ipep.Address, ipep.Port);
                    }
                    else
                    {
                        Console.WriteLine("Listener Exception:");
                        Console.WriteLine(e);
                    }
                }

                return(null);
            }
        }
示例#6
0
        private Socket Bind(IPEndPoint ipep)
        {
            Socket s = SocketPool.AcquireSocket();

            try
            {
                s.LingerState.Enabled = false;
#if !MONO
                s.ExclusiveAddressUse = false;
#endif

                s.Bind(ipep);
                s.Listen(8);

                if (ipep.Address.Equals(IPAddress.Any))
                {
                    try
                    {
                        Console.WriteLine("Listening: {0}:{1}", IPAddress.Loopback, ipep.Port);

                        IPHostEntry iphe = Dns.GetHostEntry(Dns.GetHostName());

                        IPAddress[] ip = iphe.AddressList;

                        for (int i = 0; i < ip.Length; ++i)
                        {
                            Console.WriteLine("Listening: {0}:{1}", ip[i], ipep.Port);
                        }
                    }
                    catch (Exception ex) { EventSink.InvokeLogException(new LogExceptionEventArgs(ex)); }
                }
                else
                {
                    Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
                }

                IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, m_OnAccept, s);

                return(s);
            }
            catch (Exception e)
            {
                if (e is SocketException)
                {
                    SocketException se = (SocketException)e;

                    if (se.ErrorCode == 10048)
                    {                     // WSAEADDRINUSE
                        Console.WriteLine("Listener Failed: {0}:{1} (In Use)", ipep.Address, ipep.Port);
                    }
                    else if (se.ErrorCode == 10049)
                    {                     // WSAEADDRNOTAVAIL
                        Console.WriteLine("Listener Failed: {0}:{1} (Unavailable)", ipep.Address, ipep.Port);
                    }
                    else
                    {
                        Console.WriteLine("Listener Exception:");
                        Console.WriteLine(e);
                    }
                }

                return(null);
            }
        }