private static async Task ExecuteCalculatorClientOperations(CancellationToken cancellationToken, Calculator.Client client) { await client.OpenTransportAsync(cancellationToken); // Async version Logger.LogInformation($"{client.ClientId} PingAsync()"); await client.pingAsync(cancellationToken); Logger.LogInformation($"{client.ClientId} AddAsync(1,1)"); var sum = await client.addAsync(1, 1, cancellationToken); Logger.LogInformation($"{client.ClientId} AddAsync(1,1)={sum}"); var work = new Work { Op = Operation.DIVIDE, Num1 = 1, Num2 = 0 }; try { Logger.LogInformation($"{client.ClientId} CalculateAsync(1)"); await client.calculateAsync(1, work, cancellationToken); Logger.LogInformation($"{client.ClientId} Whoa we can divide by 0"); } catch (InvalidOperation io) { Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); } work.Op = Operation.SUBTRACT; work.Num1 = 15; work.Num2 = 10; try { Logger.LogInformation($"{client.ClientId} CalculateAsync(1)"); var diff = await client.calculateAsync(1, work, cancellationToken); Logger.LogInformation($"{client.ClientId} 15-10={diff}"); } catch (InvalidOperation io) { Logger.LogInformation($"{client.ClientId} Invalid operation: " + io); } Logger.LogInformation($"{client.ClientId} GetStructAsync(1)"); var log = await client.getStructAsync(1, cancellationToken); Logger.LogInformation($"{client.ClientId} Check log: {log.Value}"); Logger.LogInformation($"{client.ClientId} ZipAsync() with delay 100mc on server side"); await client.zipAsync(cancellationToken); }
static async Task Run() { try { var transport = new TSocket("localhost", 9090, 100000); TBufferedTransport transportBuff = new TBufferedTransport(transport, 2048); TProtocol protocol = new TBinaryProtocol(transportBuff); transport.Open(); Calculator.Client client = new Calculator.Client(protocol); Console.WriteLine($"run Client host:{transport.Host},port:{transport.Port}"); await client.pingAsync(); int testCount = 100000; var sw = Stopwatch.StartNew(); foreach (var m in Enumerable.Range(0, testCount)) { int sum = client.add(1, 1); } sw.Stop(); Console.WriteLine($"TBufferedTransport Execute client.add(1, 1) do:{testCount} ms:{sw.ElapsedMilliseconds}"); sw.Restart(); foreach (var m in Enumerable.Range(0, testCount)) { int sum = await client.addAsync(1, 1); } sw.Stop(); Console.WriteLine($"TBufferedTransport Execute client.addAsync(1, 1) do:{testCount} ms:{sw.ElapsedMilliseconds}"); await client.calculateAsync(1, new Work() { Op = Operation.ADD, Comment = "add Comment" }); //InvalidOperation transport.Close(); } catch (InvalidOperation ex) { Console.WriteLine($"InvalidOperation why:{ex.Why} op:{ex.WhatOp} ex:{ex.GetType()} message:{ex.Message} {ex.StackTrace}"); } catch (TApplicationException x) { Console.WriteLine($"TApplicationException ex:{x.Type} message:{x.Message} {x.StackTrace}"); } }
private static async Task RunClientAsync(TProtocol protocol, CancellationToken cancellationToken) { try { var client = new Calculator.Client(protocol); await client.OpenTransportAsync(cancellationToken); try { // Async version Logger.LogInformation("PingAsync()"); await client.pingAsync(cancellationToken); Logger.LogInformation("AddAsync(1,1)"); var sum = await client.addAsync(1, 1, cancellationToken); Logger.LogInformation($"AddAsync(1,1)={sum}"); var work = new Work { Op = Operation.DIVIDE, Num1 = 1, Num2 = 0 }; try { Logger.LogInformation("CalculateAsync(1)"); await client.calculateAsync(1, work, cancellationToken); Logger.LogInformation("Whoa we can divide by 0"); } catch (InvalidOperation io) { Logger.LogInformation("Invalid operation: " + io); } work.Op = Operation.SUBTRACT; work.Num1 = 15; work.Num2 = 10; try { Logger.LogInformation("CalculateAsync(1)"); var diff = await client.calculateAsync(1, work, cancellationToken); Logger.LogInformation($"15-10={diff}"); } catch (InvalidOperation io) { Logger.LogInformation("Invalid operation: " + io); } Logger.LogInformation("GetStructAsync(1)"); var log = await client.getStructAsync(1, cancellationToken); Logger.LogInformation($"Check log: {log.Value}"); Logger.LogInformation("ZipAsync() with delay 100mc on server side"); await client.zipAsync(cancellationToken); } catch (Exception ex) { Logger.LogError(ex.ToString()); } finally { protocol.Transport.Close(); } } catch (TApplicationException x) { Logger.LogError(x.ToString()); } }