示例#1
0
文件: Stream.cs 项目: wbj808178/netmq
        protected override bool XRecv(SendReceiveOptions flags, out Msg msg)
        {
            msg = null;
            if (m_prefetched)
            {
                if (!m_identitySent)
                {
                    msg            = m_prefetchedId;
                    m_prefetchedId = null;
                    m_identitySent = true;
                }
                else
                {
                    msg             = m_prefetchedMsg;
                    m_prefetchedMsg = null;
                    m_prefetched    = false;
                }

                return(true);
            }

            Pipe[] pipe = new Pipe[1];

            bool isMessageAvailable = m_fq.RecvPipe(pipe, out m_prefetchedMsg);

            if (!isMessageAvailable)
            {
                return(false);
            }
            else if (m_prefetchedMsg == null)
            {
                return(true);
            }

            Debug.Assert(pipe[0] != null);
            Debug.Assert(!m_prefetchedMsg.HasMore);

            //  We have received a frame with TCP data.
            //  Rather than sendig this frame, we keep it in prefetched
            //  buffer and send a frame with peer's ID.
            Blob identity = pipe[0].Identity;

            msg = new Msg(identity.Data, true);
            msg.SetFlags(MsgFlags.More);

            m_prefetched   = true;
            m_identitySent = true;

            return(true);
        }
示例#2
0
文件: Router.cs 项目: wbj808178/netmq
        protected override bool XRecv(SendReceiveOptions flags, out Msg msg)
        {
            msg = null;
            if (m_prefetched)
            {
                if (!m_identitySent)
                {
                    msg            = m_prefetchedId;
                    m_prefetchedId = null;
                    m_identitySent = true;
                }
                else
                {
                    msg             = m_prefetchedMsg;
                    m_prefetchedMsg = null;
                    m_prefetched    = false;
                }
                m_moreIn = msg.HasMore;
                return(true);
            }

            Pipe[] pipe = new Pipe[1];

            bool isMessageAvailable = m_fq.RecvPipe(pipe, out msg);

            //  It's possible that we receive peer's identity. That happens
            //  after reconnection. The current implementation assumes that
            //  the peer always uses the same identity.
            //  TODO: handle the situation when the peer changes its identity.
            while (isMessageAvailable && msg != null && msg.IsIdentity)
            {
                isMessageAvailable = m_fq.RecvPipe(pipe, out msg);
            }

            if (!isMessageAvailable)
            {
                return(false);
            }
            else if (msg == null)
            {
                return(true);
            }

            Debug.Assert(pipe[0] != null);

            //  If we are in the middle of reading a message, just return the next part.
            if (m_moreIn)
            {
                m_moreIn = msg.HasMore;
            }
            else
            {
                //  We are at the beginning of a message.
                //  Keep the message part we have in the prefetch buffer
                //  and return the ID of the peer instead.
                m_prefetchedMsg = msg;

                m_prefetched = true;

                Blob identity = pipe[0].Identity;
                msg = new Msg(identity.Data);
                msg.SetFlags(MsgFlags.More);
                m_identitySent = true;
            }

            return(true);
        }