summaryrefslogtreecommitdiff
path: root/tutorial/netstd/Client/Program.cs
diff options
context:
space:
mode:
Diffstat (limited to 'tutorial/netstd/Client/Program.cs')
-rw-r--r--tutorial/netstd/Client/Program.cs221
1 files changed, 108 insertions, 113 deletions
diff --git a/tutorial/netstd/Client/Program.cs b/tutorial/netstd/Client/Program.cs
index abbba702e..f3e93ceb0 100644
--- a/tutorial/netstd/Client/Program.cs
+++ b/tutorial/netstd/Client/Program.cs
@@ -34,11 +34,14 @@ using Thrift.Transport.Client;
using tutorial;
using shared;
+#pragma warning disable IDE0063 // using
+#pragma warning disable IDE0057 // substr
+
namespace Client
{
public class Program
{
- private static ServiceCollection ServiceCollection = new ServiceCollection();
+ private static readonly ServiceCollection ServiceCollection = new ServiceCollection();
private static ILogger Logger;
private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed
@@ -49,26 +52,27 @@ Usage:
Client -help
will diplay help information
- Client -tr:<transport> -bf:<buffering> -pr:<protocol> -mc:<numClients>
+ Client -tr:<transport> -bf:<buffering> -pr:<protocol> [-mc:<numClients>] [-multiplex]
will run client with specified arguments (tcp transport and binary protocol by default) and with 1 client
Options:
-tr (transport):
- tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
- namedpipe - namedpipe transport will be used (pipe address - "".test"")
- http - http transport will be used (address - ""http://localhost:9090"")
- tcptls - tcp tls transport will be used (host - ""localhost"", port - 9090)
+ tcp - (default) tcp transport (localhost:9090)
+ tcptls - tcp tls transport (localhost:9090)
+ namedpipe - namedpipe transport (pipe "".test"")
+ http - http transport (http://localhost:9090)
-bf (buffering):
- none - (default) no buffering will be used
- buffered - buffered transport will be used
- framed - framed transport will be used
+ none - (default) no buffering
+ buffered - buffered transport
+ framed - framed transport
-pr (protocol):
- binary - (default) binary protocol will be used
- compact - compact protocol will be used
- json - json protocol will be used
- multiplexed - multiplexed protocol will be used
+ binary - (default) binary protocol
+ compact - compact protocol
+ json - json protocol
+
+ -multiplex - adds multiplexed protocol
-mc (multiple clients):
<numClients> - number of multiple clients to connect to server (max 100, default 1)
@@ -80,7 +84,7 @@ Sample:
public static void Main(string[] args)
{
- args = args ?? new string[0];
+ args ??= Array.Empty<string>();
ServiceCollection.AddLogging(logging => ConfigureLogging(logging));
using (var serviceProvider = ServiceCollection.BuildServiceProvider())
@@ -115,43 +119,77 @@ Sample:
Logger.LogInformation($"Selected # of clients: {numClients}");
- var transports = new TTransport[numClients];
- for (int i = 0; i < numClients; i++)
- {
- var t = GetTransport(args);
- transports[i] = t;
- }
-
- Logger.LogInformation($"Selected client transport: {transports[0]}");
+ var transport = GetTransport(args);
+ Logger.LogInformation($"Selected client transport: {transport}");
- var protocols = new Tuple<Protocol, TProtocol>[numClients];
- for (int i = 0; i < numClients; i++)
- {
- var p = GetProtocol(args, transports[i]);
- protocols[i] = p;
- }
+ var protocol = MakeProtocol( args, MakeTransport(args));
+ Logger.LogInformation($"Selected client protocol: {GetProtocol(args)}");
- Logger.LogInformation($"Selected client protocol: {protocols[0].Item1}");
+ var mplex = GetMultiplex(args);
+ Logger.LogInformation("Multiplex " + (mplex ? "yes" : "no"));
var tasks = new Task[numClients];
for (int i = 0; i < numClients; i++)
{
- var task = RunClientAsync(protocols[i], cancellationToken);
+ var task = RunClientAsync(protocol, mplex, cancellationToken);
tasks[i] = task;
}
- Task.WaitAll(tasks);
-
+ Task.WaitAll(tasks,cancellationToken);
await Task.CompletedTask;
}
- private static TTransport GetTransport(string[] args)
+ private static bool GetMultiplex(string[] args)
{
- TTransport transport = new TSocketTransport(IPAddress.Loopback, 9090, Configuration);
+ var mplex = args.FirstOrDefault(x => x.StartsWith("-multiplex"));
+ return !string.IsNullOrEmpty(mplex);
+ }
+ private static Protocol GetProtocol(string[] args)
+ {
+ var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':')?[1];
+ if (string.IsNullOrEmpty(protocol))
+ return Protocol.Binary;
+
+ protocol = protocol.Substring(0, 1).ToUpperInvariant() + protocol.Substring(1).ToLowerInvariant();
+ if (Enum.TryParse(protocol, true, out Protocol selectedProtocol))
+ return selectedProtocol;
+ else
+ return Protocol.Binary;
+ }
+
+ private static Buffering GetBuffering(string[] args)
+ {
+ var buffering = args.FirstOrDefault(x => x.StartsWith("-bf"))?.Split(":")?[1];
+ if (string.IsNullOrEmpty(buffering))
+ return Buffering.None;
+
+ buffering = buffering.Substring(0, 1).ToUpperInvariant() + buffering.Substring(1).ToLowerInvariant();
+ if (Enum.TryParse<Buffering>(buffering, out var selectedBuffering))
+ return selectedBuffering;
+ else
+ return Buffering.None;
+ }
+
+ private static Transport GetTransport(string[] args)
+ {
+ var transport = args.FirstOrDefault(x => x.StartsWith("-tr"))?.Split(':')?[1];
+ if (string.IsNullOrEmpty(transport))
+ return Transport.Tcp;
+
+ transport = transport.Substring(0, 1).ToUpperInvariant() + transport.Substring(1).ToLowerInvariant();
+ if (Enum.TryParse(transport, true, out Transport selectedTransport))
+ return selectedTransport;
+ else
+ return Transport.Tcp;
+ }
+
+
+ private static TTransport MakeTransport(string[] args)
+ {
// construct endpoint transport
- var transportArg = args.FirstOrDefault(x => x.StartsWith("-tr"))?.Split(':')?[1];
- if (Enum.TryParse(transportArg, true, out Transport selectedTransport))
+ TTransport transport = null;
+ Transport selectedTransport = GetTransport(args);
{
switch (selectedTransport)
{
@@ -179,23 +217,20 @@ Sample:
}
// optionally add layered transport(s)
- var bufferingArg = args.FirstOrDefault(x => x.StartsWith("-bf"))?.Split(':')?[1];
- if (Enum.TryParse<Buffering>(bufferingArg, out var selectedBuffering))
+ Buffering selectedBuffering = GetBuffering(args);
+ switch (selectedBuffering)
{
- switch (selectedBuffering)
- {
- case Buffering.Buffered:
- transport = new TBufferedTransport(transport);
- break;
+ case Buffering.Buffered:
+ transport = new TBufferedTransport(transport);
+ break;
- case Buffering.Framed:
- transport = new TFramedTransport(transport);
- break;
+ case Buffering.Framed:
+ transport = new TFramedTransport(transport);
+ break;
- default: // layered transport(s) are optional
- Debug.Assert(selectedBuffering == Buffering.None, "unhandled case");
- break;
- }
+ default: // layered transport(s) are optional
+ Debug.Assert(selectedBuffering == Buffering.None, "unhandled case");
+ break;
}
return transport;
@@ -207,11 +242,10 @@ Sample:
Logger.LogInformation($"Selected # of clients: {numClients}");
- int c;
- if( int.TryParse(numClients, out c) && (0 < c) && (c <= 100))
- return c;
- else
- return 1;
+ if (int.TryParse(numClients, out int c) && (0 < c) && (c <= 100))
+ return c;
+ else
+ return 1;
}
private static X509Certificate2 GetCertificate()
@@ -250,65 +284,37 @@ Sample:
return true;
}
- private static Tuple<Protocol, TProtocol> GetProtocol(string[] args, TTransport transport)
+ private static TProtocol MakeProtocol(string[] args, TTransport transport)
{
- var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':')?[1];
-
- Protocol selectedProtocol;
- if (Enum.TryParse(protocol, true, out selectedProtocol))
+ Protocol selectedProtocol = GetProtocol(args);
+ switch (selectedProtocol)
{
- switch (selectedProtocol)
- {
- case Protocol.Binary:
- return new Tuple<Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport));
- case Protocol.Compact:
- return new Tuple<Protocol, TProtocol>(selectedProtocol, new TCompactProtocol(transport));
- case Protocol.Json:
- return new Tuple<Protocol, TProtocol>(selectedProtocol, new TJsonProtocol(transport));
- case Protocol.Multiplexed:
- // it returns BinaryProtocol to avoid making wrapped protocol as public in TProtocolDecorator (in RunClientAsync it will be wrapped into Multiplexed protocol)
- return new Tuple<Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport));
- default:
- Debug.Assert(false, "unhandled case");
- break;
- }
+ case Protocol.Binary:
+ return new TBinaryProtocol(transport);
+ case Protocol.Compact:
+ return new TCompactProtocol(transport);
+ case Protocol.Json:
+ return new TJsonProtocol(transport);
+ default:
+ throw new Exception("unhandled protocol");
}
-
- return new Tuple<Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport));
}
- private static async Task RunClientAsync(Tuple<Protocol, TProtocol> protocolTuple, CancellationToken cancellationToken)
+ private static async Task RunClientAsync(TProtocol protocol, bool multiplex, CancellationToken cancellationToken)
{
try
{
- var protocol = protocolTuple.Item2;
- var protocolType = protocolTuple.Item1;
-
- TBaseClient client = null;
-
try
{
- if (protocolType != Protocol.Multiplexed)
- {
-
- client = new Calculator.Client(protocol);
- await ExecuteCalculatorClientOperations(cancellationToken, (Calculator.Client)client);
- }
- else
- {
- // it uses binary protocol there to create Multiplexed protocols
- var multiplex = new TMultiplexedProtocol(protocol, nameof(Calculator));
- client = new Calculator.Client(multiplex);
- await ExecuteCalculatorClientOperations(cancellationToken, (Calculator.Client)client);
-
- multiplex = new TMultiplexedProtocol(protocol, nameof(SharedService));
- client = new SharedService.Client(multiplex);
- await ExecuteSharedServiceClientOperations(cancellationToken, (SharedService.Client)client);
- }
+ if( multiplex)
+ protocol = new TMultiplexedProtocol(protocol, nameof(Calculator));
+
+ var client = new Calculator.Client(protocol);
+ await ExecuteCalculatorClientOperations(client, cancellationToken);
}
catch (Exception ex)
{
- Logger.LogError($"{client?.ClientId} " + ex);
+ Logger.LogError(ex.ToString());
}
finally
{
@@ -321,7 +327,7 @@ Sample:
}
}
- private static async Task ExecuteCalculatorClientOperations(CancellationToken cancellationToken, Calculator.Client client)
+ private static async Task ExecuteCalculatorClientOperations( Calculator.Client client, CancellationToken cancellationToken)
{
await client.OpenTransportAsync(cancellationToken);
@@ -374,16 +380,6 @@ Sample:
Logger.LogInformation($"{client.ClientId} ZipAsync() with delay 100mc on server side");
await client.zipAsync(cancellationToken);
}
- private static async Task ExecuteSharedServiceClientOperations(CancellationToken cancellationToken, SharedService.Client client)
- {
- await client.OpenTransportAsync(cancellationToken);
-
- // Async version
-
- Logger.LogInformation($"{client.ClientId} SharedService GetStructAsync(1)");
- var log = await client.getStructAsync(1, cancellationToken);
- Logger.LogInformation($"{client.ClientId} SharedService Value: {log.Value}");
- }
private enum Transport
@@ -401,7 +397,6 @@ Sample:
Binary,
Compact,
Json,
- Multiplexed
}
private enum Buffering