예제 #1
0
        /// <summary>
        /// Attempt to receive a string from <paramref name="socket"/>, and decode using <paramref name="encoding"/>.
        /// 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="encoding">The encoding used to convert the data to a string.</param>
        /// <param name="group">The message group</param>
        /// <param name="str">The content of the received message as a string, 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 TryReceiveString(this IGroupInSocket socket, TimeSpan timeout,
                                            Encoding encoding,
                                            [NotNullWhen(returnValue: true)] out string?group,
                                            [NotNullWhen(returnValue: true)] out string?str,
                                            CancellationToken cancellationToken = default)
        {
            var msg = new Msg();

            msg.InitEmpty();

            if (socket.TryReceive(ref msg, timeout, cancellationToken))
            {
                group = msg.Group;

                try
                {
                    str = msg.Size > 0
                        ? msg.GetString(encoding)
                        : string.Empty;
                    return(true);
                }
                finally
                {
                    msg.Close();
                }
            }

            str   = null;
            group = null;
            msg.Close();
            return(false);
        }
예제 #2
0
 /// <summary>
 /// Attempt to receive a string from <paramref name="socket"/>, and decode using <see cref="SendReceiveConstants.DefaultEncoding"/>.
 /// 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="group">The message group</param>
 /// <param name="str">The content of the received message as a string, 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 TryReceiveString(this IGroupInSocket socket, TimeSpan timeout,
                                     [NotNullWhen(returnValue: true)] out string?group,
                                     [NotNullWhen(returnValue: true)] out string?str,
                                     CancellationToken cancellationToken = default)
 {
     return(socket.TryReceiveString(timeout, SendReceiveConstants.DefaultEncoding, out group, out str,
                                    cancellationToken));
 }
예제 #3
0
 /// <summary>
 /// Provides a consuming IAsyncEnumerable for receiving messages from the socket.
 /// </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>An IAsyncEnumerable that receive and returns messages from the socket.</returns>
 /// <exception cref="System.OperationCanceledException">The token has had cancellation requested.</exception>
 public static async IAsyncEnumerable <(string, string)> ReceiveStringAsyncEnumerable(
     this IGroupInSocket socket,
     [EnumeratorCancellation] CancellationToken cancellationToken = default)
 {
     while (true)
     {
         yield return(await socket.ReceiveStringAsync(cancellationToken));
     }
 }
예제 #4
0
        /// <summary>
        /// Receive a string from <paramref name="socket"/> asynchronously.
        /// </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 a string</returns>
        /// <exception cref="System.OperationCanceledException">The token has had cancellation requested.</exception>
        public static ValueTask <(string, string)> ReceiveStringAsync(this IGroupInSocket socket,
                                                                      CancellationToken cancellationToken = default)
        {
            if (TryReceiveString(socket, out var group, out var msg))
            {
                return(new ValueTask <(string, string)>((group, msg)));
            }

            // TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
            return(new ValueTask <(string, string)>(Task.Factory.StartNew(() => socket.ReceiveString(cancellationToken),
                                                                          cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default)));
        }
예제 #5
0
        /// <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();
            }
        }
예제 #6
0
        /// <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();
            }
        }
예제 #7
0
        /// <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="group">The message group.</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 IGroupInSocket socket, TimeSpan timeout,
                                           [NotNullWhen(returnValue: true)] out string?group,
                                           [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;
                group = null;
                return(false);
            }

            bytes = msg.CloneData();
            group = msg.Group;

            msg.Close();
            return(true);
        }
예제 #8
0
 /// <summary>
 /// Attempt to receive a string from <paramref name="socket"/>, and decode using <paramref name="encoding"/>.
 /// If no message is immediately available, return <c>false</c>.
 /// </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="group">The message group.</param>
 /// <param name="str">The content of the received message as a string, 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 TryReceiveString(this IGroupInSocket socket, Encoding encoding,
                                     [NotNullWhen(returnValue: true)] out string?group,
                                     [NotNullWhen(returnValue: true)] out string?str)
 {
     return(socket.TryReceiveString(TimeSpan.Zero, encoding, out group, out str));
 }
예제 #9
0
 /// <summary>
 /// Receive a string from <paramref name="socket"/>, blocking until one arrives, and decode using <see cref="SendReceiveConstants.DefaultEncoding"/>.
 /// </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 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,
                                              CancellationToken cancellationToken = default)
 {
     return(socket.ReceiveString(SendReceiveConstants.DefaultEncoding, cancellationToken));
 }
예제 #10
0
 /// <summary>
 /// Attempt to receive a byte-array <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="group">The message group</param>
 /// <param name="bytes">The content of the received message, 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 TryReceiveBytes(this IGroupInSocket socket,
                                    [NotNullWhen(returnValue: true)] out string?group, [NotNullWhen(returnValue: true)] out byte[]?bytes)
 {
     return(socket.TryReceiveBytes(TimeSpan.Zero, out group, out bytes));
 }