private void PostCallback()
 {
     ListenPipe = new UvPipeHandle(Log);
     ListenPipe.Init(Thread.Loop, false);
     ListenPipe.Bind(_pipeName);
     ListenPipe.Listen(Constants.ListenBacklog,
         (pipe, status, error, state) => ((ListenerPrimary)state).OnListenPipe(pipe, status, error), this);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Creates the socket used to listen for incoming connections
 /// </summary>
 protected override UvStreamHandle CreateListenSocket(string host, int port)
 {
     var socket = new UvPipeHandle(Log);
     socket.Init(Thread.Loop, false);
     socket.Bind(host);
     socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
     return socket;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Creates the socket used to listen for incoming connections
 /// </summary>
 protected override UvStreamHandle CreateListenSocket()
 {
     var socket = new UvPipeHandle(Log);
     socket.Init(Thread.Loop, false);
     socket.Bind(ServerAddress.UnixPipePath);
     socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
     return socket;
 }
        public void Connect(
            UvPipeHandle pipe, 
            string name, 
            Action<UvConnectRequest, int, Exception, object> callback, 
            object state)
        {
            _callback = callback;
            _state = state;

            Pin();
            Libuv.pipe_connect(this, pipe, name, _uv_connect_cb);
        }
        public void InitAndCloseServerPipe()
        {
            var loop = new UvLoopHandle();
            var pipe = new UvPipeHandle();

            loop.Init(_uv);
            pipe.Init(loop, true);
            pipe.Bind(@"\\.\pipe\InitAndCloseServerPipe");
            pipe.Dispose();

            loop.Run();

            pipe.Dispose();
            loop.Dispose();
        }
        public async Task StartAsync(
            string pipeName,
            ServerAddress address,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            await StartAsync(address, thread, application).ConfigureAwait(false);

            await Thread.PostAsync(_ =>
            {
                ListenPipe = new UvPipeHandle(Log);
                ListenPipe.Init(Thread.Loop, false);
                ListenPipe.Bind(pipeName);
                ListenPipe.Listen(Constants.ListenBacklog, OnListenPipe, null);
            }, null).ConfigureAwait(false);
        }
        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;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Handles an incoming connection
        /// </summary>
        /// <param name="listenSocket">Socket being used to listen on</param>
        /// <param name="status">Connection status</param>
        protected override void OnConnection(UvStreamHandle listenSocket, int status)
        {
            var acceptSocket = new UvPipeHandle(Log);
            acceptSocket.Init(Thread.Loop, false);

            try
            {
                listenSocket.Accept(acceptSocket);
            }
            catch (UvException ex)
            {
                Log.LogError("PipeListener.OnConnection", ex);
                return;
            }

            DispatchConnection(acceptSocket);
        }
Exemplo n.º 9
0
        public async Task StartAsync(
            string pipeName,
            string scheme,
            string host,
            int port,
            KestrelThread thread,
            Func<Frame, Task> application)
        {
            await StartAsync(scheme, host, port, thread, application).ConfigureAwait(false);

            await Thread.PostAsync(_ =>
            {
                ListenPipe = new UvPipeHandle();
                ListenPipe.Init(Thread.Loop, false);
                ListenPipe.Bind(pipeName);
                ListenPipe.Listen(Constants.ListenBacklog, OnListenPipe, null);
            }, null).ConfigureAwait(false);
        }
Exemplo n.º 10
0
        private void OnListenPipe(UvStreamHandle pipe, int status, Exception error, object state)
        {
            if (status < 0)
            {
                return;
            }

            var dispatchPipe = new UvPipeHandle();
            dispatchPipe.Init(Thread.Loop, true);
            try
            {
                pipe.Accept(dispatchPipe);
            }
            catch (Exception)
            {
                dispatchPipe.Dispose();
                return;
            }
            _dispatchPipes.Add(dispatchPipe);
        }
Exemplo n.º 11
0
        private void OnListenPipe(UvStreamHandle pipe, int status, Exception error)
        {
            if (status < 0)
            {
                return;
            }

            var dispatchPipe = new UvPipeHandle(Log);
            dispatchPipe.Init(Thread.Loop, true);

            try
            {
                pipe.Accept(dispatchPipe);
            }
            catch (UvException ex)
            {
                dispatchPipe.Dispose();
                Log.LogError("ListenerPrimary.OnListenPipe", ex);
                return;
            }

            _dispatchPipes.Add(dispatchPipe);
        }
Exemplo n.º 12
0
 public static extern void uv_pipe_connect(UvConnectRequest req,UvPipeHandle handle,string name,uv_connect_cb cb);
Exemplo n.º 13
0
 public static extern int uv_pipe_bind(UvPipeHandle loop,string name);
Exemplo n.º 14
0
 public static extern int uv_pipe_init(UvLoopHandle loop,UvPipeHandle handle,int ipc);
Exemplo n.º 15
0
 unsafe public int pipe_pending_count(UvPipeHandle handle)
 {
     handle.Validate();
     return(_uv_pipe_pending_count(handle));
 }
Exemplo n.º 16
0
 unsafe public void pipe_connect(UvConnectRequest req, UvPipeHandle handle, string name, uv_connect_cb cb)
 {
     req.Validate();
     handle.Validate();
     _uv_pipe_connect(req, handle, name, cb);
 }
Exemplo n.º 17
0
 public void pipe_bind(UvPipeHandle handle, string name)
 {
     handle.Validate();
     Check(_uv_pipe_bind(handle, name));
 }
Exemplo n.º 18
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;
        }
Exemplo n.º 19
0
        public void ServerPipeListenForConnections()
        {
            var loop = new UvLoopHandle();
            var serverListenPipe = new UvPipeHandle();

            loop.Init(_uv);
            serverListenPipe.Init(loop, false);
            serverListenPipe.Bind(@"\\.\pipe\ServerPipeListenForConnections");
            serverListenPipe.Listen(128, (_1, status, error, _2) =>
            {
                var serverConnectionPipe = new UvPipeHandle();
                serverConnectionPipe.Init(loop, true);
                try
                {
                    serverListenPipe.Accept(serverConnectionPipe);
                }
                catch (Exception)
                {
                    serverConnectionPipe.Dispose();
                    return;
                }

                var writeRequest = new UvWriteReq();
                writeRequest.Init(loop);
                writeRequest.Write(
                    serverConnectionPipe,
                    new ArraySegment<ArraySegment<byte>>(new ArraySegment<byte>[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 }) }),
                    (_3, status2, error2, _4) =>
                    {
                        writeRequest.Dispose();
                        serverConnectionPipe.Dispose();
                        serverListenPipe.Dispose();
                    },
                    null);

            }, null);

            var worker = new Thread(() =>
            {
                var loop2 = new UvLoopHandle();
                var clientConnectionPipe = new UvPipeHandle();
                var connect = new UvConnectRequest();

                loop2.Init(_uv);
                clientConnectionPipe.Init(loop2, true);
                connect.Init(loop2);
                connect.Connect(clientConnectionPipe, @"\\.\pipe\ServerPipeListenForConnections", (_1, status, error, _2) =>
                {
                    var buf = loop2.Libuv.buf_init(Marshal.AllocHGlobal(8192), 8192);

                    connect.Dispose();
                    clientConnectionPipe.ReadStart(
                        (_3, cb, _4) => buf,
                        (_3, status2, error2, _4) =>
                        {
                            if (status2 == 0)
                            {
                                clientConnectionPipe.Dispose();
                            }
                        },
                        null);
                }, null);
                loop2.Run();
                loop2.Dispose();
            });
            worker.Start();
            loop.Run();
            loop.Dispose();
            worker.Join();
        }
        public void ServerPipeListenForConnections()
        {
            var loop = new UvLoopHandle(_logger);
            var serverListenPipe = new UvPipeHandle(_logger);

            loop.Init(_uv);
            serverListenPipe.Init(loop, false);
            serverListenPipe.Bind(@"\\.\pipe\ServerPipeListenForConnections");
            serverListenPipe.Listen(128, (_1, status, error, _2) =>
            {
                var serverConnectionPipe = new UvPipeHandle(_logger);
                serverConnectionPipe.Init(loop, true);
                try
                {
                    serverListenPipe.Accept(serverConnectionPipe);
                }
                catch (Exception)
                {
                    serverConnectionPipe.Dispose();
                    return;
                }

                var writeRequest = new UvWriteReq(new KestrelTrace(new TestKestrelTrace()));
                writeRequest.Init(loop);
                var block = MemoryPoolBlock2.Create(
                    new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 }),
                    dataPtr: IntPtr.Zero,
                    pool: null,
                    slab: null);
                var start = new MemoryPoolIterator2(block, 0);
                var end = new MemoryPoolIterator2(block, block.Data.Count);
                writeRequest.Write(
                    serverConnectionPipe,
                    start, 
                    end,
                    1,
                    (_3, status2, error2, _4) =>
                    {
                        writeRequest.Dispose();
                        serverConnectionPipe.Dispose();
                        serverListenPipe.Dispose();
                        block.Unpin();
                    },
                    null);

            }, null);

            var worker = new Thread(() =>
            {
                var loop2 = new UvLoopHandle(_logger);
                var clientConnectionPipe = new UvPipeHandle(_logger);
                var connect = new UvConnectRequest(new KestrelTrace(new TestKestrelTrace()));

                loop2.Init(_uv);
                clientConnectionPipe.Init(loop2, true);
                connect.Init(loop2);
                connect.Connect(clientConnectionPipe, @"\\.\pipe\ServerPipeListenForConnections", (_1, status, error, _2) =>
                {
                    var buf = loop2.Libuv.buf_init(Marshal.AllocHGlobal(8192), 8192);

                    connect.Dispose();
                    clientConnectionPipe.ReadStart(
                        (_3, cb, _4) => buf,
                        (_3, status2, _4) =>
                        {
                            if (status2 == 0)
                            {
                                clientConnectionPipe.Dispose();
                            }
                        },
                        null);
                }, null);
                loop2.Run();
                loop2.Dispose();
            });
            worker.Start();
            loop.Run();
            loop.Dispose();
            worker.Join();
        }
