public void Stop() { listener?.Stop(); thread?.Dispose(); listener = null; thread = null; }
public void Dispose() { _rioTcpServer?.Stop(); _listenSocket?.Dispose(); _uvTcpListener?.Stop(); _uvThread?.Dispose(); }
public override void Dispose() { _uvTcpListener?.Dispose(); _uvThread?.Dispose(); _uvTcpListener = null; _uvThread = null; }
public void Stop() { CloseAllConnections(); listener?.Stop(); thread?.Dispose(); listener = null; thread = null; }
public void Stop() { uvThread?.Dispose(); uvListener = null; uvThread = null; socketListener?.Stop(); socketListener?.Dispose(); socketListener = null; }
public void Close() { if (connection != null) { Close(connection); } connection = null; // client.Dispose(); // thread?.Dispose(); thread = null; }
private static void RunRawLibuvHttpServer() { // This sample makes some assumptions var ip = IPAddress.Any; int port = 5000; var thread = new UvThread(); var listener = new UvTcpListener(thread, new IPEndPoint(ip, port)); listener.OnConnection(async connection => { // Wait for data var input = await connection.Input; if (input.IsEmpty && connection.Input.Completion.IsCompleted) { // No more data return; } // Dump the request Console.WriteLine(input.GetAsciiString()); var formatter = connection.Output.GetFormatter(FormattingData.InvariantUtf8); unsafe { formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\nConnection: close"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); } await formatter.Buffer.FlushAsync(); // Consume the input input.Consumed(); // Close the input channel, which will tell the producer to stop producing connection.Input.CompleteReading(); // Close the output channel, which will close the connection connection.Output.CompleteWriting(); }); listener.Start(); Console.WriteLine($"Listening on {ip} on port {port}"); Console.ReadKey(); listener.Stop(); thread.Dispose(); }
private static void RunServerForNode() { UvThread uvThread = new UvThread(); var ip = IPAddress.Any; UvTcpListener listener = new UvTcpListener(uvThread, new IPEndPoint(ip, port)); listener.OnConnection(async connection => { Interlocked.Increment(ref connectionCounter); var input = connection.Input; var output = connection.Output; var flag = false; //Used for stop sending info to connected client. await Task.Factory.StartNew(async() => { //Wait for client disconnection. var result = await input.ReadAsync(); flag = true; }); while (!flag) { try { WritableBuffer oBuffer = output.Alloc(); oBuffer.WriteUtf8String(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:ms")); await oBuffer.FlushAsync(); Interlocked.Increment(ref sendCounter); await Task.Delay(r.Next(0, 500)); } catch (Exception e) { break; } } }); listener.Start(); var pid = System.Diagnostics.Process.GetCurrentProcess().Id; Console.WriteLine($"Listening on {ip} on port {port} / PID {pid}"); Console.ReadKey(); listener.Stop(); uvThread.Dispose(); }
private static void TestOpenAndCloseListener() { var thread = new UvThread(); var ep = new IPEndPoint(IPAddress.Loopback, 5003); var listener = new UvTcpListener(thread, ep); listener.OnConnection(_ => Console.WriteLine("Hi and bye")); listener.Start(); Console.WriteLine("Listening..."); Thread.Sleep(1000); Console.WriteLine("Stopping listener..."); listener.Stop(); Thread.Sleep(1000); Console.WriteLine("Disposing thread..."); thread.Dispose(); }
public Task 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 pipelineConnection = MakePipeline(connection); var decoder = new LineDecoder(); var handler = new LineHandler(); // Initialize the handler with the connection handler.Initialize(pipelineConnection); try { while (true) { // Wait for data var result = await pipelineConnection.Input.ReadAsync(); var input = result.Buffer; try { if (input.IsEmpty && result.IsCompleted) { // No more data break; } while (decoder.TryDecode(ref input, out Line line)) { await handler.HandleAsync(line); } if (!input.IsEmpty && result.IsCompleted) { // Didn't get the whole frame and the connection ended throw new EndOfStreamException(); } } finally { // Consume the input pipelineConnection.Input.Advance(input.Start, input.End); } } } finally { // Close the input, which will tell the producer to stop producing pipelineConnection.Input.Complete(); // Close the output, which will close the connection pipelineConnection.Output.Complete(); } }); listener.StartAsync().GetAwaiter().GetResult(); Console.WriteLine($"Listening on {ip} on port {port}"); Console.ReadKey(); listener.Dispose(); thread.Dispose(); return(Task.CompletedTask); }
protected override Task Stop() { listener.Dispose(); thread.Dispose(); return(Task.CompletedTask); }
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; var consumed = input.Start; var examined = input.Start; try { if (input.IsEmpty && result.IsCompleted) { // No more data break; } // Parse the input http request var parseResult = httpParser.ParseRequest(input, out consumed, out examined); 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, TextEncoder.Utf8); 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(consumed, examined); } } }); 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(); }
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(); var formatter = connection.Output.GetFormatter(EncodingData.InvariantUtf8); try { while (true) { httpParser.Reset(); // Wait for data var input = await connection.Input.ReadAsync(); try { if (input.IsEmpty && connection.Input.Reading.IsCompleted) { // No more data break; } // Parse the input http request var result = httpParser.ParseRequest(ref input); switch (result) { case HttpRequestParser.ParseResult.Incomplete: if (connection.Input.Reading.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; } unsafe { 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 formatter.FlushAsync(); } finally { // Consume the input connection.Input.Advance(input.Start, input.End); } } } finally { // Close the input channel, which will tell the producer to stop producing connection.Input.Complete(); // Close the output channel, which will close the connection connection.Output.Complete(); } }); listener.Start(); Console.WriteLine($"Listening on {ip} on port {port}"); var wh = new ManualResetEventSlim(); Console.CancelKeyPress += (sender, e) => { wh.Set(); }; wh.Wait(); listener.Stop(); thread.Dispose(); }
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 channel = MakePipeline(connection); var decoder = new LineDecoder(); var handler = new LineHandler(); // Initialize the handler with the channel handler.Initialize(channel); try { while (true) { // Wait for data var input = await channel.Input.ReadAsync(); try { if (input.IsEmpty && channel.Input.Reading.IsCompleted) { // No more data break; } Line line; if (!decoder.TryDecode(ref input, out line)) { if (channel.Input.Reading.IsCompleted) { // Didn't get the whole frame and the connection ended throw new EndOfStreamException(); } // Need more data continue; } await handler.HandleAsync(line); } finally { // Consume the input channel.Input.Advance(input.Start, input.End); } } } finally { // Close the input channel, which will tell the producer to stop producing channel.Input.Complete(); // Close the output channel, which will close the connection channel.Output.Complete(); } }); listener.Start(); Console.WriteLine($"Listening on {ip} on port {port}"); Console.ReadKey(); listener.Stop(); thread.Dispose(); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); _thread.Dispose(); }