private static async Task BidirectionalStreamingExample(Racer.RacerClient client) { var headers = new Metadata { new Metadata.Entry("race-duration", RaceDuration.ToString()) }; Console.WriteLine("Ready, set ,go!"); using (var call = client.ReadySetGo(new CallOptions(headers))) { RaceMessage?lastMessageReceived = null; var readTask = Task.Run(async() => { await foreach (var message in call.ResponseStream.ReadAllAsync()) { lastMessageReceived = message; } }); var sw = Stopwatch.StartNew(); var sent = 0; while (sw.Elapsed < RaceDuration) { await call.RequestStream.WriteAsync(new RaceMessage { Count = ++sent }); } await call.RequestStream.CompleteAsync(); await readTask; Console.WriteLine($"Messages sent: {sent}"); Console.WriteLine($"Messages received: {lastMessageReceived?.Count ?? 0}"); } }
private static async Task BidirectionalStreamingExample(Racer.RacerClient client) { var headers = new Metadata { new Metadata.Entry("race-duration", RaceDuration.ToString()) }; Console.WriteLine("Ready, set, go!"); using var call = client.ReadySetGo(new CallOptions(headers)); var complete = false; // Read incoming messages in a background task RaceMessage?lastMessageReceived = null; var readTask = Task.Run(async() => { await foreach (var message in call.ResponseStream.ReadAllAsync()) { lastMessageReceived = message; } }); // Write outgoing messages until timer is complete var sw = Stopwatch.StartNew(); var sent = 0; #region Reporting // Report requests in realtime var reportTask = Task.Run(async() => { while (true) { Console.WriteLine($"Messages sent: {sent}"); Console.WriteLine($"Messages received: {lastMessageReceived?.Count ?? 0}"); if (!complete) { await Task.Delay(TimeSpan.FromSeconds(1)); Console.SetCursorPosition(0, Console.CursorTop - 2); } else { break; } } }); #endregion while (sw.Elapsed < RaceDuration) { await call.RequestStream.WriteAsync(new RaceMessage { Count = ++sent }); } // Finish call and report results await call.RequestStream.CompleteAsync(); await readTask; complete = true; await reportTask; }
static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Racer.RacerClient(channel); Console.WriteLine("Press any key to start race..."); Console.ReadKey(); await BidirectionalStreamingExample(client); Console.WriteLine("Finished"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public async Task DuplexStream_SimultaneousSendAndReceive_Success() { var client = new Racer.RacerClient(Channel); TimeSpan raceDuration = TimeSpan.FromSeconds(1); var headers = new Metadata { new Metadata.Entry("race-duration", raceDuration.ToString()) }; using (var call = client.ReadySetGo(new CallOptions(headers))) { // Read incoming messages in a background task RaceMessage?lastMessageReceived = null; var readTask = Task.Run(async() => { while (await call.ResponseStream.MoveNext().DefaultTimeout()) { lastMessageReceived = call.ResponseStream.Current; } }); // Write outgoing messages until timer is complete var sw = Stopwatch.StartNew(); var sent = 0; while (sw.Elapsed < raceDuration) { await call.RequestStream.WriteAsync(new RaceMessage { Count = ++sent }).DefaultTimeout(); } // Finish call and report results await call.RequestStream.CompleteAsync().DefaultTimeout(); await readTask.DefaultTimeout(); Assert.Greater(sent, 0); Assert.Greater(lastMessageReceived?.Count ?? 0, 0); } }