/// <summary> /// Receive a single frame from <paramref name="socket"/>, asynchronously. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="cancellationToken">The token used to propagate notification that this operation should be canceled.</param> /// <returns>The content of the received message frame and boolean indicate if another frame of the same message follows.</returns> public static Task <(byte[], bool)> ReceiveFrameBytesAsync( this NetMQSocket socket, CancellationToken cancellationToken = default(CancellationToken) ) { if (NetMQRuntime.Current == null) { throw new InvalidOperationException("NetMQRuntime must be created before calling async functions"); } socket.AttachToRuntime(); var msg = new Msg(); msg.InitEmpty(); if (socket.TryReceive(ref msg, TimeSpan.Zero)) { var data = msg.CloneData(); bool more = msg.HasMore; msg.Close(); return(Task.FromResult((data, more))); } TaskCompletionSource <(byte[], bool)> source = new TaskCompletionSource <(byte[], bool)>(); CancellationTokenRegistration?registration = null; if (cancellationToken.CanBeCanceled) { registration = cancellationToken.Register(PropagateCancel); } void Listener(object sender, NetMQSocketEventArgs args) { if (socket.TryReceive(ref msg, TimeSpan.Zero)) { var data = msg.CloneData(); bool more = msg.HasMore; msg.Close(); socket.ReceiveReady -= Listener; registration?.Dispose(); source.TrySetResult((data, more)); } } void PropagateCancel() { socket.ReceiveReady -= Listener; registration?.Dispose(); source.TrySetCanceled(); } socket.ReceiveReady += Listener; return(source.Task); }
public static Task <(byte[], bool)> ReceiveFrameBytesAsync([NotNull] this NetMQSocket socket) { if (NetMQRuntime.Current == null) { throw new InvalidOperationException("NetMQRuntime must be created before calling async functions"); } socket.AttachToRuntime(); var msg = new Msg(); msg.InitEmpty(); if (socket.TryReceive(ref msg, TimeSpan.Zero)) { var data = msg.CloneData(); bool more = msg.HasMore; msg.Close(); return(Task.FromResult((data, more))); } TaskCompletionSource <(byte[], bool)> source = new TaskCompletionSource <(byte[], bool)>(); void Listener(object sender, NetMQSocketEventArgs args) { if (socket.TryReceive(ref msg, TimeSpan.Zero)) { var data = msg.CloneData(); bool more = msg.HasMore; msg.Close(); socket.ReceiveReady -= Listener; source.SetResult((data, more)); } } socket.ReceiveReady += Listener; return(source.Task); }
public static byte[] ReceiveFrameBytes([NotNull] this IReceivingSocket socket, out bool more) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); var data = msg.CloneData(); more = msg.HasMore; msg.Close(); return data; }
/// <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) { var 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); }
public static byte[] ReceiveFrameBytes([NotNull] this IReceivingSocket socket, out bool more) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); var data = msg.CloneData(); more = msg.HasMore; msg.Close(); return(data); }
/// <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="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static bool TryReceiveMultipartBytes([NotNull] this IReceivingSocket socket, TimeSpan timeout, ref List <byte[]> frames, int expectedFrameCount = 4) { var 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 (frames == null) { frames = new List <byte[]>(expectedFrameCount); } else { frames.Clear(); } // Add the frame frames.Add(msg.CloneData()); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); frames.Add(msg.CloneData()); } msg.Close(); return(true); }
public static byte[] Receive([NotNull] this IReceivingSocket socket, SendReceiveOptions options, out bool hasMore) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg, options); var data = msg.CloneData(); hasMore = msg.HasMore; msg.Close(); return data; }
public static NetMQMessage ReceiveMultipartMessage([NotNull] this IReceivingSocket socket, int expectedFrameCount = 4) { var msg = new Msg(); msg.InitEmpty(); var message = new NetMQMessage(expectedFrameCount); do { socket.Receive(ref msg); message.Append(msg.CloneData()); }while (msg.HasMore); msg.Close(); return(message); }
/// <summary> /// Receive a bytes from <paramref name="socket"/>, blocking until one arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> /// <returns>The content of the received message.</returns> /// <exception cref="System.OperationCanceledException">The token has had cancellation requested.</exception> public static byte[] ReceiveBytes(this IThreadSafeInSocket socket, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); var data = msg.CloneData(); return(data); } finally { msg.Close(); } }
public static (uint, byte[]) ReceiveBytes(this IRoutingIdSocket socket, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); var data = msg.CloneData(); var routingId = msg.RoutingId; return(routingId, data); } finally { msg.Close(); } }
/// <summary> /// Attempt to receive a byte-array <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="bytes">The content of the received message, or <c>null</c> if no message was available.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> /// <remarks>The method would return false if cancellation has had requested.</remarks> public static bool TryReceiveBytes(this IThreadSafeInSocket socket, TimeSpan timeout, [NotNullWhen(returnValue: true)] out byte[]?bytes, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); if (!socket.TryReceive(ref msg, timeout, cancellationToken)) { msg.Close(); bytes = null; return(false); } bytes = msg.CloneData(); msg.Close(); return(true); }
/// <summary> /// Receive a bytes from <paramref name="socket"/>, blocking until one arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> /// <returns>Tuple of group and received bytes</returns> /// <exception cref="System.OperationCanceledException">The token has had cancellation requested.</exception> public static (string, byte[]) ReceiveBytes(this IGroupInSocket socket, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); var data = msg.CloneData(); var group = msg.Group; return(group, data); } finally { msg.Close(); } }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>. /// If no message is available within <paramref name="timeout"/>, return <c>false</c>. /// Indicate whether further frames exist via <paramref name="more"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="more"><c>true</c> if another frame of the same message follows, otherwise <c>false</c>.</param> /// <param name="timeout">The maximum period of time to wait for a message to become available.</param> /// <param name="bytes">The content of the received message frame, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameBytes([NotNull] this IReceivingSocket socket, TimeSpan timeout, out byte[] bytes, out bool more) { var msg = new Msg(); msg.InitEmpty(); if (!socket.TryReceive(ref msg, timeout)) { msg.Close(); bytes = null; more = false; return(false); } bytes = msg.CloneData(); more = msg.HasMore; msg.Close(); return(true); }
/// <summary> /// Receive all frames of the next message from <paramref name="socket"/>, blocking until a message arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static void ReceiveMultipartBytes([NotNull] this IReceivingSocket socket, ref List <byte[]> frames, int expectedFrameCount = 4) { if (frames == null) { frames = new List <byte[]>(expectedFrameCount); } else { frames.Clear(); } var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg); frames.Add(msg.CloneData()); }while (msg.HasMore); msg.Close(); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref cref="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="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static bool TryReceiveMultipartBytes([NotNull] this IReceivingSocket socket, TimeSpan timeout, ref List<byte[]> frames, int expectedFrameCount = 4) { var 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 (frames == null) frames = new List<byte[]>(expectedFrameCount); else frames.Clear(); // Add the frame frames.Add(msg.CloneData()); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); frames.Add(msg.CloneData()); } msg.Close(); return true; }
/// <summary> /// Receive all frames of the next message from <paramref cref="socket"/>, blocking until a message arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static void ReceiveMultipartBytes([NotNull] this IReceivingSocket socket, ref List<byte[]> frames, int expectedFrameCount = 4) { if (frames == null) frames = new List<byte[]>(expectedFrameCount); else frames.Clear(); var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg); frames.Add(msg.CloneData()); } while (msg.HasMore); msg.Close(); }
/// <summary> /// Attempt to receive a single frame from <paramref cref="socket"/>. /// If no message is available within <paramref name="timeout"/>, return <c>false</c>. /// Indicate whether further frames exist via <paramref name="more"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="more"><c>true</c> if another frame of the same message follows, otherwise <c>false</c>.</param> /// <param name="timeout">The maximum period of time to wait for a message to become available.</param> /// <param name="bytes">The content of the received message frame, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameBytes([NotNull] this IReceivingSocket socket, TimeSpan timeout, out byte[] bytes, out bool more) { var msg = new Msg(); msg.InitEmpty(); if (!socket.TryReceive(ref msg, timeout)) { msg.Close(); bytes = null; more = false; return false; } bytes = msg.CloneData(); more = msg.HasMore; msg.Close(); return true; }
public static void ReceiveMessage([NotNull] this IReceivingSocket socket, [NotNull] NetMQMessage message, bool dontWait = false) { message.Clear(); var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg, dontWait ? SendReceiveOptions.DontWait : SendReceiveOptions.None); message.Append(msg.CloneData()); } while (msg.HasMore); msg.Close(); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref cref="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) { var 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; }
public static NetMQMessage ReceiveMultipartMessage([NotNull] this IReceivingSocket socket, int expectedFrameCount = 4) { var msg = new Msg(); msg.InitEmpty(); var message = new NetMQMessage(expectedFrameCount); do { socket.Receive(ref msg); message.Append(msg.CloneData()); } while (msg.HasMore); msg.Close(); return message; }