Esempio n. 1
0
        private void recvProc()
        {
            ZMQ.Socket reqs = CTX.Socket(ZMQ.SocketType.PULL);
            reqs.Connect(sub_addr);

            var items = new[] { reqs.CreatePollItem(ZMQ.IOMultiPlex.POLLIN | ZMQ.IOMultiPlex.POLLERR) };

            while (isRunning)
            {
                int res = CTX.Poll(items, 1000 * 1000);
                if (res == 0)
                {
                    continue;
                }
                foreach (byte[] data in reqs.RecvAll(ZMQ.SendRecvOpt.NOBLOCK))
                {
                    if (data == null)
                    {
                        continue;
                    }
                    recvQ.Enqueue(data);
                    itemsReadyToRecv.Set();
                }
            }

            reqs.Dispose();
            itemsReadyToRecv.Close();
            Interlocked.Decrement(ref threadStillRunning);
        }
Esempio n. 2
0
        public void TestIfItCanSum()
        {
            // Arrange
            Context context = null;
            Socket  slave   = null;

            ZMQ.Socket master = null;
            using (context = new Context())
            {
                using (master = context.Socket(SocketType.REQ))
                {
                    master.Connect(Transport.TCP, CONNECT_HOSTNAME, TEST_PORT);

                    slave = context.Socket(SocketType.REP);
                    slave.Bind(Transport.TCP, BIND_HOSTNAME, TEST_PORT);

                    using (SumadorSendRecvEsclavo target = new SumadorSendRecvEsclavo(slave, slave))
                    {
                        int[] numeros = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 };

                        // Act
                        var task = Task.Factory.StartNew(() => target.EjecutarOperacion());
                        master.Send(numeros);
                        double resultado = master.Recv <double>();

                        // Assert
                        Assert.That(resultado, Is.EqualTo(numeros.Sum()));
                        Assert.That(target.Resultado, Is.EqualTo(resultado));

                        // Reset
                    }
                }
            }
        }
        protected void Connect()
        {
            string address = string.Format("tcp://{0}:{1}", ServerAddr, ServrtPort);

            _subscriber.Connect(address);

            Log(LOGLEVEL.DEBUG, String.Format("AsynEventSubscriber, Func: Connect, address:{0}", address));

            foreach (var tag in EventTags)
            {
                Log(LOGLEVEL.DEBUG, string.Format("AsynEventSubscriber, Func: Connect, ZMQ subscribe node {0}", tag));

                _subscriber.Subscribe(tag, MsgEncoding);
            }
        }
Esempio n. 4
0
        private void sendProc()
        {
            ZMQ.Socket resp = CTX.Socket(ZMQ.SocketType.PUB);
            if (!string.IsNullOrEmpty(SenderId))
            {
                resp.Identity = Encoding.ASCII.GetBytes(SenderId);
            }
            resp.Connect(pub_addr);

            while (isRunning)
            {
                itemsReadyToSend.WaitOne();
                lock (sendQ)
                {
                    while (sendQ.Count != 0)
                    {
                        byte[] stuffToSend = sendQ.Dequeue();

                        bool sentOk = false;
                        while (!sentOk)
                        {
                            try
                            {
                                resp.Send(stuffToSend);
                                sentOk = true;
                            }
                            catch (ZMQ.Exception ex)
                            {
                                if (ex.Errno == (int)ZMQ.ERRNOS.EAGAIN)
                                {
                                    sentOk = false;
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }
            }

            resp.Dispose();
            itemsReadyToSend.Close();
            Interlocked.Decrement(ref threadStillRunning);
        }
Esempio n. 5
0
        /// <summary>
        /// Fancy way to connect to socket insuring that connection accepted.
        /// Seems to make sence only for in-proc transport.
        /// </summary>
        public void Connect(String address, CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                try
                {
                    _zmqSocket.Connect(address);
                    return;
                }
                catch (ZMQ.Exception ex)
                {
                    // Connection refused
                    if (ex.Errno == 107)
                    {
                        SpinWait.SpinUntil(() => token.IsCancellationRequested, 200);
                        Thread.Sleep(20);
                        continue;
                    }

                    throw;
                }
            }
        }
Esempio n. 6
0
        private void ZMQConnect()
        {
            //  Prepare our context and socket
            m_Context= new ZMQ.Context(1);

            m_Socket = m_Context.Socket(ZMQ.XREQ);
            m_Socket.Connect("tcp://localhost:5555");
        }