From 23684d51de1e2740834ea3616548c35493c88ca7 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Fri, 2 May 2025 20:15:04 +0300 Subject: [PATCH] Surprisingly, it works on the channels --- Example.Frontend/Program.cs | 2 +- .../Backend/MultiClientOwnershipRepository.cs | 6 ++---- mROA/Implementation/Backend/NetworkGatewayModule.cs | 10 ++++++++-- mROA/Implementation/ChannelInteractionModule.cs | 6 +++--- mROA/Implementation/Frontend/NetworkFrontendBridge.cs | 11 +++++++---- mROA/Implementation/Frontend/RequestExtractor.cs | 7 +++++-- mROA/Implementation/RemoteContextRepository.cs | 2 +- mROA/Implementation/RepresentationModule.cs | 5 ++++- mROA/mROA.csproj | 2 +- 9 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index e87a449..8a5b163 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -45,7 +45,7 @@ class Program Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var context = builder.GetModule(); - var factory = context.GetSingleObject(typeof(IPrinterFactory), 0) as IPrinterFactory; + var factory = context.GetSingleObject(typeof(IPrinterFactory), -TransmissionConfig.OwnershipRepository.GetHostOwnershipId()) as IPrinterFactory; using (var disposingPrinter = factory.Create("Test")) { diff --git a/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs b/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs index 56cd389..5bb5197 100644 --- a/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs +++ b/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs @@ -13,10 +13,8 @@ namespace mROA.Implementation.Backend return _ownerships.GetValueOrDefault(Environment.CurrentManagedThreadId, 0); } - public int GetHostOwnershipId() - { - return 0; - } + public int GetHostOwnershipId() => 0; + public void RegisterOwnership(int ownershipId) { diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index c5d15b8..b9f31af 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -78,7 +78,10 @@ namespace mROA.Implementation.Backend var streamExtractor = new StreamExtractor(client.GetStream(), _serialization); interaction.IsConnected = () => streamExtractor.IsConnected; - streamExtractor.MessageReceived = message => interaction.ReceiveChanel.Writer.WriteAsync(message); + streamExtractor.MessageReceived = message => + { + interaction.ReceiveChanel.Writer.WriteAsync(message); + }; streamExtractor.SingleReceive(); var connectionRequest = interaction.GetNextMessageReceiving(false) .GetAwaiter().GetResult()!; @@ -98,7 +101,10 @@ namespace mROA.Implementation.Backend var recoveryRequest = _serialization!.Deserialize(connectionRequest.Data)!; var recoveryInteraction = _hub.GetInteraction(recoveryRequest.Id); - streamExtractor.MessageReceived = message => recoveryInteraction.ReceiveChanel.Writer.WriteAsync(message); + streamExtractor.MessageReceived = message => + { + recoveryInteraction.ReceiveChanel.Writer.WriteAsync(message); + }; recoveryInteraction.Restart(false); Console.WriteLine("Connection recovery for client {0} finished", recoveryRequest.Id); diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index b52d385..8671bf1 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -28,14 +28,14 @@ namespace mROA.Implementation { SingleReader = false, SingleWriter = false, - AllowSynchronousContinuations = true + // AllowSynchronousContinuations = true }); _receiveReader = ReceiveChanel.Reader; _outputTrustedChannel = Channel.CreateBounded(new BoundedChannelOptions(1) { SingleReader = true, SingleWriter = true, - AllowSynchronousContinuations = true, + // AllowSynchronousContinuations = true, }); _trustedWriter = _outputTrustedChannel.Writer; @@ -43,7 +43,7 @@ namespace mROA.Implementation { SingleReader = true, SingleWriter = true, - AllowSynchronousContinuations = true + // AllowSynchronousContinuations = true }); _untrustedWriter = _outputUntrustedChannel.Writer; } diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index f66281e..4dab9de 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -49,11 +49,11 @@ namespace mROA.Implementation.Frontend _tcpClient.Connect(_serverEndPoint); PrepareExtractor(); - _interactionModule.IsConnected = () => _currentExtractor.IsConnected; + _interactionModule.IsConnected = () => _currentExtractor.IsConnected; _interactionModule.OnDisconnected += id => { Reconnect(); }; _interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect())).Wait(); - + _currentExtractor.SingleReceive(); var idMessage = _interactionModule.GetNextMessageReceiving(false).GetAwaiter().GetResult(); @@ -78,7 +78,10 @@ namespace mROA.Implementation.Frontend _ = _currentExtractor.SendFromChannel(_interactionModule.TrustedPostChanel, _rawExtractorCancellation.Token); - _currentExtractor.MessageReceived = message => _interactionModule.ReceiveChanel.Writer.WriteAsync(message); + _currentExtractor.MessageReceived = message => + { + _interactionModule.ReceiveChanel.Writer.WriteAsync(message); + }; } private async Task Reconnect() @@ -90,7 +93,7 @@ namespace mROA.Implementation.Frontend _rawExtractorCancellation = new CancellationTokenSource(); PrepareExtractor(); - + _ = _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token); await _interactionModule.Restart(true); diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index f5616fc..33fe375 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -48,11 +48,14 @@ namespace mROA.Implementation.Frontend { ThrowIfNotInjected(); - TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_representationModule.Id); - + var multiClientOwnershipRepository = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; multiClientOwnershipRepository?.RegisterOwnership(_representationModule!.Id); + if (multiClientOwnershipRepository is not null) + { + TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_representationModule.Id); + } try { diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 8c8ded5..43dceda 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -48,7 +48,7 @@ namespace mROA.Implementation throw new NullReferenceException("representation producer is not initialized"); var representationModule = - _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); + _representationProducer.Produce(ownerId); _producedRemoteEndpoints.Add((Activator.CreateInstance(RemoteTypes[type], -1, representationModule) as RemoteObjectBase)!); diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index acf8878..761472b 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -35,7 +35,10 @@ namespace mROA.Implementation CancellationToken token = default, params Func[] converter) { var writer = _interaction.ReceiveChanel.Writer; - await foreach (var message in _interaction.ReceiveChanel.Reader.ReadAllAsync(token)) + var reader = _interaction.ReceiveChanel.Reader; + + + await foreach (var message in reader.ReadAllAsync(token)) { if (!rule(message)) { diff --git a/mROA/mROA.csproj b/mROA/mROA.csproj index e52150a..3c172de 100644 --- a/mROA/mROA.csproj +++ b/mROA/mROA.csproj @@ -21,7 +21,7 @@ - TRACE; +