Esempio n. 1
0
        private void OnAccept(IAsyncResult asyncResult)
        {
            Socket listener = (Socket)asyncResult.AsyncState;
            Socket accepted = null;

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

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

            try {
                listener.BeginAccept(SocketPool.AcquireSocket(), 0, mOnAccept, listener);
            } catch (SocketException ex) {
                ExceptionHandler.Trace(ex);
            } catch (ObjectDisposedException) {
            }
        }
Esempio n. 2
0
        private void Release(Socket socket)
        {
            try {
                socket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                ExceptionHandler.Trace(ex);
            }

            try {
                socket.Close();

                SocketPool.ReleaseSocket(socket);
            } catch (SocketException ex) {
                ExceptionHandler.Trace(ex);
            }
        }
Esempio n. 3
0
        public virtual void Dispose(bool flush)
        {
            if (mSocket == null || mDisposing)
            {
                return;
            }

            mDisposing = true;

            //##PACKET## Logout
            Thread.Sleep(100);

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

            try {
                mSocket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                ExceptionHandler.Trace(ex);
            }

            try {
                mSocket.Close();
                SocketPool.ReleaseSocket(mSocket);
            } catch (SocketException ex) {
                ExceptionHandler.Trace(ex);
            }

            mSocket = null;

            mBuffer     = null;
            mRecvBuffer = null;
            mOnReceive  = null;
            mOnSend     = null;
            mRunning    = false;

            mDisposed.Enqueue(this);

            if (!mSendQueue.IsEmpty)
            {
                lock (mSendQueue)
                    mSendQueue.Clear();
            }
        }
Esempio n. 4
0
        private Socket Bind(IPEndPoint ipep, SocketConnector Connector)
        {
            Socket s = SocketPool.AcquireSocket();

            try {
                s.LingerState.Enabled = false;
                s.ExclusiveAddressUse = false;

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

                if (ipep.Address.Equals(IPAddress.Any))
                {
                    try {
                        CConsole.StatusLine(String.Format("start listen on {0}:{1}", IPAddress.Loopback, ipep.Port));

                        IPHostEntry iphe = Dns.GetHostEntry(Dns.GetHostName());
                        IPAddress[] ip   = iphe.AddressList;
                        for (int i = 0; i < ip.Length; ++i)
                        {
                            CConsole.StatusLine(String.Format("# {0}:{1}", ip[i], ipep.Port));
                        }
                    } catch {
                    }
                }
                else
                {
                    if (ipep.Address.ToString() != Connector.IP)
                    {
                        CConsole.StatusLine(String.Format("start listen on {0} -> {1}:{2}", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        CConsole.StatusLine(String.Format("start listen on {0}:{1}", ipep.Address, ipep.Port));
                    }
                }

                IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, mOnAccept, s);
                return(s);
            } catch (Exception e) {
                /* TODO
                 * throws more Exceptions like this
                 */
                if (e is SocketException)
                {
                    SocketException se = (SocketException)e;

                    if (se.ErrorCode == 10048)                        // WSAEADDRINUSE
                    {
                        CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (In Use)", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else if (se.ErrorCode == 10049)                          // WSAEADDRNOTAVAIL
                    {
                        CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (Unavailable)", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        CConsole.ErrorLine("Listener Exception:");
                        CConsole.WriteLine(e);
                    }
                }

                return(null);
            }
        }