コード例 #1
0
ファイル: MultiClient.cs プロジェクト: hiksa/nkn-sdk-net
        private static async Task <SendMessageResponse <T> > HandleSendMessageTasksAsync <T>(
            IEnumerable <Task <byte[]> > resultTasks,
            SendOptions options,
            Channel <T> responseChannel,
            Channel <T> timeoutChannel)
        {
            var messageId = await await Task.WhenAny(resultTasks);

            var response = new SendMessageResponse <T> {
                MessageId = messageId
            };

            if (options.NoReply == true || messageId == null)
            {
                responseChannel.Writer.TryComplete();
                timeoutChannel.Writer.TryComplete();

                return(response);
            }

            var cts          = new CancellationTokenSource();
            var channelTasks = new List <Task <Channel <T> > >
            {
                responseChannel.WaitToRead(cts.Token),
                timeoutChannel.WaitToRead(cts.Token)
            };

            var channel = await channelTasks.FirstAsync(cts);

            if (channel == timeoutChannel)
            {
                response.ErrorMessage = $"A response was not returned in specified time: {options.ResponseTimeout} ms. Request timed out.";
                return(response);
            }

            var result = await responseChannel.Reader.ReadAsync().AsTask();

            response.Result = result;
            return(response);
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: MutsiMutsi/nkn-sdk-net
        private static SendMessageResponse <T> HandleSendResultTaskAsync <T>(
            byte[] resultTask,
            SendOptions options,
            Channel <T> responseChannel,
            Channel <T> timeoutChannel)
        {
            var messageId = resultTask;

            var response = new SendMessageResponse <T> {
                MessageId = messageId
            };

            if (options.NoReply == true)
            {
                return(response);
            }

            var cts          = new CancellationTokenSource();
            var channelTasks = new List <Task <Channel <T> > >
            {
                responseChannel.WaitToRead(cts.Token),
                timeoutChannel.WaitToRead(cts.Token)
            };

            var channel = channelTasks.FirstAsync(cts);

            if (channel == timeoutChannel)
            {
                response.ErrorMessage = $"A response was not returned in specified time: {options.ResponseTimeout} ms. Request timed out.";

                return(response);
            }

            var result = responseChannel.Reader.ReadAsync().GetAwaiter().GetResult();

            response.Result = result;

            return(response);
        }