コード例 #1
0
        public override async Task ServerStream(ServerStreamRequest request, IServerStreamWriter <ServerStreamResponse> responseStream, ServerCallContext context)
        {
            var responseDelay = TimeSpan.FromMilliseconds(100);

            _logger.LogInformation("=> ServerStream({})", request.Message);
            while (!context.CancellationToken.IsCancellationRequested)
            {
                try
                {
                    var ticks = Stopwatch.GetTimestamp();

                    await responseStream.WriteAsync(new ServerStreamResponse {
                        Message   = $"Message #{ticks}",
                        Payload   = Payload,
                        Timestamp = ticks
                    });

                    var elapsedTicks = Stopwatch.GetTimestamp() - ticks;
                    var elapsed      = elapsedTicks > 0 ? TimeSpan.FromTicks(elapsedTicks) : TimeSpan.Zero;
                    if (responseDelay > elapsed)
                    {
                        await Task.Delay(responseDelay.Subtract(elapsed), context.CancellationToken);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Unexpected error");
                }
            }
        }
コード例 #2
0
        private async Task ServerStreamWorkerAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    _logger.LogInformation("<= ServerStream()");
                    var request = new ServerStreamRequest {
                        Message = "Please start stream"
                    };
                    using (var responseStream = _myStackServerClient.ServerStream(request, cancellationToken: cancellationToken))
                    {
                        while (await responseStream.ResponseStream.MoveNext(cancellationToken))
                        {
                            var response      = responseStream.ResponseStream.Current;
                            var payloadSizeKb = response.Payload.Length / 1024;
                            var elapsed       = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - response.Timestamp);

                            await Console.Out.WriteLineAsync($"=> {payloadSizeKb} KB in {elapsed.TotalMilliseconds}");
                        }
                        _logger.LogInformation("No more data from server");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Unexpected error");
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="responseStream"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task ServerStream(ServerStreamRequest request, IServerStreamWriter <ServerStreamResponse> responseStream, ServerCallContext context)
        {
            using var fs = File.Open(request.FilePath, FileMode.Open);
            var leftSize = fs.Length;
            var buff     = new byte[1048576]; // 1M 分批返回

            while (leftSize > 0)
            {
                var len = await fs.ReadAsync(buff);

                leftSize -= len;
                Console.WriteLine($"response {len} bytes");
                await responseStream.WriteAsync(new ServerStreamResponse
                {
                    Data = ByteString.CopyFrom(buff, 0, len)
                });
            }
        }