コード例 #1
0
        private void OnAccept(IAsyncResult asyncResult)
        {
            var    listener = (Socket)asyncResult.AsyncState;
            Socket accepted = null;

            try {
                accepted = listener.EndAccept(asyncResult);
            } catch (SocketException ex) {
                Debug.WriteLine(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) {
                Debug.WriteLine(ex);
            } catch (ObjectDisposedException) { }
        }
コード例 #2
0
        private static void Release(Socket socket)
        {
            try {
                socket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                Debug.WriteLine(ex);
            }

            try {
                socket.Close();

                SocketPool.ReleaseSocket(socket);
            } catch (SocketException ex) {
                Debug.WriteLine(ex);
            }
        }
コード例 #3
0
        public virtual void Dispose(bool flush)
        {
            if (mSocket == null || mDisposing)
            {
                return;
            }

            mDisposing = true;

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

            if (flush)
            {
                Flush();
            }

            try {
                mSocket.Shutdown(SocketShutdown.Both);
            } catch (SocketException ex) {
                Debug.WriteLine(ex);
            }

            try {
                mSocket.Close();
                SocketPool.ReleaseSocket(mSocket);
            } catch (SocketException ex) {
                Debug.WriteLine(ex);
            }

            mSocket = null;

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

            mDisposed.Enqueue(this);

            if (!mSendQueue.IsEmpty)
            {
                lock (mSendQueue)
                    mSendQueue.Clear();
            }
        }
コード例 #4
0
        protected 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[] ipList = iphe.AddressList;
                        foreach (IPAddress ip in ipList)
                        {
                            CConsole.StatusLine(String.Format("# {0}:{1}", ip, 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
                 */
                var se = e as SocketException;
                if (se != null)
                {
                    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);
            }
        }