Ejemplo n.º 1
0
        public Task StartAsync(
            ServerAddress address,
            KestrelThread thread)
        {
            ServerAddress     = address;
            Thread            = thread;
            ConnectionManager = new ConnectionManager(thread);

            var tcs = new TaskCompletionSource <int>(this);

            Thread.Post(state =>
            {
                var tcs2 = (TaskCompletionSource <int>)state;
                try
                {
                    var listener          = ((Listener)tcs2.Task.AsyncState);
                    listener.ListenSocket = listener.CreateListenSocket();
                    tcs2.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs2.SetException(ex);
                }
            }, tcs);

            return(tcs.Task);
        }
Ejemplo n.º 2
0
        public Task StartAsync(
            string scheme,
            string host,
            int port,
            KestrelThread thread,
            Func <Frame, Task> application)
        {
            Thread      = thread;
            Application = application;

            var tcs = new TaskCompletionSource <int>();

            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = new UvTcpHandle();
                    ListenSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
                    ListenSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                    ListenSocket.Listen(10, _connectionCallback, this);
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return(tcs.Task);
        }
Ejemplo n.º 3
0
        public Task StartAsync(
            string scheme,
            string host,
            int port,
            KestrelThread thread,
            Func <Frame, Task> application)
        {
            Thread      = thread;
            Application = application;

            var tcs = new TaskCompletionSource <int>();

            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = CreateListenSocket(host, port);
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return(tcs.Task);
        }
Ejemplo n.º 4
0
        public Task StartAsync(
            ServerAddress address,
            KestrelThread thread)
        {
            ServerAddress = address;
            Thread = thread;

            var tcs = new TaskCompletionSource<int>(this);

            Thread.Post(tcs2 =>
            {
                try
                {
                    var listener = ((Listener)tcs2.Task.AsyncState);
                    listener.ListenSocket = listener.CreateListenSocket();
                    tcs2.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs2.SetException(ex);
                }
            }, tcs);

            return tcs.Task;
        }
Ejemplo n.º 5
0
        public Task StartAsync(
            ServerAddress address,
            KestrelThread thread,
            Func <Frame, Task> application)
        {
            ServerAddress = address;
            Thread        = thread;
            Application   = application;

            var tcs = new TaskCompletionSource <int>();

            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = CreateListenSocket();
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return(tcs.Task);
        }
Ejemplo n.º 6
0
        public async Task <bool> WalkConnectionsAndCloseAsync(TimeSpan timeout)
        {
            var tcs = new TaskCompletionSource <object>();

            _thread.Post(state => ((ConnectionManager)state).WalkConnectionsAndCloseCore(tcs), this);

            return(await Task.WhenAny(tcs.Task, Task.Delay(timeout)).ConfigureAwait(false) == tcs.Task);
        }
Ejemplo n.º 7
0
        public void Add(int count)
        {
            Debug.Assert(count >= 0);

            if (count == 0)
            {
                // No-op and avoid taking lock to reduce contention
                return;
            }

            lock (_lock)
            {
                Size += count;
                if (!_connectionPaused && Size >= _maxSize)
                {
                    _connectionPaused = true;
                    _connectionThread.Post(
                        (connectionControl) => ((IConnectionControl)connectionControl).Pause(),
                        _connectionControl);
                }
            }
        }
        public Task StartAsync(
            string pipeName,
            ServerAddress address,
            KestrelThread thread)
        {
            _pipeName = pipeName;
            _buf = thread.Loop.Libuv.buf_init(_ptr, 4);

            ServerAddress = address;
            Thread = thread;

            DispatchPipe = new UvPipeHandle(Log);

            var tcs = new TaskCompletionSource<int>(this);
            Thread.Post(tcs2 => StartCallback(tcs2), tcs);
            return tcs.Task;
        }
