public static Task <NetMQMessage> ReceiveMultipartMessageAsync(
            this NetMQSocket socket,
            TimeSpan?timeout = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var cts = new CancellationTokenSource();

            if (timeout is TimeSpan timeoutNotNull)
            {
                cts.CancelAfter(timeoutNotNull);
            }

            var ct = CancellationTokenSource.CreateLinkedTokenSource(
                cancellationToken,
                cts.Token
                );

            return(socket.ReceiveMultipartMessageAsync(
                       expectedFrameCount: 4,
                       cancellationToken: ct.Token
                       ).ContinueWith(t =>
            {
                if (t.IsCanceled && cts.IsCancellationRequested)
                {
                    throw new TimeoutException(
                        $"The operation exceeded the specified time: {timeout}."
                        );
                }

                cts.Dispose();
                ct.Dispose();
                return t.Result;
            }));
        }
Пример #2
0
        public static async Task <NetMQMessage> ReceiveMultipartMessageAsync(
            this NetMQSocket socket,
            TimeSpan?timeout = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var cts = new CancellationTokenSource();

            if (timeout is TimeSpan timeoutNotNull)
            {
                cts.CancelAfter(timeoutNotNull);
            }

            var ct = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);

            try
            {
                return(await socket.ReceiveMultipartMessageAsync(
                           expectedFrameCount : 4,
                           cancellationToken : ct.Token));
            }
            catch (TaskCanceledException)
            {
                if (cts.Token.IsCancellationRequested)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time: {timeout}."
                              );
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                cts.Dispose();
                ct.Dispose();
            }
        }