Esempio n. 1
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 = new Msg();

            msg.InitEmpty();
            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
                        {
                            frontend.Recv(ref msg, SendReceiveOptions.None);
                        }
                        catch (TerminatingException)
                        {
                            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();
                            ctrl.InitEmpty();

                            ctrl.Copy(ref msg);

                            capture.Send(ref ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);
                        }

                        backend.Send(ref msg, more > 0 ? SendReceiveOptions.SendMore : 0);
                        if (more == 0)
                        {
                            break;
                        }
                    }
                }
                //  Process a reply.
                if ((items[1].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn)
                {
                    while (true)
                    {
                        try
                        {
                            backend.Recv(ref msg, SendReceiveOptions.None);
                        }
                        catch (TerminatingException)
                        {
                            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();
                            ctrl.InitEmpty();

                            ctrl.Copy(ref msg);

                            capture.Send(ref ctrl, more > 0 ? SendReceiveOptions.SendMore : 0);
                        }

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