Exemple #1
0
        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 ProcessProtocol(MultiplexingSocketProtocol <AddIntegerRequest, int> protocol)
        {
            while (true)
            {
                var res = await protocol.Read();

                var sum = res.Item2.A + res.Item2.B;
                var id  = res.Item1;
                await protocol.Write(sum, id);
            }
        }