Пример #1
0
        public static async Task SendMultipartMessageAsync(
            this IOutgoingSocket socket,
            NetMQMessage message,
            int?timeout = null,
            int delay   = 100)
        {
            int elapsed = 0;

            while (!socket.TrySendMultipartMessage(message))
            {
                await Task.Delay(delay);

                elapsed += 100;
                if (elapsed > timeout)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time[{timeout}].");
                }
            }
        }
Пример #2
0
        public static async Task SendMultipartMessageAsync(
            this IOutgoingSocket socket,
            NetMQMessage message,
            TimeSpan?timeout = null,
            TimeSpan?delay   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            TimeSpan delayNotNull = delay ?? TimeSpan.FromMilliseconds(100);
            TimeSpan elapsed      = TimeSpan.Zero;

            while (!socket.TrySendMultipartMessage(message))
            {
                await Task.Delay(delayNotNull, cancellationToken);

                elapsed += delayNotNull;
                if (elapsed > timeout)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time[{timeout}].");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Send the ZreMsg to the socket.
        /// Warning re WHISPER and SHOUT: The 0MQ spec http://rfc.zeromq.org/spec:36 
        ///     says "message content defined as one 0MQ frame. ZRE does not support multi-frame message contents."
        ///     But this C# implementation allows multi-frame contents.
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
                output.SendMoreFrame(RoutingId);

            int frameSize = 2 + 1; //  Signature and message ID
            switch (Id)
            {
                case MessageId.Hello:
                    frameSize += Hello.GetFrameSize();
                    break;
                case MessageId.Whisper:
                    frameSize += Whisper.GetFrameSize();
                    break;
                case MessageId.Shout:
                    frameSize += Shout.GetFrameSize();
                    break;
                case MessageId.Join:
                    frameSize += Join.GetFrameSize();
                    break;
                case MessageId.Leave:
                    frameSize += Leave.GetFrameSize();
                    break;
                case MessageId.Ping:
                    frameSize += Ping.GetFrameSize();
                    break;
                case MessageId.PingOk:
                    frameSize += PingOk.GetFrameSize();
                    break;
            }

            //  Now serialize message into the buffer    
            Msg msg = new Msg();
            msg.InitPool(frameSize);

            try
            {
                _offset = 0;
                _buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 1);

                // put message id
                PutNumber1((byte) Id);

                switch (Id)
                {
                    case MessageId.Hello:
                        Hello.Write(this);
                        break;
                    case MessageId.Whisper:
                        Whisper.Write(this);
                        break;
                    case MessageId.Shout:
                        Shout.Write(this);
                        break;
                    case MessageId.Join:
                        Join.Write(this);
                        break;
                    case MessageId.Leave:
                        Leave.Write(this);
                        break;
                    case MessageId.Ping:
                        Ping.Write(this);
                        break;
                    case MessageId.PingOk:
                        PingOk.Write(this);
                        break;
                }

                //  Send the data frame				
                var more = Id == MessageId.Whisper || Id == MessageId.Shout;
                output.TrySend(ref msg, TimeSpan.Zero, more);

                // Send message content for types with content
                switch (Id)
                {
                    case MessageId.Whisper:
                        if (Whisper.Content == null)
                        {
                            Whisper.Content = new NetMQMessage();
                            Whisper.Content.PushEmptyFrame();
                        }
                        output.TrySendMultipartMessage(Whisper.Content);
                        break;
                    case MessageId.Shout:
                        if (Shout.Content == null)
                        {
                            Shout.Content = new NetMQMessage();
                            Shout.Content.PushEmptyFrame();
                        }
                        output.TrySendMultipartMessage(Shout.Content);
                        break;
                }
            }
            finally
            {
                _buffer = null;
                msg.Close();
            }
        }
Пример #4
0
 /// <summary>
 ///     Attempt to transmit a multiple message on <paramref name="socket" />.
 ///     If frames cannot be sent immediately, return <c>false</c>.
 /// </summary>
 /// <param name="socket">the IOutgoingSocket to transmit on</param>
 /// <param name="message">message to transmit</param>
 public static bool TrySendMultipartMessage([NotNull] this IOutgoingSocket socket, [NotNull] NetMQMessage message)
 {
     return(socket.TrySendMultipartMessage(TimeSpan.Zero, message));
 }
Пример #5
0
        /// <summary>
        /// Send the ZreMsg to the socket.
        /// Warning re WHISPER and SHOUT: The 0MQ spec http://rfc.zeromq.org/spec:36
        ///     says "message content defined as one 0MQ frame. ZRE does not support multi-frame message contents."
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
            {
                output.SendMoreFrame(RoutingId);
            }

            int frameSize = 2 + 1; //  Signature and message ID

            switch (Id)
            {
            case MessageId.Hello:
                frameSize += Hello.GetFrameSize();
                break;

            case MessageId.Whisper:
                frameSize += Whisper.GetFrameSize();
                break;

            case MessageId.Shout:
                frameSize += Shout.GetFrameSize();
                break;

            case MessageId.Join:
                frameSize += Join.GetFrameSize();
                break;

            case MessageId.Leave:
                frameSize += Leave.GetFrameSize();
                break;

            case MessageId.Ping:
                frameSize += Ping.GetFrameSize();
                break;

            case MessageId.PingOk:
                frameSize += PingOk.GetFrameSize();
                break;
            }

            //  Now serialize message into the buffer
            Msg msg = new Msg();

            msg.InitPool(frameSize);

            try
            {
                m_offset = 0;
                m_buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 1);

                // put message id
                PutNumber1((byte)Id);

                switch (Id)
                {
                case MessageId.Hello:
                    Hello.Write(this);
                    break;

                case MessageId.Whisper:
                    Whisper.Write(this);
                    break;

                case MessageId.Shout:
                    Shout.Write(this);
                    break;

                case MessageId.Join:
                    Join.Write(this);
                    break;

                case MessageId.Leave:
                    Leave.Write(this);
                    break;

                case MessageId.Ping:
                    Ping.Write(this);
                    break;

                case MessageId.PingOk:
                    PingOk.Write(this);
                    break;
                }

                //  Send the data frame
                var more = Id == MessageId.Whisper || Id == MessageId.Shout;
                output.TrySend(ref msg, TimeSpan.Zero, more);

                // Send message content for types with content
                switch (Id)
                {
                case MessageId.Whisper:
                    if (Whisper.Content == null)
                    {
                        Whisper.Content = new NetMQMessage();
                        Whisper.Content.PushEmptyFrame();
                    }
                    output.TrySendMultipartMessage(Whisper.Content);
                    break;

                case MessageId.Shout:
                    if (Shout.Content == null)
                    {
                        Shout.Content = new NetMQMessage();
                        Shout.Content.PushEmptyFrame();
                    }
                    output.TrySendMultipartMessage(Shout.Content);
                    break;
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }