static async Task ProcessAddInteger() { await using var connection = await connectionFactory.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5005)); MultiplexingSocketProtocol <int, AddIntegerRequest> protocol = new MultiplexingSocketProtocol <int, AddIntegerRequest>(connection, new AddIntegerResponseReader(), new AddIntegerRequestWritter(), new Int32MessageIdGenerator(), new Int32MessageIdParser()); IMessageIdGenerator idGenerator = new Int32MessageIdGenerator(); int cnt = 20; for (int i = 0; i < cnt; i++) { var id = await idGenerator.Next(); await protocol.Write(new AddIntegerRequest { A = i, B = i + 1 }, id); var resp = await protocol.Read(); if (!resp.Item1.Equals(id)) { Console.WriteLine($"error, id not matched, reqId:{id},respId:{resp.Item1}"); } else { Console.WriteLine($"{i} + {i + 1} = {resp.Item2}"); } } }
static async Task ProcessAddInteger() { await using var connection = await connectionFactory.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5005)); MultiplexingSocketProtocol <int, AddIntegerRequest> protocol = new MultiplexingSocketProtocol <int, AddIntegerRequest>(connection, new AddIntegerResponseReader(), new AddIntegerRequestWritter(), new Int32MessageIdGenerator(), new Int32MessageIdParser()); IMessageIdGenerator idGenerator = new Int32MessageIdGenerator(); IMultiplexingSocketClientProtocol <int, AddIntegerRequest> clientProtocol = new MultiplexingSocketClientProtocol <int, AddIntegerRequest>(protocol, idGenerator); int cnt = 20; Task[] tasks = new Task[cnt]; for (int i = 0; i < cnt; i++) { tasks[i] = PeformAddInteger(i, i + 1, clientProtocol); } await Task.WhenAll(tasks); }