From c384b18c5a06466e22f606116148bfada56c8ac1 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 4 May 2025 20:28:23 +0300 Subject: [PATCH] Basic setup of endpoint context --- Example.Frontend/Program.cs | 2 +- .../IContextualSerializationToolKit.cs | 2 +- mROA/Abstract/IEndPointContext.cs | 4 +-- .../Backend/NetworkGatewayModule.cs | 11 ++++--- mROA/Implementation/EndPointContext.cs | 18 ++++++----- .../Frontend/NetworkFrontendBridge.cs | 14 ++++++--- .../Frontend/RequestExtractor.cs | 31 +++++-------------- mROA/Implementation/SharedObjectShell.cs | 4 +-- 8 files changed, 40 insertions(+), 46 deletions(-) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 6dbf673..e9cbf51 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -22,7 +22,7 @@ class Program new RemoteTypeBinder(); // builder.Modules.Add(new JsonSerializationToolkit()); builder.Modules.Add(new CborSerializationToolkit()); - + builder.Modules.Add(new EndPointContext()); builder.Modules.Add(new RemoteContextRepository()); builder.Modules.Add(new ChannelInteractionModule()); builder.Modules.Add(new UdpUntrustedInteraction()); diff --git a/mROA/Abstract/IContextualSerializationToolKit.cs b/mROA/Abstract/IContextualSerializationToolKit.cs index d027205..6cfa9ac 100644 --- a/mROA/Abstract/IContextualSerializationToolKit.cs +++ b/mROA/Abstract/IContextualSerializationToolKit.cs @@ -2,7 +2,7 @@ namespace mROA.Abstract { - public interface IContextualSerializationToolKit + public interface IContextualSerializationToolKit : IInjectableModule { byte[] Serialize(object objectToSerialize, IEndPointContext? context); void Serialize(object objectToSerialize, Span destination, IEndPointContext? context); diff --git a/mROA/Abstract/IEndPointContext.cs b/mROA/Abstract/IEndPointContext.cs index 494669e..44db796 100644 --- a/mROA/Abstract/IEndPointContext.cs +++ b/mROA/Abstract/IEndPointContext.cs @@ -4,7 +4,7 @@ { IContextRepository RealRepository { get; } IContextRepository RemoteRepository { get; } - int HostId { get; } - int OwnerId { get; } + int HostId { get; set; } + int OwnerId { get; set; } } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index e170b2d..7f19f32 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -18,8 +18,6 @@ namespace mROA.Implementation.Backend private IContextualSerializationToolKit? _serialization; private Dictionary _extractorsCTS = new(); - - public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType, IInjectableModule[] injectableModules) { @@ -82,8 +80,9 @@ namespace mROA.Implementation.Backend //TODO сделать контекст var context = new EndPointContext(); - - var streamExtractor = new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context); + + var streamExtractor = + new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context); interaction.IsConnected = () => streamExtractor.IsConnected; streamExtractor.MessageReceived = message => { interaction.ReceiveChanel.Writer.WriteAsync(message); }; streamExtractor.SingleReceive(); @@ -94,6 +93,8 @@ namespace mROA.Implementation.Backend switch (connectionRequest.MessageType) { case EMessageType.ClientConnect: + context.HostId = 0; + context.OwnerId = interaction.ConnectionId; Task.Run(async () => await streamExtractor.LoopedReceive(cts.Token)); _ = streamExtractor.SendFromChannel(interaction.TrustedPostChanel, cts.Token); interaction.PostMessageAsync(new NetworkMessageHeader(_serialization!, @@ -129,7 +130,7 @@ namespace mROA.Implementation.Backend } } } - + private void ThrowIfNotInjected() { if (_hub is null) diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index b249aeb..6962f86 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -1,24 +1,28 @@ using System; using mROA.Abstract; +using mROA.Implementation.Backend; namespace mROA.Implementation { public class EndPointContext : IEndPointContext { - public Func OwnerFunc; public IContextRepository RealRepository { get; set; } public IContextRepository RemoteRepository { get; set; } public int HostId { get; set; } - public int OwnerId - { - get => OwnerFunc(); - // ReSharper disable once UnusedMember.Global - set { OwnerFunc = () => value; } - } + public int OwnerId { get; set; } public void Inject(T dependency) { + switch (dependency) + { + case RemoteContextRepository remoteRepository: + RemoteRepository = remoteRepository; + break; + case ContextRepository realRepository: + RealRepository = realRepository; + break; + } } } } \ No newline at end of file diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index eabc2bd..049a108 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -20,6 +20,7 @@ namespace mROA.Implementation.Frontend private ChannelInteractionModule.StreamExtractor _currentExtractor; private CancellationTokenSource _rawExtractorCancellation; private IEndPointContext _context; + public NetworkFrontendBridge(IPEndPoint serverEndPoint) { _serverEndPoint = serverEndPoint; @@ -55,7 +56,8 @@ namespace mROA.Implementation.Frontend _interactionModule.IsConnected = () => _currentExtractor.IsConnected; _interactionModule.OnDisconnected += _ => { Reconnect(); }; - _interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect(), _context)).Wait(); + _interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect(), _context)) + .Wait(); _currentExtractor.SingleReceive(); var idMessage = _interactionModule.GetNextMessageReceiving(false).GetAwaiter().GetResult(); @@ -69,14 +71,17 @@ namespace mROA.Implementation.Frontend Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); - var assignment = _serialization.Deserialize(idMessage.Data, _context)!; + var assignment = _serialization.Deserialize(idMessage.Data, _context); _interactionModule.ConnectionId = -assignment.Id; TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(assignment.Id); + _context.HostId = assignment.Id; + _context.OwnerId = assignment.Id; } private void PrepareExtractor() { - _currentExtractor = new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization!, _context); + _currentExtractor = + new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization!, _context); _ = _currentExtractor.SendFromChannel(_interactionModule!.TrustedPostChanel, _rawExtractorCancellation.Token); @@ -108,7 +113,8 @@ namespace mROA.Implementation.Frontend public void Disconnect() { - _ = _interactionModule!.PostMessageAsync(new NetworkMessageHeader(_serialization!, new ClientDisconnect(), _context)); + _ = _interactionModule!.PostMessageAsync(new NetworkMessageHeader(_serialization!, new ClientDisconnect(), + _context)); _interactionModule.Dispose(); _tcpClient.Dispose(); } diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index 821b035..3311ca6 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -5,7 +5,6 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using mROA.Abstract; -using mROA.Implementation.Backend; // ReSharper disable MethodHasAsyncOverload @@ -14,9 +13,11 @@ namespace mROA.Implementation.Frontend public class RequestExtractor : IRequestExtractor { private IExecuteModule? _executeModule; + private IMethodRepository? _methodRepository; - private IContextRepository? _realContextRepository; - private IContextRepository? _remoteContextRepository; + + // private IContextRepository? _realContextRepository; + // private IContextRepository? _remoteContextRepository; private IRepresentationModule? _representationModule; private IContextualSerializationToolKit? _serializationToolkit; private IEndPointContext _context; @@ -28,13 +29,6 @@ namespace mROA.Implementation.Frontend case IExecuteModule executeModule: _executeModule = executeModule; break; - case MultiClientContextRepository: - case ContextRepository: - _realContextRepository = dependency as IContextRepository; - break; - case RemoteContextRepository remoteContextRepository: - _remoteContextRepository = remoteContextRepository; - break; case IMethodRepository methodRepository: _methodRepository = methodRepository; break; @@ -55,14 +49,6 @@ namespace mROA.Implementation.Frontend ThrowIfNotInjected(); - var multiClientOwnershipRepository = - TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; - multiClientOwnershipRepository?.RegisterOwnership(_representationModule!.Id); - if (multiClientOwnershipRepository is not null) - { - TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_representationModule.Id); - } - try { #if TRACE @@ -117,7 +103,6 @@ namespace mROA.Implementation.Frontend } catch { - multiClientOwnershipRepository?.FreeOwnership(); } } @@ -127,8 +112,6 @@ namespace mROA.Implementation.Frontend throw new NullReferenceException("Serializing toolkit is null."); if (_executeModule == null) throw new NullReferenceException("Execute module is null."); - if (_realContextRepository == null) - throw new NullReferenceException("Context repository is null."); if (_representationModule == null) throw new NullReferenceException("Representation module is null."); if (_methodRepository == null) @@ -137,12 +120,12 @@ namespace mROA.Implementation.Frontend private void HandleCancelRequest(CancelRequest req) { - _executeModule!.Execute(req, _realContextRepository!, _representationModule!, _context); + _executeModule!.Execute(req, _context.RealRepository, _representationModule!, _context); } private void HandleCallRequest(DefaultCallRequest request) { - var result = _executeModule!.Execute(request, _realContextRepository!, _representationModule!, _context); + var result = _executeModule!.Execute(request, _context.RealRepository, _representationModule!, _context); var resultType = result.MessageType; @@ -156,7 +139,7 @@ namespace mROA.Implementation.Frontend private void HandleEventRequest(DefaultCallRequest request) { - _executeModule!.Execute(request, _remoteContextRepository!, _representationModule!, _context); + _executeModule!.Execute(request, _context.RemoteRepository, _representationModule!, _context); } } } \ No newline at end of file diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 0fe0450..46a632a 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -63,8 +63,8 @@ namespace mROA.Implementation { RealRepository = TransmissionConfig.RealContextRepository, RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository, - HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(), - OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId + HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(), + OwnerId = 0 }; public ComplexObjectIdentifier Identifier