Ejemplo n.º 9
0
        public void Write(ArraySegment <byte> buffer, Action <Exception, object> callback, object state)
        {
            //TODO: need buffering that works
            var copy = new byte[buffer.Count];

            Array.Copy(buffer.Array, buffer.Offset, copy, 0, buffer.Count);
            buffer = new ArraySegment <byte>(copy);

            KestrelTrace.Log.ConnectionWrite(0, buffer.Count);
            var req = new ThisWriteReq();

            req.Init(_thread.Loop);
            req.Contextualize(this, _socket, buffer, callback, state);
            _thread.Post(x =>
            {
                ((ThisWriteReq)x).Write();
            }, req);
        }
        public Task StartAsync(
            string pipeName,
            ServerAddress address,
            KestrelThread thread)
        {
            _pipeName = pipeName;
            _buf      = thread.Loop.Libuv.buf_init(_ptr, 4);

            ServerAddress = address;
            Thread        = thread;

            DispatchPipe = new UvPipeHandle(Log);

            var tcs = new TaskCompletionSource <int>(this);

            Thread.Post(tcs2 => StartCallback(tcs2), tcs);
            return(tcs.Task);
        }
Ejemplo n.º 11
0
        public Task StartAsync(
            ServerAddress address,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            ServerAddress = address;
            Thread = thread;
            Application = application;

            var tcs = new TaskCompletionSource<int>();
            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = CreateListenSocket();
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return tcs.Task;
        }
Ejemplo n.º 12
0
        public Task StartAsync(
            string scheme,
            string host,
            int port,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            Thread = thread;
            Application = application;

            var tcs = new TaskCompletionSource<int>();
            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = new UvTcpHandle();
                    ListenSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
                    ListenSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                    ListenSocket.Listen(10, _connectionCallback, this);
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return tcs.Task;
        }
