public void Run(CancellationToken cancellationToken)
        {
            using (var context = new ZContext())
                using (var receiver = new ZSocket(context, ZSocketType.PULL))
                    using (var sink = new ZSocket(context, ZSocketType.PUSH))
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            Console.WriteLine($"Woker{workerId} cancelled....");

                            context.Shutdown();
                            cancellationToken.ThrowIfCancellationRequested();
                        }

                        receiver.Connect("tcp://127.0.0.1:5557");
                        sink.Connect("tcp://127.0.0.1:5558");

                        Console.WriteLine($"Worker{workerId} started....");

                        while (!cancellationToken.IsCancellationRequested)
                        {
                            int    workload;
                            ZError error;

                            using (ZFrame frame = receiver.ReceiveFrame(out error))
                            {
                                if (frame == null)
                                {
                                    if (Equals(error, ZError.EAGAIN))
                                    {
                                        Thread.Sleep(1);
                                        continue;
                                    }
                                    throw new ZException(error);
                                }
                                workload = frame.ReadInt32();
                            }

                            sink.SendFrame(new ZFrame($"worker{workerId} workload:{workload}"));

                            ProcessedMessages++;

                            if (cancellationToken.IsCancellationRequested)
                            {
                                Console.WriteLine($"Woker{workerId} cancelled....");

                                context.Shutdown();
                                cancellationToken.ThrowIfCancellationRequested();
                            }

                            Thread.SpinWait(100);
                        }
                    }
        }
Beispiel #2
0
        public static void SyncSub(string[] args)
        {
            //
            // Synchronized subscriber
            //
            // Author: metadings
            //

            using (var context = new ZContext())
                using (var subscriber = new ZSocket(context, ZSocketType.SUB))
                    using (var syncclient = new ZSocket(context, ZSocketType.REQ))
                    {
                        // First, connect our subscriber socket
                        subscriber.Connect("tcp://127.0.0.1:5561");
                        subscriber.SubscribeAll();

                        // 0MQ is so fast, we need to wait a while…
                        Thread.Sleep(1);

                        // Second, synchronize with publisher
                        syncclient.Connect("tcp://127.0.0.1:5562");

                        // - send a synchronization request
                        syncclient.Send(new ZFrame());

                        // - wait for synchronization reply
                        syncclient.ReceiveFrame();

                        // Third, get our updates and report how many we got
                        int i = 0;
                        while (true)
                        {
                            using (ZFrame frame = subscriber.ReceiveFrame())
                            {
                                string text = frame.ReadString();
                                if (text == "END")
                                {
                                    break;
                                }

                                frame.Position = 0;
                                Console.WriteLine("Receiving {0}...", frame.ReadInt32());

                                ++i;
                            }
                        }
                        Console.WriteLine("Received {0} updates.", i);
                    }
        }
Beispiel #3
0
            public ZMessage Request(ZMessage request)
            {
                // This method does the hard work. It sends a request to all
                // connected servers in parallel (for this to work, all connections
                // must be successful and completed by this time). It then waits
                // for a single successful reply, and returns that to the caller.
                // Any other replies are just dropped:

                ZMessage reply = null;

                using (request)
                {
                    // Prefix request with sequence number and empty envelope
                    this.sequence++;
                    request.Prepend(new ZFrame(this.sequence));
                    request.Prepend(new ZFrame());

                    // Blast the request to all connected servers
                    for (int server = 0; server < this.servers; ++server)
                    {
                        using (var outgoing = request.Duplicate())
                        {
                            this.socket.Send(outgoing);
                        }
                    }

                    // Wait for a matching reply to arrive from anywhere
                    // Since we can poll several times, calculate each one
                    ZError   error;
                    DateTime endtime = DateTime.UtcNow + GLOBAL_TIMEOUT;
                    var      poll    = ZPollItem.CreateReceiver();
                    while (endtime > DateTime.UtcNow)
                    {
                        if (this.socket.PollIn(poll, out reply, out error, endtime - DateTime.UtcNow))
                        {
                            // Reply is [empty][sequence][OK]
                            if (reply.Count < 3)
                            {
                                throw new InvalidOperationException();
                            }

                            reply.RemoveAt(0);

                            using (ZFrame sequenceFrame = reply.RemoveAt(0, false))
                            {
                                int sequence = sequenceFrame.ReadInt32();

                                if (sequence == this.sequence)
                                {
                                    break;                                      // Reply is ok
                                }
                            }

                            reply.Dispose();
                        }
                        else
                        {
                            if (error == ZError.ETERM)
                            {
                                break;                                  // Interrupted
                            }
                            if (error != ZError.EAGAIN)
                            {
                                throw new ZException(error);
                            }
                        }
                    }
                }
                return(reply);
            }