Exemplo n.º 1
0
        void AcceptNext()
        {
            try
            {
                NPx.Log.Debug("KayakServer: accepting connection");
                listener.BeginAccept(iasr =>
                {
                    NPx.Log.Debug("KayakServer: accepted connection callback");
                    Socket socket   = null;
                    Exception error = null;
                    try
                    {
                        socket = listener.EndAccept(iasr);
                        AcceptNext();
                    }
                    catch (Exception e)
                    {
                        error = e;
                    }

                    if (error is ObjectDisposedException)
                    {
                        return;
                    }

                    scheduler.Post(() =>
                    {
                        NPx.Log.Debug("KayakServer: accepted connection");
                        if (error != null)
                        {
                            HandleAcceptError(error);
                        }

                        var s = new DefaultKayakSocket(new SocketWrapper(socket), this.scheduler);
                        state.IncrementConnections();

                        var socketDelegate = del.OnConnection(this, s);
                        s.del = socketDelegate;
                        s.BeginRead();
                    });
                }, null);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception e)
            {
                HandleAcceptError(e);
            }
        }