示例#1
0
 public void Dispose()
 {
     _rioTcpServer?.Stop();
     _listenSocket?.Dispose();
     _uvTcpListener?.Stop();
     _uvThread?.Dispose();
 }
示例#2
0
 public void Stop()
 {
     listener?.Stop();
     thread?.Dispose();
     listener = null;
     thread   = null;
 }
示例#3
0
 public void Stop()
 {
     CloseAllConnections();
     listener?.Stop();
     thread?.Dispose();
     listener = null;
     thread   = null;
 }
        public void Stop()
        {
            uvListener?.Stop();
            uvThread?.Dispose();
            uvListener = null;
            uvThread   = null;

            socketListener?.Stop();
            socketListener?.Dispose();
            socketListener = null;
        }
示例#5
0
        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();
        }
示例#6
0
        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();
        }
示例#7
0
        //[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);
        }
示例#8
0
        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();
        }