/// <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(); } }
/// <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(); } }