Exemple #1
0
        protected bool XSend(Msg msg, SendReceiveOptions flags)
        {
            if (m_pipe == null || !m_pipe.Write(msg))
            {
                return(false);
            }

            if ((flags & SendReceiveOptions.SendMore) == 0)
            {
                m_pipe.Flush();
            }

            //  Detach the original message from the data buffer.
            return(true);
        }
Exemple #2
0
 //  Write the message to the pipe. Make the pipe inactive if writing
 //  fails. In such a case false is returned.
 private bool Write(Pipe pipe, Msg msg)
 {
     if (!pipe.Write(msg))
     {
         Utils.Swap(m_pipes, m_pipes.IndexOf(pipe), m_matching - 1);
         m_matching--;
         Utils.Swap(m_pipes, m_pipes.IndexOf(pipe), m_active - 1);
         m_active--;
         Utils.Swap(m_pipes, m_active, m_eligible - 1);
         m_eligible--;
         return(false);
     }
     if (!msg.HasMore)
     {
         pipe.Flush();
     }
     return(true);
 }
Exemple #3
0
        public virtual void PushMsg(Msg msg)
        {
            //  First message to receive is identity (if required).
            if (!m_identityReceived)
            {
                msg.SetFlags(MsgFlags.Identity);
                m_identityReceived = true;

                if (!m_options.RecvIdentity)
                {
                    return;
                }
            }

            if (m_pipe != null && m_pipe.Write(msg))
            {
                return;
            }

            throw AgainException.Create();
        }
Exemple #4
0
        static XSub()
        {
            s_sendSubscription = (data, size, arg) => {
                Pipe pipe = (Pipe)arg;

                //  Create the subsctription message.
                Msg msg = new Msg(size + 1);
                msg.Put((byte)1);
                msg.Put(data, 1, size);

                //  Send it to the pipe.
                bool sent = pipe.Write(msg);
                //  If we reached the SNDHWM, and thus cannot send the subscription, drop
                //  the subscription message instead. This matches the behaviour of
                //  zmq_setsockopt(ZMQ_SUBSCRIBE, ...), which also drops subscriptions
                //  when the SNDHWM is reached.
                if (!sent)
                {
                    msg.Close();
                }
            };
        }
Exemple #5
0
        public virtual bool PushMsg(ref Msg msg)
        {
            //  First message to receive is identity (if required).
            if (!m_identityReceived)
            {
                msg.SetFlags(MsgFlags.Identity);
                m_identityReceived = true;

                if (!m_options.RecvIdentity)
                {
                    msg.Close();
                    msg.InitEmpty();
                    return(true);
                }
            }

            if (m_pipe != null && m_pipe.Write(ref msg))
            {
                msg.InitEmpty();
                return(true);
            }

            return(false);
        }
Exemple #6
0
        protected override bool XSend(Msg msg, SendReceiveOptions flags)
        {
            //  If this is the first part of the message it's the ID of the
            //  peer to send the message to.
            if (!m_moreOut)
            {
                Debug.Assert(m_currentOut == null);

                //  If we have malformed message (prefix with no subsequent message)
                //  then just silently ignore it.
                //  TODO: The connections should be killed instead.
                if (msg.HasMore)
                {
                    //  Find the pipe associated with the identity stored in the prefix.
                    //  If there's no such pipe just silently ignore the message, unless
                    //  mandatory is set.
                    Blob    identity = new Blob(msg.Data);
                    Outpipe op;

                    if (m_outpipes.TryGetValue(identity, out op))
                    {
                        m_currentOut = op.Pipe;
                        if (!m_currentOut.CheckWrite())
                        {
                            op.Active    = false;
                            m_currentOut = null;
                            return(false);
                        }
                    }
                    else
                    {
                        throw NetMQException.Create(ErrorCode.EHOSTUNREACH);
                    }
                }

                m_moreOut = true;

                return(true);
            }

            //  Ignore the MORE flag
            msg.ResetFlags(MsgFlags.More);

            //  This is the last part of the message.
            m_moreOut = false;

            //  Push the message into the pipe. If there's no out pipe, just drop it.
            if (m_currentOut != null)
            {
                if (msg.Size == 0)
                {
                    m_currentOut.Terminate(false);
                    m_currentOut = null;
                    return(true);
                }

                bool ok = m_currentOut.Write(msg);
                if (ok)
                {
                    m_currentOut.Flush();
                }

                m_currentOut = null;
            }
            else
            {
            }

            //  Detach the message from the data buffer.

            return(true);
        }
Exemple #7
0
 //  Write the message to the pipe. Make the pipe inactive if writing
 //  fails. In such a case false is returned.
 private bool Write(Pipe pipe, Msg msg)
 {
     if (!pipe.Write (msg)) {
         Utils.Swap(m_pipes, m_pipes.IndexOf (pipe), m_matching - 1);
         m_matching--;
         Utils.Swap(m_pipes, m_pipes.IndexOf (pipe), m_active - 1);
         m_active--;
         Utils.Swap(m_pipes, m_active, m_eligible - 1);
         m_eligible--;
         return false;
     }
     if (!msg.HasMore)
         pipe.Flush ();
     return true;
 }
Exemple #8
0
        protected override bool XSend(ref Msg msg, SendReceiveOptions flags)
        {
            //  If this is the first part of the message it's the ID of the
            //  peer to send the message to.
            if (!m_moreOut)
            {
                Debug.Assert(m_currentOut == null);

                //  If we have malformed message (prefix with no subsequent message)
                //  then just silently ignore it.
                //  TODO: The connections should be killed instead.
                if (msg.HasMore)
                {
                    m_moreOut = true;

                    //  Find the pipe associated with the identity stored in the prefix.
                    //  If there's no such pipe just silently ignore the message, unless
                    //  mandatory is set.
                    Blob    identity = new Blob(msg.Data, msg.Size);
                    Outpipe op;

                    if (m_outpipes.TryGetValue(identity, out op))
                    {
                        m_currentOut = op.Pipe;
                        if (!m_currentOut.CheckWrite())
                        {
                            op.Active    = false;
                            m_currentOut = null;
                            if (m_mandatory)
                            {
                                m_moreOut = false;
                                return(false);
                            }
                        }
                    }
                    else if (m_mandatory)
                    {
                        m_moreOut = false;
                        throw NetMQException.Create(ErrorCode.EHOSTUNREACH);
                    }
                }

                //  Detach the message from the data buffer.
                msg.Close();
                msg.InitEmpty();

                return(true);
            }

            if (m_options.RawSocket)
            {
                msg.ResetFlags(MsgFlags.More);
            }

            //  Check whether this is the last part of the message.
            m_moreOut = msg.HasMore;

            //  Push the message into the pipe. If there's no out pipe, just drop it.
            if (m_currentOut != null)
            {
                // Close the remote connection if user has asked to do so
                // by sending zero length message.
                // Pending messages in the pipe will be dropped (on receiving term- ack)
                if (m_rawSocket && msg.Size == 0)
                {
                    m_currentOut.Terminate(false);
                    msg.Close();
                    msg.InitEmpty();
                    m_currentOut = null;
                    return(true);
                }

                bool ok = m_currentOut.Write(ref msg);
                if (!ok)
                {
                    m_currentOut = null;
                }
                else if (!m_moreOut)
                {
                    m_currentOut.Flush();
                    m_currentOut = null;
                }
            }
            else
            {
                msg.Close();
            }

            //  Detach the message from the data buffer.
            msg.InitEmpty();

            return(true);
        }