Exemple #1
0
        public void ShouldAllowInfinitePolling()
        {
            Ctx contextNew = ZMQ.CtxNew();
            SocketBase receiver = ZMQ.Socket(contextNew, ZmqSocketType.Dealer);

            ZMQ.Bind(receiver, "inproc://test");

            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(500);
                SocketBase sender = ZMQ.Socket(contextNew, ZmqSocketType.Dealer);
                ZMQ.Connect(sender, "inproc://test");
                ZMQ.Send(sender, "ping", SendReceiveOptions.None);
                ZMQ.Close(sender);
            });

            var pollItems = new PollItem[] { new PollItem(receiver, PollEvents.PollIn) };

            Assert.DoesNotThrow(() => ZMQ.Poll(pollItems, -1));

            var actual = Encoding.ASCII.GetString(ZMQ.Recv(receiver, SendReceiveOptions.DontWait).Data);
            Assert.AreEqual("ping", actual);

            ZMQ.Close(receiver);
            ZMQ.Term(contextNew);
        }
        public static NetMQMessage ReceiveMessage(this NetMQSocket socket, TimeSpan timeout)
        {
            var item = new PollItem(socket.SocketHandle, PollEvents.PollIn);
            var items = new[] { item };
            ZMQ.Poll(items, (int)timeout.TotalMilliseconds);

            if (item.ResultEvent.HasFlag(PollEvents.PollError) && !socket.IgnoreErrors)
                throw new ErrorPollingException("Error while polling", socket);

            if (!item.ResultEvent.HasFlag(PollEvents.PollIn))
                return null;

            var msg = socket.ReceiveMessage();
            return msg;
        }
Exemple #3
0
        public static int Poll(PollItem[] items, int itemsCount, int timeout)
        {
            if (items == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }
            if (itemsCount == 0)
            {
                if (timeout <= 0)
                    return 0;
                Thread.Sleep(timeout);
                return 0;
            }

            bool firstPass = true;
            int nevents = 0;

            List<Socket> writeList = new List<Socket>();
            List<Socket> readList = new List<Socket>();
            List<Socket> errorList = new List<Socket>();

            for (int i = 0; i < itemsCount; i++)
            {
                var pollItem = items[i];

                if (pollItem.Socket != null)
                {
                    if (pollItem.Events != PollEvents.None)
                    {
                        readList.Add(pollItem.Socket.FD);
                    }
                }
                else
                {
                    if ((pollItem.Events & PollEvents.PollIn) == PollEvents.PollIn)
                    {
                        readList.Add(pollItem.FileDescriptor);
                    }

                    if ((pollItem.Events & PollEvents.PollOut) == PollEvents.PollOut)
                    {
                        writeList.Add(pollItem.FileDescriptor);
                    }

                    if ((pollItem.Events & PollEvents.PollError) == PollEvents.PollError)
                    {
                        errorList.Add(pollItem.FileDescriptor);
                    }
                }
            }

            List<Socket> inset = new List<Socket>(readList.Count);
            List<Socket> outset = new List<Socket>(writeList.Count);
            List<Socket> errorset = new List<Socket>(errorList.Count);

            Stopwatch stopwatch = null;

            while (true)
            {
                int currentTimeoutMicroSeconds;

                if (firstPass)
                {
                    currentTimeoutMicroSeconds = 0;
                }
                else if (timeout == -1)
                {
                    currentTimeoutMicroSeconds = -1;
                }
                else
                {
                    currentTimeoutMicroSeconds = (int)((timeout - stopwatch.ElapsedMilliseconds) * 1000);

                    if (currentTimeoutMicroSeconds < 0)
                    {
                        currentTimeoutMicroSeconds = 0;
                    }
                }

                inset.AddRange(readList);
                outset.AddRange(writeList);
                errorset.AddRange(errorList);

                try
                {
                    System.Net.Sockets.Socket.Select(inset, outset, errorset, currentTimeoutMicroSeconds);
                }
                catch (SocketException ex)
                {
                    throw NetMQException.Create(ErrorCode.ESOCKET, ex);
                }

                for (int i = 0; i < itemsCount; i++)
                {
                    var pollItem = items[i];

                    pollItem.ResultEvent = PollEvents.None;

                    if (pollItem.Socket != null)
                    {
                        PollEvents events = (PollEvents)GetSocketOption(pollItem.Socket, ZmqSocketOptions.Events);

                        if ((pollItem.Events & PollEvents.PollIn) == PollEvents.PollIn &&
                            (events & PollEvents.PollIn) == PollEvents.PollIn)
                        {
                            pollItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if ((pollItem.Events & PollEvents.PollOut) == PollEvents.PollOut &&
                            (events & PollEvents.PollOut) == PollEvents.PollOut)
                        {
                            pollItem.ResultEvent |= PollEvents.PollOut;
                        }
                    }
                    else
                    {
                        if (inset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if (outset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollOut;
                        }

                        if (errorset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollError;
                        }
                    }

                    if (pollItem.ResultEvent != PollEvents.None)
                    {
                        nevents++;
                    }
                }

                inset.Clear();
                outset.Clear();
                errorset.Clear();

                if (timeout == 0)
                {
                    break;
                }

                if (nevents > 0)
                {
                    break;
                }

                if (timeout < 0)
                {
                    if (firstPass)
                    {
                        firstPass = false;
                    }

                    continue;
                }

                if (firstPass)
                {
                    stopwatch = Stopwatch.StartNew();
                    firstPass = false;
                    continue;
                }

                if (stopwatch.ElapsedMilliseconds > timeout)
                {
                    break;
                }
            }

            return nevents;
        }
Exemple #4
0
 //[Obsolete]
 //public static bool zmq_device(int device_, SocketBase insocket_,
 //        SocketBase outsocket_)
 //{
 //    return Proxy.proxy(insocket_, outsocket_, null);
 //}
 public static int Poll(PollItem[] items, int timeout)
 {
     return Poll(items, items.Length, timeout);
 }
Exemple #5
0
        public static bool CreateProxy(SocketBase frontend,
						SocketBase backend, SocketBase capture)
        {
            //  The algorithm below assumes ratio of requests and replies processed
            //  under full load to be 1:1.

            int more;
            int rc;
            Msg msg;
            PollItem[] items = new PollItem[2];

            items[0] = new PollItem(frontend, PollEvents.PollIn);
            items[1] = new PollItem(backend, PollEvents.PollIn);

            while (true)
            {
                //  Wait while there are either requests or replies to process.
                rc = ZMQ.Poll(items, -1);
                if (rc < 0)
                    return false;

                //  Process a request.
                if ((items[0].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn)
                {
                    while (true)
                    {
                        try
                        {
                            msg = frontend.Recv(0);
                        }
                        catch (TerminatingException)
                        {
                            return false;
                        }

                        if (msg == null)
                        {
                            return false;
                        }

                        more = frontend.GetSocketOption(ZmqSocketOptions.ReceiveMore);

                        if (more < 0)
                            return false;

                        //  Copy message to capture socket if any
                        if (capture != null)
                        {
                            Msg ctrl = new Msg(msg);
                            capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);
                        }

                        backend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0);
                        if (more == 0)
                            break;
                    }
                }
                //  Process a reply.
                if ((items[1].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn)
                {
                    while (true)
                    {
                        try
                        {
                            msg = backend.Recv(0);
                        }
                        catch (TerminatingException)
                        {
                            return false;
                        }

                        if (msg == null)
                        {
                            return false;
                        }

                        more = backend.GetSocketOption(ZmqSocketOptions.ReceiveMore);

                        if (more < 0)
                            return false;

                        //  Copy message to capture socket if any
                        if (capture != null)
                        {
                            Msg ctrl = new Msg(msg);
                            capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);

                        }

                        frontend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0);
                        if (more == 0)
                            break;
                    }
                }
            }
        }
