コード例 #1
0
ファイル: MonitorEvent.cs プロジェクト: knocte/netmq
        public static MonitorEvent Read(SocketBase s)
        {
            Msg msg = s.Recv(0);
            if (msg == null)
                return null;

            int pos = 0;
            ByteArraySegment data = msg.Data;

            SocketEvent @event =(SocketEvent) data.GetInteger(pos);
            pos += 4;
            int len = (int)data[pos++];
            string addr = data.GetString(len, pos);
            pos += len;
            int flag = (int)data[pos++];
            Object arg = null;

            if (flag == ValueInteger)
            {
                arg = data.GetInteger(pos);
            }
            else if (flag == ValueChannel)
            {
                if (SizeOfIntPtr == 4)
                {
                    arg = new IntPtr(data.GetInteger(pos));
                }
                else
                {
                    arg = new IntPtr(data.GetLong(pos));
                }
            }

            return new MonitorEvent(@event, addr, arg);
        }
コード例 #2
0
ファイル: Proxy.cs プロジェクト: knocte/netmq
    public static bool proxy(SocketBase frontend_,
        SocketBase backend_, SocketBase capture_)
    {
        //  The algorithm below assumes ratio of requests and replies processed
        //  under full load to be 1:1.

        //  TODO: The current implementation drops messages when
        //  any of the pipes becomes full.

        bool success = true;
        int rc;
        long more;
        Msg msg;
        PollItem[] items = new PollItem[2];

        items[0] = new PollItem (frontend_, ZMQ.ZmqPollin );
        items[1] = new PollItem (backend_, ZMQ.ZmqPollin );

        Selector selector;
        try {
            selector = Selector.open();
        } catch (IOException e) {
            throw new ZError.IOException(e);
        }

        try {
            while (!Thread.currentThread().isInterrupted()) {
                //  Wait while there are either requests or replies to process.
                rc = ZMQ.zmq_poll (selector, items, -1);
                if (rc < 0)
                    return false;

                //  Process a request.
                if (items [0].isReadable()) {
                    while (true) {
                        msg = frontend_.Recv (0);
                        if (msg == null) {
                            return false;
                        }

                        more = frontend_.getsockopt (ZMQ.ZMQ_RCVMORE);

                        if (more < 0)
                            return false;

                        //  Copy message to capture socket if any
                        if (capture_ != null) {
                            Msg ctrl = new Msg (msg);
                            success = capture_.Send (ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE: 0);
                            if (!success)
                                return false;
                        }

                        success = backend_.Send (msg, more > 0 ? ZMQ.ZMQ_SNDMORE: 0);
                        if (!success)
                            return false;
                        if (more == 0)
                            break;
                    }
                }
                //  Process a reply.
                if (items [1].isReadable()) {
                    while (true) {
                        msg = backend_.Recv (0);
                        if (msg == null) {
                            return false;
                        }

                        more = backend_.getsockopt (ZMQ.ZMQ_RCVMORE);

                        if (more < 0)
                            return false;

                        //  Copy message to capture socket if any
                        if (capture_ != null) {
                            Msg ctrl = new Msg (msg);
                            success = capture_.Send (ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE: 0);
                            if (!success)
                                return false;
                        }

                        success = frontend_.Send (msg, more > 0 ? ZMQ.ZMQ_SNDMORE: 0);
                        if (!success)
                            return false;
                        if (more == 0)
                            break;
                    }
                }

            }
        } finally {
            try {
                selector.close();
            } catch (Exception e) {
            }
        }

        return true;
    }
コード例 #3
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static Msg RecvMsg(SocketBase s, SendRecieveOptions flags)
 {
     return s.Recv(flags);
 }