Exemplo n.º 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) {
            }
        }
Exemplo n.º 2
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();
            _PingListener = new PingListener(ipep);

            m_OnAccept = OnAccept;
            try
            {
                IAsyncResult res = m_Listener.BeginAccept(m_OnAccept, m_Listener);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
            catch (ObjectDisposedException)
            { }
        }
Exemplo n.º 3
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 NewAsyncSockets
            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
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 6
0
        private static bool VerifySocket(ConnectionContext connection)
        {
            try
            {
                var args = new SocketConnectEventArgs(connection);

                EventSink.InvokeSocketConnect(args);

                return(args.AllowConnection);
            }
            catch (Exception ex)
            {
                NetState.TraceException(ex);
                return(false);
            }
        }
Exemplo n.º 7
0
        public virtual async Task Start(MessagePump pump)
        {
            try
            {
                await m_Listener.BindAsync();
            }
            catch (AddressInUseException)
            {
                Console.WriteLine("Listener Failed: {0}:{1} (In Use)", m_EndPoint.Address, m_EndPoint.Port);
                m_Listener = null;
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine("Listener Exception:");
                Console.WriteLine(e);

                m_Listener = null;
                return;
            }

            DisplayListener();

            while (true)
            {
                ConnectionContext context;
                try
                {
                    context = await m_Listener.AcceptAsync();
                }
                catch (SocketException ex)
                {
                    NetState.TraceException(ex);
                    continue;
                }

                if (VerifySocket(context))
                {
                    _ = new NetState(context, pump);
                }
                else
                {
                    Release(context);
                }
            }
        }
Exemplo n.º 8
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 );
        }
Exemplo n.º 9
0
        private void OnAccept(IAsyncResult asyncResult)
        {
            Socket accepted = null;

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

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

            try
            {
                m_Listener.BeginAccept(m_OnAccept, m_Listener);
            }
            catch (SocketException ex)
            {
                NetState.TraceException(ex);
            }
            catch (ObjectDisposedException)
            {
            }
        }
Exemplo n.º 10
0
        private static void Release(ConnectionContext connection)
        {
            try
            {
                connection.Abort(new ConnectionAbortedException("Failed socket verification."));
            }
            catch (Exception ex)
            {
                NetState.TraceException(ex);
            }

            try
            {
                // TODO: Is this needed?
                connection.DisposeAsync();
            }
            catch (Exception ex)
            {
                NetState.TraceException(ex);
            }
        }
Exemplo n.º 11
0
        public static void DisplayGumpResponse(NetState state, CircularBufferReader reader, ref int packetLength)
        {
            var serial   = reader.ReadUInt32();
            var typeID   = reader.ReadInt32();
            var buttonID = reader.ReadInt32();

            foreach (var gump in state.Gumps)
            {
                if (gump.Serial != serial || gump.TypeID != typeID)
                {
                    continue;
                }

                var buttonExists = buttonID == 0; // 0 is always 'close'

                if (!buttonExists)
                {
                    foreach (var e in gump.Entries)
                    {
                        if (e is GumpButton button && button.ButtonID == buttonID)
                        {
                            buttonExists = true;
                            break;
                        }

                        if (e is GumpImageTileButton tileButton && tileButton.ButtonID == buttonID)
                        {
                            buttonExists = true;
                            break;
                        }
                    }
                }

                if (!buttonExists)
                {
                    state.WriteConsole("Invalid gump response, disconnecting...");
                    var exception = new InvalidGumpResponseException($"Button {buttonID} doesn't exist");
                    exception.SetStackTrace(new StackTrace());
                    NetState.TraceException(exception);
                    state.Mobile?.SendMessage("Invalid gump response.");

                    // state.Disconnect("Invalid gump response.");
                    return;
                }

                var switchCount = reader.ReadInt32();

                if (switchCount < 0 || switchCount > gump.m_Switches)
                {
                    state.WriteConsole("Invalid gump response, disconnecting...");
                    var exception = new InvalidGumpResponseException($"Bad switch count {switchCount}");
                    exception.SetStackTrace(new StackTrace());
                    NetState.TraceException(exception);
                    state.Mobile?.SendMessage("Invalid gump response.");

                    // state.Disconnect("Invalid gump response.");
                    return;
                }

                var switches = new int[switchCount];

                for (var i = 0; i < switches.Length; ++i)
                {
                    switches[i] = reader.ReadInt32();
                }

                var textCount = reader.ReadInt32();

                if (textCount < 0 || textCount > gump.m_TextEntries)
                {
                    state.WriteConsole("Invalid gump response, disconnecting...");
                    var exception = new InvalidGumpResponseException($"Bad text entry count {textCount}");
                    exception.SetStackTrace(new StackTrace());
                    NetState.TraceException(exception);
                    state.Mobile?.SendMessage("Invalid gump response.");

                    // state.Disconnect("Invalid gump response.");
                    return;
                }

                var textEntries = new TextRelay[textCount];

                for (var i = 0; i < textEntries.Length; ++i)
                {
                    int entryID    = reader.ReadUInt16();
                    int textLength = reader.ReadUInt16();

                    if (textLength > 239)
                    {
                        state.WriteConsole("Invalid gump response, disconnecting...");
                        var exception = new InvalidGumpResponseException($"Text entry {i} is too long ({textLength})");
                        exception.SetStackTrace(new StackTrace());
                        NetState.TraceException(exception);
                        state.Mobile?.SendMessage("Invalid gump response.");

                        // state.Disconnect("Invalid gump response.");
                        return;
                    }

                    var text = reader.ReadBigUniSafe(textLength);
                    textEntries[i] = new TextRelay(entryID, text);
                }

                state.RemoveGump(gump);

                var prof = GumpProfile.Acquire(gump.GetType());

                prof?.Start();

                gump.OnResponse(state, new RelayInfo(buttonID, switches, textEntries));

                prof?.Finish();

                return;
            }

            if (typeID == 461)
            {
                // Virtue gump
                var switchCount = reader.ReadInt32();

                if (buttonID == 1 && switchCount > 0)
                {
                    var beheld = World.FindMobile(reader.ReadUInt32());

                    if (beheld != null)
                    {
                        EventSink.InvokeVirtueGumpRequest(state.Mobile, beheld);
                    }
                }
                else
                {
                    var beheld = World.FindMobile(serial);

                    if (beheld != null)
                    {
                        EventSink.InvokeVirtueItemRequest(state.Mobile, beheld, buttonID);
                    }
                }
            }
        }