From f8998178ffe1509710f4ba59cdfbdd3f95dfbc83 Mon Sep 17 00:00:00 2001 From: Mitrofanov Mikhail Date: Sun, 20 Jul 2025 00:20:05 +0300 Subject: [PATCH] Global refactoring with ReSharper --- Example.Load/Program.cs | 1 - Example.Shared/ILoadTest.cs | 1 - Example.Shared/IPage.cs | 1 - Example.Shared/IPrinterFactory.cs | 1 - mROA.Codegen/mROASourceGenerator.cs | 5 +- mROA/Abstract/IChannelInteractionModule.cs | 2 +- mROA/Abstract/IConnectionHub.cs | 6 - .../IContextualSerializationToolKit.cs | 2 - mROA/Abstract/IEventBinder.cs | 2 +- mROA/Abstract/IOwnershipRepository.cs | 8 - mROA/Abstract/IRealStoreInstanceRepository.cs | 1 - mROA/Abstract/IRemoteObjectFactory.cs | 9 - mROA/Abstract/IRequestExtractor.cs | 2 +- .../Backend/BasicExecutionModule.cs | 187 ++++++++---------- mROA/Implementation/Backend/ConnectionHub.cs | 3 - .../Backend/HubRequestExtractor.cs | 4 +- .../Backend/InstanceRepository.cs | 2 - .../Backend/MultiClientInstanceRepository.cs | 2 - .../Backend/NetworkGatewayModule.cs | 30 +-- mROA/Implementation/Backend/UdpGateway.cs | 3 +- mROA/Implementation/CallRequest.cs | 3 - .../ChannelInteractionModule.cs | 14 +- .../CollectableMethodRepository.cs | 5 +- .../CommandExecution/FinalCommandExecution.cs | 2 - .../Implementation/ComplexObjectIdentifier.cs | 8 +- .../CreativeRepresentationModuleProducer.cs | 3 +- mROA/Implementation/EMessageType.cs | 2 +- mROA/Implementation/EndPointContext.cs | 2 +- mROA/Implementation/ExtensibleStorage.cs | 2 +- .../Frontend/NetworkFrontendBridge.cs | 23 ++- .../Frontend/RequestExtractor.cs | 10 +- .../Frontend/StaticOwnershipRepository.cs | 24 --- .../Frontend/UdpUntrustedInteraction.cs | 3 +- mROA/Implementation/IdAssignment.cs | 1 + mROA/Implementation/NetworkMessageHeader.cs | 7 +- .../RemoteInstanceRepository.cs | 7 +- mROA/Implementation/RemoteObjectBase.cs | 6 +- mROA/Implementation/RepresentationModule.cs | 8 +- mROA/Implementation/RequestContext.cs | 2 +- mROA/Implementation/SharedObjectShell.cs | 4 - .../StaticRepresentationModuleProducer.cs | 3 +- mROA/LegacyExtentions.cs | 16 +- mROA/mROA.csproj | 10 +- 43 files changed, 157 insertions(+), 280 deletions(-) delete mode 100644 mROA/Abstract/IOwnershipRepository.cs delete mode 100644 mROA/Abstract/IRemoteObjectFactory.cs delete mode 100644 mROA/Implementation/Frontend/StaticOwnershipRepository.cs diff --git a/Example.Load/Program.cs b/Example.Load/Program.cs index 7188833..ceab60a 100644 --- a/Example.Load/Program.cs +++ b/Example.Load/Program.cs @@ -1,6 +1,5 @@ using System.Net; using Example.Shared; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using mROA.Abstract; diff --git a/Example.Shared/ILoadTest.cs b/Example.Shared/ILoadTest.cs index 54212b3..9ddfdc4 100644 --- a/Example.Shared/ILoadTest.cs +++ b/Example.Shared/ILoadTest.cs @@ -1,7 +1,6 @@ using System.Threading; using System.Threading.Tasks; using mROA.Abstract; -using mROA.Implementation; using mROA.Implementation.Attributes; namespace Example.Shared diff --git a/Example.Shared/IPage.cs b/Example.Shared/IPage.cs index 5d3654f..7f1fa8b 100644 --- a/Example.Shared/IPage.cs +++ b/Example.Shared/IPage.cs @@ -1,5 +1,4 @@ using mROA.Abstract; -using mROA.Implementation; using mROA.Implementation.Attributes; namespace Example.Shared diff --git a/Example.Shared/IPrinterFactory.cs b/Example.Shared/IPrinterFactory.cs index 52f20aa..de7a2b0 100644 --- a/Example.Shared/IPrinterFactory.cs +++ b/Example.Shared/IPrinterFactory.cs @@ -1,5 +1,4 @@ using mROA.Abstract; -using mROA.Implementation; using mROA.Implementation.Attributes; namespace Example.Shared diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 95ef5b1..bd4eba0 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -11,7 +11,6 @@ using Microsoft.CodeAnalysis.Text; using mROA.CodegenTools; using mROA.CodegenTools.Reading; -#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. namespace mROA.Codegen { @@ -314,7 +313,7 @@ namespace mROA.Codegen parametersInsertList.Add("(CancellationToken)special[1]"); break; case "RequestContext": - parametersInsertList.Add("special[0] as RequestContext"); + parametersInsertList.Add("(RequestContext)special[0]"); break; default: parametersInsertList.Add(Caster(parameter.Type, @@ -428,7 +427,7 @@ namespace mROA.Codegen parametersInsertList.Add("(CancellationToken)special[1]"); break; case "RequestContext": - parametersInsertList.Add("special[0] as RequestContext"); + parametersInsertList.Add("(RequestContext)special[0]"); break; default: parametersInsertList.Add(Caster(parameter.i, diff --git a/mROA/Abstract/IChannelInteractionModule.cs b/mROA/Abstract/IChannelInteractionModule.cs index 2358d3d..e7054c0 100644 --- a/mROA/Abstract/IChannelInteractionModule.cs +++ b/mROA/Abstract/IChannelInteractionModule.cs @@ -13,7 +13,7 @@ namespace mROA.Abstract ChannelReader TrustedPostChanel { get; } ChannelReader UntrustedPostChanel { get; } Func IsConnected { get; set; } - ValueTask GetNextMessageReceiving(bool infinite = true); + ValueTask GetNextMessageReceiving(); Task PostMessageAsync(NetworkMessageHeader messageHeader); Task PostMessageUntrustedAsync(NetworkMessageHeader messageHeader); event Action OnDisconnected; diff --git a/mROA/Abstract/IConnectionHub.cs b/mROA/Abstract/IConnectionHub.cs index 2b5e6c6..e547d48 100644 --- a/mROA/Abstract/IConnectionHub.cs +++ b/mROA/Abstract/IConnectionHub.cs @@ -1,14 +1,8 @@ namespace mROA.Abstract { - public delegate void ConnectionHandler(IRepresentationModule representationModule); - - public delegate void DisconnectionHandler(IRepresentationModule representationModule); - public interface IConnectionHub { void RegisterInteraction(IChannelInteractionModule interaction); IChannelInteractionModule GetInteraction(int id); - event ConnectionHandler? OnConnected; - event DisconnectionHandler? OnDisconnected; } } \ No newline at end of file diff --git a/mROA/Abstract/IContextualSerializationToolKit.cs b/mROA/Abstract/IContextualSerializationToolKit.cs index 906657b..ebf604c 100644 --- a/mROA/Abstract/IContextualSerializationToolKit.cs +++ b/mROA/Abstract/IContextualSerializationToolKit.cs @@ -9,8 +9,6 @@ namespace mROA.Abstract T Deserialize(byte[] rawData, IEndPointContext? context); object? Deserialize(byte[] rawData, Type type, IEndPointContext? context); 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); } } \ No newline at end of file diff --git a/mROA/Abstract/IEventBinder.cs b/mROA/Abstract/IEventBinder.cs index d621bce..6216cbd 100644 --- a/mROA/Abstract/IEventBinder.cs +++ b/mROA/Abstract/IEventBinder.cs @@ -1,6 +1,6 @@ namespace mROA.Abstract { - public interface IEventBinder : IEventBinder + public interface IEventBinder : IEventBinder { public void BindEvents(T source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, int index); diff --git a/mROA/Abstract/IOwnershipRepository.cs b/mROA/Abstract/IOwnershipRepository.cs deleted file mode 100644 index 55ec3f3..0000000 --- a/mROA/Abstract/IOwnershipRepository.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace mROA.Abstract -{ - public interface IOwnershipRepository - { - int GetOwnershipId(); - int GetHostOwnershipId(); - } -} \ No newline at end of file diff --git a/mROA/Abstract/IRealStoreInstanceRepository.cs b/mROA/Abstract/IRealStoreInstanceRepository.cs index b740cfa..bccf32a 100644 --- a/mROA/Abstract/IRealStoreInstanceRepository.cs +++ b/mROA/Abstract/IRealStoreInstanceRepository.cs @@ -2,6 +2,5 @@ namespace mROA.Abstract { public interface IRealStoreInstanceRepository : IInstanceRepository { - } } \ No newline at end of file diff --git a/mROA/Abstract/IRemoteObjectFactory.cs b/mROA/Abstract/IRemoteObjectFactory.cs deleted file mode 100644 index ac17640..0000000 --- a/mROA/Abstract/IRemoteObjectFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using mROA.Implementation; - -namespace mROA.Abstract -{ - public interface IRemoteObjectFactory - { - T Produce(ComplexObjectIdentifier id, IEndPointContext context); - } -} \ No newline at end of file diff --git a/mROA/Abstract/IRequestExtractor.cs b/mROA/Abstract/IRequestExtractor.cs index 2cc176f..d4a3c44 100644 --- a/mROA/Abstract/IRequestExtractor.cs +++ b/mROA/Abstract/IRequestExtractor.cs @@ -9,6 +9,6 @@ namespace mROA.Abstract Task StartExtraction(); void PushMessage(object parced, EMessageType originalType); Predicate Rule { get; } - Func[] Converters { get; } + Func[] Converters { get; } } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index d8ce917..91c5a1d 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -1,6 +1,5 @@ using System; using System.Threading; -using Microsoft.Extensions.Logging; using mROA.Abstract; using mROA.Implementation.CommandExecution; @@ -11,14 +10,13 @@ namespace mROA.Implementation.Backend private readonly ICancellationRepository _cancellationRepo; private readonly IMethodRepository _methodRepo; private readonly IContextualSerializationToolKit _serialization; - private readonly ILogger _logger; - public BasicExecutionModule(ICancellationRepository cancellationRepo, IMethodRepository methodRepo, IContextualSerializationToolKit serialization, ILogger logger) + public BasicExecutionModule(ICancellationRepository cancellationRepo, IMethodRepository methodRepo, + IContextualSerializationToolKit serialization) { _cancellationRepo = cancellationRepo; _methodRepo = methodRepo; _serialization = serialization; - _logger = logger; } public ICommandExecution Execute(ICallRequest command, IInstanceRepository instanceRepository, @@ -36,9 +34,9 @@ namespace mROA.Implementation.Backend if (invoker == null) throw new Exception($"Command {command.CommandId} not found"); - var context = GetContext(command, instanceRepository, invoker, endPointContext); + var instance = GetInstance(command, instanceRepository, invoker, endPointContext); - if (context == null) + if (instance is null) throw new NullReferenceException("Instance can't be null"); @@ -50,24 +48,10 @@ namespace mROA.Implementation.Backend var execContext = new RequestContext(command.Id, representationModule.Id); - switch (invoker) - { - case AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker: - return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, - _cancellationRepo, - representationModule, execContext, endPointContext); - case AsyncMethodInvoker asyncMethodInvoker: - return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, - representationModule, execContext, endPointContext); - default: - var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); - if (command.CommandId == -1) - { - instanceRepository.ClearObject(command.ObjectId, endPointContext); - } - - return result; - } + var executionResult = ExecuteRequest(command, instanceRepository, representationModule, endPointContext, + invoker, + instance, castedParams, execContext); + return executionResult; } catch (Exception e) { @@ -79,12 +63,37 @@ namespace mROA.Implementation.Backend } } - private static object GetContext(ICallRequest command, IInstanceRepository instanceRepository, + private ICommandExecution ExecuteRequest(ICallRequest command, IInstanceRepository instanceRepository, + IRepresentationModule representationModule, IEndPointContext endPointContext, IMethodInvoker invoker, + object context, object?[]? castedParams, RequestContext execContext) + { + switch (invoker) + { + case AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker: + return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, + _cancellationRepo, + representationModule, execContext, endPointContext); + case AsyncMethodInvoker asyncMethodInvoker: + return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, + representationModule, execContext, endPointContext); + default: + var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); + if (command.CommandId == -1) + { + instanceRepository.ClearObject(command.ObjectId, endPointContext); + } + + return result; + } + } + + private static object GetInstance(ICallRequest command, IInstanceRepository instanceRepository, IMethodInvoker invoker, IEndPointContext endPointContext) { var context = command.ObjectId.ContextId != -1 ? instanceRepository.GetObject(command.ObjectId, endPointContext) : instanceRepository.GetSingletonObject(invoker.SuitableType, endPointContext); + return context; } @@ -116,42 +125,26 @@ namespace mROA.Implementation.Backend private static ICommandExecution Execute(MethodInvoker invoker, object instance, object?[] parameter, ICallRequest command, RequestContext executionContext) { - try + var finalResult = invoker.Invoke(instance, parameter, new object[] { executionContext }); + + if (!invoker.IsTrusted) { - var finalResult = invoker.Invoke(instance, parameter, new object[] { executionContext }); - - if (!invoker.IsTrusted) - { - return new AsyncCommandExecution(); - } - - if (invoker.IsVoid) - { - return new FinalCommandExecution - { - Id = command.Id - }; - } - - return new FinalCommandExecution - { - Result = finalResult, - Id = command.Id - }; + return new AsyncCommandExecution(); } - catch (Exception e) + + if (invoker.IsVoid) { - if (invoker.IsTrusted) - return new ExceptionCommandExecution - { - Id = command.Id, - Exception = e.ToString() - }; - return new AsyncCommandExecution + return new FinalCommandExecution { Id = command.Id }; } + + return new FinalCommandExecution + { + Result = finalResult, + Id = command.Id + }; } private ICommandExecution ExecuteAsync(AsyncMethodInvoker invoker, object instance, object?[]? parameters, @@ -162,43 +155,27 @@ namespace mROA.Implementation.Backend cancellationRepository.RegisterCancellation(command.Id, tokenSource); var token = tokenSource.Token; - try + invoker.Invoke(instance, parameters, new object[] { executionContext, token }, _ => { - invoker.Invoke(instance, parameters, new object[] { executionContext, token }, _ => - { - if (token.IsCancellationRequested) - return; + if (token.IsCancellationRequested) + return; - var payload = new FinalCommandExecution - { - Id = command.Id - }; - _cancellationRepo?.FreeCancelation(command.Id); - - - if (invoker.IsTrusted) - representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, - payload, context); - }); - - return new AsyncCommandExecution + var payload = new FinalCommandExecution { Id = command.Id }; - } - catch (Exception e) - { + _cancellationRepo.FreeCancelation(command.Id); + + if (invoker.IsTrusted) - return new ExceptionCommandExecution - { - Id = command.Id, - Exception = e.ToString() - }; - return new AsyncCommandExecution - { - Id = command.Id - }; - } + representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, + payload, context); + }); + + return new AsyncCommandExecution + { + Id = command.Id + }; } private ICommandExecution TypedExecuteAsync(AsyncMethodInvoker invoker, object instance, object?[]? parameters, @@ -209,35 +186,25 @@ namespace mROA.Implementation.Backend cancellationRepository.RegisterCancellation(command.Id, tokenSource); var token = tokenSource.Token; - try - { - invoker.Invoke(instance, parameters, new object[] { executionContext, token }, - finalResult => + + invoker.Invoke(instance, parameters, new object[] { executionContext, token }, + finalResult => + { + var payload = new FinalCommandExecution { - var payload = new FinalCommandExecution - { - Id = command.Id, - Result = finalResult - }; - _cancellationRepo.FreeCancelation(command.Id); + Id = command.Id, + Result = finalResult + }; + _cancellationRepo.FreeCancelation(command.Id); - representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, - payload, context); - }); + representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, + payload, context); + }); - return new AsyncCommandExecution - { - Id = command.Id - }; - } - catch (Exception e) + return new AsyncCommandExecution { - return new ExceptionCommandExecution - { - Id = command.Id, - Exception = e.ToString() - }; - } + Id = command.Id + }; } } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/ConnectionHub.cs b/mROA/Implementation/Backend/ConnectionHub.cs index bfc72e9..5f865b1 100644 --- a/mROA/Implementation/Backend/ConnectionHub.cs +++ b/mROA/Implementation/Backend/ConnectionHub.cs @@ -18,8 +18,5 @@ namespace mROA.Implementation.Backend return _connections!.GetValueOrDefault(id, null) ?? _connections!.GetValueOrDefault(-id, null) ?? throw new Exception("No connection found"); } - - public event ConnectionHandler? OnConnected; - public event DisconnectionHandler? OnDisconnected; } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/HubRequestExtractor.cs b/mROA/Implementation/Backend/HubRequestExtractor.cs index 4a3c48a..a7f27f5 100644 --- a/mROA/Implementation/Backend/HubRequestExtractor.cs +++ b/mROA/Implementation/Backend/HubRequestExtractor.cs @@ -11,7 +11,7 @@ namespace mROA.Implementation.Backend private readonly IInstanceRepository _remoteContextRepository; private readonly IExecuteModule _executeModule; private readonly DistributionOptions _mode; - private Dictionary _producedExtractors = new(); + private readonly Dictionary _producedExtractors = new(); public HubRequestExtractor(IRealStoreInstanceRepository contextRepository, IInstanceRepository remoteContextRepository, IExecuteModule executeModule, @@ -24,7 +24,7 @@ namespace mROA.Implementation.Backend } public IRequestExtractor this[int id] => _producedExtractors[id]; - + public IRequestExtractor HubOnOnConnected(IRepresentationModule interaction) { var extractor = CreateExtractor(interaction); diff --git a/mROA/Implementation/Backend/InstanceRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs index cfd42ec..1f568c4 100644 --- a/mROA/Implementation/Backend/InstanceRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -22,8 +22,6 @@ namespace mROA.Implementation.Backend _storage = new ExtensibleStorage(); } - public int HostId { get; set; } - public int ResisterObject(object o, IEndPointContext context) { var last = _storage.Place(o); diff --git a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs index 47e8c31..0488e21 100644 --- a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs +++ b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs @@ -14,8 +14,6 @@ namespace mROA.Implementation.Backend _produceRepository = produceRepository; } - public int HostId { get; set; } - public int ResisterObject(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(context.OwnerId); diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 701cc79..189496a 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -24,7 +24,8 @@ namespace mROA.Implementation.Backend public NetworkGatewayModule(IOptions options, IIdentityGenerator identityGenerator, IContextualSerializationToolKit serialization, ICallIndexProvider callIndexProvider, IConnectionHub hub, - IOptions distribution, HubRequestExtractor hre, ILogger logger) + IOptions distribution, HubRequestExtractor hre, + ILogger logger) { _tcpListener = new(options.Value.Endpoint); _identityGenerator = identityGenerator; @@ -69,7 +70,7 @@ namespace mROA.Implementation.Backend CallIndexProvider = _callIndexProvider }; var streamExtractor = - new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context, _logger); + new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context); interaction.IsConnected = () => streamExtractor.IsConnected; streamExtractor.MessageReceived = async message => { @@ -106,7 +107,7 @@ namespace mROA.Implementation.Backend interaction.PostMessageAsync(new NetworkMessageHeader(_serialization, new IdAssignment { Id = interaction.ConnectionId }, null)); _extractorsTokenSources[interaction.ConnectionId] = cts; - + _hub.RegisterInteraction(interaction); var requestExtractor = _hre.HubOnOnConnected(new RepresentationModule(interaction, _serialization)); @@ -124,20 +125,19 @@ namespace mROA.Implementation.Backend { if (requestExtractor.Rule(message)) { - for (int i = 0; i < converters.Length; i++) + for (var i = 0; i < converters.Length; i++) { var func = converters[i]; - if (func(message) is { } t) - { - var deserialized = _serialization.Deserialize(message.Data, t, context); - Task.Run(() => requestExtractor.PushMessage(deserialized, message.MessageType)); - break; - } - } + if (func(message) is not { } t) continue; + var deserialized = _serialization.Deserialize(message.Data, t, context); + Task.Run(() => requestExtractor.PushMessage(deserialized, message.MessageType)); + break; + } + return; } - + interaction.ReceiveChanel.Writer.WriteAsync(message).ConfigureAwait(false); }; } @@ -160,9 +160,10 @@ namespace mROA.Implementation.Backend if (_distribution.DistributionType == EDistributionType.ExtractorFirst) { - BindRequestFirstDistribution(recoveryInteraction.Context, recoveryInteraction, streamExtractor, _hre[recoveryInteraction.ConnectionId]); + BindRequestFirstDistribution(recoveryInteraction.Context, recoveryInteraction, streamExtractor, + _hre[recoveryInteraction.ConnectionId]); } - + Task.Run(async () => await streamExtractor.LoopedReceive(cts.Token).ConfigureAwait(false)); recoveryInteraction.Restart(false); @@ -172,6 +173,5 @@ namespace mROA.Implementation.Backend public class GatewayOptions { public IPEndPoint Endpoint { get; set; } - public Type InteractionModuleType { get; set; } } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/UdpGateway.cs b/mROA/Implementation/Backend/UdpGateway.cs index 20b56b2..12a5297 100644 --- a/mROA/Implementation/Backend/UdpGateway.cs +++ b/mROA/Implementation/Backend/UdpGateway.cs @@ -18,7 +18,8 @@ namespace mROA.Implementation.Backend private readonly CancellationTokenSource _tokenSource = new(); private readonly IContextualSerializationToolKit _serializationToolkit; - public UdpGateway(IOptions options, IConnectionHub hub, IContextualSerializationToolKit serializationToolkit) + public UdpGateway(IOptions options, IConnectionHub hub, + IContextualSerializationToolKit serializationToolkit) { _hub = hub; _serializationToolkit = serializationToolkit; diff --git a/mROA/Implementation/CallRequest.cs b/mROA/Implementation/CallRequest.cs index 397581d..cf4834b 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -1,8 +1,5 @@ using System; -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global - namespace mROA.Implementation { public interface ICallRequest diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index 6fd3b38..154c5fa 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -1,10 +1,8 @@ using System; -using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using mROA.Abstract; namespace mROA.Implementation @@ -50,7 +48,7 @@ namespace mROA.Implementation _untrustedWriter = _outputUntrustedChannel.Writer; _reconnection = new TaskCompletionSource(); } - + public int ConnectionId { get; set; } public IEndPointContext Context { get; set; } @@ -60,10 +58,11 @@ namespace mROA.Implementation public ChannelReader UntrustedPostChanel => _outputUntrustedChannel.Reader; public Func IsConnected { get; set; } = () => false; - public ValueTask GetNextMessageReceiving(bool infinite = true) + public ValueTask GetNextMessageReceiving() { return _receiveReader.ReadAsync(); } + private async ValueTask PostMessageInternal(NetworkMessageHeader messageHeader) { if (!IsConnected()) @@ -148,16 +147,14 @@ namespace mROA.Implementation private readonly IContextualSerializationToolKit _serializationToolkit; private readonly Memory _buffer = new byte[BufferSize]; private readonly IEndPointContext _context; - private readonly ILogger _logger; private readonly byte[] _lenBuffer; public StreamExtractor(Stream ioStream, IContextualSerializationToolKit serializationToolkit, - IEndPointContext context, ILogger logger) + IEndPointContext context) { _ioStream = ioStream; _serializationToolkit = serializationToolkit; _context = context; - _logger = logger; _lenBuffer = new byte[2]; } @@ -168,7 +165,7 @@ namespace mROA.Implementation await _ioStream.ReadAsync(_lenBuffer); var len = BitConverter.ToUInt16(_lenBuffer); - + return len; } @@ -200,7 +197,6 @@ namespace mROA.Implementation var sendingSpan = _buffer[..(len + 2)]; await _ioStream.WriteAsync(sendingSpan, token); // _logger.LogTrace("SEND {0}", message.ToString()); - } public async Task SendFromChannel(ChannelReader channel, diff --git a/mROA/Implementation/CollectableMethodRepository.cs b/mROA/Implementation/CollectableMethodRepository.cs index 135380f..b52ef2e 100644 --- a/mROA/Implementation/CollectableMethodRepository.cs +++ b/mROA/Implementation/CollectableMethodRepository.cs @@ -14,10 +14,7 @@ namespace mROA.Implementation public IMethodInvoker GetMethod(int id) { - if (id == -1) - return MethodInvoker.Dispose; - - return _methods[id]; + return id == -1 ? MethodInvoker.Dispose : _methods[id]; } } } \ No newline at end of file diff --git a/mROA/Implementation/CommandExecution/FinalCommandExecution.cs b/mROA/Implementation/CommandExecution/FinalCommandExecution.cs index f22723f..5911109 100644 --- a/mROA/Implementation/CommandExecution/FinalCommandExecution.cs +++ b/mROA/Implementation/CommandExecution/FinalCommandExecution.cs @@ -1,8 +1,6 @@ using System; using mROA.Abstract; -// ReSharper disable UnusedAutoPropertyAccessor.Global - namespace mROA.Implementation.CommandExecution { public struct FinalCommandExecution : ICommandExecution diff --git a/mROA/Implementation/ComplexObjectIdentifier.cs b/mROA/Implementation/ComplexObjectIdentifier.cs index ae2e4e0..a9d91f2 100644 --- a/mROA/Implementation/ComplexObjectIdentifier.cs +++ b/mROA/Implementation/ComplexObjectIdentifier.cs @@ -2,7 +2,6 @@ using System; namespace mROA.Implementation { -#pragma warning disable CS8618, CS9264 public struct ComplexObjectIdentifier : IEquatable { public int ContextId; @@ -14,14 +13,9 @@ namespace mROA.Implementation OwnerId = ownerId; } - public static ComplexObjectIdentifier Singleton(int ownerId) => new() { ContextId = -1, OwnerId = ownerId }; - - public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = 0 }; + public static ComplexObjectIdentifier Null = new() { ContextId = -2, OwnerId = 0 }; public static ComplexObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat }; - public int ClientId => Math.Abs(OwnerId); - public bool IsSererStored => OwnerId > 0; - public bool IsClientStored => OwnerId < 0; public override string ToString() { diff --git a/mROA/Implementation/CreativeRepresentationModuleProducer.cs b/mROA/Implementation/CreativeRepresentationModuleProducer.cs index c5eaf6d..cfd2cb1 100644 --- a/mROA/Implementation/CreativeRepresentationModuleProducer.cs +++ b/mROA/Implementation/CreativeRepresentationModuleProducer.cs @@ -6,6 +6,7 @@ namespace mROA.Implementation { private readonly IConnectionHub _hub; private readonly IContextualSerializationToolKit _serialization; + public CreativeRepresentationModuleProducer(IConnectionHub hub, IContextualSerializationToolKit serialization) { _hub = hub; @@ -16,7 +17,7 @@ namespace mROA.Implementation public IRepresentationModule Produce(int id) { var interaction = _hub.GetInteraction(id); - + var produced = new RepresentationModule(interaction, _serialization); return produced; diff --git a/mROA/Implementation/EMessageType.cs b/mROA/Implementation/EMessageType.cs index d53aa72..205e85d 100644 --- a/mROA/Implementation/EMessageType.cs +++ b/mROA/Implementation/EMessageType.cs @@ -12,6 +12,6 @@ namespace mROA.Implementation ClientRecovery, ClientConnect, ClientDisconnect, - UntrustedConnect, + UntrustedConnect } } \ No newline at end of file diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index 9f8ad12..ac0324f 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -6,8 +6,8 @@ namespace mROA.Implementation { public EndPointContext() { - } + public EndPointContext(IRealStoreInstanceRepository realRepository, IInstanceRepository remoteRepository) { RealRepository = realRepository; diff --git a/mROA/Implementation/ExtensibleStorage.cs b/mROA/Implementation/ExtensibleStorage.cs index 6543519..498262a 100644 --- a/mROA/Implementation/ExtensibleStorage.cs +++ b/mROA/Implementation/ExtensibleStorage.cs @@ -55,7 +55,7 @@ namespace mROA.Implementation public void Free(int index) { _freePlaces.AddFirst(index); - _array[index] = default; + _array[index] = null; } } } \ No newline at end of file diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index 40159ee..7203285 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -1,9 +1,8 @@ -using System; +using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using mROA.Abstract; using mROA.Implementation.Backend; @@ -16,20 +15,20 @@ namespace mROA.Implementation.Frontend private readonly IPEndPoint _serverEndPoint; private TcpClient _tcpClient = new(); private readonly IChannelInteractionModule _interactionModule; - private readonly ILogger _logger; private readonly IContextualSerializationToolKit _serialization; - private ChannelInteractionModule.StreamExtractor? _currentExtractor; + private ChannelInteractionModule.StreamExtractor _currentExtractor; private CancellationTokenSource _rawExtractorCancellation; private readonly IEndPointContext _context; - public NetworkFrontendBridge(IOptions options, IEndPointContext context, IContextualSerializationToolKit serialization, IChannelInteractionModule interactionModule, ILogger logger) + public NetworkFrontendBridge(IOptions options, IEndPointContext context, + IContextualSerializationToolKit serialization, IChannelInteractionModule interactionModule) { _serverEndPoint = options.Value.Endpoint; _context = context; _serialization = serialization; _interactionModule = interactionModule; - _logger = logger; _rawExtractorCancellation = new CancellationTokenSource(); + _currentExtractor = new ChannelInteractionModule.StreamExtractor(Stream.Null, _serialization, context); } public async Task Connect() @@ -38,13 +37,13 @@ namespace mROA.Implementation.Frontend _tcpClient.NoDelay = true; PrepareExtractor(); _interactionModule.IsConnected = () => _currentExtractor.IsConnected; - _interactionModule.OnDisconnected += _ => { Reconnect(); }; + _interactionModule.OnDisconnected += _ => { Reconnect().ConfigureAwait(false); }; _interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect(), _context)) .Wait(); - _currentExtractor.SingleReceive(); - var idMessage = await _interactionModule.GetNextMessageReceiving(false); + _ = _currentExtractor.SingleReceive().ConfigureAwait(false); + var idMessage = await _interactionModule.GetNextMessageReceiving(); if (idMessage.MessageType != EMessageType.IdAssigning) { @@ -53,7 +52,7 @@ namespace mROA.Implementation.Frontend } - Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); + _ = Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); var assignment = _serialization.Deserialize(idMessage.Data, _context); _interactionModule.ConnectionId = -assignment.Id; @@ -64,7 +63,7 @@ namespace mROA.Implementation.Frontend private void PrepareExtractor() { _currentExtractor = - new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization, _context, _logger); + new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization, _context); _ = _currentExtractor.SendFromChannel(_interactionModule.TrustedPostChanel, _rawExtractorCancellation.Token); @@ -84,7 +83,7 @@ namespace mROA.Implementation.Frontend PrepareExtractor(); - Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); + _ = Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); await _interactionModule.Restart(true); } diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index 0ebb92a..1db877d 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -3,19 +3,18 @@ using System.Threading; using System.Threading.Tasks; using mROA.Abstract; -// ReSharper disable MethodHasAsyncOverload namespace mROA.Implementation.Frontend { public class RequestExtractor : IRequestExtractor { - private readonly IExecuteModule _executeModule; private readonly IRepresentationModule _representationModule; private readonly IEndPointContext _context; - public RequestExtractor(IExecuteModule executeModule, IRepresentationModule representationModule, IEndPointContext context) + public RequestExtractor(IExecuteModule executeModule, IRepresentationModule representationModule, + IEndPointContext context) { _executeModule = executeModule; _representationModule = representationModule; @@ -53,7 +52,7 @@ namespace mROA.Implementation.Frontend HandleCancelRequest((parced as CancelRequest)!); break; default: - return; + throw new ArgumentOutOfRangeException(); } } @@ -61,7 +60,8 @@ namespace mROA.Implementation.Frontend m.MessageType is EMessageType.CallRequest or EMessageType.CancelRequest or EMessageType.EventRequest or EMessageType.ClientDisconnect; - public Func[] Converters { get; } = { + public Func[] Converters { get; } = + { m => m.MessageType == EMessageType.CallRequest ? typeof(DefaultCallRequest) : null, m => m.MessageType == EMessageType.CancelRequest ? typeof(CancelRequest) : null, m => m.MessageType == EMessageType.EventRequest ? typeof(DefaultCallRequest) : null, diff --git a/mROA/Implementation/Frontend/StaticOwnershipRepository.cs b/mROA/Implementation/Frontend/StaticOwnershipRepository.cs deleted file mode 100644 index 1739824..0000000 --- a/mROA/Implementation/Frontend/StaticOwnershipRepository.cs +++ /dev/null @@ -1,24 +0,0 @@ -using mROA.Abstract; - -namespace mROA.Implementation.Frontend -{ - public class StaticOwnershipRepository : IOwnershipRepository - { - private readonly int _id; - - public StaticOwnershipRepository(int id) - { - _id = id; - } - - public int GetOwnershipId() - { - return _id; - } - - public int GetHostOwnershipId() - { - return _id; - } - } -} \ No newline at end of file diff --git a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs index 183be31..b663248 100644 --- a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs +++ b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs @@ -14,7 +14,8 @@ namespace mROA.Implementation.Frontend private readonly CancellationTokenSource _tokenSource = new(); private readonly IEndPointContext _context; - public UdpUntrustedInteraction(IContextualSerializationToolKit serializationToolkit, IChannelInteractionModule channelInteractionModule, IEndPointContext context) + public UdpUntrustedInteraction(IContextualSerializationToolKit serializationToolkit, + IChannelInteractionModule channelInteractionModule, IEndPointContext context) { _serializationToolkit = serializationToolkit; _channelInteractionModule = channelInteractionModule; diff --git a/mROA/Implementation/IdAssignment.cs b/mROA/Implementation/IdAssignment.cs index 42b492f..f32050f 100644 --- a/mROA/Implementation/IdAssignment.cs +++ b/mROA/Implementation/IdAssignment.cs @@ -9,6 +9,7 @@ namespace mROA.Implementation public class ClientRecovery : INetworkMessage { + // ReSharper disable once UnusedMember.Global public ClientRecovery() { Id = 0; diff --git a/mROA/Implementation/NetworkMessageHeader.cs b/mROA/Implementation/NetworkMessageHeader.cs index d5b1c4c..39715d5 100644 --- a/mROA/Implementation/NetworkMessageHeader.cs +++ b/mROA/Implementation/NetworkMessageHeader.cs @@ -1,8 +1,6 @@ using System; using mROA.Abstract; -// ReSharper disable UnusedMember.Global - namespace mROA.Implementation { public class NetworkMessageHeader @@ -16,12 +14,9 @@ namespace mROA.Implementation { if (obj is null) return false; if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals((NetworkMessageHeader)obj); + return obj.GetType() == GetType() && Equals((NetworkMessageHeader)obj); } - public static readonly NetworkMessageHeader Null = new(); - public NetworkMessageHeader() { Data = Array.Empty(); diff --git a/mROA/Implementation/RemoteInstanceRepository.cs b/mROA/Implementation/RemoteInstanceRepository.cs index 76ad9ab..dec5c9c 100644 --- a/mROA/Implementation/RemoteInstanceRepository.cs +++ b/mROA/Implementation/RemoteInstanceRepository.cs @@ -12,7 +12,8 @@ namespace mROA.Implementation private readonly IRepresentationModuleProducer _representationProducer; - public RemoteInstanceRepository(ICallIndexProvider callIndexProvider, IRepresentationModuleProducer representationProducer) + public RemoteInstanceRepository(ICallIndexProvider callIndexProvider, + IRepresentationModuleProducer representationProducer) { _callIndexProvider = callIndexProvider; _representationProducer = representationProducer; @@ -60,9 +61,7 @@ namespace mROA.Implementation var instance = _callIndexProvider.Activators[type](-1, representationModule, context, _callIndexProvider.GetIndices(type))!; - var remoteObjectBase = instance; - - _producedProxies.Add(remoteObjectBase); + _producedProxies.Add(instance); return _producedProxies.Last(); } diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index a35f78d..bc70cf1 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using mROA.Abstract; using mROA.Implementation.CommandExecution; -// ReSharper disable UnusedMember.Global namespace mROA.Implementation { @@ -21,8 +20,7 @@ namespace mROA.Implementation { if (obj is null) return false; if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals((RemoteObjectBase)obj); + return obj.GetType() == GetType() && Equals((RemoteObjectBase)obj); } public override int GetHashCode() @@ -142,6 +140,8 @@ namespace mROA.Implementation return; case EMessageType.ExceptionCommandExecution: throw (responseRequest.Deserialized as ExceptionCommandExecution)!.GetException(); + default: + throw new ArgumentOutOfRangeException(); } } diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index 735ee27..ccb20b8 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -6,7 +6,6 @@ using System.Threading; using System.Threading.Tasks; using mROA.Abstract; -#pragma warning disable CS8602 // Dereference of a possibly null reference. namespace mROA.Implementation { @@ -15,7 +14,8 @@ namespace mROA.Implementation private readonly IChannelInteractionModule _interaction; private readonly IContextualSerializationToolKit _serialization; - public RepresentationModule(IChannelInteractionModule interaction, IContextualSerializationToolKit serialization) + public RepresentationModule(IChannelInteractionModule interaction, + IContextualSerializationToolKit serialization) { _interaction = interaction; _serialization = serialization; @@ -56,7 +56,7 @@ namespace mROA.Implementation [EnumeratorCancellation] CancellationToken token = default, params Func[] converter) { - var writer = _interaction?.ReceiveChanel.Writer; + var writer = _interaction.ReceiveChanel.Writer; await foreach (var message in _interaction.ReceiveChanel.Reader.ReadAllAsync(token)) { if (!rule(message)) @@ -66,7 +66,7 @@ namespace mROA.Implementation } - for (int i = 0; i < converter.Length; i++) + for (var i = 0; i < converter.Length; i++) { var func = converter[i]; if (func(message) is { } t) diff --git a/mROA/Implementation/RequestContext.cs b/mROA/Implementation/RequestContext.cs index 9daf956..81db0d6 100644 --- a/mROA/Implementation/RequestContext.cs +++ b/mROA/Implementation/RequestContext.cs @@ -2,7 +2,7 @@ using System; namespace mROA.Implementation { - public sealed class RequestContext + public struct RequestContext { public int OwnerId { get; } public Guid RequestId { get; } diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 5977288..b85d910 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -3,14 +3,10 @@ using System.Text.Json.Serialization; using mROA.Abstract; using mROA.Implementation.Attributes; -// ReSharper disable UnusedMember.Global -// #pragma warning disable CS8618, CS9264 - namespace mROA.Implementation { public interface ISharedObjectShell { - // ReSharper disable once UnusedMemberInSuper.Global IEndPointContext EndPointContext { get; set; } ComplexObjectIdentifier Identifier { get; set; } object UniversalValue { get; set; } diff --git a/mROA/Implementation/StaticRepresentationModuleProducer.cs b/mROA/Implementation/StaticRepresentationModuleProducer.cs index ceafbb7..29289f5 100644 --- a/mROA/Implementation/StaticRepresentationModuleProducer.cs +++ b/mROA/Implementation/StaticRepresentationModuleProducer.cs @@ -1,5 +1,4 @@ -using System; -using mROA.Abstract; +using mROA.Abstract; namespace mROA.Implementation { diff --git a/mROA/LegacyExtentions.cs b/mROA/LegacyExtentions.cs index ed05e04..44757cb 100644 --- a/mROA/LegacyExtentions.cs +++ b/mROA/LegacyExtentions.cs @@ -9,11 +9,11 @@ namespace mROA { public static async ValueTask ReadExactlyAsync(this Stream stream, byte[] buffer, int offset, int count) { - return await stream.ReadAtLeastAsyncCore(buffer.AsMemory(offset, count), count, true, default); + return await stream.ReadAtLeastAsyncCore(buffer.AsMemory(offset, count), count, true, CancellationToken.None); } public static ValueTask ReadExactlyAsync(this Stream stream, Memory buffer, - CancellationToken cancellationToken = default(CancellationToken)) + CancellationToken cancellationToken = default) { return stream.ReadAtLeastAsyncCore(buffer, buffer.Length, true, cancellationToken); } @@ -28,13 +28,11 @@ namespace mROA int num; for (totalRead = 0; totalRead < minimumBytes; totalRead += num) { - num = await stream.ReadAsync(buffer.Slice(totalRead), cancellationToken).ConfigureAwait(false); - if (num == 0) - { - if (throwOnEndOfStream) - throw new EndOfStreamException(); - return totalRead; - } + num = await stream.ReadAsync(buffer[totalRead..], cancellationToken).ConfigureAwait(false); + if (num != 0) continue; + if (throwOnEndOfStream) + throw new EndOfStreamException(); + return totalRead; } return totalRead; diff --git a/mROA/mROA.csproj b/mROA/mROA.csproj index f4a4354..6e47d56 100644 --- a/mROA/mROA.csproj +++ b/mROA/mROA.csproj @@ -26,8 +26,8 @@ - - + + @@ -40,9 +40,9 @@ - - ..\..\..\..\.nuget\packages\microsoft.extensions.logging.abstractions\9.0.7\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll - + + ..\..\..\..\.nuget\packages\microsoft.extensions.logging.abstractions\9.0.7\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll +