public async Task SocketCanListenAndAccept() { var loop = new UvLoopHandle(_logger); loop.Init(_uv); var tcp = new UvTcpHandle(_logger); tcp.Init(loop, (a, b) => { }); var address = ServerAddress.FromUrl($"http://127.0.0.1:0/"); tcp.Bind(address); var port = tcp.GetSockIPEndPoint().Port; tcp.Listen(10, (stream, status, error, state) => { var tcp2 = new UvTcpHandle(_logger); tcp2.Init(loop, (a, b) => { }); stream.Accept(tcp2); tcp2.Dispose(); stream.Dispose(); }, null); var t = Task.Run(() => { var socket = TestConnection.CreateConnectedLoopbackSocket(port); socket.Dispose(); }); loop.Run(); loop.Dispose(); await t; }
public void ConnectionCanReadAndWrite(TestServiceContext testContext) { var port = TestServer.GetNextPort(); testContext.App = App; var engine = new KestrelEngine(testContext); engine.Start(1); var address = ServerAddress.FromUrl($"http://localhost:{port}/"); var started = engine.CreateServer(address); var socket = TestConnection.CreateConnectedLoopbackSocket(port); socket.Send(Encoding.ASCII.GetBytes("POST / HTTP/1.0\r\n\r\nHello World")); socket.Shutdown(SocketShutdown.Send); var buffer = new byte[8192]; while (true) { var length = socket.Receive(buffer); if (length == 0) { break; } var text = Encoding.ASCII.GetString(buffer, 0, length); } started.Dispose(); engine.Dispose(); }
public async Task SocketCanRead() { var loop = new UvLoopHandle(_logger); loop.Init(_uv); var tcp = new UvTcpHandle(_logger); tcp.Init(loop, (a, b) => { }); var address = ServerAddress.FromUrl($"http://127.0.0.1:0/"); tcp.Bind(address); var port = tcp.GetSockIPEndPoint().Port; tcp.Listen(10, (_, status, error, state) => { var tcp2 = new UvTcpHandle(_logger); tcp2.Init(loop, (a, b) => { }); tcp.Accept(tcp2); var data = Marshal.AllocCoTaskMem(500); tcp2.ReadStart( (a, b, c) => _uv.buf_init(data, 500), (__, nread, state2) => { if (nread <= 0) { tcp2.Dispose(); } }, null); tcp.Dispose(); }, null); var t = Task.Run(async() => { var socket = TestConnection.CreateConnectedLoopbackSocket(port); #if NET451 await Task.Factory.FromAsync( socket.BeginSend, socket.EndSend, new[] { new ArraySegment <byte>(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None, null, TaskCreationOptions.None); #else await socket.SendAsync(new[] { new ArraySegment <byte>(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None); #endif socket.Dispose(); }); loop.Run(); loop.Dispose(); await t; }
public async Task DisconnectingClient(ServiceContext testContext) { using (var server = new TestServer(App, testContext)) { var socket = TestConnection.CreateConnectedLoopbackSocket(server.Port); await Task.Delay(200); socket.Dispose(); await Task.Delay(200); using (var connection = new TestConnection(server.Port)) { await connection.SendEnd( "GET / HTTP/1.0", "\r\n"); await connection.ReceiveEnd( "HTTP/1.0 200 OK", "\r\n"); } } }
public void ServerPipeDispatchConnections() { var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n"); var loop = new UvLoopHandle(_logger); loop.Init(_uv); var serverConnectionPipe = default(UvPipeHandle); var serverConnectionPipeAcceptedEvent = new ManualResetEvent(false); var serverConnectionTcpDisposedEvent = new ManualResetEvent(false); var serverListenPipe = new UvPipeHandle(_logger); serverListenPipe.Init(loop, (a, b) => { }, false); serverListenPipe.Bind(pipeName); serverListenPipe.Listen(128, (handle, status, error, state) => { serverConnectionPipe = new UvPipeHandle(_logger); serverConnectionPipe.Init(loop, (a, b) => { }, true); try { serverListenPipe.Accept(serverConnectionPipe); serverConnectionPipeAcceptedEvent.Set(); } catch (Exception ex) { Console.WriteLine(ex); serverConnectionPipe.Dispose(); serverConnectionPipe = null; } }, null); var serverListenTcp = new UvTcpHandle(_logger); serverListenTcp.Init(loop, (a, b) => { }); var address = ServerAddress.FromUrl($"http://127.0.0.1:0/"); serverListenTcp.Bind(address); var port = serverListenTcp.GetSockIPEndPoint().Port; serverListenTcp.Listen(128, (handle, status, error, state) => { var serverConnectionTcp = new UvTcpHandle(_logger); serverConnectionTcp.Init(loop, (a, b) => { }); serverListenTcp.Accept(serverConnectionTcp); serverConnectionPipeAcceptedEvent.WaitOne(); var writeRequest = new UvWriteReq(new KestrelTrace(new TestKestrelTrace())); writeRequest.Init(loop); writeRequest.Write2( serverConnectionPipe, new ArraySegment <ArraySegment <byte> >(new ArraySegment <byte>[] { new ArraySegment <byte>(new byte[] { 1, 2, 3, 4 }) }), serverConnectionTcp, (handle2, status2, error2, state2) => { writeRequest.Dispose(); serverConnectionTcp.Dispose(); serverConnectionTcpDisposedEvent.Set(); serverConnectionPipe.Dispose(); serverListenPipe.Dispose(); serverListenTcp.Dispose(); }, 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, (a, b) => { }, true); connect.Init(loop2); connect.Connect(clientConnectionPipe, pipeName, (handle, status, error, state) => { connect.Dispose(); var buf = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64); serverConnectionTcpDisposedEvent.WaitOne(); clientConnectionPipe.ReadStart( (handle2, cb, state2) => buf, (handle2, status2, state2) => { if (status2 == Constants.EOF) { clientConnectionPipe.Dispose(); return; } var clientConnectionTcp = new UvTcpHandle(_logger); clientConnectionTcp.Init(loop2, (a, b) => { }); clientConnectionPipe.Accept(clientConnectionTcp); var buf2 = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64); clientConnectionTcp.ReadStart( (handle3, cb, state3) => buf2, (handle3, status3, state3) => { if (status3 == Constants.EOF) { clientConnectionTcp.Dispose(); } }, null); }, null); }, null); loop2.Run(); loop2.Dispose(); }); var worker2 = new Thread(() => { try { serverConnectionPipeAcceptedEvent.WaitOne(); var socket = TestConnection.CreateConnectedLoopbackSocket(port); 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(); }
public async Task SocketCanReadAndWrite() { var loop = new UvLoopHandle(_logger); loop.Init(_uv); var tcp = new UvTcpHandle(_logger); tcp.Init(loop, (a, b) => { }); var address = ServerAddress.FromUrl($"http://127.0.0.1:0/"); tcp.Bind(address); var port = tcp.GetSockIPEndPoint().Port; tcp.Listen(10, (_, status, error, state) => { var tcp2 = new UvTcpHandle(_logger); tcp2.Init(loop, (a, b) => { }); tcp.Accept(tcp2); var data = Marshal.AllocCoTaskMem(500); tcp2.ReadStart( (a, b, c) => tcp2.Libuv.buf_init(data, 500), (__, nread, state2) => { if (nread <= 0) { tcp2.Dispose(); } else { for (var x = 0; x < 2; x++) { var req = new UvWriteReq(new KestrelTrace(new TestKestrelTrace())); req.Init(loop); var pool = new MemoryPool(); var block = pool.Lease(); block.GetIterator().CopyFrom(new ArraySegment <byte>(new byte[] { 65, 66, 67, 68, 69 })); var start = new MemoryPoolIterator(block, 0); var end = new MemoryPoolIterator(block, block.Data.Count); req.Write( tcp2, start, end, 1, (_1, _2, _3, _4) => { pool.Return(block); pool.Dispose(); }, null); } } }, null); tcp.Dispose(); }, null); var t = Task.Run(async() => { var socket = TestConnection.CreateConnectedLoopbackSocket(port); #if NET451 await Task.Factory.FromAsync( socket.BeginSend, socket.EndSend, new[] { new ArraySegment <byte>(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None, null, TaskCreationOptions.None); #else await socket.SendAsync(new[] { new ArraySegment <byte>(new byte[] { 1, 2, 3, 4, 5 }) }, SocketFlags.None); #endif socket.Shutdown(SocketShutdown.Send); var buffer = new ArraySegment <byte>(new byte[2048]); while (true) { #if NET451 var count = await Task.Factory.FromAsync( socket.BeginReceive, socket.EndReceive, new[] { buffer }, SocketFlags.None, null, TaskCreationOptions.None); #else var count = await socket.ReceiveAsync(new[] { buffer }, SocketFlags.None); #endif if (count <= 0) { break; } } socket.Dispose(); }); loop.Run(); loop.Dispose(); await t; }