Пример #1
0
        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);
        }
Пример #2
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 <(uint, string)> ReceiveStringAsyncEnumerable(
     this IRoutingIdSocket socket,
     [EnumeratorCancellation] CancellationToken cancellationToken = default)
 {
     while (true)
     {
         yield return(await socket.ReceiveStringAsync(cancellationToken));
     }
 }
Пример #3
0
        public static void Send(this IRoutingIdSocket socket, uint routingId, byte[] data, int length)
        {
            var msg = new Msg();

            msg.InitPool(length);
            msg.RoutingId = routingId;
            data.Slice(0, length).CopyTo(msg);
            socket.Send(ref msg);
            msg.Close();
        }
Пример #4
0
        public static ValueTask SendAsync(this IRoutingIdSocket socket, uint routingId, string message)
        {
            if (socket.TrySend(routingId, message))
            {
                return(new ValueTask());
            }

            return(new ValueTask(Task.Factory.StartNew(() => Send(socket, routingId, message),
                                                       TaskCreationOptions.LongRunning)));
        }
Пример #5
0
        public static ValueTask SendAsync(this IRoutingIdSocket socket, uint routingId, byte[] data, int length)
        {
            if (socket.TrySend(routingId, data, length))
            {
                return(new ValueTask());
            }

            return(new ValueTask(Task.Factory.StartNew(() => Send(socket, routingId, data, length),
                                                       TaskCreationOptions.LongRunning)));
        }
Пример #6
0
        public static ValueTask <(uint, byte[])> ReceiveBytesAsync(this IRoutingIdSocket socket, CancellationToken cancellationToken = default)
        {
            if (TryReceiveBytes(socket, out var routingId, out var bytes))
            {
                return(new ValueTask <(uint, byte[])>((routingId, bytes)));
            }

            // TODO: this is a hack, eventually we need kind of IO ThreadPool for thread-safe socket to wait on asynchronously
            return(new ValueTask <(uint, byte[])>(Task.Factory.StartNew(() => socket.ReceiveBytes(cancellationToken),
                                                                        cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default)));
        }
Пример #7
0
        public static bool TrySend(this IRoutingIdSocket socket, TimeSpan timeout, uint routingId, byte[] data, int length)
        {
            var msg = new Msg();

            msg.InitPool(length);
            msg.RoutingId = routingId;
            data.CopyTo(msg);
            if (!socket.TrySend(ref msg, timeout))
            {
                msg.Close();
                return(false);
            }

            msg.Close();
            return(true);
        }
Пример #8
0
        public static void Send(this IRoutingIdSocket socket, uint routingId, string message)
        {
            var msg = new Msg();

            // Count the number of bytes required to encode the string.
            // Note that non-ASCII strings may not have an equal number of characters
            // and bytes. The encoding must be queried for this answer.
            // With this number, request a buffer from the pool.
            msg.InitPool(SendReceiveConstants.DefaultEncoding.GetByteCount(message));
            msg.RoutingId = routingId;

            // Encode the string into the buffer
            SendReceiveConstants.DefaultEncoding.GetBytes(message, msg);

            socket.Send(ref msg);
            msg.Close();
        }
Пример #9
0
        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();
            }
        }
Пример #10
0
        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();
            }
        }
Пример #11
0
        public static bool TryReceiveBytes(this IRoutingIdSocket socket, TimeSpan timeout, out uint routingId,
                                           [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;
                routingId = 0;
                return(false);
            }

            bytes     = msg.CloneData();
            routingId = msg.RoutingId;

            msg.Close();
            return(true);
        }
Пример #12
0
 public static bool TrySend(this IRoutingIdSocket socket, TimeSpan timeout, uint routingId, byte[] data)
 {
     return(TrySend(socket, timeout, routingId, data, data.Length));
 }
Пример #13
0
 public static void Send(this IRoutingIdSocket socket, uint routingId, byte[] data)
 {
     Send(socket, routingId, data, data.Length);
 }
Пример #14
0
 public static bool TrySend(this IRoutingIdSocket socket, uint routingId, string message)
 {
     return(TrySend(socket, TimeSpan.Zero, routingId, message));
 }
Пример #15
0
 public static bool TryReceiveString(this IRoutingIdSocket socket, TimeSpan timeout,
                                     out uint routingId, [NotNullWhen(returnValue: true)] out string?str,
                                     CancellationToken cancellationToken = default)
 {
     return(socket.TryReceiveString(timeout, SendReceiveConstants.DefaultEncoding, out routingId, out str, cancellationToken));
 }
Пример #16
0
 public static bool TryReceiveString(this IRoutingIdSocket socket, Encoding encoding, out uint routingId,
                                     [NotNullWhen(returnValue: true)] out string?str)
 {
     return(socket.TryReceiveString(TimeSpan.Zero, encoding, out routingId, out str));
 }
Пример #17
0
 public static bool TryReceiveBytes(this IRoutingIdSocket socket, out uint routingId, [NotNullWhen(returnValue: true)] out byte[]?bytes)
 {
     return(socket.TryReceiveBytes(TimeSpan.Zero, out routingId, out bytes));
 }
Пример #18
0
 public static (uint, string) ReceiveString(this IRoutingIdSocket socket, CancellationToken cancellationToken = default)
 {
     return(socket.ReceiveString(SendReceiveConstants.DefaultEncoding, cancellationToken));
 }
Пример #19
0
 public static bool TrySend(this IRoutingIdSocket socket, uint routingId, byte[] data, int length)
 {
     return(TrySend(socket, TimeSpan.Zero, routingId, data, length));
 }