From ce14258bf7808b6af6699bd531e8e591c0bf2e6f Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 10 Mar 2025 11:19:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BA=D0=B8=D0=B4=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D1=82=D0=B0=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=87=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D1=82=D0=BE=D1=87=D0=BA=D0=B8=20=D0=B2=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA/Abstract/IContextRepository.cs | 4 +- .../Backend/ContextRepository.cs | 6 +- .../Backend/MultiClientContextRepository.cs | 8 +- .../Frontend/RequestExtractor.cs | 125 +++++++++--------- .../Implementation/RemoteContextRepository.cs | 4 +- mROA/Implementation/SharedObjectShell.cs | 2 +- 6 files changed, 76 insertions(+), 73 deletions(-) diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index f9c643b..6307373 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -5,11 +5,11 @@ namespace mROA.Abstract { public interface IContextRepository : IInjectableModule { - int ResisterObject(object o); + int ResisterObject(object o, IEndPointContext context); void ClearObject(int id); T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell); T? GetObject(int id); object GetSingleObject(Type type); - int GetObjectIndex(object o); + int GetObjectIndex(object o, IEndPointContext context); } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index e0371f8..4351e96 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -39,7 +39,7 @@ namespace mROA.Implementation.Backend Activator.CreateInstance); } - public int ResisterObject(object o) + public int ResisterObject(object o, IEndPointContext context) { if (!_lastIndexFinder.IsCompleted) _lastIndexFinder.Wait(); @@ -79,10 +79,10 @@ namespace mROA.Implementation.Backend return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); } - public int GetObjectIndex(object o) + public int GetObjectIndex(object o, IEndPointContext context) { var index = Array.IndexOf(_storage, o); - return index == -1 ? ResisterObject(o) : index; + return index == -1 ? ResisterObject(o, context) : index; } private int FindLastIndex() diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index f5c7bf4..e8dbef3 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -27,10 +27,10 @@ namespace mROA.Implementation.Backend { } - public int ResisterObject(object o) + public int ResisterObject(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.ResisterObject(o); + return repository.ResisterObject(o, context); } public void ClearObject(int id) @@ -63,10 +63,10 @@ namespace mROA.Implementation.Backend return repository.GetSingleObject(type); } - public int GetObjectIndex(object o) + public int GetObjectIndex(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.GetObjectIndex(o); + return repository.GetObjectIndex(o, context); } public IContextRepository GetRepository(int clientId) diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index d4b141c..ecda96d 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -39,84 +39,87 @@ namespace mROA.Implementation.Frontend } } - public async Task StartExtraction() + public Task StartExtraction() { - if (_serializationToolkit == null) - throw new NullReferenceException("Serializing toolkit is null."); - if (_executeModule == null) - throw new NullReferenceException("Execute module is null."); - if (_contextRepository == null) - throw new NullReferenceException("Context repository is null."); - if (_representationModule == null) - throw new NullReferenceException("Representation module is null."); - if (_methodRepository == null) - throw new NullReferenceException("Method repository is null."); - - await Task.Yield(); - - var multiClientOwnershipRepository = - TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; - multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id); - - try + return Task.Run(() => { - while (true) + if (_serializationToolkit == null) + throw new NullReferenceException("Serializing toolkit is null."); + if (_executeModule == null) + throw new NullReferenceException("Execute module is null."); + if (_contextRepository == null) + throw new NullReferenceException("Context repository is null."); + if (_representationModule == null) + throw new NullReferenceException("Representation module is null."); + if (_methodRepository == null) + throw new NullReferenceException("Method repository is null."); + + // await Task.Yield(); + + var multiClientOwnershipRepository = + TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; + multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id); + + try { + while (true) + { #if TRACE Console.WriteLine("Waiting for request..."); #endif - var tokenSource = new CancellationTokenSource(); - var token = tokenSource.Token; - var defaultRequest = - _representationModule!.GetMessageAsync( - messageType: MessageType.CallRequest, token: token); - var cancelRequest = - _representationModule!.GetMessageAsync( - messageType: MessageType.CancelRequest, token: token); + var tokenSource = new CancellationTokenSource(); + var token = tokenSource.Token; + var defaultRequest = + _representationModule!.GetMessageAsync( + messageType: MessageType.CallRequest, token: token); + var cancelRequest = + _representationModule!.GetMessageAsync( + messageType: MessageType.CancelRequest, token: token); - Task.WaitAny(defaultRequest, cancelRequest); + Task.WaitAny(defaultRequest, cancelRequest); #if TRACE Console.WriteLine("Request received"); #endif - if (cancelRequest.IsCompleted) - { + if (cancelRequest.IsCompleted) + { #if TRACE Console.WriteLine("Cancelling request"); #endif - var req = cancelRequest.Result; - tokenSource.Cancel(); - _executeModule.Execute(req, _contextRepository, _representationModule); - } - else - { - tokenSource.Cancel(); - var request = defaultRequest.Result; - - var result = _executeModule.Execute(request, _contextRepository, _representationModule); - - var resultType = MessageType.Unknown; - - switch (result) - { - case FinalCommandExecution: - resultType = MessageType.FinishedCommandExecution; - break; - case AsyncCommandExecution: - resultType = MessageType.AsyncCommandExecution; - break; - case ExceptionCommandExecution: - resultType = MessageType.ExceptionCommandExecution; - break; + var req = cancelRequest.Result; + tokenSource.Cancel(); + _executeModule.Execute(req, _contextRepository, _representationModule); } + else + { + tokenSource.Cancel(); + var request = defaultRequest.Result; - _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); + var result = _executeModule.Execute(request, _contextRepository, _representationModule); + + var resultType = MessageType.Unknown; + + switch (result) + { + case FinalCommandExecution: + resultType = MessageType.FinishedCommandExecution; + break; + case AsyncCommandExecution: + resultType = MessageType.AsyncCommandExecution; + break; + case ExceptionCommandExecution: + resultType = MessageType.ExceptionCommandExecution; + break; + } + + _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); + } } } - } - catch - { - multiClientOwnershipRepository?.FreeOwnership(); - } + catch + { + multiClientOwnershipRepository?.FreeOwnership(); + } + }); } } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 7648d80..4e0f6b6 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -9,7 +9,7 @@ namespace mROA.Implementation private IRepresentationModuleProducer? _representationProducer; public static Dictionary RemoteTypes = new(); - public int ResisterObject(object o) + public int ResisterObject(object o, IEndPointContext context) { throw new NotSupportedException(); } @@ -58,7 +58,7 @@ namespace mROA.Implementation representationModule)!; } - public int GetObjectIndex(object o) + public int GetObjectIndex(object o, IEndPointContext context) { if (o is RemoteObjectBase remote) { diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 27e1ab2..8a2917d 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -49,7 +49,7 @@ namespace mROA.Implementation else { _identifier.OwnerId = EndPointContext.HostId; - _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value); + _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value, EndPointContext); } } }