From e7e0cbac055d22822ea4efc2c4162704c28dc2da Mon Sep 17 00:00:00 2001 From: Mitrofanov Mikhail Date: Thu, 10 Jul 2025 18:58:49 +0300 Subject: [PATCH] Code cleanup --- Example.Shared/IPage.cs | 1 + Example.Shared/IPrinter.cs | 1 + Example.Shared/IPrinterFactory.cs | 1 + mROA.Codegen/IndexProvider.cstmpl | 2 +- mROA.Codegen/mROASourceGenerator.cs | 14 ++--- mROA/Abstract/ICallIndexProvider.cs | 29 ++------- .../IContextualSerializationToolKit.cs | 2 +- mROA/Abstract/IEndPointContext.cs | 8 +-- mROA/Abstract/IEventBinder.cs | 3 +- mROA/Abstract/IInstanceRepository.cs | 2 +- mROA/Abstract/IRepresentationModule.cs | 1 + mROA/Abstract/ISerializationToolkit.cs | 14 ----- mROA/Abstract/IShared.cs | 3 +- .../Attributes/ApiLevelAttribute.cs | 4 +- .../Attributes/UntrustedAttribute.cs | 1 + .../Backend/BasicExecutionModule.cs | 5 +- mROA/Implementation/Backend/ConnectionHub.cs | 3 +- .../Backend/HubRequestExtractor.cs | 6 +- .../Backend/InstanceRepository.cs | 13 ++-- .../Backend/MultiClientInstanceRepository.cs | 2 +- .../Backend/NetworkGatewayModule.cs | 2 +- mROA/Implementation/Backend/UdpGateway.cs | 1 + .../Bootstrap/FullMixBuilder.cs | 16 ++++- mROA/Implementation/CallRequest.cs | 1 - .../ChannelInteractionModule.cs | 11 +--- .../CollectableMethodRepository.cs | 9 +-- .../ExceptionCommandExecution.cs | 1 + mROA/Implementation/EndPointContext.cs | 4 +- mROA/Implementation/EventBinder.cs | 3 +- .../Frontend/RequestExtractor.cs | 60 ++++++++----------- .../Frontend/UdpUntrustedInteraction.cs | 2 +- mROA/Implementation/IdAssignment.cs | 1 + mROA/Implementation/NetworkMessageHeader.cs | 3 + .../RemoteInstanceRepository.cs | 6 +- mROA/Implementation/RemoteObjectBase.cs | 4 +- mROA/Implementation/RepresentationModule.cs | 8 ++- mROA/Implementation/SharedObjectShell.cs | 4 +- mROA/mROA.csproj | 12 ++-- 38 files changed, 118 insertions(+), 145 deletions(-) delete mode 100644 mROA/Abstract/ISerializationToolkit.cs diff --git a/Example.Shared/IPage.cs b/Example.Shared/IPage.cs index c7517ff..5d3654f 100644 --- a/Example.Shared/IPage.cs +++ b/Example.Shared/IPage.cs @@ -1,3 +1,4 @@ +using mROA.Abstract; using mROA.Implementation; using mROA.Implementation.Attributes; diff --git a/Example.Shared/IPrinter.cs b/Example.Shared/IPrinter.cs index 83138f6..5f96cca 100644 --- a/Example.Shared/IPrinter.cs +++ b/Example.Shared/IPrinter.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using mROA.Abstract; using mROA.Implementation; using mROA.Implementation.Attributes; diff --git a/Example.Shared/IPrinterFactory.cs b/Example.Shared/IPrinterFactory.cs index 60d4d0e..52f20aa 100644 --- a/Example.Shared/IPrinterFactory.cs +++ b/Example.Shared/IPrinterFactory.cs @@ -1,3 +1,4 @@ +using mROA.Abstract; using mROA.Implementation; using mROA.Implementation.Attributes; diff --git a/mROA.Codegen/IndexProvider.cstmpl b/mROA.Codegen/IndexProvider.cstmpl index 33d053c..bec9b09 100644 --- a/mROA.Codegen/IndexProvider.cstmpl +++ b/mROA.Codegen/IndexProvider.cstmpl @@ -30,7 +30,7 @@ namespace }; - public void Inject(T dependency) + public void Inject(object dependency) { } diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 8b015bf..95ef5b1 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -38,7 +38,7 @@ namespace mROA.Codegen private TemplateDocument _interfaceTemplateOriginal; private TemplateDocument _methodInvokerOriginal; private TemplateDocument _methodRepoTemplate; - private int _currentInternalCallIndex = 0; + private int _currentInternalCallIndex; private void GenerateCode(SourceProductionContext context, Compilation compilation, @@ -274,7 +274,7 @@ namespace mROA.Codegen if (isUntrusted) { - caller = $"CallUntrustedAsync(_callIndices[{_currentInternalCallIndex++}]{parameterLink})"; + caller = $"CallUntrustedAsync(CallIndices[{_currentInternalCallIndex++}]{parameterLink})"; } else { @@ -285,10 +285,10 @@ namespace mROA.Codegen : string.Empty; caller = isVoid - ? $"CallAsync(_callIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})" + ? $"CallAsync(CallIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})" : isAsync - ? $"GetResultAsync<{ExtractTaskType(method.ReturnType)}>(_callIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})" - : $"GetResultAsync<{ToFullString(method.ReturnType)}>(_callIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})"; + ? $"GetResultAsync<{ExtractTaskType(method.ReturnType)}>(CallIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})" + : $"GetResultAsync<{ToFullString(method.ReturnType)}>(CallIndices[{_currentInternalCallIndex++}]{parameterLink}{tokenInsert})"; if (!isVoid) prefix = "return " + prefix; @@ -501,7 +501,7 @@ namespace mROA.Codegen } frontend = - $"get => GetResultAsync<{method.ReturnType.ToUnityString()}>(_callIndices[{_currentInternalCallIndex++}]{parametersArray}).GetAwaiter().GetResult();"; + $"get => GetResultAsync<{method.ReturnType.ToUnityString()}>(CallIndices[{_currentInternalCallIndex++}]{parametersArray}).GetAwaiter().GetResult();"; } else { @@ -547,7 +547,7 @@ namespace mROA.Codegen backend = invokerTemplate.Compile(); } - frontend = $"set => CallAsync(_callIndices[{_currentInternalCallIndex++}], new System.Object[] {{ {parametersArray} }}).Wait();"; + frontend = $"set => CallAsync(CallIndices[{_currentInternalCallIndex++}], new System.Object[] {{ {parametersArray} }}).Wait();"; } propsCollection.Add((frontend, method)); diff --git a/mROA/Abstract/ICallIndexProvider.cs b/mROA/Abstract/ICallIndexProvider.cs index bef6018..7dc2b7d 100644 --- a/mROA/Abstract/ICallIndexProvider.cs +++ b/mROA/Abstract/ICallIndexProvider.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using mROA.Implementation; @@ -8,30 +7,12 @@ namespace mROA.Abstract { public interface ICallIndexProvider : IInjectableModule { - Dictionary> Activators { get; } + Dictionary> Activators + { + get; + } + void SetupOffset(int offset); int[] GetIndices(Type type); } - - // public class GeneratedInvokersCollection : IReadOnlyList - // { - // private readonly List _invokers = new() - // { - // - // }; - // - // public IEnumerator GetEnumerator() - // { - // return _invokers.GetEnumerator(); - // } - // - // IEnumerator IEnumerable.GetEnumerator() - // { - // return GetEnumerator(); - // } - // - // public int Count => _invokers.Count; - // - // public IMethodInvoker this[int index] => _invokers[index]; - // } } \ No newline at end of file diff --git a/mROA/Abstract/IContextualSerializationToolKit.cs b/mROA/Abstract/IContextualSerializationToolKit.cs index 9cbcb4f..ca6f5a0 100644 --- a/mROA/Abstract/IContextualSerializationToolKit.cs +++ b/mROA/Abstract/IContextualSerializationToolKit.cs @@ -11,6 +11,6 @@ namespace mROA.Abstract T Deserialize(ReadOnlyMemory rawMemory, IEndPointContext? context); object? Deserialize(ReadOnlyMemory rawMemory, Type type, IEndPointContext? context); T Cast(object nonCasted, IEndPointContext? context); - object? Cast(object nonCasted, Type type, IEndPointContext? context); + object? Cast(object? nonCasted, Type type, IEndPointContext? context); } } \ No newline at end of file diff --git a/mROA/Abstract/IEndPointContext.cs b/mROA/Abstract/IEndPointContext.cs index e66cd85..2e885f6 100644 --- a/mROA/Abstract/IEndPointContext.cs +++ b/mROA/Abstract/IEndPointContext.cs @@ -1,12 +1,10 @@ -using mROA.Implementation; - -namespace mROA.Abstract +namespace mROA.Abstract { public interface IEndPointContext : IInjectableModule { IInstanceRepository RealRepository { get; set; } - IInstanceRepository RemoteRepository { get; set; } - ICallIndexProvider CallIndexProvider { get; set; } + IInstanceRepository RemoteRepository { get; set; } + ICallIndexProvider CallIndexProvider { get; set; } int HostId { get; set; } int OwnerId { get; set; } } diff --git a/mROA/Abstract/IEventBinder.cs b/mROA/Abstract/IEventBinder.cs index 51b11a5..d621bce 100644 --- a/mROA/Abstract/IEventBinder.cs +++ b/mROA/Abstract/IEventBinder.cs @@ -5,7 +5,8 @@ public void BindEvents(T source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, int index); - void IEventBinder.BindEvents(object source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, + void IEventBinder.BindEvents(object source, IEndPointContext context, + IRepresentationModuleProducer representationModuleProducer, int index) { BindEvents((T)source, context, representationModuleProducer, index); diff --git a/mROA/Abstract/IInstanceRepository.cs b/mROA/Abstract/IInstanceRepository.cs index a0886ba..7c48159 100644 --- a/mROA/Abstract/IInstanceRepository.cs +++ b/mROA/Abstract/IInstanceRepository.cs @@ -8,7 +8,7 @@ namespace mROA.Abstract int ResisterObject(object o, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id, IEndPointContext context); T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class; - T GetSingletonObject(IEndPointContext context) where T : class, IShared; + T GetSingletonObject(IEndPointContext context) where T : class, IShared; object GetSingletonObject(Type type, IEndPointContext context); int GetObjectIndex(object o, IEndPointContext context); } diff --git a/mROA/Abstract/IRepresentationModule.cs b/mROA/Abstract/IRepresentationModule.cs index c75d64f..de90b69 100644 --- a/mROA/Abstract/IRepresentationModule.cs +++ b/mROA/Abstract/IRepresentationModule.cs @@ -10,6 +10,7 @@ namespace mROA.Abstract { int Id { get; } IEndPointContext Context { get; } + Task<(object? Deserialized, EMessageType MessageType)> GetSingle(Predicate rule, IEndPointContext? context, CancellationToken token = default, params Func[] converter); diff --git a/mROA/Abstract/ISerializationToolkit.cs b/mROA/Abstract/ISerializationToolkit.cs deleted file mode 100644 index 6761e10..0000000 --- a/mROA/Abstract/ISerializationToolkit.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace mROA.Abstract -{ - // public interface IContextualSerializationToolKit : IInjectableModule - // { - // byte[] Serialize(T objectToSerialize); - // byte[] Serialize(object objectToSerialize, Type type); - // T? Deserialize(byte[] rawData); - // object? Deserialize(byte[] rawData, Type type); - // T? Deserialize(Span rawData); - // object? Deserialize(Span rawData, Type type); - // T? Cast(object? nonCasted); - // object? Cast(object? nonCasted, Type type); - // } -} \ No newline at end of file diff --git a/mROA/Abstract/IShared.cs b/mROA/Abstract/IShared.cs index 6758159..65f365b 100644 --- a/mROA/Abstract/IShared.cs +++ b/mROA/Abstract/IShared.cs @@ -1,6 +1,5 @@ -namespace mROA.Implementation +namespace mROA.Abstract { -#pragma warning disable CS8618, CS9264 public interface IShared { } diff --git a/mROA/Implementation/Attributes/ApiLevelAttribute.cs b/mROA/Implementation/Attributes/ApiLevelAttribute.cs index ccc0146..9a486b1 100644 --- a/mROA/Implementation/Attributes/ApiLevelAttribute.cs +++ b/mROA/Implementation/Attributes/ApiLevelAttribute.cs @@ -2,9 +2,10 @@ using System; namespace mROA.Implementation.Attributes { - public class ApiLevelAttribute : Attribute + public class ApiLevelAttribute : Attribute { public int ApiLevel { get; set; } + public ApiLevelAttribute(int level) { ApiLevel = level; @@ -12,7 +13,6 @@ namespace mROA.Implementation.Attributes public ApiLevelAttribute() : this(0) { - } } } \ No newline at end of file diff --git a/mROA/Implementation/Attributes/UntrustedAttribute.cs b/mROA/Implementation/Attributes/UntrustedAttribute.cs index 244c908..53965aa 100644 --- a/mROA/Implementation/Attributes/UntrustedAttribute.cs +++ b/mROA/Implementation/Attributes/UntrustedAttribute.cs @@ -5,6 +5,7 @@ namespace mROA.Implementation.Attributes public class UntrustedAttribute : Attribute { public bool UseAwait { get; private set; } + public UntrustedAttribute(bool useAwait = true) { UseAwait = useAwait; diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 94c3b0c..6e8bf65 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -30,7 +30,6 @@ namespace mROA.Implementation.Backend public ICommandExecution Execute(ICallRequest command, IInstanceRepository instanceRepository, IRepresentationModule representationModule, IEndPointContext endPointContext) { - try { ThrowIfNotInjected(instanceRepository); @@ -194,7 +193,7 @@ namespace mROA.Implementation.Backend }; _cancellationRepo?.FreeCancelation(command.Id); - + if (invoker.IsTrusted) representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, payload, context); @@ -239,7 +238,7 @@ namespace mROA.Implementation.Backend Result = finalResult }; _cancellationRepo!.FreeCancelation(command.Id); - + representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, payload, context); }); diff --git a/mROA/Implementation/Backend/ConnectionHub.cs b/mROA/Implementation/Backend/ConnectionHub.cs index 33efe13..ed02c65 100644 --- a/mROA/Implementation/Backend/ConnectionHub.cs +++ b/mROA/Implementation/Backend/ConnectionHub.cs @@ -23,7 +23,8 @@ namespace mROA.Implementation.Backend public IChannelInteractionModule GetInteraction(int id) { - return _connections!.GetValueOrDefault(id, null) ?? _connections!.GetValueOrDefault(-id, null) ?? throw new Exception("No connection found"); + return _connections!.GetValueOrDefault(id, null) ?? _connections!.GetValueOrDefault(-id, null) ?? + throw new Exception("No connection found"); } public event ConnectionHandler? OnConnected; diff --git a/mROA/Implementation/Backend/HubRequestExtractor.cs b/mROA/Implementation/Backend/HubRequestExtractor.cs index 42e0155..3358b29 100644 --- a/mROA/Implementation/Backend/HubRequestExtractor.cs +++ b/mROA/Implementation/Backend/HubRequestExtractor.cs @@ -51,7 +51,7 @@ namespace mROA.Implementation.Backend if (_contextRepository is IContextRepositoryHub contextHub) contextHub.FreeRepository(representationModule.Id); } - + private IRequestExtractor CreateExtractor(IRepresentationModule interaction) { var extractor = new RequestExtractor(); @@ -61,9 +61,9 @@ namespace mROA.Implementation.Backend context.RealRepository = contextHub.GetRepository(interaction.Id); else context.RealRepository = _contextRepository!; - + context.RemoteRepository = _remoteContextRepository!; - + extractor.Inject(context); extractor.Inject(_methodRepository); extractor.Inject(_serializationToolkit); diff --git a/mROA/Implementation/Backend/InstanceRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs index 582421d..c12c71c 100644 --- a/mROA/Implementation/Backend/InstanceRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -11,15 +11,10 @@ namespace mROA.Implementation.Backend { public static object[] EventBinders = { }; - private static int LastDebugId = -1; - private int _debugId = -1; - private IRepresentationModuleProducer? _representationModuleProducer; - // [CanBeNull] - private Dictionary _singletons; - private IStorage _storage; - + private Dictionary _singletons = new(); + private readonly IStorage _storage; public InstanceRepository() { @@ -36,7 +31,7 @@ namespace mROA.Implementation.Backend var interfaces = o.GetType().GetInterfaces(); var generic = interfaces.Where(i => sharedType.IsAssignableFrom(i) && i != sharedType) .Select(i => typeof(IEventBinder<>).MakeGenericType(i)); - + var binders = EventBinders.Where(i => generic.Any(g => g.IsAssignableFrom(i.GetType()))); foreach (var binder in binders) ((IEventBinder)binder).BindEvents(o, context, _representationModuleProducer!, last); @@ -49,7 +44,7 @@ namespace mROA.Implementation.Backend _storage.Free(id.ContextId); } - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class { var value = _storage.GetValue(id.ContextId); diff --git a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs index 0e96476..97630ff 100644 --- a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs +++ b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs @@ -32,7 +32,7 @@ namespace mROA.Implementation.Backend repository.ClearObject(id, context); } - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class { var repository = GetRepositoryByClientId(context.OwnerId); return repository.GetObject(id, context); diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 604c1ba..34a18b1 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -82,7 +82,7 @@ namespace mROA.Implementation.Backend { await interaction.ReceiveChanel.Writer.WriteAsync(message); }; - Task.Run(() => streamExtractor.SingleReceive()); + _ = Task.Run(() => streamExtractor.SingleReceive()); var connectionRequest = await interaction.ReceiveChanel.Reader.ReadAsync(); var cts = new CancellationTokenSource(); diff --git a/mROA/Implementation/Backend/UdpGateway.cs b/mROA/Implementation/Backend/UdpGateway.cs index be7786f..fe3f8eb 100644 --- a/mROA/Implementation/Backend/UdpGateway.cs +++ b/mROA/Implementation/Backend/UdpGateway.cs @@ -17,6 +17,7 @@ namespace mROA.Implementation.Backend private CancellationTokenSource _tokenSource = new(); private IContextualSerializationToolKit _serializationToolkit; private IEndPointContext _context; + public UdpGateway(IPEndPoint listeningEndpoint) { _client = new UdpClient(listeningEndpoint); diff --git a/mROA/Implementation/Bootstrap/FullMixBuilder.cs b/mROA/Implementation/Bootstrap/FullMixBuilder.cs index 2129f35..e018a7e 100644 --- a/mROA/Implementation/Bootstrap/FullMixBuilder.cs +++ b/mROA/Implementation/Bootstrap/FullMixBuilder.cs @@ -10,15 +10,27 @@ namespace mROA.Implementation.Bootstrap public void Build() { - foreach (var module in Modules) foreach (var injection in Modules) module.Inject(injection); } - public T? GetModule() + public T GetModule() { return Modules.OfType().FirstOrDefault(); } + + public bool TryGetModule(out IInjectableModule module) where T : IInjectableModule + { + var mod = Modules.OfType().FirstOrDefault(); + if (mod is null) + { + module = default; + return false; + } + + module = mod; + return true; + } } } \ No newline at end of file diff --git a/mROA/Implementation/CallRequest.cs b/mROA/Implementation/CallRequest.cs index b965e5b..397581d 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -23,7 +23,6 @@ namespace mROA.Implementation public override string ToString() { - return $"Call request {{ Id : {Id}, CommandId : {CommandId}, ObjectId : {ObjectId} }}"; } } diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index f8d9606..cc6f57b 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -160,12 +160,11 @@ namespace mROA.Implementation public class StreamExtractor { - private static Stopwatch profiler = new Stopwatch(); + private const int BufferSize = ushort.MaxValue; + private readonly Stream _ioStream; private readonly IContextualSerializationToolKit _serializationToolkit; - private const int BufferSize = ushort.MaxValue; private readonly Memory _buffer = new byte[BufferSize]; - private bool _manualConnectionState = true; private readonly IEndPointContext _context; private readonly byte[] _lenBuffer; @@ -175,7 +174,6 @@ namespace mROA.Implementation _ioStream = ioStream; _serializationToolkit = serializationToolkit; _context = context; - profiler.Start(); _lenBuffer = new byte[2]; } @@ -196,7 +194,6 @@ namespace mROA.Implementation var localSpan = _buffer[..len]; await _ioStream.ReadExactlyAsync(localSpan, cancellationToken: token); - profiler.Restart(); var message = _serializationToolkit.Deserialize(localSpan, _context); MessageReceived(message); } @@ -217,7 +214,6 @@ namespace mROA.Implementation header.CopyTo(_buffer); var sendingSpan = _buffer[..(len + 2)]; await _ioStream.WriteAsync(sendingSpan, token); - profiler.Restart(); } public async Task SendFromChannel(ChannelReader channel, @@ -230,8 +226,7 @@ namespace mROA.Implementation } } - - public bool IsConnected => _ioStream is { CanRead: true, CanWrite: true } && _manualConnectionState; + public bool IsConnected => _ioStream is { CanRead: true, CanWrite: true }; } } } \ No newline at end of file diff --git a/mROA/Implementation/CollectableMethodRepository.cs b/mROA/Implementation/CollectableMethodRepository.cs index 09f4db8..525b7ac 100644 --- a/mROA/Implementation/CollectableMethodRepository.cs +++ b/mROA/Implementation/CollectableMethodRepository.cs @@ -5,25 +5,22 @@ namespace mROA.Implementation { public class CollectableMethodRepository : IMethodRepository { - private List _methods = new(); + private List _methods = new(); + public void Inject(object dependency) { - } public void AppendInvokers(IEnumerable methodInvokers) { _methods.AddRange(methodInvokers); } - + public IMethodInvoker GetMethod(int id) { if (id == -1) return MethodInvoker.Dispose; - if (_methods.Count <= id) - return null; - return _methods[id]; } } diff --git a/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs b/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs index 3199141..ddbc1f3 100644 --- a/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs +++ b/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs @@ -1,5 +1,6 @@ using System; using mROA.Abstract; +using mROA.Implementation.Bootstrap; using mROA.Implementation.Frontend; namespace mROA.Implementation.CommandExecution diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index 03e1b6e..39a2215 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -5,14 +5,14 @@ namespace mROA.Implementation { public class EndPointContext : IEndPointContext { - public IInstanceRepository RealRepository { get; set; } + public IInstanceRepository RealRepository { get; set; } public IInstanceRepository RemoteRepository { get; set; } public ICallIndexProvider CallIndexProvider { get; set; } public CallIndexConfig CallIndexConfig { get; set; } public int HostId { get; set; } public int OwnerId { get; set; } - + public void Inject(object dependency) { diff --git a/mROA/Implementation/EventBinder.cs b/mROA/Implementation/EventBinder.cs index 429a882..3f59cdf 100644 --- a/mROA/Implementation/EventBinder.cs +++ b/mROA/Implementation/EventBinder.cs @@ -5,7 +5,8 @@ namespace mROA.Implementation { public class EventBinder : IEventBinder { - public Action BindAction { get; set; } = (_, _, _, _) => { }; + public Action BindAction { get; set; } = + (_, _, _, _) => { }; public void BindEvents(T source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, int index) diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index b09077a..876fdee 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -43,45 +43,37 @@ namespace mROA.Implementation.Frontend { ThrowIfNotInjected(); + var streamTokenSource = new CancellationTokenSource(); - try + var query = _representationModule!.GetStream(m => + m.MessageType is EMessageType.CallRequest or EMessageType.CancelRequest + or EMessageType.EventRequest or EMessageType.ClientDisconnect, _context, + streamTokenSource.Token, + m => m.MessageType == EMessageType.CallRequest ? typeof(DefaultCallRequest) : null, + m => m.MessageType == EMessageType.CancelRequest ? typeof(CancelRequest) : null, + m => m.MessageType == EMessageType.EventRequest ? typeof(DefaultCallRequest) : null, + m => m.MessageType == EMessageType.ClientDisconnect ? typeof(ClientDisconnect) : null); + + + await foreach (var command in query) { - - var streamTokenSource = new CancellationTokenSource(); - - var query = _representationModule!.GetStream(m => - m.MessageType is EMessageType.CallRequest or EMessageType.CancelRequest - or EMessageType.EventRequest or EMessageType.ClientDisconnect, _context, - streamTokenSource.Token, - m => m.MessageType == EMessageType.CallRequest ? typeof(DefaultCallRequest) : null, - m => m.MessageType == EMessageType.CancelRequest ? typeof(CancelRequest) : null, - m => m.MessageType == EMessageType.EventRequest ? typeof(DefaultCallRequest) : null, - m => m.MessageType == EMessageType.ClientDisconnect ? typeof(ClientDisconnect) : null); - - - await foreach (var command in query) + switch (command.originalType) { - switch (command.originalType) - { - case EMessageType.CallRequest: - HandleCallRequest((DefaultCallRequest)command.parced); - break; - case EMessageType.ClientDisconnect: - return; - case EMessageType.EventRequest: - HandleEventRequest((DefaultCallRequest)command.parced); - break; - case EMessageType.CancelRequest: - HandleCancelRequest((command.parced as CancelRequest)!); - break; - default: - continue; - } + case EMessageType.CallRequest: + HandleCallRequest((DefaultCallRequest)command.parced); + break; + case EMessageType.ClientDisconnect: + return; + case EMessageType.EventRequest: + HandleEventRequest((DefaultCallRequest)command.parced); + break; + case EMessageType.CancelRequest: + HandleCancelRequest((command.parced as CancelRequest)!); + break; + default: + continue; } } - catch - { - } } private void ThrowIfNotInjected() diff --git a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs index 9e2611e..4e40dea 100644 --- a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs +++ b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs @@ -13,6 +13,7 @@ namespace mROA.Implementation.Frontend private IChannelInteractionModule _channelInteractionModule; private CancellationTokenSource _tokenSource = new(); private IEndPointContext _context; + public void Dispose() { _tokenSource.Cancel(); @@ -62,7 +63,6 @@ namespace mROA.Implementation.Frontend var serialized = _serializationToolkit.Serialize(post, _context); await udpClient.SendAsync(serialized, serialized.Length); - } } diff --git a/mROA/Implementation/IdAssignment.cs b/mROA/Implementation/IdAssignment.cs index efc6fef..42b492f 100644 --- a/mROA/Implementation/IdAssignment.cs +++ b/mROA/Implementation/IdAssignment.cs @@ -13,6 +13,7 @@ namespace mROA.Implementation { Id = 0; } + public ClientRecovery(int id) { Id = id; diff --git a/mROA/Implementation/NetworkMessageHeader.cs b/mROA/Implementation/NetworkMessageHeader.cs index e97db87..d5b1c4c 100644 --- a/mROA/Implementation/NetworkMessageHeader.cs +++ b/mROA/Implementation/NetworkMessageHeader.cs @@ -21,10 +21,12 @@ namespace mROA.Implementation } public static readonly NetworkMessageHeader Null = new(); + public NetworkMessageHeader() { Data = Array.Empty(); } + public NetworkMessageHeader(IContextualSerializationToolKit serializationToolkit, INetworkMessage networkMessage, IEndPointContext? context) { @@ -32,6 +34,7 @@ namespace mROA.Implementation Data = serializationToolkit.Serialize(networkMessage, context); Id = Guid.NewGuid(); } + public Guid Id { get; set; } public EMessageType MessageType { get; set; } diff --git a/mROA/Implementation/RemoteInstanceRepository.cs b/mROA/Implementation/RemoteInstanceRepository.cs index 21e2bbd..b360e77 100644 --- a/mROA/Implementation/RemoteInstanceRepository.cs +++ b/mROA/Implementation/RemoteInstanceRepository.cs @@ -32,7 +32,8 @@ namespace mROA.Implementation if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); - if (!_callIndexProvider.Activators.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); + if (!_callIndexProvider.Activators.TryGetValue(typeof(T), out var remoteType)) + throw new NotSupportedException(); var representationModule = _representationProducer.Produce(context.OwnerId); var remote = remoteType(id.ContextId, @@ -56,7 +57,8 @@ namespace mROA.Implementation var representationModule = _representationProducer.Produce(context.OwnerId); - var instance = _callIndexProvider.Activators[type](-1, representationModule, context, _callIndexProvider.GetIndices(type))!; + var instance = _callIndexProvider.Activators[type](-1, representationModule, context, + _callIndexProvider.GetIndices(type))!; var remoteObjectBase = instance; diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index e6f2b8f..a35f78d 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -32,7 +32,7 @@ namespace mROA.Implementation private readonly ComplexObjectIdentifier _identifier; private readonly IRepresentationModule _representationModule; - protected readonly int[] _callIndices; + protected readonly int[] CallIndices; protected RemoteObjectBase(int id, IRepresentationModule representationModule, IEndPointContext context, int[] indices) @@ -40,7 +40,7 @@ namespace mROA.Implementation _identifier = new ComplexObjectIdentifier { ContextId = id, OwnerId = representationModule.Id }; _representationModule = representationModule; _context = context; - _callIndices = indices; + CallIndices = indices; } public int Id => _identifier.ContextId; diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index c3d23ae..0b7e209 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using mROA.Abstract; + #pragma warning disable CS8602 // Dereference of a possibly null reference. namespace mROA.Implementation @@ -71,11 +72,11 @@ namespace mROA.Implementation continue; } - + for (int i = 0; i < converter.Length; i++) { var func = converter[i]; - if (func(message) is {} t) + if (func(message) is { } t) { var deserialized = _serialization.Deserialize(message.Data, t, context); yield return (deserialized, message.MessageType)!; @@ -85,7 +86,8 @@ namespace mROA.Implementation } } - public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull + public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload, + IEndPointContext? context) where T : notnull { if (_interaction == null) throw new NullReferenceException("Interaction toolkit is not initialized"); diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index c2ce05e..5977288 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -60,7 +60,9 @@ namespace mROA.Implementation } } - [SerializationIgnore] [JsonIgnore] public IEndPointContext EndPointContext { get; set; } + [SerializationIgnore] + [JsonIgnore] + public IEndPointContext EndPointContext { get; set; } public ComplexObjectIdentifier Identifier { diff --git a/mROA/mROA.csproj b/mROA/mROA.csproj index 67f1c3d..37ecbb2 100644 --- a/mROA/mROA.csproj +++ b/mROA/mROA.csproj @@ -26,15 +26,15 @@ - - + + - - True - - + + True + +