Esempio n. 1
0
        bool RawMessageReady()
        {
            //  Destroy content of the old message.
            m_inProgress = null;

            //  Read new message. If there is none, return false.
            //  Note that new state is set only if write is successful. That way
            //  unsuccessful write will cause retry on the next state machine
            //  invocation.
            if (m_msgSource == null)
            {
                return false;
            }

            m_inProgress = m_msgSource.PullMsg();

            if (m_inProgress == null)
                return false;

            m_inProgress.ResetFlags(MsgFlags.Shared | MsgFlags.More | MsgFlags.Identity);

            NextStep(null, 0, RawMessageSizeReadyState, true);

            return true;
        }
Esempio n. 2
0
        bool RawMessageReady()
        {
            //  Destroy content of the old message.
            m_inProgress.Close();

            //  Read new message. If there is none, return false.
            //  Note that new state is set only if write is successful. That way
            //  unsuccessful write will cause retry on the next state machine
            //  invocation.
            if (m_msgSource == null)
            {
                return(false);
            }

            bool result = m_msgSource.PullMsg(ref m_inProgress);

            if (!result)
            {
                m_inProgress.InitEmpty();

                return(false);
            }


            m_inProgress.ResetFlags(MsgFlags.Shared | MsgFlags.More | MsgFlags.Identity);

            NextStep(null, 0, RawMessageSizeReadyState, true);

            return(true);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// Transmit the given Msg across the message-queueing system.
        /// If the msg fails to immediately send, then - if DontWait is specified and no SendTimeout was set
        /// then throw an AgainException.
        /// </summary>
        /// <param name="msg">the Msg to transmit</param>
        /// <param name="flags">a SendReceiveOptions: either don't specify DontWait, or set a timeout</param>
        public void Send(ref Msg msg, SendReceiveOptions flags)
        {
            CheckContextTerminated();

            //  Check whether message passed to the function is valid.
            if (!msg.IsInitialised)
            {
                throw new FaultException("SocketBase.Send passed an uninitialised Msg.");
            }

            //  Process pending commands, if any.
            ProcessCommands(0, true);

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendReceiveOptions.SendMore) > 0)
            {
                msg.SetFlags(MsgFlags.More);
            }

            //  Try to send the message.
            bool isMessageSent = XSend(ref msg, flags);

            if (isMessageSent)
            {
                return;
            }

            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            bool isDontWaitSet = (flags & SendReceiveOptions.DontWait) > 0;

            if (isDontWaitSet || m_options.SendTimeout == 0)
            {
#if DEBUG
                string xMsg;
                if (isDontWaitSet && m_options.SendTimeout == 0)
                {
                    xMsg = "SocketBase.Send failed, and DontWait is true AND SendTimeout is 0.";
                }
                else if (isDontWaitSet)
                {
                    xMsg = "SocketBase.Send failed and DontWait is specified.";
                }
                else
                {
                    xMsg = "SocketBase.Send failed and no SendTimeout is specified.";
                }
                throw new AgainException(innerException: null, message: xMsg);
#else
                throw new AgainException(innerException: null, message: "SocketBase.Send failed");
#endif
            }

            //  Compute the time when the timeout should occur.
            //  If the timeout is infinite, don't care.
            int  timeout = m_options.SendTimeout;
            long end     = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeout, false);

                isMessageSent = XSend(ref msg, flags);

                if (isMessageSent)
                {
                    break;
                }

                if (timeout <= 0)
                {
                    continue;
                }

                timeout = (int)(end - Clock.NowMs());

                if (timeout <= 0)
                {
                    throw new AgainException(innerException: null, message: "SocketBase.Send failed and timeout <= 0");
                }
            }
        }
Esempio n. 5
0
        public void Send(Msg msg, SendReceiveOptions flags)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether message passed to the function is valid.
            if (msg == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }

            //  Process pending commands, if any.
            ProcessCommands(0, true);

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendReceiveOptions.SendMore) > 0)
                msg.SetFlags(MsgFlags.More);

            //  Try to send the message.

            bool isMessageSent = XSend(msg, flags);

            if (isMessageSent)
            {
                return;
            }

            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.SendTimeout == 0)
                throw AgainException.Create();

            //  Compute the time when the timeout should occur.
            //  If the timeout is infite, don't care.
            int timeout = m_options.SendTimeout;
            long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeout, false);

                isMessageSent = XSend(msg, flags);
                if (isMessageSent)
                    break;

                if (timeout > 0)
                {
                    timeout = (int)(end - Clock.NowMs());
                    if (timeout <= 0)
                    {
                        throw AgainException.Create();
                    }
                }
            }
        }
