private async ValueTask <IConnection?> InternalConnectAsync(ICap cap, string serviceType, CancellationToken cancellationToken)
        {
            using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            linkedTokenSource.CancelAfter(TimeSpan.FromSeconds(20));

            var baseConnectionOptions = new BaseConnectionOptions()
            {
                MaxSendByteCount    = 4 * 1024 * 1024,
                MaxReceiveByteCount = 4 * 1024 * 1024,
                BytesPool           = _bytesPool,
            };
            var baseConnection = new BaseConnection(cap, _baseConnectionDispatcher, baseConnectionOptions);

            var omniSecureConnectionOptions = new OmniSecureConnectionOptions()
            {
                Type       = OmniSecureConnectionType.Connected,
                BufferPool = _bytesPool,
            };
            var omniSecureConnection = new OmniSecureConnection(baseConnection, omniSecureConnectionOptions);

            await omniSecureConnection.HandshakeAsync(linkedTokenSource.Token);

            var helloMessage = new ConnectionHelloMessage(serviceType);
            await omniSecureConnection.EnqueueAsync((bufferWriter) => helloMessage.Export(bufferWriter, _bytesPool), linkedTokenSource.Token);

            return(omniSecureConnection);
        }
        private async ValueTask <(IConnection?, string?)> InternalAcceptAsync(ICap cap, CancellationToken cancellationToken)
        {
            using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            linkedTokenSource.CancelAfter(TimeSpan.FromSeconds(20));

            var baseConnectionOptions = new BaseConnectionOptions()
            {
                MaxSendByteCount    = 4 * 1024 * 1024,
                MaxReceiveByteCount = 4 * 1024 * 1024,
                BytesPool           = _bytesPool,
            };
            var baseConnection = new BaseConnection(cap, _baseConnectionDispatcher, baseConnectionOptions);

            var omniSecureConnectionOptions = new OmniSecureConnectionOptions()
            {
                Type       = OmniSecureConnectionType.Accepted,
                BufferPool = _bytesPool,
            };
            var omniSecureConnection = new OmniSecureConnection(baseConnection, omniSecureConnectionOptions);

            await omniSecureConnection.HandshakeAsync(linkedTokenSource.Token);

            ConnectionHelloMessage?helloMessage = null;
            await omniSecureConnection.DequeueAsync((sequence) => helloMessage = ConnectionHelloMessage.Import(sequence, _bytesPool), linkedTokenSource.Token);

            return(omniSecureConnection, helloMessage?.ServiceType);
        }
Beispiel #3
0
    public async Task RandomSendAndReceiveTest(OmniDigitalSignature?clientDigitalSignature, OmniDigitalSignature?serverDigitalSignature)
    {
        var random = new Random();

        var(clientSocket, serverSocket) = SocketHelper.GetSocketPair();

        await using var batchActionDispatcher = new BatchActionDispatcher(TimeSpan.FromMilliseconds(10));

        var bridgeConnectionOptions = new BridgeConnectionOptions(1024 * 1024 * 256);

        await using var clientBridgeConnection = new BridgeConnection(new SocketCap(clientSocket), null, null, batchActionDispatcher, BytesPool.Shared, bridgeConnectionOptions);
        await using var serverBridgeConnection = new BridgeConnection(new SocketCap(serverSocket), null, null, batchActionDispatcher, BytesPool.Shared, bridgeConnectionOptions);

        var clientSecureConnectionOptions = new OmniSecureConnectionOptions(OmniSecureConnectionType.Connected, clientDigitalSignature, 1024 * 1024 * 256);

        await using var clientSecureConnection = OmniSecureConnection.CreateV1(clientBridgeConnection, BytesPool.Shared, clientSecureConnectionOptions);

        var serverSecureConnectionOptions = new OmniSecureConnectionOptions(OmniSecureConnectionType.Accepted, serverDigitalSignature, 1024 * 1024 * 256);

        await using var serverSecureConnection = OmniSecureConnection.CreateV1(serverBridgeConnection, BytesPool.Shared, serverSecureConnectionOptions);

        // ハンドシェイクを行う
        var valueTask1 = clientSecureConnection.HandshakeAsync();
        var valueTask2 = serverSecureConnection.HandshakeAsync();
        await Task.WhenAll(valueTask1.AsTask(), valueTask2.AsTask());

        if (clientDigitalSignature != null)
        {
            Assert.Equal(serverSecureConnection.Signature, clientDigitalSignature.GetOmniSignature());
        }

        if (serverDigitalSignature != null)
        {
            Assert.Equal(clientSecureConnection.Signature, serverDigitalSignature.GetOmniSignature());
        }

        await TestHelper.RandomSendAndReceive(random, clientSecureConnection, serverSecureConnection);

        await TestHelper.RandomSendAndReceive(random, serverSecureConnection, clientSecureConnection);
    }
Beispiel #4
0
        public void RandomSendAndReceiveTest()
        {
            var random = new Random();

            var caseList = new List <int>();

            caseList.AddRange(Enumerable.Range(1, 32));
            caseList.AddRange(new int[] { 100, 1000, 10000, 1024 * 1024, 1024 * 1024 * 32 });

            var(socket1, socket2) = SocketHelpers.GetSockets();

            var options = new OmniNonblockingConnectionOptions()
            {
                MaxReceiveByteCount = 1024 * 1024 * 256,
                MaxSendByteCount    = 1024 * 1024 * 256,
                BufferPool          = BufferPool.Shared,
            };

            using (var baseConnection1 = new OmniNonblockingConnection(new SocketCap(socket1, false), options))
                using (var baseConnection2 = new OmniNonblockingConnection(new SocketCap(socket2, false), options))
                    using (var connection1 = new OmniSecureConnection(baseConnection1, new OmniSecureConnectionOptions()
                    {
                        Type = OmniSecureConnectionType.Connect
                    }))
                        using (var connection2 = new OmniSecureConnection(baseConnection2, new OmniSecureConnectionOptions()
                        {
                            Type = OmniSecureConnectionType.Accept
                        }))
                        {
                            // ハンドシェイクを行う
                            {
                                var valueTask1 = connection1.Handshake();
                                var valueTask2 = connection2.Handshake();

                                var stopwatch = Stopwatch.StartNew();

                                while (!valueTask1.IsCompleted || !valueTask2.IsCompleted)
                                {
                                    if (stopwatch.Elapsed.TotalSeconds > 60)
                                    {
                                        throw new TimeoutException("Handshake");
                                    }

                                    Thread.Sleep(100);

                                    connection1.DoEvents();
                                    connection2.DoEvents();
                                }
                            }

                            foreach (var bufferSize in caseList)
                            {
                                var buffer1 = new byte[bufferSize];
                                var buffer2 = new byte[bufferSize];

                                random.NextBytes(buffer1);

                                var valueTask1 = connection1.EnqueueAsync((bufferWriter) =>
                                {
                                    bufferWriter.Write(buffer1);
                                });

                                var valueTask2 = connection2.DequeueAsync((sequence) =>
                                {
                                    sequence.CopyTo(buffer2);
                                });

                                var stopwatch = Stopwatch.StartNew();

                                while (!valueTask1.IsCompleted || !valueTask2.IsCompleted)
                                {
                                    if (stopwatch.Elapsed.TotalSeconds > 60)
                                    {
                                        throw new TimeoutException("SendAndReceive");
                                    }

                                    Thread.Sleep(100);

                                    connection1.DoEvents();
                                    connection2.DoEvents();
                                }

                                Assert.True(BytesOperations.SequenceEqual(buffer1, buffer2));
                            }
                        }
        }