Esempio n. 1
0
        public void StartListeners(string id, params IPEndPoint[] stratumPorts)
        {
            Contract.RequiresNonNull(stratumPorts, nameof(stratumPorts));

            // every port gets serviced by a dedicated loop thread
            foreach (var endpoint in stratumPorts)
            {
                var thread = new Thread(_ =>
                {
                    var loop = new Loop();

                    try
                    {
                        var listener = loop
                                       .CreateTcp()
                                       .NoDelay(true)
                                       .SimultaneousAccepts(false)
                                       .Listen(endpoint, (con, ex) =>
                        {
                            if (ex == null)
                            {
                                OnClientConnected(con, endpoint, loop);
                            }
                            else
                            {
                                logger.Error(() => $"[{LogCat}] Connection error state: {ex.Message}");
                            }
                        });

                        lock (ports)
                        {
                            ports[endpoint.Port] = listener;
                        }
                    }

                    catch (Exception ex)
                    {
                        logger.Error(ex, $"[{LogCat}] {ex}");
                        throw;
                    }

                    logger.Info(() => $"[{LogCat}] Stratum port {endpoint.Address}:{endpoint.Port} online");

                    try
                    {
                        loop.RunDefault();
                    }

                    catch (Exception ex)
                    {
                        logger.Error(ex, $"[{LogCat}] {ex}");
                    }
                })
                {
                    Name = $"UvLoopThread {id}:{endpoint.Port}"
                };

                thread.Start();
            }
        }
Esempio n. 2
0
        public void Start(IPool pool)
        {
            _pool = pool;

            var thread = new Thread(_ => // every port gets serviced by a dedicated loop thread
            {
                var loop = new Loop();   // libuv loop.

                var endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3333);

                var listener = loop
                               .CreateTcp()
                               .NoDelay(true)
                               .SimultaneousAccepts(false)
                               .Listen(endpoint, (con, ex) =>
                {
                    if (ex != null)
                    {
                        _logger.Error($"Connection error: {ex.Message}");
                    }
                    else
                    {
                        OnClientConnected(con, endpoint, loop);
                    }
                });

                _logger.Information($"stratum server [{endpoint.ToString()}] started..");

                // add to ports list.
                lock (_ports)
                {
                    _ports[endpoint.Port] = listener;
                }

                try
                {
                    loop.RunDefault();
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, Thread.CurrentThread.Name);
                }
            })
            {
                Name = $"UVLoopThread"
            };

            thread.Start();
        }
Esempio n. 3
0
        static void StartServer()
        {
            loop = new Loop();
            tcp  = loop
                   .CreateTcp()
                   .SimultaneousAccepts(true)
                   .Bind(EndPoint, OnRead)
                   .Listen(OnConnection);

            Console.WriteLine($"Echo server starting on {EndPoint}.");
            loop.RunDefault();
            Console.WriteLine("Echo server loop completed.");

            connections.ForEach(x => x.Dispose());
            connections.Clear();
        }
Esempio n. 4
0
    public void Run()
    {
        var loop = new Loop();

        this.client = loop
                      .CreateTcp()
                      .NoDelay(true)
                      .ConnectTo(
            new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort),
            new IPEndPoint(IPAddress.Loopback, this.port),
            this.OnConnected);

        log($"client loop starting");
        loop.RunDefault();
        log($"client loop completed");
    }
Esempio n. 5
0
        static void RunLoop()
        {
            loop = new Loop();

            var localEndPoint  = new IPEndPoint(IPAddress.IPv6Any, IPEndPoint.MinPort);
            var remoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, Port);
            Tcp tcp            = loop
                                 .CreateTcp()
                                 .NoDelay(true)
                                 .Bind(localEndPoint, OnRead)
                                 .ConnectTo(remoteEndPoint, SendMessage);

            Console.WriteLine("Echo client loop starting.");
            loop.RunDefault();
            Console.WriteLine("Echo client loop dropped out");

            tcp.Dispose();
        }
Esempio n. 6
0
        public void StartListeners(params IPEndPoint[] stratumPorts)
        {
            Contract.RequiresNonNull(stratumPorts, nameof(stratumPorts));

            // start ports
            foreach (var endpoint in stratumPorts)
            {
                // host it and its message loop in a dedicated background thread
                var task = new Task(() =>
                {
                    var loop = new Loop();

                    var listener = loop
                                   .CreateTcp()
                                   .SimultaneousAccepts(true)
                                   .KeepAlive(true, 1)
                                   .NoDelay(true)
                                   .Listen(endpoint, (con, ex) =>
                    {
                        if (ex == null)
                        {
                            OnClientConnected(con, endpoint, loop);
                        }
                        else
                        {
                            logger.Error(() => $"[{LogCat}] Connection error state: {ex.Message}");
                        }
                    });

                    lock (ports)
                    {
                        ports[endpoint.Port] = listener;
                    }

                    loop.RunDefault();
                }, TaskCreationOptions.LongRunning);

                task.Start();

                logger.Info(() => $"[{LogCat}] Stratum port {endpoint.Address}:{endpoint.Port} online");
            }
        }
Esempio n. 7
0
        static void RunLoop(ServerType serverType)
        {
            loop = new Loop();

            IDisposable handle;
            var         localEndPoint  = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
            var         remoteEndPoint = new IPEndPoint(IPAddress.Loopback, Port);

            switch (serverType)
            {
            case ServerType.Udp:
                Udp udp = loop
                          .CreateUdp()
                          .ReceiveStart(OnReceive);
                WritableBuffer buffer = CreateMessage();
                udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted);
                handle = udp;
                break;

            case ServerType.Pipe:
                string name = GetPipeName();
                handle = loop
                         .CreatePipe()
                         .ConnectTo(name, OnConnected);
                break;

            default:     // Default tcp
                handle = loop
                         .CreateTcp()
                         .NoDelay(true)
                         .ConnectTo(localEndPoint, remoteEndPoint, OnConnected);
                break;
            }

            Console.WriteLine($"{serverType}:Echo client loop starting.");
            loop.RunDefault();
            Console.WriteLine($"{serverType}:Echo client loop dropped out");
            handle?.Dispose();
        }
Esempio n. 8
0
        static void StartServer(ServerType serverType)
        {
            loop = new Loop();

            switch (serverType)
            {
            case ServerType.Udp:
                var endPoint = new IPEndPoint(IPAddress.Any, Port);
                server = loop
                         .CreateUdp()
                         .Bind(endPoint)
                         .MulticastLoopback(true)
                         .ReceiveStart(OnReceive);
                Console.WriteLine($"{serverType}:Echo server receive started.");
                break;

            case ServerType.Pipe:
                string name = GetPipeName();
                server = loop
                         .CreatePipe()
                         .Listen(name, OnConnection);
                Console.WriteLine($"{serverType}:Echo server started on {name}.");
                break;

            default:     // Default to tcp
                server = loop
                         .CreateTcp()
                         .SimultaneousAccepts(true)
                         .Listen(EndPoint, OnConnection);
                Console.WriteLine($"{serverType}:Echo server started on {EndPoint}.");
                break;
            }

            loop.RunDefault();
            Console.WriteLine($"{serverType}Echo server loop completed.");
        }