public async Task SimpleTest() { 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 clientMultiplexerOption = new OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Connected, TimeSpan.FromMinutes(1), 3, 1024 * 1024 * 256, 3); await using var clientMultiplexer = OmniConnectionMultiplexer.CreateV1(clientBridgeConnection, batchActionDispatcher, BytesPool.Shared, clientMultiplexerOption); var serverMultiplexerOption = new OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Accepted, TimeSpan.FromMinutes(1), 3, 1024 * 1024 * 256, 3); await using var serverMultiplexer = OmniConnectionMultiplexer.CreateV1(serverBridgeConnection, batchActionDispatcher, BytesPool.Shared, serverMultiplexerOption); var clientMultiplexerHandshakeTask = clientMultiplexer.HandshakeAsync().AsTask(); var serverMultiplexerHandshakeTask = serverMultiplexer.HandshakeAsync().AsTask(); await Task.WhenAll(clientMultiplexerHandshakeTask, serverMultiplexerHandshakeTask); var connectTask = clientMultiplexer.ConnectAsync().AsTask(); var acceptTask = serverMultiplexer.AcceptAsync().AsTask(); await TestHelper.RandomSendAndReceive(random, connectTask.Result, acceptTask.Result); await TestHelper.RandomSendAndReceive(random, acceptTask.Result, connectTask.Result); }
private async ValueTask ConnectAsync(CancellationToken cancellationToken = default) { if (!_listenAddress.TryGetTcpEndpoint(out var ipAddress, out var port)) { throw new Exception("address is invalid format."); } var bytesPool = BytesPool.Shared; _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); await _socket.ConnectAsync(new IPEndPoint(ipAddress, port), TimeSpan.FromSeconds(3), cancellationToken); _cap = new SocketCap(_socket); _batchActionDispatcher = new BatchActionDispatcher(TimeSpan.FromMilliseconds(10)); var bridgeConnectionOptions = new BridgeConnectionOptions(int.MaxValue); _bridgeConnection = new BridgeConnection(_cap, null, null, _batchActionDispatcher, bytesPool, bridgeConnectionOptions); var multiplexerOptions = new MultiplexerV1.OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Connected, TimeSpan.FromMilliseconds(1000 * 10), 10, uint.MaxValue, 10); _multiplexer = OmniConnectionMultiplexer.CreateV1(_bridgeConnection, _batchActionDispatcher, bytesPool, multiplexerOptions); await _multiplexer.HandshakeAsync(cancellationToken); var rocketRemotingCallerFactory = new RocketRemotingCallerFactory <DefaultErrorMessage>(_multiplexer, bytesPool); _axisServiceRemotingClient = new AxisServiceRemoting.Client <DefaultErrorMessage>(rocketRemotingCallerFactory, bytesPool); }
public async ValueTask <IConnection?> ConnectAsync(OmniAddress address, CancellationToken cancellationToken = default) { var cap = await this.ConnectCapAsync(address, cancellationToken); if (cap == null) { return(null); } var bridgeConnectionOptions = new BridgeConnectionOptions(MaxReceiveByteCount); var bridgeConnection = new BridgeConnection(cap, _senderBandwidthLimiter, _receiverBandwidthLimiter, _batchActionDispatcher, _bytesPool, bridgeConnectionOptions); return(bridgeConnection); }
public async Task RandomSendAndReceiveTest() { var random = new Random(); var(socket1, socket2) = SocketHelper.GetSocketPair(); await using var batchActionDispatcher = new BatchActionDispatcher(TimeSpan.FromMilliseconds(10)); var options = new BridgeConnectionOptions(1024 * 1024 * 256); await using var connection1 = new BridgeConnection(new SocketCap(socket1), null, null, batchActionDispatcher, BytesPool.Shared, options); await using var connection2 = new BridgeConnection(new SocketCap(socket2), null, null, batchActionDispatcher, BytesPool.Shared, options); await TestHelper.RandomSendAndReceive(random, connection1, connection2); await TestHelper.RandomSendAndReceive(random, connection2, connection1); }
public async Task SimpleFunctionTest() { var(clientSocket, serverSocket) = SocketHelper.GetSocketPair(); var bytesPool = BytesPool.Shared; 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 clientMultiplexerOption = new OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Connected, TimeSpan.FromMinutes(1), 3, 1024 * 1024 * 256, 3); await using var clientMultiplexer = OmniConnectionMultiplexer.CreateV1(clientBridgeConnection, batchActionDispatcher, BytesPool.Shared, clientMultiplexerOption); var serverMultiplexerOption = new OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Accepted, TimeSpan.FromMinutes(1), 3, 1024 * 1024 * 256, 3); await using var serverMultiplexer = OmniConnectionMultiplexer.CreateV1(serverBridgeConnection, batchActionDispatcher, BytesPool.Shared, serverMultiplexerOption); var clientMultiplexerHandshakeTask = clientMultiplexer.HandshakeAsync().AsTask(); var serverMultiplexerHandshakeTask = serverMultiplexer.HandshakeAsync().AsTask(); await Task.WhenAll(clientMultiplexerHandshakeTask, serverMultiplexerHandshakeTask); var mockTestService = new Mock <ITestService>(); mockTestService.Setup(n => n.Unary1Async(It.IsAny <TestParam>(), It.IsAny <CancellationToken>())).Returns(new ValueTask <TestResult>(new TestResult(1))); var remotingConnector = new RocketRemotingCallerFactory <DefaultErrorMessage>(clientMultiplexer, bytesPool); var remotingAccepter = new RocketRemotingListenerFactory <DefaultErrorMessage>(serverMultiplexer, DefaultErrorMessageFactory.Default, bytesPool); var client = new TestServiceRemoting.Client <DefaultErrorMessage>(remotingConnector, bytesPool); var server = new TestServiceRemoting.Server <DefaultErrorMessage>(mockTestService.Object, remotingAccepter, bytesPool); var cancellationTokenSource = new CancellationTokenSource(); var eventLoop = server.EventLoopAsync(cancellationTokenSource.Token); var testResult1 = await client.Unary1Async(new TestParam(1), default); mockTestService.Verify(n => n.Unary1Async(new TestParam(1), default), Times.AtMostOnce()); cancellationTokenSource.Cancel(); await Assert.ThrowsAsync <TaskCanceledException>(async() => await eventLoop); cancellationTokenSource.Dispose(); }
public async Task CloseTest() { var random = new Random(); var(socket1, socket2) = SocketHelper.GetSocketPair(); await using var batchActionDispatcher = new BatchActionDispatcher(TimeSpan.FromMilliseconds(10)); var options = new BridgeConnectionOptions(1024 * 1024 * 256); var connection1 = new BridgeConnection(new SocketCap(socket1), null, null, batchActionDispatcher, BytesPool.Shared, options); var connection2 = new BridgeConnection(new SocketCap(socket2), null, null, batchActionDispatcher, BytesPool.Shared, options); await connection1.DisposeAsync(); await Assert.ThrowsAsync <ObjectDisposedException>(async() => { await connection1.Sender.SendAsync(_ => { }); }); Assert.Throws <ObjectDisposedException>(() => { connection1.Sender.TrySend(_ => { }); }); await Assert.ThrowsAsync <ObjectDisposedException>(async() => { await connection1.Receiver.ReceiveAsync(_ => { }); }); Assert.Throws <ObjectDisposedException>(() => { connection1.Receiver.TryReceive(_ => { }); }); socket2.Dispose(); await Task.Delay(1000); await Assert.ThrowsAsync <ConnectionException>(async() => { await connection2.Sender.SendAsync(_ => { }); }); Assert.Throws <ConnectionException>(() => { connection2.Sender.TrySend(_ => { }); }); await Assert.ThrowsAsync <ConnectionException>(async() => { await connection2.Receiver.ReceiveAsync(_ => { }); }); Assert.Throws <ConnectionException>(() => { connection2.Receiver.TryReceive(_ => { }); }); await connection2.DisposeAsync(); await Assert.ThrowsAsync <ObjectDisposedException>(async() => { await connection2.Sender.SendAsync(_ => { }); }); Assert.Throws <ObjectDisposedException>(() => { connection2.Sender.TrySend(_ => { }); }); await Assert.ThrowsAsync <ObjectDisposedException>(async() => { await connection2.Receiver.ReceiveAsync(_ => { }); }); Assert.Throws <ObjectDisposedException>(() => { connection2.Receiver.TryReceive(_ => { }); }); }
private static async Task InternalEventLoopAsync(AxisService service, Socket socket, CancellationToken cancellationToken = default) { using var socketCap = new SocketCap(socket); var bytesPool = BytesPool.Shared; await using var batchActionDispatcher = new BatchActionDispatcher(TimeSpan.FromMilliseconds(10)); var bridgeConnectionOptions = new BridgeConnectionOptions(int.MaxValue); await using var bridgeConnection = new BridgeConnection(socketCap, null, null, batchActionDispatcher, bytesPool, bridgeConnectionOptions); var multiplexerOption = new OmniConnectionMultiplexerOptions(OmniConnectionMultiplexerType.Accepted, TimeSpan.FromMinutes(1), 3, 1024 * 1024 * 4, 3); await using var multiplexer = OmniConnectionMultiplexer.CreateV1(bridgeConnection, batchActionDispatcher, bytesPool, multiplexerOption); await multiplexer.HandshakeAsync(cancellationToken); var errorMessageFactory = new DefaultErrorMessageFactory(); var listenerFactory = new RocketRemotingListenerFactory <DefaultErrorMessage>(multiplexer, errorMessageFactory, bytesPool); try { _logger.Debug("InternalEventLoopAsync: Start"); var server = new AxisServiceRemoting.Server <DefaultErrorMessage>(service, listenerFactory, bytesPool); var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); using var onCloseListener = bridgeConnection.Events.OnClosed.Listen(() => linkedCancellationTokenSource.Cancel()); await server.EventLoopAsync(linkedCancellationTokenSource.Token); } catch (Exception e) { _logger.Error(e); } finally { _logger.Debug("InternalEventLoopAsync: End"); } }
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); }