コード例 #1
0
        /// <summary>
        ///     Begin writing to a network stream
        /// </summary>
        private Task WriteAsync <T, U>(PipeTaskState <T> state, PipeRequest <U> request, CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                }

                if (!_pipeClient.IsConnected)
                {
                    throw new IOException("Unable to send message, no connection established.");
                }

                if (_pipeClient.CanWrite)
                {
                    return(_pipeClient.WriteAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)), cancellationToken).AsTask());
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"{nameof(WriteAsync)}-{ex.Message}");
                state.Response = PipeResponse <T> .SetFail(ex.Message);
            }

            return(Task.CompletedTask);
        }
コード例 #2
0
        /// <summary>
        ///     Begin reading from a network stream
        /// </summary>
        private Task ReadAsync <T>(PipeTaskState <T> state, CancellationToken cancellationToken)
        {
            if (state.Response != null && state.Response.IsSuccess == false)
            {
                return(Task.CompletedTask);
            }

            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                }

                if (!_pipeClient.IsConnected)
                {
                    throw new IOException("Unable to recieve message, no connection established.");
                }

                if (_pipeClient.CanRead)
                {
                    return(_pipeClient.ReadAsync(_memoryOwner.Memory, cancellationToken).AsTask());
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"{nameof(ReadAsync)}-{ex.Message}");
                state.Response = PipeResponse <T> .SetFail(ex.Message);
            }

            return(Task.CompletedTask);
        }
コード例 #3
0
 /// <summary>
 ///     Wait for a pipe to drain before another write/read
 /// </summary>
 private void WaitForPipeDrain <T>(PipeTaskState <T> state)
 {
     try
     {
         _pipeClient.WaitForPipeDrain();
     }
     catch (Exception ex)
     {
         _logger.Error(ex.Message);
         state.Response = PipeResponse <T> .SetFail(ex.Message);
     }
 }
コード例 #4
0
        /// <summary>
        ///     Send message to server and return the response
        /// </summary>
        public async Task <PipeResponse <T> > SendMessage <T, U>(PipeRequest <U> request, CancellationToken cancellationToken)
        {
            var state = new PipeTaskState <T>();

            await WriteAsync <T, U>(state, request, cancellationToken);

            WaitForPipeDrain <T>(state);

            await ReadAsync <T>(state, cancellationToken);

            ConvertResponse <T>(state);

            return(state.Response);
        }
コード例 #5
0
        /// <summary>
        ///     Convert response from
        /// </summary>
        private void ConvertResponse <T>(PipeTaskState <T> state)
        {
            if (state.Response != null && state.Response.IsSuccess == false)
            {
                return;
            }

            try
            {
                var json = Encoding.UTF8.GetString(_memoryOwner.Memory.Span).TrimEnd('\0');

                if (string.IsNullOrWhiteSpace(json))
                {
                    throw new IOException("Response from server is empty.");
                }

                state.Response = JsonConvert.DeserializeObject <PipeResponse <T> >(json);
            }
            catch (Exception ex)
            {
                _logger.Error($"{nameof(ReadAsync)}-{ex.Message}");
                state.Response = PipeResponse <T> .SetFail(ex.Message);
            }
        }