From 323e9c90035d44978c264d7120f9f596a6765897 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Wed, 7 May 2025 23:34:12 +0300 Subject: [PATCH 1/4] ChannelInteractionModule.cs refactor --- mROA/Abstract/IChannelInteractionModule.cs | 1 - .../ChannelInteractionModule.cs | 42 +++++++------------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/mROA/Abstract/IChannelInteractionModule.cs b/mROA/Abstract/IChannelInteractionModule.cs index 1abdb10..e9030d1 100644 --- a/mROA/Abstract/IChannelInteractionModule.cs +++ b/mROA/Abstract/IChannelInteractionModule.cs @@ -9,7 +9,6 @@ namespace mROA.Abstract public interface IChannelInteractionModule : IInjectableModule, IDisposable { int ConnectionId { get; set; } - IEndPointContext Context { get; set; } Channel ReceiveChanel { get; } ChannelReader TrustedPostChanel { get; } ChannelReader UntrustedPostChanel { get; } diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index e9f36e8..fcec7bd 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -14,12 +14,11 @@ namespace mROA.Implementation private readonly ChannelWriter _untrustedWriter; private readonly Channel _outputTrustedChannel; private readonly Channel _outputUntrustedChannel; - private Task? _currentReceiving; private IContextualSerializationToolKit? _serialization; private bool _isConnected = true; private bool _isActive = true; private TaskCompletionSource _reconnection; - public IEndPointContext Context { get; set; } + private IEndPointContext? _context; public ChannelInteractionModule() { @@ -50,7 +49,7 @@ namespace mROA.Implementation public ChannelReader TrustedPostChanel => _outputTrustedChannel.Reader; public ChannelReader UntrustedPostChanel => _outputUntrustedChannel.Reader; - public Func IsConnected { get; set; } + public Func IsConnected { get; set; } = () => false; public void Inject(T dependency) { @@ -63,7 +62,7 @@ namespace mROA.Implementation ConnectionId = identityGenerator.GetNextIdentity(); break; case IEndPointContext endpointContext: - Context = endpointContext; + _context = endpointContext; break; } } @@ -93,8 +92,7 @@ namespace mROA.Implementation { if (_serialization == null) throw new NullReferenceException("Serialization toolkit is not initialized"); - - bool withError = false; + while (true) { if (await PostMessageInternal(messageHeader)) @@ -106,8 +104,7 @@ namespace mROA.Implementation } _isConnected = false; - withError = true; - await MakeRecovery("OUT"); + await MakeRecovery(); } } @@ -123,23 +120,21 @@ namespace mROA.Implementation if (sendRecovery) { await PostMessageAsync( - new NetworkMessageHeader(_serialization!, new ClientRecovery(Math.Abs(ConnectionId)), Context)); - var ping = await ReceiveChanel.Reader.ReadAsync(); + new NetworkMessageHeader(_serialization!, new ClientRecovery(Math.Abs(ConnectionId)), _context)); + await ReceiveChanel.Reader.ReadAsync(); } else { await _trustedWriter.WriteAsync(new NetworkMessageHeader()); } - var setting = _reconnection.TrySetResult(null); - // _isInReconnectionState = false; + _reconnection.TrySetResult(Stream.Null); _isConnected = true; _reconnection = new TaskCompletionSource(); } - private async Task MakeRecovery(string source) + private async Task MakeRecovery() { - //TODO переделать реконнект lock (_reconnection) { OnDisconnected?.Invoke(ConnectionId); @@ -154,10 +149,6 @@ namespace mROA.Implementation public void Dispose() { _isActive = false; - if (_currentReceiving is { IsCompleted: true }) - { - _currentReceiving?.Dispose(); - } } public class StreamExtractor @@ -167,15 +158,14 @@ namespace mROA.Implementation private const int BufferSize = ushort.MaxValue; private readonly Memory _buffer = new byte[BufferSize]; private bool _manualConnectionState = true; - private IEndPointContext Context; - public readonly int Id = new Random().Next(); + private readonly IEndPointContext _context; public StreamExtractor(Stream ioStream, IContextualSerializationToolKit serializationToolkit, IEndPointContext context) { _ioStream = ioStream; _serializationToolkit = serializationToolkit; - Context = context; + _context = context; } public Action MessageReceived = _ => { }; @@ -197,14 +187,14 @@ namespace mROA.Implementation return len; } - public async Task SingleReceive(CancellationToken Token = default) + public async Task SingleReceive(CancellationToken token = default) { var len = ReadMessageLength(); var localSpan = _buffer[..len]; - await _ioStream.ReadExactlyAsync(localSpan, cancellationToken: Token); + await _ioStream.ReadExactlyAsync(localSpan, cancellationToken: token); - var message = _serializationToolkit.Deserialize(localSpan, Context); + var message = _serializationToolkit.Deserialize(localSpan, _context); MessageReceived(message); } @@ -216,9 +206,9 @@ namespace mROA.Implementation } } - public async Task Send(NetworkMessageHeader message, CancellationToken token = default) + private async Task Send(NetworkMessageHeader message, CancellationToken token = default) { - var rawMessage = _serializationToolkit.Serialize(message, Context); + var rawMessage = _serializationToolkit.Serialize(message, _context); var header = BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort)); await _ioStream.WriteAsync(header, token); From dbde8c3d30bc211f2b263863847f353910506f5f Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Wed, 7 May 2025 23:50:15 +0300 Subject: [PATCH 2/4] Renaming context repository to instance repository --- Example.Backend/Program.cs | 6 ++-- Example.Frontend/Program.cs | 4 +-- mROA/Abstract/IContextRepositoryHub.cs | 2 +- mROA/Abstract/IEndPointContext.cs | 4 +-- mROA/Abstract/IExecuteModule.cs | 2 +- ...xtRepository.cs => IInstanceRepository.cs} | 2 +- .../Backend/BasicConfigurationExtensions.cs | 2 +- .../Backend/BasicExecutionModule.cs | 18 +++++----- .../Backend/HubRequestExtractor.cs | 12 +++---- ...extRepository.cs => InstanceRepository.cs} | 4 +-- ...ry.cs => MultiClientInstanceRepository.cs} | 12 +++---- ...sitory.cs => ComplexInstanceRepository.cs} | 2 +- mROA/Implementation/EndPointContext.cs | 8 ++--- ...ository.cs => RemoteInstanceRepository.cs} | 2 +- mROA/Implementation/SharedObjectShell.cs | 10 +++--- mROA/Implementation/TransmissionConfig.cs | 35 ------------------- 16 files changed, 46 insertions(+), 79 deletions(-) rename mROA/Abstract/{IContextRepository.cs => IInstanceRepository.cs} (88%) rename mROA/Implementation/Backend/{ContextRepository.cs => InstanceRepository.cs} (97%) rename mROA/Implementation/Backend/{MultiClientContextRepository.cs => MultiClientInstanceRepository.cs} (79%) rename mROA/Implementation/{ComplexContextRepository.cs => ComplexInstanceRepository.cs} (97%) rename mROA/Implementation/{RemoteContextRepository.cs => RemoteInstanceRepository.cs} (97%) delete mode 100644 mROA/Implementation/TransmissionConfig.cs diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 88a9490..15e0604 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -30,11 +30,11 @@ class Program builder.Modules.Add(new CreativeRepresentationModuleProducer( new IInjectableModule[] { builder.GetModule()! }, typeof(RepresentationModule))); - builder.Modules.Add(new RemoteContextRepository()); + builder.Modules.Add(new RemoteInstanceRepository()); // builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); - builder.Modules.Add(new MultiClientContextRepository(i => + builder.Modules.Add(new MultiClientInstanceRepository(i => { - var repo = new ContextRepository(); + var repo = new InstanceRepository(); repo.FillSingletons(typeof(PrinterFactory).Assembly); repo.Inject(builder.Modules.OfType().First()); return repo; diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 129e91d..20758fb 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -23,7 +23,7 @@ class Program builder.Modules.Add(new CborSerializationToolkit()); builder.Modules.Add(new EndPointContext()); - builder.Modules.Add(new RemoteContextRepository()); + builder.Modules.Add(new RemoteInstanceRepository()); builder.Modules.Add(new ChannelInteractionModule()); builder.Modules.Add(new UdpUntrustedInteraction()); builder.Modules.Add(new RepresentationModule()); @@ -43,7 +43,7 @@ class Program _ = builder.GetModule()!.StartExtraction(); _ = builder.GetModule().Start(serverEndPoint); Console.WriteLine(builder.GetModule().HostId); - var context = builder.GetModule(); + var context = builder.GetModule(); var factory = context.GetSingleObject(typeof(IPrinterFactory), diff --git a/mROA/Abstract/IContextRepositoryHub.cs b/mROA/Abstract/IContextRepositoryHub.cs index 19a7987..ca2e205 100644 --- a/mROA/Abstract/IContextRepositoryHub.cs +++ b/mROA/Abstract/IContextRepositoryHub.cs @@ -2,7 +2,7 @@ namespace mROA.Abstract { public interface IContextRepositoryHub { - IContextRepository GetRepository(int clientId); + IInstanceRepository GetRepository(int clientId); void FreeRepository(int clientId); } } \ No newline at end of file diff --git a/mROA/Abstract/IEndPointContext.cs b/mROA/Abstract/IEndPointContext.cs index 44db796..8b5db20 100644 --- a/mROA/Abstract/IEndPointContext.cs +++ b/mROA/Abstract/IEndPointContext.cs @@ -2,8 +2,8 @@ { public interface IEndPointContext : IInjectableModule { - IContextRepository RealRepository { get; } - IContextRepository RemoteRepository { get; } + IInstanceRepository RealRepository { get; } + IInstanceRepository RemoteRepository { get; } int HostId { get; set; } int OwnerId { get; set; } } diff --git a/mROA/Abstract/IExecuteModule.cs b/mROA/Abstract/IExecuteModule.cs index abf76d6..ea87f0d 100644 --- a/mROA/Abstract/IExecuteModule.cs +++ b/mROA/Abstract/IExecuteModule.cs @@ -4,7 +4,7 @@ namespace mROA.Abstract { public interface IExecuteModule : IInjectableModule { - ICommandExecution Execute(ICallRequest command, IContextRepository contextRepository, + ICommandExecution Execute(ICallRequest command, IInstanceRepository instanceRepository, IRepresentationModule representationModule, IEndPointContext context); } } \ No newline at end of file diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IInstanceRepository.cs similarity index 88% rename from mROA/Abstract/IContextRepository.cs rename to mROA/Abstract/IInstanceRepository.cs index f4857c7..2e3ef2f 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IInstanceRepository.cs @@ -3,7 +3,7 @@ using mROA.Implementation; namespace mROA.Abstract { - public interface IContextRepository : IInjectableModule + public interface IInstanceRepository : IInjectableModule { int HostId { get; set; } int ResisterObject(object o, IEndPointContext context); diff --git a/mROA/Implementation/Backend/BasicConfigurationExtensions.cs b/mROA/Implementation/Backend/BasicConfigurationExtensions.cs index 57b5f65..a10bdd3 100644 --- a/mROA/Implementation/Backend/BasicConfigurationExtensions.cs +++ b/mROA/Implementation/Backend/BasicConfigurationExtensions.cs @@ -21,7 +21,7 @@ namespace mROA.Implementation.Backend public static void UseCollectableContextRepository(this FullMixBuilder builder, params Assembly[] assemblies) { - var repo = new ContextRepository(); + var repo = new InstanceRepository(); repo.FillSingletons(assemblies); builder.Modules.Add(repo); } diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 5848de9..69ceee4 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -27,13 +27,13 @@ namespace mROA.Implementation.Backend } } - public ICommandExecution Execute(ICallRequest command, IContextRepository contextRepository, + public ICommandExecution Execute(ICallRequest command, IInstanceRepository instanceRepository, IRepresentationModule representationModule, IEndPointContext endPointContext) { try { - ThrowIfNotInjected(contextRepository); + ThrowIfNotInjected(instanceRepository); if (command is CancelRequest) { return CancelExecution(command); @@ -43,7 +43,7 @@ namespace mROA.Implementation.Backend if (invoker == null) throw new Exception($"Command {command.CommandId} not found"); - var context = GetContext(command, contextRepository, invoker, endPointContext); + var context = GetContext(command, instanceRepository, invoker, endPointContext); if (context == null) throw new NullReferenceException("Instance can't be null"); @@ -70,7 +70,7 @@ namespace mROA.Implementation.Backend var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); if (command.CommandId == -1) { - contextRepository.ClearObject(command.ObjectId, endPointContext); + instanceRepository.ClearObject(command.ObjectId, endPointContext); } return result; @@ -86,12 +86,12 @@ namespace mROA.Implementation.Backend } } - private static object GetContext(ICallRequest command, IContextRepository contextRepository, + private static object GetContext(ICallRequest command, IInstanceRepository instanceRepository, IMethodInvoker invoker, IEndPointContext endPointContext) { var context = command.ObjectId.ContextId != -1 - ? contextRepository.GetObject(command.ObjectId, endPointContext) - : contextRepository.GetSingleObject(invoker.SuitableType, endPointContext); + ? instanceRepository.GetObject(command.ObjectId, endPointContext) + : instanceRepository.GetSingleObject(invoker.SuitableType, endPointContext); return context; } @@ -106,7 +106,7 @@ namespace mROA.Implementation.Backend return castedParams; } - private void ThrowIfNotInjected(IContextRepository contextRepository) + private void ThrowIfNotInjected(IInstanceRepository instanceRepository) { if (_cancellationRepo is null) throw new NullReferenceException("Method repository was not defined"); @@ -114,7 +114,7 @@ namespace mROA.Implementation.Backend if (_methodRepo is null) throw new NullReferenceException("Method repository was not defined"); - if (contextRepository is null) + if (instanceRepository is null) throw new NullReferenceException("Context repository was not defined"); } diff --git a/mROA/Implementation/Backend/HubRequestExtractor.cs b/mROA/Implementation/Backend/HubRequestExtractor.cs index 7c35b33..7a51339 100644 --- a/mROA/Implementation/Backend/HubRequestExtractor.cs +++ b/mROA/Implementation/Backend/HubRequestExtractor.cs @@ -7,8 +7,8 @@ namespace mROA.Implementation.Backend { private IConnectionHub? _hub; - private IContextRepository? _contextRepository; - private IContextRepository? _remoteContextRepository; + private IInstanceRepository? _contextRepository; + private IInstanceRepository? _remoteContextRepository; private IMethodRepository? _methodRepository; private IContextualSerializationToolKit? _serializationToolkit; private IExecuteModule? _executeModule; @@ -21,11 +21,11 @@ namespace mROA.Implementation.Backend _hub = connectionHub; _hub.OnConnected += HubOnOnConnected; break; - case MultiClientContextRepository: - case ContextRepository: - _contextRepository = dependency as IContextRepository; + case MultiClientInstanceRepository: + case InstanceRepository: + _contextRepository = dependency as IInstanceRepository; break; - case RemoteContextRepository remoteContextRepository: + case RemoteInstanceRepository remoteContextRepository: _remoteContextRepository = remoteContextRepository; break; case IMethodRepository methodRepository: diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs similarity index 97% rename from mROA/Implementation/Backend/ContextRepository.cs rename to mROA/Implementation/Backend/InstanceRepository.cs index a18d15c..9b71859 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -8,7 +8,7 @@ using mROA.Implementation.Attributes; namespace mROA.Implementation.Backend { - public class ContextRepository : IContextRepository + public class InstanceRepository : IInstanceRepository { public static object[] EventBinders = { }; @@ -22,7 +22,7 @@ namespace mROA.Implementation.Backend private IStorage _storage; - public ContextRepository() + public InstanceRepository() { _storage = new ExtensibleStorage(); } diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs similarity index 79% rename from mROA/Implementation/Backend/MultiClientContextRepository.cs rename to mROA/Implementation/Backend/MultiClientInstanceRepository.cs index 8d63cd3..520e4cb 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs @@ -4,12 +4,12 @@ using mROA.Abstract; namespace mROA.Implementation.Backend { - public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub + public class MultiClientInstanceRepository : IInstanceRepository, IContextRepositoryHub { - private readonly Func _produceRepository; - private readonly Dictionary _repositories = new(); + private readonly Func _produceRepository; + private readonly Dictionary _repositories = new(); - public MultiClientContextRepository(Func produceRepository) + public MultiClientInstanceRepository(Func produceRepository) { _produceRepository = produceRepository; } @@ -50,7 +50,7 @@ namespace mROA.Implementation.Backend return repository.GetObjectIndex(o, context); } - public IContextRepository GetRepository(int clientId) + public IInstanceRepository GetRepository(int clientId) { var repository = GetRepositoryByClientId(clientId); return repository; @@ -61,7 +61,7 @@ namespace mROA.Implementation.Backend _repositories.Remove(clientId); } - private IContextRepository GetRepositoryByClientId(int clientId) + private IInstanceRepository GetRepositoryByClientId(int clientId) { if (_repositories.TryGetValue(clientId, out var repository)) return repository; diff --git a/mROA/Implementation/ComplexContextRepository.cs b/mROA/Implementation/ComplexInstanceRepository.cs similarity index 97% rename from mROA/Implementation/ComplexContextRepository.cs rename to mROA/Implementation/ComplexInstanceRepository.cs index b074da9..09be507 100644 --- a/mROA/Implementation/ComplexContextRepository.cs +++ b/mROA/Implementation/ComplexInstanceRepository.cs @@ -5,7 +5,7 @@ using mROA.Abstract; namespace mROA.Implementation { - public class ComplexContextRepository : IContextRepository + public class ComplexInstanceRepository : IInstanceRepository { private List>> _storages = new(); public static object[] EventBinders = { }; diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index 6962f86..1978f2c 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -6,8 +6,8 @@ namespace mROA.Implementation { public class EndPointContext : IEndPointContext { - public IContextRepository RealRepository { get; set; } - public IContextRepository RemoteRepository { get; set; } + public IInstanceRepository RealRepository { get; set; } + public IInstanceRepository RemoteRepository { get; set; } public int HostId { get; set; } public int OwnerId { get; set; } @@ -16,10 +16,10 @@ namespace mROA.Implementation { switch (dependency) { - case RemoteContextRepository remoteRepository: + case RemoteInstanceRepository remoteRepository: RemoteRepository = remoteRepository; break; - case ContextRepository realRepository: + case InstanceRepository realRepository: RealRepository = realRepository; break; } diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteInstanceRepository.cs similarity index 97% rename from mROA/Implementation/RemoteContextRepository.cs rename to mROA/Implementation/RemoteInstanceRepository.cs index 9ab5dd8..1d00f42 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteInstanceRepository.cs @@ -5,7 +5,7 @@ using mROA.Abstract; namespace mROA.Implementation { - public class RemoteContextRepository : IContextRepository + public class RemoteInstanceRepository : IInstanceRepository { private List _producedRemoteEndpoints = new(); public static Dictionary RemoteTypes = new(); diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 01305db..a9f5246 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -20,12 +20,14 @@ namespace mROA.Implementation { private ComplexObjectIdentifier _identifier = ComplexObjectIdentifier.Null; - private T _value; + private T? _value; // ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once UnusedMember.Global public SharedObjectShellShell() { + _value = default; + EndPointContext = new EndPointContext(); } // ReSharper disable once UnusedMember.Global @@ -41,7 +43,7 @@ namespace mROA.Implementation // ReSharper disable once MemberCanBePrivate.Global public T Value { - get => _value; + get => _value!; set { _value = value; @@ -76,11 +78,11 @@ namespace mROA.Implementation public object UniversalValue { - get => _value; + get => _value!; set => _value = (T)value; } - private IContextRepository GetDefaultContextRepository() => + private IInstanceRepository GetDefaultContextRepository() => (_identifier.OwnerId == EndPointContext.HostId ? EndPointContext.RealRepository : EndPointContext.RemoteRepository) ?? diff --git a/mROA/Implementation/TransmissionConfig.cs b/mROA/Implementation/TransmissionConfig.cs deleted file mode 100644 index 013995a..0000000 --- a/mROA/Implementation/TransmissionConfig.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using mROA.Abstract; - -namespace mROA.Implementation -{ -#pragma warning disable CS8618, CS9264 - public static class TransmissionConfig - { -#if TRACE - public static int TotalTransmittedBytes { get; set; } -#endif - // private static IContextRepository? _realContextRepository; - // private static IContextRepository? _remoteEndpointContextRepository; - // private static IOwnershipRepository? _ownershipRepository; - // - // public static IContextRepository RealContextRepository - // { - // get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null"); - // set => _realContextRepository = value; - // } - // - // public static IContextRepository RemoteEndpointContextRepository - // { - // get => _remoteEndpointContextRepository ?? - // throw new NullReferenceException("RemoteEndpointContextRepository is null"); - // set => _remoteEndpointContextRepository = value; - // } - // - // public static IOwnershipRepository OwnershipRepository - // { - // get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null"); - // set => _ownershipRepository = value; - // } - } -} \ No newline at end of file From 5a0357c42e0b5af1d028080c3dfa5ae5f2e1ae0c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 8 May 2025 00:04:53 +0300 Subject: [PATCH 3/4] Some interfaces cleanup --- Example.Frontend/Program.cs | 1 - mROA.Codegen/RemoteTypeBinder.cstmpl | 4 +- mROA/Abstract/IChannelInteractionModule.cs | 1 - mROA/Abstract/IInstanceRepository.cs | 1 - mROA/Abstract/ISerializationToolkit.cs | 4 +- mROA/Abstract/IUntrustedGateway.cs | 1 - .../Backend/InstanceRepository.cs | 1 - .../Backend/NetworkGatewayModule.cs | 1 - mROA/Implementation/Backend/UdpGateway.cs | 1 - mROA/Implementation/EndPointContext.cs | 1 - mROA/Implementation/EventBinder.cs | 5 +- .../JsonSerializationToolkit.cs | 61 ------------------- mROA/Implementation/MethodInvoker.cs | 2 +- mROA/Implementation/NetworkMessageHeader.cs | 14 +---- mROA/Implementation/RemoteObjectBase.cs | 1 - mROA/Implementation/RepresentationModule.cs | 1 + 16 files changed, 9 insertions(+), 91 deletions(-) delete mode 100644 mROA/Implementation/JsonSerializationToolkit.cs diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 20758fb..07d7ff4 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -1,6 +1,5 @@ using System; using System.Net; -using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/mROA.Codegen/RemoteTypeBinder.cstmpl b/mROA.Codegen/RemoteTypeBinder.cstmpl index bbef815..69eff9c 100644 --- a/mROA.Codegen/RemoteTypeBinder.cstmpl +++ b/mROA.Codegen/RemoteTypeBinder.cstmpl @@ -11,11 +11,11 @@ namespace mROA.Codegen public sealed class RemoteTypeBinder { static RemoteTypeBinder(){ - RemoteContextRepository.RemoteTypes = new Dictionary { + RemoteInstanceRepository.RemoteTypes = new Dictionary { , }; - ContextRepository.EventBinders = new object[] { + InstanceRepository.EventBinders = new object[] { new EventBinder<> diff --git a/mROA/Abstract/IChannelInteractionModule.cs b/mROA/Abstract/IChannelInteractionModule.cs index e9030d1..2c59de3 100644 --- a/mROA/Abstract/IChannelInteractionModule.cs +++ b/mROA/Abstract/IChannelInteractionModule.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Threading.Channels; using System.Threading.Tasks; using mROA.Implementation; diff --git a/mROA/Abstract/IInstanceRepository.cs b/mROA/Abstract/IInstanceRepository.cs index 2e3ef2f..7086178 100644 --- a/mROA/Abstract/IInstanceRepository.cs +++ b/mROA/Abstract/IInstanceRepository.cs @@ -5,7 +5,6 @@ namespace mROA.Abstract { public interface IInstanceRepository : IInjectableModule { - int HostId { get; set; } int ResisterObject(object o, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id, IEndPointContext context); T GetObject(ComplexObjectIdentifier id, IEndPointContext context); diff --git a/mROA/Abstract/ISerializationToolkit.cs b/mROA/Abstract/ISerializationToolkit.cs index 92b806a..6761e10 100644 --- a/mROA/Abstract/ISerializationToolkit.cs +++ b/mROA/Abstract/ISerializationToolkit.cs @@ -1,6 +1,4 @@ -using System; - -namespace mROA.Abstract +namespace mROA.Abstract { // public interface IContextualSerializationToolKit : IInjectableModule // { diff --git a/mROA/Abstract/IUntrustedGateway.cs b/mROA/Abstract/IUntrustedGateway.cs index 9cb5fee..1c6f25c 100644 --- a/mROA/Abstract/IUntrustedGateway.cs +++ b/mROA/Abstract/IUntrustedGateway.cs @@ -1,5 +1,4 @@ using System; -using System.Net; using System.Threading.Tasks; namespace mROA.Abstract diff --git a/mROA/Implementation/Backend/InstanceRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs index 9b71859..4c7c9e5 100644 --- a/mROA/Implementation/Backend/InstanceRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Threading.Tasks; using mROA.Abstract; using mROA.Implementation.Attributes; diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 5fc2548..f4e9d3c 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; -using System.Threading.Channels; using System.Threading.Tasks; using mROA.Abstract; diff --git a/mROA/Implementation/Backend/UdpGateway.cs b/mROA/Implementation/Backend/UdpGateway.cs index d2642bb..1945e0f 100644 --- a/mROA/Implementation/Backend/UdpGateway.cs +++ b/mROA/Implementation/Backend/UdpGateway.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; -using System.Threading.Channels; using System.Threading.Tasks; using mROA.Abstract; using static mROA.Implementation.EMessageType; diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs index 1978f2c..5694614 100644 --- a/mROA/Implementation/EndPointContext.cs +++ b/mROA/Implementation/EndPointContext.cs @@ -1,4 +1,3 @@ -using System; using mROA.Abstract; using mROA.Implementation.Backend; diff --git a/mROA/Implementation/EventBinder.cs b/mROA/Implementation/EventBinder.cs index 8354c4f..429a882 100644 --- a/mROA/Implementation/EventBinder.cs +++ b/mROA/Implementation/EventBinder.cs @@ -1,10 +1,11 @@ using System; +using mROA.Abstract; -namespace mROA.Abstract +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/JsonSerializationToolkit.cs b/mROA/Implementation/JsonSerializationToolkit.cs deleted file mode 100644 index 38f8c19..0000000 --- a/mROA/Implementation/JsonSerializationToolkit.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Text.Json; -using mROA.Abstract; - -namespace mROA.Implementation -{ - // public class JsonSerializationToolkit : IContextualSerializationToolKit - // { - // public byte[] Serialize(T objectToSerialize) - // { - // return JsonSerializer.SerializeToUtf8Bytes(objectToSerialize); - // } - // - // public byte[] Serialize(object objectToSerialize, Type type) - // { - // return JsonSerializer.SerializeToUtf8Bytes(objectToSerialize, type); - // } - // - // public T? Deserialize(byte[] rawData) - // { - // return JsonSerializer.Deserialize(rawData); - // } - // - // public object? Deserialize(byte[] rawData, Type type) - // { - // return JsonSerializer.Deserialize(rawData, type); - // } - // - // public T? Deserialize(Span rawData) - // { - // return JsonSerializer.Deserialize(rawData); - // } - // - // public object? Deserialize(Span rawData, Type type) - // { - // return JsonSerializer.Deserialize(rawData, type); - // } - // - // public T Cast(object nonCasted) - // { - // return nonCasted switch - // { - // JsonElement jsonElement => jsonElement.Deserialize()!, - // T casted => casted, - // _ => throw new JsonException("Cannot cast object to type " + typeof(T).FullName) - // }; - // } - // - // public object Cast(object nonCasted, Type type) - // { - // if (nonCasted is JsonElement jsonElement) - // return jsonElement.Deserialize(type)!; - // - // throw new JsonException("Cannot cast object to type " + type.FullName); - // } - // - // public void Inject(T dependency) - // { - // } - // } -} \ No newline at end of file diff --git a/mROA/Implementation/MethodInvoker.cs b/mROA/Implementation/MethodInvoker.cs index ff713db..4f1e26c 100644 --- a/mROA/Implementation/MethodInvoker.cs +++ b/mROA/Implementation/MethodInvoker.cs @@ -36,7 +36,7 @@ namespace mROA.Implementation public bool IsTrusted { get; set; } = true; public Type[] ParameterTypes { get; set; } = Type.EmptyTypes; public Type? ReturnType { get; set; } - public Type SuitableType { get; set; } + public Type SuitableType { get; set; } = typeof(object); public Action> Invoking { get; set; } = (_, _, _, post) => { post.Invoke(null); }; diff --git a/mROA/Implementation/NetworkMessageHeader.cs b/mROA/Implementation/NetworkMessageHeader.cs index cb31c3e..0a0a271 100644 --- a/mROA/Implementation/NetworkMessageHeader.cs +++ b/mROA/Implementation/NetworkMessageHeader.cs @@ -1,7 +1,5 @@ using System; -using System.Text.Json.Serialization; using mROA.Abstract; -using mROA.Implementation.Attributes; // ReSharper disable UnusedMember.Global @@ -9,7 +7,7 @@ namespace mROA.Implementation { public class NetworkMessageHeader { - protected bool Equals(NetworkMessageHeader other) + private bool Equals(NetworkMessageHeader other) { return Id.Equals(other.Id) && MessageType == other.MessageType; } @@ -22,11 +20,6 @@ namespace mROA.Implementation return Equals((NetworkMessageHeader)obj); } - public override int GetHashCode() - { - return HashCode.Combine(Id, (int)MessageType); - } - public static readonly NetworkMessageHeader Null = new(); public NetworkMessageHeader() { @@ -43,13 +36,8 @@ namespace mROA.Implementation } public Guid Id { get; set; } - [JsonConverter(typeof(JsonStringEnumConverter))] public EMessageType MessageType { get; set; } public byte[] Data { get; set; } - - [JsonIgnore] - [SerializationIgnore] - public object Parced { get; set; } } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index efcbeed..3410e97 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -1,5 +1,4 @@ using System; -using System.Net; using System.Threading; using System.Threading.Tasks; using mROA.Abstract; diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index bb9d40c..d406d59 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 { From 56d82ecdef095ab208603455cc64d1d4bfee5135 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 8 May 2025 00:11:24 +0300 Subject: [PATCH 4/4] Load test back --- Example.Frontend/Program.cs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 07d7ff4..0434e06 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Net; using System.Text; using System.Threading; @@ -120,23 +121,25 @@ class Program Console.WriteLine($"Token state {cts.Token.IsCancellationRequested}"); DemoCheck.TaskCancelation = true; + const int iterations = 10000; + var timer = Stopwatch.StartNew(); + var x = 0; + for (int i = 0; i < iterations; i++) + { + x = loadSingleton.Next(x); + } + + timer.Stop(); + Console.WriteLine("X is {0}", x); + Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds); + Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms"); + frontendBridge.Disconnect(); DemoCheck.Show(); Console.ReadKey(); - // - // const int iterations = 10000; - // var timer = Stopwatch.StartNew(); - // var x = 0; - // for (int i = 0; i < iterations; i++) - // { - // x = loadSingleton.Next(x); - // } - // - // timer.Stop(); - // Console.WriteLine("X is {0}", x); - // Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds); - // Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms"); + + } } \ No newline at end of file