public void Start(IPEndPoint endpoint) { if (listener == null) { thread = new UvThread(); listener = new UvTcpListener(thread, endpoint); listener.OnConnection(OnConnection); listener.Start(); } }
private void StartAcceptingLibuvConnections <TContext>(IHttpApplication <TContext> application, IPAddress ip, int port) { _uvTcpListener = new UvTcpListener(ip, port); _uvTcpListener.OnConnection(async connection => { await ProcessClient(application, connection.Input, connection.Output); }); _uvTcpListener.Start(); }
public void StartLibuv(IPAddress ip, int port) { if (uvListener == null && socketListener == null) { uvThread = new UvThread(); uvListener = new UvTcpListener(uvThread, new IPEndPoint(ip, port)); uvListener.OnConnection(OnConnection); uvListener.Start(); } }
public void Start(IPAddress ip, int port) { if (listener == null) { thread = new UvThread(); listener = new UvTcpListener(thread, new IPEndPoint(ip, port)); listener.OnConnection(OnConnection); listener.Start(); } }
private void StartAcceptingLibuvConnections <TContext>(IHttpApplication <TContext> application, IPAddress ip, int port) { _uvThread = new UvThread(); _uvTcpListener = new UvTcpListener(_uvThread, new IPEndPoint(ip, port)); _uvTcpListener.OnConnection(async connection => { await ProcessClient(application, connection); }); _uvTcpListener.Start(); }
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(); }
//[Fact] public void CanCreateWorkingEchoServer_ChannelLibuvServer_NonChannelClient() { var endpoint = new IPEndPoint(IPAddress.Loopback, 5010); const string MessageToSend = "Hello world!"; string reply = null; using (var thread = new UvThread()) { var server = new UvTcpListener(thread, endpoint); server.OnConnection(Echo); server.Start(); try { reply = SendBasicSocketMessage(endpoint, MessageToSend); } finally { server.Stop(); } } Assert.Equal(MessageToSend, reply); }
//[Fact] public async Task RunStressPingPongTest_Libuv() { var endpoint = new IPEndPoint(IPAddress.Loopback, 5020); using (var thread = new UvThread()) { var server = new UvTcpListener(thread, endpoint); server.OnConnection(PongServer); server.Start(); 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 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(); }