コード例 #1
0
        public void Monitoring()
        {
            ManualResetEvent listeningEvent = new ManualResetEvent(false);
            ManualResetEvent acceptedEvent  = new ManualResetEvent(false);
            ManualResetEvent connectedEvent = new ManualResetEvent(false);


            using (NetMQContext contex = NetMQContext.Create())
            {
                Poller poller = new Poller();

                using (var rep = contex.CreateResponseSocket())
                {
                    using (NetMQMonitor monitor = new NetMQMonitor(contex, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening))
                    {
                        monitor.Accepted  += (s, a) => acceptedEvent.Set();
                        monitor.Listening += (s, a) => listeningEvent.Set();

                        monitor.AttachToPoller(poller);

                        rep.Bind("tcp://127.0.0.1:5002");

                        using (var req = contex.CreateRequestSocket())
                        {
                            using (NetMQMonitor reqMonitor = new NetMQMonitor(contex, req, "inproc://req.inproc", SocketEvent.Connected))
                            {
                                reqMonitor.Connected += (s, a) => connectedEvent.Set();

                                reqMonitor.AttachToPoller(poller);
                                try
                                {
                                    var pollerTask = Task.Factory.StartNew(poller.Start);

                                    req.Connect("tcp://127.0.0.1:5002");
                                    req.Send("a");

                                    bool more;

                                    string m = rep.ReceiveString(out more);

                                    rep.Send("b");

                                    string m2 = req.ReceiveString(out more);

                                    Assert.IsTrue(listeningEvent.WaitOne(300));
                                    Assert.IsTrue(connectedEvent.WaitOne(300));
                                    Assert.IsTrue(acceptedEvent.WaitOne(300));
                                }
                                finally
                                {
                                    poller.Stop();
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        private NetMQMonitor CreateMonitor(NetMQContext context, NetMQSocket socket, Poller poller)
        {
            var monitor = new NetMQMonitor(context, socket, $"inproc://{Guid.NewGuid()}.inproc",
                SocketEvents.Connected | SocketEvents.Disconnected | SocketEvents.ConnectRetried);

            monitor.Connected += Monitor_Connected;
            monitor.Disconnected += Monitor_Disconnected;
            monitor.ConnectRetried += Monitor_ConnectRetried;

            monitor.AttachToPoller(poller);

            return monitor;
        }
コード例 #3
0
        private NetMQMonitor CreateMonitor(NetMQSocket socket, NetMQPoller poller)
        {
            var monitor = new NetMQMonitor(socket, $"inproc://{Guid.NewGuid()}.inproc",
                                           SocketEvents.Connected | SocketEvents.Disconnected | SocketEvents.ConnectRetried);

            monitor.Connected      += Monitor_Connected;
            monitor.Disconnected   += Monitor_Disconnected;
            monitor.ConnectRetried += Monitor_ConnectRetried;

            monitor.AttachToPoller(poller);

            return(monitor);
        }
コード例 #4
0
        public void Monitoring()
        {
            var listeningEvent = new ManualResetEvent(false);
            var acceptedEvent  = new ManualResetEvent(false);
            var connectedEvent = new ManualResetEvent(false);

            using (var context = NetMQContext.Create())
                using (var rep = context.CreateResponseSocket())
                    using (var req = context.CreateRequestSocket())
                        using (var poller = new Poller {
                            PollTimeout = TestPollTimeoutMillis
                        })
                            using (var repMonitor = new NetMQMonitor(context, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening))
                                using (var reqMonitor = new NetMQMonitor(context, req, "inproc://req.inproc", SocketEvent.Connected))
                                {
                                    repMonitor.Accepted  += (s, e) => acceptedEvent.Set();
                                    repMonitor.Listening += (s, e) => listeningEvent.Set();

                                    repMonitor.AttachToPoller(poller);

                                    int port = rep.BindRandomPort("tcp://127.0.0.1");

                                    reqMonitor.Connected += (s, e) => connectedEvent.Set();

                                    reqMonitor.AttachToPoller(poller);

                                    poller.PollTillCancelledNonBlocking();

                                    req.Connect("tcp://127.0.0.1:" + port);
                                    req.Send("a");

                                    rep.SkipFrame();

                                    rep.Send("b");

                                    req.SkipFrame();

                                    Assert.IsTrue(listeningEvent.WaitOne(300));
                                    Assert.IsTrue(connectedEvent.WaitOne(300));
                                    Assert.IsTrue(acceptedEvent.WaitOne(300));

                                    poller.CancelAndJoin();
                                }
        }
コード例 #5
0
        public bool TryConnectAndMonitorSocket(NetMQSocket socket, string address, NetMQPoller poller = null, Action <IConnectionMonitor, bool> onConnectionStateChanged = null)
        {
            _onConnectionStateChanged = onConnectionStateChanged;

            _monitor               = new NetMQMonitor(socket, $"inproc://monitor.socket/{Guid.NewGuid()}", SocketEvents.Connected | SocketEvents.Disconnected);
            _monitor.Connected    += MonitorConnected;
            _monitor.Disconnected += MonitorDisconnected;

            if (poller == null)
            {
                _monitorTask = _monitor.StartAsync();
            }
            else
            {
                _monitor.AttachToPoller(poller);
            }

            socket.Connect(address);
            return(WaitForConnection());
        }
コード例 #6
0
ファイル: NetMQPollerTest.cs プロジェクト: tlotter/netmq
        public void Monitoring()
        {
            var listeningEvent = new ManualResetEvent(false);
            var acceptedEvent  = new ManualResetEvent(false);
            var connectedEvent = new ManualResetEvent(false);

            using (var rep = new ResponseSocket())
                using (var req = new RequestSocket())
                    using (var poller = new NetMQPoller())
                        using (var repMonitor = new NetMQMonitor(rep, "inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
                            using (var reqMonitor = new NetMQMonitor(req, "inproc://req.inproc", SocketEvents.Connected))
                            {
                                repMonitor.Accepted  += (s, e) => acceptedEvent.Set();
                                repMonitor.Listening += (s, e) => listeningEvent.Set();

                                repMonitor.AttachToPoller(poller);

                                int port = rep.BindRandomPort("tcp://127.0.0.1");

                                reqMonitor.Connected += (s, e) => connectedEvent.Set();

                                reqMonitor.AttachToPoller(poller);

                                poller.RunAsync();

                                req.Connect("tcp://127.0.0.1:" + port);
                                req.SendFrame("a");

                                rep.SkipFrame();

                                rep.SendFrame("b");

                                req.SkipFrame();

                                Assert.IsTrue(listeningEvent.WaitOne(300));
                                Assert.IsTrue(connectedEvent.WaitOne(300));
                                Assert.IsTrue(acceptedEvent.WaitOne(300));

                                poller.Stop();
                            }
        }
コード例 #7
0
ファイル: NetMQPollerTest.cs プロジェクト: cjkao/netmq
        public void Monitoring()
        {
            var listeningEvent = new ManualResetEvent(false);
            var acceptedEvent = new ManualResetEvent(false);
            var connectedEvent = new ManualResetEvent(false);

            using (var rep = new ResponseSocket())
            using (var req = new RequestSocket())
            using (var poller = new NetMQPoller())
            using (var repMonitor = new NetMQMonitor(rep, "inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
            using (var reqMonitor = new NetMQMonitor(req, "inproc://req.inproc", SocketEvents.Connected))
            {
                repMonitor.Accepted += (s, e) => acceptedEvent.Set();
                repMonitor.Listening += (s, e) => listeningEvent.Set();

                repMonitor.AttachToPoller(poller);

                int port = rep.BindRandomPort("tcp://127.0.0.1");

                reqMonitor.Connected += (s, e) => connectedEvent.Set();

                reqMonitor.AttachToPoller(poller);

                poller.RunAsync();

                req.Connect("tcp://127.0.0.1:" + port);
                req.SendFrame("a");

                rep.SkipFrame();

                rep.SendFrame("b");

                req.SkipFrame();

                Assert.IsTrue(listeningEvent.WaitOne(300));
                Assert.IsTrue(connectedEvent.WaitOne(300));
                Assert.IsTrue(acceptedEvent.WaitOne(300));

                poller.Stop();
            }
        }
コード例 #8
0
ファイル: PollerTests.cs プロジェクト: hecwow/netmq
        public void Monitoring()
        {
            ManualResetEvent listeningEvent = new ManualResetEvent(false);
            ManualResetEvent acceptedEvent = new ManualResetEvent(false);
            ManualResetEvent connectedEvent = new ManualResetEvent(false);

            using (NetMQContext contex = NetMQContext.Create())
            using (Poller poller = new Poller())
            {
                using (var rep = contex.CreateResponseSocket())
                {
                    using (NetMQMonitor monitor = new NetMQMonitor(contex, rep, "inproc://rep.inproc", SocketEvent.Accepted | SocketEvent.Listening))
                    {
                        monitor.Accepted += (s, a) => acceptedEvent.Set();
                        monitor.Listening += (s, a) => listeningEvent.Set();

                        monitor.AttachToPoller(poller);

                        rep.Bind("tcp://127.0.0.1:5002");

                        using (var req = contex.CreateRequestSocket())
                        {
                            using (NetMQMonitor reqMonitor = new NetMQMonitor(contex, req, "inproc://req.inproc", SocketEvent.Connected))
                            {
                                reqMonitor.Connected += (s, a) => connectedEvent.Set();

                                reqMonitor.AttachToPoller(poller);
                                try
                                {
                                    var pollerTask = Task.Factory.StartNew(poller.Start);

                                    req.Connect("tcp://127.0.0.1:5002");
                                    req.Send("a");

                                    bool more;

                                    string m = rep.ReceiveString(out more);

                                    rep.Send("b");

                                    string m2 = req.ReceiveString(out more);

                                    Assert.IsTrue(listeningEvent.WaitOne(300));
                                    Assert.IsTrue(connectedEvent.WaitOne(300));
                                    Assert.IsTrue(acceptedEvent.WaitOne(300));
                                }
                                finally
                                {
                                    poller.Stop();
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #9
0
ファイル: PollerTests.cs プロジェクト: romanros/netmq
        public void Monitoring()
        {
            var listeningEvent = new ManualResetEvent(false);
            var acceptedEvent = new ManualResetEvent(false);
            var connectedEvent = new ManualResetEvent(false);

            using (var context = NetMQContext.Create())
            using (var rep = context.CreateResponseSocket())
            using (var req = context.CreateRequestSocket())
            using (var poller = new Poller { PollTimeout = TestPollTimeoutMillis })
            using (var repMonitor = new NetMQMonitor(context, rep, "inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
            using (var reqMonitor = new NetMQMonitor(context, req, "inproc://req.inproc", SocketEvents.Connected))
            {
                repMonitor.Accepted += (s, e) => acceptedEvent.Set();
                repMonitor.Listening += (s, e) => listeningEvent.Set();

                repMonitor.AttachToPoller(poller);

                int port = rep.BindRandomPort("tcp://127.0.0.1");

                reqMonitor.Connected += (s, e) => connectedEvent.Set();

                reqMonitor.AttachToPoller(poller);

                poller.PollTillCancelledNonBlocking();

                req.Connect("tcp://127.0.0.1:" + port);
                req.SendFrame("a");

                rep.SkipFrame();

                rep.SendFrame("b");

                req.SkipFrame();

                Assert.IsTrue(listeningEvent.WaitOne(300));
                Assert.IsTrue(connectedEvent.WaitOne(300));
                Assert.IsTrue(acceptedEvent.WaitOne(300));

                poller.CancelAndJoin();
            }
        }
コード例 #10
0
        void IShimHandler.Run(PairSocket shim)
        {
            _poller = new NetMQPoller();

            // Listen to actor dispose
            _ = Observable.FromEventPattern <NetMQSocketEventArgs>(e => shim.ReceiveReady += e, e => shim.ReceiveReady -= e)
                .Select(e => e.EventArgs)
                .ObserveOn(Scheduler.CurrentThread)
                .Subscribe(OnShimReady);

            _poller.Add(shim);

            _subscriberSocket = new SubscriberSocket();
            _subscriberSocket.Options.ReceiveHighWatermark = 1000;
            _subscriberSocket.Options.Linger               = TimeSpan.FromSeconds(2);
            _subscriberSocket.Options.TcpKeepalive         = true;
            _subscriberSocket.Options.TcpKeepaliveIdle     = TimeSpan.FromSeconds(5);
            _subscriberSocket.Options.TcpKeepaliveInterval = TimeSpan.FromSeconds(1);

#if MONITOR
            var monitor = new NetMQMonitor(_subscriberSocket, "inproc://#monitor", SocketEvents.All);
            _ =
                Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor, "Connected")
                .Select(e => new { Event = "Connected", e })
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor, nameof(monitor.Listening)).Select(e => new { Event = "Listening", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor, nameof(monitor.Accepted)).Select(e => new { Event = "Accepted", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor, nameof(monitor.Closed)).Select(e => new { Event = "Closed", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor, nameof(monitor.Disconnected)).Select(e => new { Event = "Disconnected", e }))
                .Do(e => _logger.Info("Monitor socket: {0}, {1}", e.Event, e.e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor, nameof(monitor.AcceptFailed))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor, nameof(monitor.ConnectDelayed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor, nameof(monitor.CloseFailed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor, nameof(monitor.BindFailed)))
                .Do(e => _logger.Error("Monitor error socket: {0}, {1}", e.EventArgs.ErrorCode, e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorIntervalEventArgs>(monitor, nameof(monitor.ConnectRetried))
                .Do(e => _logger.Info("Monitor retry socket: {0}, {1}", e.EventArgs.Interval, e.EventArgs.Address))
                .Subscribe();
            monitor.AttachToPoller(_poller);
#endif

            _ = Observable.FromEventPattern <NetMQSocketEventArgs>(
                e => _subscriberSocket.ReceiveReady += e, e => _subscriberSocket.ReceiveReady -= e)
                .Select(e => e.EventArgs)
                .ObserveOn(Scheduler.CurrentThread)
                .Subscribe(ReceivedMessage);

            _poller.Add(_subscriberSocket);

            _subscriberSocket.Connect($"tcp://{_address}:{_port + 1}");

            shim.SignalOK();

            try
            {
                _poller.Run();
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, "Fatal error on NetMQ poller.");
            }

            // Cleanup stuff after stopping
            _poller.Remove(_subscriberSocket);
            _poller.Remove(shim);
#if MONITOR
            if (monitor != null)
            {
                monitor.DetachFromPoller();
                monitor.Dispose();
                monitor = null;
            }
#endif
            _poller.Dispose();
        }
コード例 #11
0
ファイル: HubShimHandler.cs プロジェクト: ycdivfx/nyx
        void IShimHandler.Run(PairSocket shim)
        {
            _poller = new NetMQPoller();

            // Listen to actor dispose
            _ = Observable.FromEventPattern <NetMQSocketEventArgs>(e => shim.ReceiveReady += e, e => shim.ReceiveReady -= e)
                .Select(e => e.EventArgs)
                .ObserveOn(Scheduler.CurrentThread)
                .Subscribe(OnShimReady);

            _poller.Add(shim);

            _publisherSocket = new PublisherSocket();
            _responseSocket  = new RouterSocket();
#if MONITOR
            var monitor1 = new NetMQMonitor(_publisherSocket, "inproc://#monitor1", SocketEvents.All);
            _ = Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor1, "Connected")
                .Select(e => new { Event = "Connected", e })
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor1, nameof(monitor1.Listening)).Select(e => new { Event = "Listening", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor1, nameof(monitor1.Accepted)).Select(e => new { Event = "Accepted", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor1, nameof(monitor1.Closed)).Select(e => new { Event = "Closed", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor1, nameof(monitor1.Disconnected)).Select(e => new { Event = "Disconnected", e }))
                .Do(e => _logger.Info("Monitor socket: {0}, {1}", e.Event, e.e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor1, nameof(monitor1.AcceptFailed))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor1, nameof(monitor1.ConnectDelayed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor1, nameof(monitor1.CloseFailed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor1, nameof(monitor1.BindFailed)))
                .Do(e => _logger.Error("Monitor error socket: {0}, {1}", e.EventArgs.ErrorCode, e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorIntervalEventArgs>(monitor1, nameof(monitor1.ConnectRetried))
                .Do(e => _logger.Info("Monitor retry socket: {0}, {1}", e.EventArgs.Interval, e.EventArgs.Address))
                .Subscribe();
            monitor1.AttachToPoller(_poller);

            var monitor2 = new NetMQMonitor(_responseSocket, "inproc://#monitor2", SocketEvents.All);
            _ = Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor2, "Connected")
                .Select(e => new { Event = "Connected", e })
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor2, nameof(monitor2.Listening)).Select(e => new { Event = "Listening", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor2, nameof(monitor2.Accepted)).Select(e => new { Event = "Accepted", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor2, nameof(monitor2.Closed)).Select(e => new { Event = "Closed", e }))
                .Merge(Observable.FromEventPattern <NetMQMonitorSocketEventArgs>(monitor2, nameof(monitor2.Disconnected)).Select(e => new { Event = "Disconnected", e }))
                .Do(e => _logger.Info("Monitor socket: {0}, {1}", e.Event, e.e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor2, nameof(monitor2.AcceptFailed))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor2, nameof(monitor2.ConnectDelayed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor2, nameof(monitor2.CloseFailed)))
                .Merge(Observable.FromEventPattern <NetMQMonitorErrorEventArgs>(monitor2, nameof(monitor2.BindFailed)))
                .Do(e => _logger.Error("Monitor error socket: {0}, {1}", e.EventArgs.ErrorCode, e.EventArgs.Address))
                .Subscribe();
            _ = Observable.FromEventPattern <NetMQMonitorIntervalEventArgs>(monitor2, nameof(monitor2.ConnectRetried))
                .Do(e => _logger.Info("Monitor retry socket: {0}, {1}", e.EventArgs.Interval, e.EventArgs.Address))
                .Subscribe();
            monitor2.AttachToPoller(_poller);
#endif

            // To avoid crashes if the queue builds up too fast.
            _publisherSocket.Options.SendHighWatermark    = 1000;
            _publisherSocket.Options.TcpKeepalive         = true;
            _publisherSocket.Options.TcpKeepaliveIdle     = TimeSpan.FromSeconds(5);
            _publisherSocket.Options.TcpKeepaliveInterval = TimeSpan.FromSeconds(1);
            // Number of network hops to jump, default is 1, which means local network.
            //_publisherSocket.Options.MulticastHops = 100;
            _publisherSocket.Bind($"tcp://*:{_port + 1}");
            _poller.Add(_publisherSocket);

            _responseSocket.Bind($"tcp://*:{_port}");
            _ = Observable.FromEventPattern <NetMQSocketEventArgs>(
                e => _responseSocket.ReceiveReady += e, e => _responseSocket.ReceiveReady -= e)
                .Select(e => e.EventArgs)
                .ObserveOn(Scheduler.CurrentThread)
                .Subscribe(e =>
            {
                try
                {
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // Ignore messages that have less than 3 frames or no empty second frame.
                    var address = msg.First();
                    var data    = msg.Last().ConvertToString();
                    var nyxMsg  = new NyxMessage().FromDefault(data);
                    _messageSubject.OnNext(new Tuple <byte[], INyxMessage>(address.ToByteArray(), nyxMsg));
                }
                catch (Exception ex)
                {
                    _messageSubject.OnError(ex);
                }
            });

            _poller.Add(_responseSocket);

            var heartbeatTimer = new NetMQTimer(TimeSpan.FromSeconds(2));
            _ = Observable.FromEventPattern <NetMQTimerEventArgs>(e => heartbeatTimer.Elapsed += e, e => heartbeatTimer.Elapsed -= e)
                .Select(e => e.EventArgs)
                .Subscribe(OnTimeoutElapsed);
            _poller.Add(heartbeatTimer);

            heartbeatTimer.Reset();
            shim.SignalOK();

            _poller.Run();

#if MONITOR
            if (monitor1 != null)
            {
                monitor1.DetachFromPoller();
                monitor2.DetachFromPoller();
            }
#endif
            _poller.Remove(_responseSocket);
            _poller.Remove(_publisherSocket);
            _poller.Remove(heartbeatTimer);
            _poller.Remove(shim);
            _poller.Dispose();
        }