From 8beca7f8d71ab6354e2cdc28de7e16576d8b5977 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Tue, 11 Mar 2025 14:43:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BE=D1=82=20=D1=83=D1=81=D1=82=D0=B0=D1=80=D0=B5=D0=B2=D1=88?= =?UTF-8?q?=D0=B5=D0=B3=D0=BE=20=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA/Abstract/ISerialisationModule.cs | 28 ++++++---------- .../Backend/BasicExecutionModule.cs | 32 ++++++++++--------- .../Backend/ContextRepository.cs | 15 ++------- .../Backend/NetworkGatewayModule.cs | 32 +++++++++++-------- mROA/Implementation/EndPointContext.cs | 1 + .../Frontend/RequestExtractor.cs | 19 ++++------- .../NextGenerationInteractionModule.cs | 2 +- .../Implementation/RemoteContextRepository.cs | 5 --- mROA/Implementation/SharedObjectShell.cs | 1 + 9 files changed, 56 insertions(+), 79 deletions(-) diff --git a/mROA/Abstract/ISerialisationModule.cs b/mROA/Abstract/ISerialisationModule.cs index a297fd8..17e7911 100644 --- a/mROA/Abstract/ISerialisationModule.cs +++ b/mROA/Abstract/ISerialisationModule.cs @@ -2,34 +2,24 @@ using System; using System.Threading; using System.Threading.Tasks; using mROA.Implementation; -using mROA.Implementation.CommandExecution; namespace mROA.Abstract { - public interface ISerialisationModule : IInjectableModule - { - void HandleIncomingRequest(int clientId, byte[] message); - void PostResponse(NetworkMessage message, int clientId); - void SendWelcomeMessage(int clientId); - public interface IFrontendSerialisationModule : IInjectableModule - { - int ClientId { get; } - Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution; - Task> GetFinalCommandExecution(Guid requestId); - void PostCallRequest(ICallRequest callRequest); - } - } - public interface IRepresentationModule : IInjectableModule { int Id { get; } - Task GetMessageAsync(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default); + + Task GetMessageAsync(Guid? requestId = null, MessageType? messageType = null, + CancellationToken token = default); + T GetMessage(Guid? requestId = null, MessageType? messageType = null); - Task GetRawMessage(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default); - + + Task GetRawMessage(Guid? requestId = null, MessageType? messageType = null, + CancellationToken token = default); + Task PostCallMessageAsync(Guid id, MessageType messageType, T payload) where T : notnull; Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType); void PostCallMessage(Guid id, MessageType messageType, T payload) where T : notnull; - void PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType); + void PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType); } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index d5ec4ab..0924999 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -81,7 +81,7 @@ namespace mROA.Implementation.Backend if (invoker.ParameterTypes.Length != 0) { castedParams = new object[invoker.ParameterTypes.Length]; - for (int i = 0; i < castedParams.Length; i++) + for (var i = 0; i < castedParams.Length; i++) { castedParams[i] = _serialization.Cast(command.Parameters![i], invoker.ParameterTypes[i]); } @@ -89,25 +89,27 @@ namespace mROA.Implementation.Backend var execContext = new RequestContext(command.Id, representationModule.Id); - if (invoker is AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker) - return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, - _cancellationRepo, - representationModule, execContext); - - if (invoker is AsyncMethodInvoker asyncMethodInvoker) - return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, - representationModule, execContext); - - var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); - if (command.CommandId == -1) + switch (invoker) { + case AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker: + return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, + _cancellationRepo, + representationModule, execContext); + case AsyncMethodInvoker asyncMethodInvoker: + return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, + representationModule, execContext); + default: + var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); + if (command.CommandId == -1) + { #if TRACE Console.WriteLine("Disposing object"); #endif - contextRepository.ClearObject(command.ObjectId); - } + contextRepository.ClearObject(command.ObjectId); + } - return result; + return result; + } } catch (Exception e) { diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index fe1a780..55b8169 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -12,7 +12,7 @@ namespace mROA.Implementation.Backend { private const int StartupSize = 1024; private const int GrowSize = 128; - public static object[] EventBinders = new object[] { }; + public static object[] EventBinders = { }; private static int LastDebugId = -1; private int _debugId = -1; @@ -55,12 +55,7 @@ namespace mROA.Implementation.Backend _lastIndexFinder = Task.FromResult(id.ContextId); } - public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) - { - return (T)GetObject(sharedObjectShellShell.Identifier.ContextId); - } - - public T? GetObject(ComplexObjectIdentifier id) + public T GetObject(ComplexObjectIdentifier id) { return id.ContextId == -1 || _storage.Length <= id.ContextId ? throw new NullReferenceException("Cannot find that object. It is null") @@ -99,12 +94,6 @@ namespace mROA.Implementation.Backend Activator.CreateInstance); } - public object GetObject(int id) - { - // Debug.Log($"Reading object {id} from repository with debug ID {_debugId}"); - return (id == -1 || _storage.Length <= id ? null : _storage[id]) ?? throw new NullReferenceException(); - } - private int FindLastIndex() { for (var i = 0; i < _storage.Length; i++) diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index aeba65c..81d6266 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -8,13 +8,14 @@ namespace mROA.Implementation.Backend { public class NetworkGatewayModule : IGatewayModule { - private readonly Type? _interactionModuleType; private readonly IInjectableModule[]? _injectableModules; + private readonly Type? _interactionModuleType; private readonly TcpListener _tcpListener; private IConnectionHub? _hub; private ISerializationToolkit? _serialization; - public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType, IInjectableModule[] injectableModules) + public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType, + IInjectableModule[] injectableModules) { _tcpListener = new(endpoint); _interactionModuleType = interactionModuleType; @@ -44,6 +45,19 @@ namespace mROA.Implementation.Backend _tcpListener.Stop(); } + public void Inject(T dependency) + { + switch (dependency) + { + case IConnectionHub interactionModule: + _hub = interactionModule; + break; + case ISerializationToolkit serializationToolkit: + _serialization = serializationToolkit; + break; + } + } + private void HandleIncomingConnections() { if (_hub is null) @@ -56,7 +70,7 @@ namespace mROA.Implementation.Backend throw new NullReferenceException("InteractionModuleType is null"); if (_serialization is null) throw new NullReferenceException("Serialization is null"); - + while (true) { var client = _tcpListener.AcceptTcpClient(); @@ -67,9 +81,9 @@ namespace mROA.Implementation.Backend interaction!.Inject(injectableModule); interaction!.Inject(_serialization); - + interaction.BaseStream = client.GetStream(); - + interaction.PostMessage(new NetworkMessage { Id = Guid.NewGuid(), SchemaId = MessageType.IdAssigning, @@ -79,13 +93,5 @@ namespace mROA.Implementation.Backend Console.WriteLine("Client registered"); } } - - public void Inject(T dependency) - { - if (dependency is IConnectionHub interactionModule) - _hub = interactionModule; - if (dependency is ISerializationToolkit serializationToolkit) - _serialization = serializationToolkit; - } } } \ No newline at end of file diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index 1d80548..e6351e9 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -13,6 +13,7 @@ namespace mROA.Implementation public int OwnerId { get => OwnerFunc(); + // ReSharper disable once UnusedMember.Global set { OwnerFunc = () => value; } } } diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index d8c9e10..978a4ab 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -98,20 +98,13 @@ namespace mROA.Implementation.Frontend var result = _executeModule.Execute(request, _contextRepository, _representationModule); - var resultType = MessageType.Unknown; - - switch (result) + var resultType = result switch { - case FinalCommandExecution: - resultType = MessageType.FinishedCommandExecution; - break; - case AsyncCommandExecution: - resultType = MessageType.AsyncCommandExecution; - break; - case ExceptionCommandExecution: - resultType = MessageType.ExceptionCommandExecution; - break; - } + FinalCommandExecution => MessageType.FinishedCommandExecution, + AsyncCommandExecution => MessageType.AsyncCommandExecution, + ExceptionCommandExecution => MessageType.ExceptionCommandExecution, + _ => MessageType.Unknown + }; _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); } diff --git a/mROA/Implementation/NextGenerationInteractionModule.cs b/mROA/Implementation/NextGenerationInteractionModule.cs index 5defb89..7ae9170 100644 --- a/mROA/Implementation/NextGenerationInteractionModule.cs +++ b/mROA/Implementation/NextGenerationInteractionModule.cs @@ -81,7 +81,7 @@ namespace mROA.Implementation var secondBit = (byte)BaseStream.ReadByte(); var len = BitConverter.ToUInt16(new[] { firstBit, secondBit }); - var localSpan = _buffer.Slice(0, len); + var localSpan = _buffer[..len]; await BaseStream.ReadExactlyAsync(localSpan); diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index e31ff0a..535db4e 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -60,10 +60,5 @@ namespace mROA.Implementation if (dependency is IRepresentationModuleProducer serialisationModule) _representationProducer = serialisationModule; } - - public object GetObject(int id) - { - throw new NotSupportedException(); - } } } \ No newline at end of file diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 6abd096..14ee815 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -10,6 +10,7 @@ namespace mROA.Implementation { public interface ISharedObjectShell { + // ReSharper disable once UnusedMemberInSuper.Global IEndPointContext EndPointContext { get; set; } ComplexObjectIdentifier Identifier { get; set; } object UniversalValue { get; set; }