Ejemplo n.º 1
0
        private void ObserveHealth()
        {
            // For proper cleanup
            Task.Factory.StartNew(() =>
            {
                var pooling = new Polling(PollingEvents.SendReady, this._socket);

                try
                {
                    while (this._started)
                    {
                        // since under normal circunstance a SendReady
                        // is always ready, we force a wait
                        Thread.Sleep(1000);

                        // this should b
                        pooling.PollForever();
                    }
                }
                catch (Exception e)
                {
                    // the expected error (eterm)
                    // will cause this socket to be unusable.
                    // thus we force the disposal
                    this.Dispose();
                }
            });
        }
Ejemplo n.º 2
0
		private void WorkerProc(object state)
		{
			try
			{
				using (var socket = this._ctx.CreateSocket(SocketType.Rep))
				{
					socket.Connect(this._backendEndpoint);

					var polling = new Polling(PollingEvents.RecvReady, socket);

					// once data is ready, we pass it along to the actual worker
					polling.RecvReady += _ =>
					{
						var msg = socket.Recv();
						this._proc(msg, socket);
					};

					while (_running)
					{
						polling.PollForever();
					}
				}
			}
			catch (ZmqException e)
			{
				if (LogAdapter.LogEnabled)
				{
					LogAdapter.LogError(this.GetType().FullName, e.ToString());
				}

				if (e.ZmqErrorCode != ZmqErrorCode.ETERM) throw;
			}
		}
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var isMono = (Type.GetType("Mono.Runtime", false) != null);

            Console.WriteLine("Is Mono? " + isMono);


            using (var ctx = new Context())
            {
                // Create req/rep vanilla in tcp
                var req   = ctx.Req();
                var reply = ctx.Rep();
                req.SetOption(SocketOpt.RCVTIMEO, 3000);

                reply.Bind("tcp://0.0.0.0:9000");
                req.Connect("tcp://127.0.0.1:9000");
                req.Send("A cup of coffee please");

                var reqStr = reply.RecvString();
                Console.WriteLine(reqStr);
                reply.Send("Here's your coffee sir");
                var replyStr = req.RecvString();
                Console.WriteLine(replyStr);
                req.Dispose();
                reply.Dispose();


                // Polling

                reply = ctx.Rep();
                reply.Bind("tcp://0.0.0.0:9001");
                var polling = new Polling(PollingEvents.RecvReady, reply);
                polling.RecvReady += socket =>
                {
                    reqStr = reply.RecvString();
                    reply.Send("Here's another cup of coffee sir");
                };

                req = ctx.Req();
                req.Connect("tcp://127.0.0.1:9001");
                req.Send("Another cup of coffee please");

                polling.PollForever();

                replyStr = req.RecvString();
                Console.WriteLine(replyStr);
                req.Dispose();
                reply.Dispose();


                // Pub sub


                // Devices
            }
        }
Ejemplo n.º 4
0
 private static void ContinuePoll(Polling polling)
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             polling.PollForever();
         }
         catch
         { }
     });
 }
Ejemplo n.º 5
0
		public void poll_in_for_req_rep()
		{
			using (var repSocket = base.Context.CreateSocket(SocketType.Rep))
			using (var reqSocket = base.Context.CreateSocket(SocketType.Req))
			{
				repSocket.Bind("tcp://0.0.0.0:90001");

				var polling = new Polling(PollingEvents.RecvReady, repSocket, reqSocket);

				var rcEvCalled = false;

				polling.RecvReady += (socket) =>
				{
					rcEvCalled = true;
				};
				
				reqSocket.Connect("tcp://127.0.0.1:90001");
				reqSocket.Send("Hello");
				
				polling.PollForever();

				Assert.IsTrue(rcEvCalled);
			}
		}
Ejemplo n.º 6
0
        public void poll_in_for_req_rep()
        {
            using (var repSocket = base.Context.CreateSocket(SocketType.Rep))
                using (var reqSocket = base.Context.CreateSocket(SocketType.Req))
                {
                    repSocket.Bind("tcp://0.0.0.0:90001");

                    var polling = new Polling(PollingEvents.RecvReady, repSocket, reqSocket);

                    var rcEvCalled = false;

                    polling.RecvReady += (socket) =>
                    {
                        rcEvCalled = true;
                    };

                    reqSocket.Connect("tcp://127.0.0.1:90001");
                    reqSocket.Send("Hello");

                    polling.PollForever();

                    Assert.IsTrue(rcEvCalled);
                }
        }