Esempio n. 6
0
        public bool Send(Msg msg, SendRecieveOptions flags)
        {
            if (m_ctxTerminated)
            {
                ZError.ErrorNumber = (ErrorNumber.ETERM);
                return false;
            }

            //  Check whether message passed to the function is valid.
            if (msg == null)
            {
                ZError.ErrorNumber = (ErrorNumber.EFAULT);
                throw new ArgumentException();
            }

            //  Process pending commands, if any.
            bool rc = ProcessCommands(0, true);
            if (!rc)
                return false;

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendRecieveOptions.SendMore) > 0)
                msg.SetFlags(MsgFlags.More);

            //  Try to send the message.
            rc = XSend(msg, flags);
            if (rc)
                return true;
            if (!ZError.IsError(ErrorNumber.EAGAIN))
                return false;

            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            if ((flags & SendRecieveOptions.DontWait) > 0 || m_options.SendTimeout == 0)
                return false;

            //  Compute the time when the timeout should occur.
            //  If the timeout is infite, don't care.
            int timeout = m_options.SendTimeout;
            long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                if (!ProcessCommands(timeout, false))
                    return false;

                rc = XSend(msg, flags);
                if (rc)
                    break;

                if (!ZError.IsError(ErrorNumber.EAGAIN))
                    return false;

                if (timeout > 0)
                {
                    timeout = (int)(end - Clock.NowMs());
                    if (timeout <= 0)
                    {
                        ZError.ErrorNumber = (ErrorNumber.EAGAIN);
                        return false;
                    }
                }
            }
            return true;
        }
Esempio n. 7
0
        public void Send(Msg msg, SendReceiveOptions flags)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether message passed to the function is valid.
            if (msg == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }

            //  Process pending commands, if any.
            ProcessCommands(0, true);

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendReceiveOptions.SendMore) > 0)
            {
                msg.SetFlags(MsgFlags.More);
            }

            //  Try to send the message.

            bool isMessageSent = XSend(msg, flags);

            if (isMessageSent)
            {
                return;
            }


            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.SendTimeout == 0)
            {
                throw AgainException.Create();
            }

            //  Compute the time when the timeout should occur.
            //  If the timeout is infite, don't care.
            int  timeout = m_options.SendTimeout;
            long end     = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeout, false);

                isMessageSent = XSend(msg, flags);
                if (isMessageSent)
                {
                    break;
                }

                if (timeout > 0)
                {
                    timeout = (int)(end - Clock.NowMs());
                    if (timeout <= 0)
                    {
                        throw AgainException.Create();
                    }
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Transmit the given Msg across the message-queueing system.
        /// If the msg fails to immediately send, then - if DontWait is specified and no SendTimeout was set
        /// then throw an AgainException.
        /// </summary>
        /// <param name="msg">the Msg to transmit</param>
        /// <param name="flags">a SendReceiveOptions: either don't specify DontWait, or set a timeout</param>
        public void Send(ref Msg msg, SendReceiveOptions flags)
        {
            CheckContextTerminated();

            //  Check whether message passed to the function is valid.
            if (!msg.IsInitialised)
                throw new FaultException("SocketBase.Send passed an uninitialised Msg.");

            //  Process pending commands, if any.
            ProcessCommands(0, true);

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendReceiveOptions.SendMore) > 0)
                msg.SetFlags(MsgFlags.More);

            //  Try to send the message.
            bool isMessageSent = XSend(ref msg);

            if (isMessageSent)
                return;

            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            bool isDontWaitSet = (flags & SendReceiveOptions.DontWait) > 0;
            if (isDontWaitSet || m_options.SendTimeout == 0)
            {
            #if DEBUG
                string xMsg;
                if (isDontWaitSet && m_options.SendTimeout == 0)
                    xMsg = "SocketBase.Send failed, and DontWait is true AND SendTimeout is 0.";
                else if (isDontWaitSet)
                    xMsg = "SocketBase.Send failed and DontWait is specified.";
                else
                    xMsg = "SocketBase.Send failed and no SendTimeout is specified.";
                throw new AgainException(innerException: null, message: xMsg);
            #else
                throw new AgainException(innerException: null, message: "SocketBase.Send failed");
            #endif
            }

            //  Compute the time when the timeout should occur.
            //  If the timeout is infinite, don't care.
            int timeout = m_options.SendTimeout;
            long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeout, false);

                isMessageSent = XSend(ref msg);

                if (isMessageSent)
                    break;

                if (timeout <= 0)
                    continue;

                timeout = (int)(end - Clock.NowMs());

                if (timeout <= 0)
                    throw new AgainException(innerException: null, message: "SocketBase.Send failed and timeout <= 0");
            }
        }
Esempio n. 9
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;
        }
Esempio n. 10
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);
        }