Exemplo n.º 21
0
        public void ServerPipeDispatchConnections()
        {
            var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n");

            var loop = new UvLoopHandle();
            loop.Init(_uv);

            var serverConnectionPipe = default(UvPipeHandle);
            var serverConnectionPipeAcceptedEvent = new ManualResetEvent(false);
            var serverConnectionTcpDisposedEvent = new ManualResetEvent(false);

            var serverListenPipe = new UvPipeHandle();
            serverListenPipe.Init(loop, false);
            serverListenPipe.Bind(pipeName);
            serverListenPipe.Listen(128, (_1, status, error, _2) =>
            {
                serverConnectionPipe = new UvPipeHandle();
                serverConnectionPipe.Init(loop, true);
                try
                {
                    serverListenPipe.Accept(serverConnectionPipe);
                    serverConnectionPipeAcceptedEvent.Set();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    serverConnectionPipe.Dispose();
                    serverConnectionPipe = null;
                }
            }, null);

            var serverListenTcp = new UvTcpHandle();
            serverListenTcp.Init(loop);
            serverListenTcp.Bind(new IPEndPoint(0, 54321));
            serverListenTcp.Listen(128, (_1, status, error, _2) =>
            {
                var serverConnectionTcp = new UvTcpHandle();
                serverConnectionTcp.Init(loop);
                serverListenTcp.Accept(serverConnectionTcp);

                serverConnectionPipeAcceptedEvent.WaitOne();

                var writeRequest = new UvWriteReq();
                writeRequest.Init(loop);
                writeRequest.Write2(
                    serverConnectionPipe,
                    new ArraySegment<ArraySegment<byte>>(new ArraySegment<byte>[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 }) }),
                    serverConnectionTcp,
                    (_3, status2, error2, _4) =>
                    {
                        writeRequest.Dispose();
                        serverConnectionTcp.Dispose();
                        serverConnectionTcpDisposedEvent.Set();
                        serverConnectionPipe.Dispose();
                        serverListenPipe.Dispose();
                        serverListenTcp.Dispose();
                    },
                    null);
            }, null);

            var worker = new Thread(() =>
            {
                var loop2 = new UvLoopHandle();
                var clientConnectionPipe = new UvPipeHandle();
                var connect = new UvConnectRequest();

                loop2.Init(_uv);
                clientConnectionPipe.Init(loop2, true);
                connect.Init(loop2);
                connect.Connect(clientConnectionPipe, pipeName, (_1, status, error, _2) =>
                {
                    connect.Dispose();

                    var buf = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64);

                    serverConnectionTcpDisposedEvent.WaitOne();

                    clientConnectionPipe.ReadStart(
                        (_3, cb, _4) => buf,
                        (_3, status2, error2, _4) =>
                        {
                            if (status2 == 0)
                            {
                                clientConnectionPipe.Dispose();
                                return;
                            }
                            var clientConnectionTcp = new UvTcpHandle();
                            clientConnectionTcp.Init(loop2);
                            clientConnectionPipe.Accept(clientConnectionTcp);
                            var buf2 = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64);
                            clientConnectionTcp.ReadStart(
                                (_5, cb, _6) => buf2,
                                (_5, status3, error3, _6) =>
                                {
                                    if (status3 == 0)
                                    {
                                        clientConnectionTcp.Dispose();
                                    }
                                },
                                null);
                        },
                        null);
                }, null);
                loop2.Run();
                loop2.Dispose();
            });

            var worker2 = new Thread(() =>
            {
                try
                {
                    serverConnectionPipeAcceptedEvent.WaitOne();

                    var socket = new Socket(SocketType.Stream, ProtocolType.IP);
                    socket.Connect(IPAddress.Loopback, 54321);
                    socket.Send(new byte[] { 6, 7, 8, 9 });
                    socket.Shutdown(SocketShutdown.Send);
                    var cb = socket.Receive(new byte[64]);
                    socket.Dispose();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                }
            });

            worker.Start();
            worker2.Start();

            loop.Run();
            loop.Dispose();
            worker.Join();
            worker2.Join();
        }