Ejemplo n.º 7
0
        private void WorkerProc(object state)
        {
            try
            {
                using (var socket = this._ctx.CreateSocket(SocketType.Rep))
                {
                    socket.Connect(this._backendEndpoint);

                    var polling = new Polling(PollingEvents.RecvReady, socket);

                    // once data is ready, we pass it along to the actual worker
                    polling.RecvReady += _ =>
                    {
                        var msg = socket.Recv();
                        this._proc(msg, socket);
                    };

                    while (_running)
                    {
                        polling.PollForever();
                    }
                }
            }
            catch (ZmqException e)
            {
                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError(this.GetType().FullName, e.ToString());
                }

                if (e.ZmqErrorCode != ZmqErrorCode.ETERM)
                {
                    throw;
                }
            }
        }
Ejemplo n.º 8
0
        private static void PubSubWithPolling(Context ctx)
        {
            using (var pubSocket = ctx.CreateSocket(SocketType.Pub))
                using (var subSocket1 = ctx.CreateSocket(SocketType.Sub))
                    using (var subSocket2 = ctx.CreateSocket(SocketType.Sub))
                        using (var subSocket3 = ctx.CreateSocket(SocketType.Sub))
                        {
                            pubSocket.Bind("tcp://0.0.0.0:8809");

                            subSocket1.Connect("tcp://127.0.0.1:8809");
                            subSocket2.Connect("tcp://127.0.0.1:8809");
                            subSocket3.Connect("tcp://127.0.0.1:8809");

                            subSocket1.Subscribe("t1");
                            subSocket2.Subscribe("t2");
                            subSocket3.Subscribe("t3");

                            Task.Factory.StartNew(() =>
                            {
                                while (true)
                                {
                                    pubSocket.Send("t1", hasMoreToSend: true);
                                    pubSocket.Send("Upon successful completion, the zmq_poll() function shall return the number of zmq_pollitem_t structures with events signaled in revents or 0 if no events have been signaled. Upon failure, zmq_poll() shall return -1 and set errno to one of the values defined below.");

                                    pubSocket.Send("t2", hasMoreToSend: true);
                                    pubSocket.Send("The zmq_poll() function may be implemented or emulated using operating system interfaces other than poll(), and as such may be subject to the limits of those interfaces in ways not defined in this documentation.");

                                    pubSocket.Send("t3", hasMoreToSend: true);
                                    pubSocket.Send("For standard sockets, this flag is passed through zmq_poll() to the underlying poll() system call and generally means that some sort of error condition is present on the socket specified by fd. For ØMQ sockets this flag has no effect if set in events, and shall never be returned in revents by zmq_poll().");

                                    Thread.Sleep(1);
                                }
                            });

                            var p = new Polling(PollingEvents.RecvReady, subSocket1, subSocket2, subSocket3);

                            p.RecvReady += socket =>
                            {
                                var topic = socket.Recv();
                                if (topic.Length != 2)
                                {
                                    throw new Exception("unexpected topic");
                                }

                                var data = socket.Recv();
                                if (data.Length < 30)
                                {
                                    throw new Exception("unexpected data");
                                }

//					Console.Write(".");
                            };

                            ulong counter = 0;

                            Task.Factory.StartNew(() =>
                            {
                                while (true)
                                {
                                    counter++;

                                    if (counter % 1000 == 0)
                                    {
                                        var collCount0 = GC.CollectionCount(0);
                                        var collCount1 = GC.CollectionCount(1);
                                        var collCount2 = GC.CollectionCount(2);

                                        Console.WriteLine("");
                                        Console.WriteLine("{3}\tCollections on Gen 0 {0}, Gen 1 {1}, Gen 2 {2} ", collCount0, collCount1, collCount2, counter);
                                    }

                                    p.PollForever();
                                }
                            });

                            Thread.CurrentThread.Join();
                        }
        }