Ejemplo 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) {
            }
        }
Ejemplo n.º 2
0
        public virtual void Dispose(bool flush)
        {
            if (mDisposing)
            {
                return;
            }

            ServerConsole.StatusLine("{0}: Disconnected.", this);
            mDisposing = true;

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

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

            // Check for existing objects in the World
            // FIXME - Account cant be removed
            // We need to check using World.GetAccount() after a new connection
            // from login -> char or char -> world
            // ...
            //if (Account != null) {
            //	World.RemoveObject(Account);
            //}

            if (mSocket != null)
            {
                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();
                }
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo 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 {
                        ServerConsole.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)
                        {
                            ServerConsole.StatusLine(String.Format("# {0}:{1}", ip[i], ipep.Port));
                            IPs.Add(ip[i]);
                            Ports.Add(ipep.Port);
                        }
                    } catch {
                    }
                }
                else
                {
                    IPs.Add(ipep.Address);
                    Ports.Add(ipep.Port);
                    if (ipep.Address.ToString() != Connector.IP)
                    {
                        ServerConsole.StatusLine(String.Format("start listen on {0} -> {1}:{2}", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        ServerConsole.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
                    {
                        ServerConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (In Use)", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else if (se.ErrorCode == 10049)                         // WSAEADDRNOTAVAIL
                    {
                        ServerConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (Unavailable)", Connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        ServerConsole.ErrorLine("Listener Exception:");
                        ServerConsole.WriteLine(e);
                    }
                }

                return(null);
            }
        }