/// <summary> /// Receive a single frame from <paramref name="socket"/>, asynchronously, and decode as a string using <paramref name="encoding"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="encoding">The encoding used to convert the frame's data to a string.</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 as a string and boolean indicate if another frame of the same message follows..</returns> public static Task <(string, bool)> ReceiveFrameStringAsync( this NetMQSocket socket, Encoding encoding, 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 str = msg.Size > 0 ? msg.GetString(encoding) : string.Empty; msg.Close(); return(Task.FromResult((str, msg.HasMore))); } TaskCompletionSource <(string, bool)> source = new TaskCompletionSource <(string, bool)>(); cancellationToken.Register(() => source.SetCanceled()); void Listener(object sender, NetMQSocketEventArgs args) { if (socket.TryReceive(ref msg, TimeSpan.Zero)) { var str = msg.Size > 0 ? msg.GetString(encoding) : string.Empty; bool more = msg.HasMore; msg.Close(); socket.ReceiveReady -= Listener; source.SetResult((str, more)); } } socket.ReceiveReady += Listener; return(source.Task); }
public static bool TryReceiveString(this IRoutingIdSocket socket, TimeSpan timeout, Encoding encoding, out uint routingId, [NotNullWhen(returnValue: true)] out string?str, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); if (socket.TryReceive(ref msg, timeout, cancellationToken)) { routingId = msg.RoutingId; try { str = msg.Size > 0 ? msg.GetString(encoding) : string.Empty; return(true); } finally { msg.Close(); } } str = null; routingId = 0; msg.Close(); return(false); }
/// <summary> /// Receive a string from <paramref name="socket"/>, blocking until one arrives, and decode using <paramref name="encoding"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="encoding">The encoding used to convert the data to a string.</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 string ReceiveString(this IThreadSafeInSocket socket, Encoding encoding, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); return(msg.Size > 0 ? msg.GetString(encoding) : string.Empty); } finally { msg.Close(); } }
public static (uint, string) ReceiveString(this IRoutingIdSocket socket, Encoding encoding, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); var routingId = msg.RoutingId; var str = msg.Size > 0 ? msg.GetString(encoding) : string.Empty; return(routingId, str); } finally { msg.Close(); } }
/// <summary> /// Receive a string from <paramref name="socket"/>, blocking until one arrives, and decode using <paramref name="encoding"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="encoding">The encoding used to convert the data to a string.</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 the content of the received message as a string.</returns> /// <exception cref="System.OperationCanceledException">The token has had cancellation requested.</exception> public static (string, string) ReceiveString(this IGroupInSocket socket, Encoding encoding, CancellationToken cancellationToken = default) { var msg = new Msg(); msg.InitEmpty(); try { socket.Receive(ref msg, cancellationToken); var group = msg.Group; var str = msg.Size > 0 ? msg.GetString(encoding) : string.Empty; return(group, str); } finally { msg.Close(); } }