Esempio n. 1
0
        private static (MicroRatchetContext client, MicroRatchetContext server) CreateAndInitialize(int?mtu = null)
        {
            BouncyCastleServices clientServices = new BouncyCastleServices(KeyGeneration.GeneratePrivateKey());
            BouncyCastleServices serverServices = new BouncyCastleServices(KeyGeneration.GeneratePrivateKey());

            var client = new MicroRatchetContext(clientServices, true);
            var server = new MicroRatchetContext(serverServices, false);

            var packet = client.InitiateInitialization();

            while (packet != null && !client.IsInitialized || !server.IsInitialized)
            {
                packet = server.Receive(packet).ToSendBack;
                if (packet != null)
                {
                    packet = client.Receive(packet).ToSendBack;
                }
            }

            if (!client.IsInitialized || !server.IsInitialized)
            {
                throw new InvalidOperationException("Initialization failed");
            }

            using var msc = new MemoryStream();
            using var mss = new MemoryStream();
            client.SaveState(msc); msc.Position = 0;
            server.SaveState(mss); mss.Position = 0;

            return(
                new MicroRatchetContext(clientServices, new MicroRatchetConfiguration {
                IsClient = true, MaximumMessageSize = mtu ?? 80
            }, msc),
                new MicroRatchetContext(serverServices, new MicroRatchetConfiguration {
                IsClient = false, MaximumMessageSize = mtu ?? 80
            }, mss));
        }