Ejemplo n.º 13
0
        public Task StartAsync(
            string scheme,
            string host,
            int port,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            Thread = thread;
            Application = application;

            var tcs = new TaskCompletionSource<int>();
            Thread.Post(_ =>
            {
                try
                {
                    ListenSocket = CreateListenSocket(host, port);
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }, null);
            return tcs.Task;
        }
Ejemplo n.º 14
0
        public Task StartAsync(
            string pipeName,
            ServerAddress address,
            KestrelThread thread,
            Func <Frame, Task> application)
        {
            ServerAddress = address;
            Thread        = thread;
            Application   = application;

            DispatchPipe = new UvPipeHandle(Log);

            var tcs = new TaskCompletionSource <int>();

            Thread.Post(_ =>
            {
                try
                {
                    DispatchPipe.Init(Thread.Loop, true);
                    var connect = new UvConnectRequest(Log);
                    connect.Init(Thread.Loop);
                    connect.Connect(
                        DispatchPipe,
                        pipeName,
                        (connect2, status, error, state) =>
                    {
                        connect.Dispose();
                        if (error != null)
                        {
                            tcs.SetException(error);
                            return;
                        }

                        try
                        {
                            var ptr = Marshal.AllocHGlobal(4);
                            var buf = Thread.Loop.Libuv.buf_init(ptr, 4);

                            DispatchPipe.ReadStart(
                                (_1, _2, _3) => buf,
                                (_1, status2, state2) =>
                            {
                                if (status2 < 0)
                                {
                                    if (status2 != Constants.EOF)
                                    {
                                        Exception ex;
                                        Thread.Loop.Libuv.Check(status2, out ex);
                                        Log.LogError("DispatchPipe.ReadStart", ex);
                                    }

                                    DispatchPipe.Dispose();
                                    Marshal.FreeHGlobal(ptr);
                                    return;
                                }

                                if (DispatchPipe.PendingCount() == 0)
                                {
                                    return;
                                }

                                var acceptSocket = CreateAcceptSocket();

                                try
                                {
                                    DispatchPipe.Accept(acceptSocket);
                                }
                                catch (UvException ex)
                                {
                                    Log.LogError("DispatchPipe.Accept", ex);
                                    acceptSocket.Dispose();
                                    return;
                                }

                                var connection = new Connection(this, acceptSocket);
                                connection.Start();
                            },
                                null);

                            tcs.SetResult(0);
                        }
                        catch (Exception ex)
                        {
                            DispatchPipe.Dispose();
                            tcs.SetException(ex);
                        }
                    },
                        null);
                }
                catch (Exception ex)
                {
                    DispatchPipe.Dispose();
                    tcs.SetException(ex);
                }
            }, null);
            return(tcs.Task);
        }
Ejemplo n.º 15
0
        public Task StartAsync(
            string pipeName,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            Thread = thread;
            Application = application;

            DispatchPipe = new UvPipeHandle();

            var tcs = new TaskCompletionSource<int>();
            Thread.Post(_ =>
            {
                try
                {
                    DispatchPipe.Init(Thread.Loop, true);
                    var connect = new UvConnectRequest();
                    connect.Init(Thread.Loop);
                    connect.Connect(
                        DispatchPipe,
                        pipeName,
                        (connect2, status, error, state) =>
                        {
                            connect.Dispose();
                            if (error != null)
                            {
                                tcs.SetException(error);
                                return;
                            }

                            try
                            {
                                var ptr = Marshal.AllocHGlobal(4);
                                var buf = Thread.Loop.Libuv.buf_init(ptr, 4);

                                DispatchPipe.ReadStart(
                                    (_1, _2, _3) => buf,
                                    (_1, status2, error2, state2) =>
                                    {
                                        if (status2 == 0)
                                        {
                                            DispatchPipe.Dispose();
                                            Marshal.FreeHGlobal(ptr);
                                            return;
                                        }

                                        var acceptSocket = new UvTcpHandle();
                                        acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);

                                        try
                                        {
                                            DispatchPipe.Accept(acceptSocket);
                                        }
                                        catch (Exception ex)
                                        {
                                            Trace.WriteLine("DispatchPipe.Accept " + ex.Message);
                                            acceptSocket.Dispose();
                                            return;
                                        }

                                        var connection = new Connection(this, acceptSocket);
                                        connection.Start();
                                    },
                                    null);

                                tcs.SetResult(0);
                            }
                            catch (Exception ex)
                            {
                                DispatchPipe.Dispose();
                                tcs.SetException(ex);
                            }
                        },
                        null);
                }
                catch (Exception ex)
                {
                    DispatchPipe.Dispose();
                    tcs.SetException(ex);
                }
            }, null);
            return tcs.Task;
        }
        public Task StartAsync(
            string pipeName,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            Thread = thread;
            Application = application;

            DispatchPipe = new UvPipeHandle(Log);

            var tcs = new TaskCompletionSource<int>();
            Thread.Post(_ =>
            {
                try
                {
                    DispatchPipe.Init(Thread.Loop, true);
                    var connect = new UvConnectRequest(Log);
                    connect.Init(Thread.Loop);
                    connect.Connect(
                        DispatchPipe,
                        pipeName,
                        (connect2, status, error, state) =>
                        {
                            connect.Dispose();
                            if (error != null)
                            {
                                tcs.SetException(error);
                                return;
                            }

                            try
                            {
                                var ptr = Marshal.AllocHGlobal(4);
                                var buf = Thread.Loop.Libuv.buf_init(ptr, 4);

                                DispatchPipe.ReadStart(
                                    (_1, _2, _3) => buf,
                                    (_1, status2, errCode, error2, state2) =>
                                    {
                                        if (status2 < 0)
                                        {
                                            if (status2 != Constants.EOF)
                                            {
                                                Exception ex;
                                                Thread.Loop.Libuv.Check(status2, out ex);
                                                Log.LogError("DispatchPipe.ReadStart", ex);
                                            }

                                            DispatchPipe.Dispose();
                                            Marshal.FreeHGlobal(ptr);
                                            return;
                                        }

                                        if (DispatchPipe.PendingCount() == 0)
                                        {
                                            return;
                                        }

                                        var acceptSocket = CreateAcceptSocket();

                                        try
                                        {
                                            DispatchPipe.Accept(acceptSocket);
                                        }
                                        catch (UvException ex)
                                        {
                                            Log.LogError("DispatchPipe.Accept", ex);
                                            acceptSocket.Dispose();
                                            return;
                                        }

                                        var connection = new Connection(this, acceptSocket);
                                        connection.Start();
                                    },
                                    null);

                                tcs.SetResult(0);
                            }
                            catch (Exception ex)
                            {
                                DispatchPipe.Dispose();
                                tcs.SetException(ex);
                            }
                        },
                        null);
                }
                catch (Exception ex)
                {
                    DispatchPipe.Dispose();
                    tcs.SetException(ex);
                }
            }, null);
            return tcs.Task;
        }