private static async void ExecuteCallback(UvTcpListener listener, UvTcpConnection connection) { try { await listener._callback?.Invoke(connection); } catch { // Swallow exceptions } finally { await connection.DisposeAsync(); } }
public async Task CanCreateWorkingEchoServer_PipelineLibuvServer_NonPipelineClient() { var endpoint = new IPEndPoint(IPAddress.Loopback, 5010); const string MessageToSend = "Hello world!"; string reply = null; using (var thread = new UvThread()) using (var server = new UvTcpListener(thread, endpoint)) { server.OnConnection(Echo); await server.StartAsync(); reply = SendBasicSocketMessage(endpoint, MessageToSend); } Assert.Equal(MessageToSend, reply); }
private static async void ExecuteCallback(UvTcpListener listener, UvTcpConnection connection) { try { await listener._callback?.Invoke(connection); // Make sure we dispose off the libuv thread await Task.Yield(); } catch { // Swallow exceptions } finally { // Dispose the connection on task completion connection.Dispose(); } }
private static async void ExecuteCallback(UvTcpListener listener, UvTcpConnection connection) { try { await listener._callback?.Invoke(connection); } catch { // Swallow exceptions } finally { // Dispose the connection on task completion connection.Dispose(); } }
public async Task RunStressPingPongTest_Libuv() { var endpoint = new IPEndPoint(IPAddress.Loopback, 5020); using (var thread = new UvThread()) using (var server = new UvTcpListener(thread, endpoint)) { server.OnConnection(PongServer); await server.StartAsync(); const int SendCount = 500, ClientCount = 5; for (int loop = 0; loop < ClientCount; loop++) { using (var client = await new UvTcpClient(thread, endpoint).ConnectAsync()) { var tuple = await PingClient(client, SendCount); Assert.Equal(SendCount, tuple.Item1); Assert.Equal(SendCount, tuple.Item2); Console.WriteLine($"Ping: {tuple.Item1}; Pong: {tuple.Item2}; Time: {tuple.Item3}ms"); } } } }
public static void Run() { var ip = IPAddress.Any; int port = 5000; var thread = new UvThread(); var listener = new UvTcpListener(thread, new IPEndPoint(ip, port)); listener.OnConnection(async connection => { var httpParser = new HttpRequestParser(); while (true) { // Wait for data var result = await connection.Input.ReadAsync(); var input = result.Buffer; try { if (input.IsEmpty && result.IsCompleted) { // No more data break; } // Parse the input http request var parseResult = httpParser.ParseRequest(ref input); switch (parseResult) { case HttpRequestParser.ParseResult.Incomplete: if (result.IsCompleted) { // Didn't get the whole request and the connection ended throw new EndOfStreamException(); } // Need more data continue; case HttpRequestParser.ParseResult.Complete: break; case HttpRequestParser.ParseResult.BadRequest: throw new Exception(); default: break; } // Writing directly to pooled buffers var output = connection.Output.Alloc(); var formatter = new OutputFormatter<WritableBuffer>(output, EncodingData.InvariantUtf8); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\nContent-Length: 13"); formatter.Append("\r\nContent-Type: text/plain"); formatter.Append("\r\n\r\n"); formatter.Append("Hello, World!"); await output.FlushAsync(); httpParser.Reset(); } finally { // Consume the input connection.Input.Advance(input.Start, input.End); } } }); listener.StartAsync().GetAwaiter().GetResult(); Console.WriteLine($"Listening on {ip} on port {port}"); var wh = new ManualResetEventSlim(); Console.CancelKeyPress += (sender, e) => { wh.Set(); }; wh.Wait(); listener.Dispose(); thread.Dispose(); }