Exemple #6
0
        private void RebuildPollset()
        {
            m_pollset = null;
            m_pollact = null;

            m_pollSize = m_sockets.Count;
            m_pollset = new PollItem[m_pollSize];
            m_pollact = new NetMQSocket[m_pollSize];

            uint itemNbr = 0;
            foreach (var socket in m_sockets)
            {
                m_pollset[itemNbr] = new PollItem(socket.SocketHandle, socket.GetPollEvents());
                m_pollact[itemNbr] = socket;
                itemNbr++;
            }
            m_isDirty = false;
        }
Exemple #7
0
        public static bool CreateProxy(SocketBase frontend,
                                       SocketBase backend, SocketBase capture)
        {
            //  The algorithm below assumes ratio of requests and replies processed
            //  under full load to be 1:1.

            int more;
            int rc;
            Msg msg;

            PollItem[] items = new PollItem[2];

            items[0] = new PollItem(frontend, PollEvents.PollIn);
            items[1] = new PollItem(backend, PollEvents.PollIn);

            while (true)
            {
                //  Wait while there are either requests or replies to process.
                rc = ZMQ.Poll(items, -1);
                if (rc < 0)
                {
                    return(false);
                }

                //  Process a request.
                if ((items[0].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn)
                {
                    while (true)
                    {
                        try
                        {
                            msg = frontend.Recv(0);
                        }
                        catch (TerminatingException)
                        {
                            return(false);
                        }

                        if (msg == null)
                        {
                            return(false);
                        }

                        more = frontend.GetSocketOption(ZmqSocketOptions.ReceiveMore);

                        if (more < 0)
                        {
                            return(false);
                        }

                        //  Copy message to capture socket if any
                        if (capture != null)
                        {
                            Msg ctrl = new Msg(msg);
                            capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);
                        }

                        backend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0);
                        if (more == 0)
                        {
                            break;
                        }
                    }
                }
                //  Process a reply.
                if ((items[1].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn)
                {
                    while (true)
                    {
                        try
                        {
                            msg = backend.Recv(0);
                        }
                        catch (TerminatingException)
                        {
                            return(false);
                        }

                        if (msg == null)
                        {
                            return(false);
                        }

                        more = backend.GetSocketOption(ZmqSocketOptions.ReceiveMore);

                        if (more < 0)
                        {
                            return(false);
                        }

                        //  Copy message to capture socket if any
                        if (capture != null)
                        {
                            Msg ctrl = new Msg(msg);
                            capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);
                        }

                        frontend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0);
                        if (more == 0)
                        {
                            break;
                        }
                    }
                }
            }
        }