/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket" />. /// If no message is available within <paramref name="timeout" />, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="timeout">The maximum period of time to wait for a message to become available.</param> /// <param name="message">The received message. Untouched if no message was available.</param> /// <param name="expectedFrameCount"> /// Specifies the initial capacity of the <see cref="List{T}" /> used /// to buffer results. If the number of frames is known, set it here. If more frames arrive than expected, /// an extra allocation will occur, but the result will still be correct. /// </param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveMultipartMessage([NotNull] this IReceivingSocket socket, TimeSpan timeout, [CanBeNull] ref NetMQMessage message, int expectedFrameCount = 4) { Msg msg = new Msg(); msg.InitEmpty(); // Try to read the first frame if (!socket.TryReceive(ref msg, timeout)) { msg.Close(); return(false); } // We have one, so prepare the container if (message == null) { message = new NetMQMessage(expectedFrameCount); } else { message.Clear(); } // Add the frame message.Append(new NetMQFrame(msg.CloneData())); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); message.Append(new NetMQFrame(msg.CloneData())); } msg.Close(); return(true); }
private void OnSocketReady(object sender, NetMQSocketEventArgs e) { NetMQMessage message = this.m_receiveSocket.ReceiveMultipartMessage(); this.m_handler(this.m_receiveSocket, message); }
/// <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)); }
/// <summary> /// Attempt to transmit a multiple message on <paramref name="socket" />. /// If message cannot be sent within <paramref name="timeout" />, return <c>false</c>. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="timeout">The maximum period of time to try to send a message.</param> /// <param name="message">message to transmit</param> public static bool TrySendMultipartMessage([NotNull] this IOutgoingSocket socket, TimeSpan timeout, [NotNull] NetMQMessage message) { if (message.FrameCount == 0) { throw new ArgumentException("message is empty", nameof(message)); } if (message.FrameCount == 1) { return(socket.TrySendFrame(timeout, message[0].Buffer, message[0].MessageSize)); } bool sentSuccessfully = socket.TrySendFrame(timeout, message[0].Buffer, message[0].MessageSize, true); if (!sentSuccessfully) { return(false); } for (int i = 1; i < message.FrameCount - 1; i++) { socket.SendMoreFrame(message[i].Buffer, message[i].MessageSize); } socket.SendFrame(message.Last.Buffer, message.Last.MessageSize); return(true); }
/// <summary> /// Send multiple message on <paramref name="socket" />, blocking until all entire message is sent. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="message">message to transmit</param> public static void SendMultipartMessage([NotNull] this IOutgoingSocket socket, [NotNull] NetMQMessage message) { if (message.FrameCount == 0) { throw new ArgumentException("message is empty", nameof(message)); } for (int i = 0; i < message.FrameCount - 1; i++) { socket.SendMoreFrame(message[i].Buffer, message[i].MessageSize); } socket.SendFrame(message.Last.Buffer, message.Last.MessageSize); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket" />. /// If no message is immediately available, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="message">The received message. Untouched if no message was available.</param> /// <param name="expectedFrameCount"> /// Specifies the initial capacity of the <see cref="List{T}" /> used /// to buffer results. If the number of frames is known, set it here. If more frames arrive than expected, /// an extra allocation will occur, but the result will still be correct. /// </param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveMultipartMessage([NotNull] this IReceivingSocket socket, [CanBeNull] ref NetMQMessage message, int expectedFrameCount = 4) { return(socket.TryReceiveMultipartMessage(TimeSpan.Zero, ref message, expectedFrameCount)); }