Exemplo n.º 22
0
 public static extern int uv_pipe_init(UvLoopHandle loop, UvPipeHandle handle, int ipc);
Exemplo n.º 23
0
 unsafe public int pipe_pending_count(UvPipeHandle handle)
 {
     handle.Validate();
     return _uv_pipe_pending_count(handle);
 }
Exemplo n.º 24
0
 unsafe public void pipe_connect(UvConnectRequest req, UvPipeHandle handle, string name, uv_connect_cb cb)
 {
     req.Validate();
     handle.Validate();
     _uv_pipe_connect(req, handle, name, cb);
 }
Exemplo n.º 25
0
 public extern static int uv_pipe_pending_count(UvPipeHandle handle);
 /// <summary>
 /// Creates a socket which can be used to accept an incoming connection
 /// </summary>
 protected override UvStreamHandle CreateAcceptSocket()
 {
     var acceptSocket = new UvPipeHandle(Log);
     acceptSocket.Init(Thread.Loop, false);
     return acceptSocket;
 }
Exemplo n.º 27
0
 public void pipe_bind(UvPipeHandle handle, string name)
 {
     handle.Validate();
     Check(_uv_pipe_bind(handle, name));
 }
Exemplo n.º 28
0
 public extern static int uv_pipe_pending_count(UvPipeHandle handle);
Exemplo n.º 29
0
 public void pipe_init(UvLoopHandle loop, UvPipeHandle handle, bool ipc)
 {
     loop.Validate();
     handle.Validate();
     Check(_uv_pipe_init(loop, handle, ipc ? -1 : 0));
 }
Exemplo n.º 30
0
 public static extern int uv_pipe_bind(UvPipeHandle loop, string name);
Exemplo n.º 31
0
 public void pipe_init(UvLoopHandle loop, UvPipeHandle handle, bool ipc)
 {
     loop.Validate();
     handle.Validate();
     Check(_uv_pipe_init(loop, handle, ipc ? -1 : 0));
 }
Exemplo n.º 32
0
 public static extern void uv_pipe_connect(UvConnectRequest req, UvPipeHandle handle, string name, uv_connect_cb cb);
        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;
        }