Причесывание кода

This commit is contained in:
2025-02-24 18:01:34 +03:00
parent 14c74c5775
commit 05ee651e89
11 changed files with 56 additions and 32 deletions
@@ -88,10 +88,11 @@ namespace mROA.Implementation.Backend
{ {
try try
{ {
var finalResult = currentCommand.Invoke(context, parameter is null var finalParameter = parameter is null
? Array.Empty<object>() ? Array.Empty<object>()
: new[] : new[]
{ parameter }); { parameter };
var finalResult = currentCommand.Invoke(context, finalParameter);
return new TypedFinalCommandExecution return new TypedFinalCommandExecution
{ {
@@ -120,10 +121,11 @@ namespace mROA.Implementation.Backend
token.Register(() => Console.WriteLine($"Cancellation requested check {command.Id}")); token.Register(() => Console.WriteLine($"Cancellation requested check {command.Id}"));
try try
{ {
var result = (Task)currentCommand.Invoke(context, parameter is null var finalParameter = parameter is null
? new object[] { token } ? new object[] { token }
: new[] : new[]
{ parameter, token })!; { parameter, token };
var result = (Task)currentCommand.Invoke(context, finalParameter)!;
result.ContinueWith(_ => result.ContinueWith(_ =>
@@ -140,6 +142,7 @@ namespace mROA.Implementation.Backend
var multiClientOwnershipRepository = var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id); multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
representationModule.PostCallMessage(command.Id, MessageType.FinishedCommandExecution, payload); representationModule.PostCallMessage(command.Id, MessageType.FinishedCommandExecution, payload);
multiClientOwnershipRepository?.FreeOwnership(); multiClientOwnershipRepository?.FreeOwnership();
@@ -170,11 +173,12 @@ namespace mROA.Implementation.Backend
var token = tokenSource.Token; var token = tokenSource.Token;
try try
{ {
var finalParameter = parameter is null
? new object[] { token }
: new[]
{ parameter, token };
var result = var result =
(Task)currentCommand.Invoke(context, parameter is null (Task)currentCommand.Invoke(context, finalParameter)!;
? new object[] { token }
: new[]
{ parameter, token })!;
result.ContinueWith(t => result.ContinueWith(t =>
{ {
@@ -76,7 +76,7 @@ namespace mROA.Implementation.Backend
public object GetSingleObject(Type type) public object GetSingleObject(Type type)
{ {
return _singletons!.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type");
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o)
@@ -29,42 +29,50 @@ namespace mROA.Implementation.Backend
public int ResisterObject(object o) public int ResisterObject(object o)
{ {
return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).ResisterObject(o); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.ResisterObject(o);
} }
public void ClearObject(int id) public void ClearObject(int id)
{ {
GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).ClearObject(id); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
repository.ClearObject(id);
} }
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
{ {
return GetRepository(sharedObject.OwnerId).GetObject<T>(sharedObject.ContextId); var repository = GetRepository(sharedObject.OwnerId);
return repository.GetObject<T>(sharedObject.ContextId);
} }
public object GetObject(int id) public object GetObject(int id)
{ {
return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject<object>(id); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<object>(id);
} }
public T? GetObject<T>(int id) public T? GetObject<T>(int id)
{ {
return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject<T>(id); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<T>(id);
} }
public object GetSingleObject(Type type) public object GetSingleObject(Type type)
{ {
return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetSingleObject(type); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetSingleObject(type);
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o)
{ {
return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObjectIndex(o); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObjectIndex(o);
} }
public IContextRepository GetRepository(int clientId) public IContextRepository GetRepository(int clientId)
{ {
return GetRepositoryByClientId(clientId); var repository = GetRepositoryByClientId(clientId);
return repository;
} }
} }
} }
@@ -34,7 +34,8 @@ namespace mROA.Implementation
foreach (var creationModule in _creationModules) foreach (var creationModule in _creationModules)
produced.Inject(creationModule); produced.Inject(creationModule);
produced.Inject(_hub.GetInteracion(id)); var interaction = _hub.GetInteracion(id);
produced.Inject(interaction);
return produced; return produced;
} }
@@ -45,7 +45,9 @@ namespace mROA.Implementation.Frontend
throw new Exception($"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.SchemaId.ToString()}"); throw new Exception($"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.SchemaId.ToString()}");
} }
TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_serialization.Deserialize<IdAssingnment>(welcomeMessage.Data)!.Id);
var assignment = _serialization.Deserialize<IdAssingnment>(welcomeMessage.Data)!;
TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(assignment.Id);
} }
} }
} }
@@ -91,7 +91,8 @@ namespace mROA.Implementation.Frontend
if (request.Parameter is not null) if (request.Parameter is not null)
{ {
var parameterType = _methodRepository!.GetMethod(request.CommandId).GetParameters().First() var method = _methodRepository!.GetMethod(request.CommandId);
var parameterType = method.GetParameters().First()
.ParameterType; .ParameterType;
request.Parameter = _serializationToolkit.Cast(request.Parameter, parameterType); request.Parameter = _serializationToolkit.Cast(request.Parameter, parameterType);
@@ -51,7 +51,8 @@ namespace mROA.Implementation
var rawMessage = _serialization.Serialize(message); var rawMessage = _serialization.Serialize(message);
await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort))); var header = BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort));
await BaseStream.WriteAsync(header);
await BaseStream.WriteAsync(rawMessage); await BaseStream.WriteAsync(rawMessage);
} }
@@ -25,8 +25,9 @@ namespace mROA.Implementation
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObject.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.ContextId, var remote = (T)Activator.CreateInstance(remoteType, sharedObject.ContextId,
_representationProducer.Produce(sharedObject.OwnerId))!; representationModule)!;
return remote; return remote;
} }
@@ -41,8 +42,9 @@ namespace mROA.Implementation
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var remote = (T)Activator.CreateInstance(remoteType, id, var remote = (T)Activator.CreateInstance(remoteType, id,
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; representationModule)!;
return remote; return remote;
} }
@@ -51,8 +53,9 @@ namespace mROA.Implementation
if (_representationProducer == null) if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
var representationModule = _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return Activator.CreateInstance(RemoteTypes[type], -1, return Activator.CreateInstance(RemoteTypes[type], -1,
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; representationModule)!;
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o)
+1
View File
@@ -27,6 +27,7 @@ namespace mROA.Implementation
{ {
var request = new DefaultCallRequest var request = new DefaultCallRequest
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() }; { CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
var localTokenSource = new CancellationTokenSource(); var localTokenSource = new CancellationTokenSource();
+6 -3
View File
@@ -30,7 +30,8 @@ namespace mROA.Implementation
if (_serialization == null) if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is not initialized"); throw new NullReferenceException("Serialization toolkit is not initialized");
return _serialization.Deserialize<T>(await GetRawMessage(requestId, messageType, token))!; var rawMessage = await GetRawMessage(requestId, messageType, token);
return _serialization.Deserialize<T>(rawMessage)!;
} }
public T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null) public T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null)
@@ -38,7 +39,8 @@ namespace mROA.Implementation
if (_serialization == null) if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is not initialized"); throw new NullReferenceException("Serialization toolkit is not initialized");
return _serialization.Deserialize<T>(GetRawMessage(requestId, messageType).GetAwaiter().GetResult())!; var rawMessage = GetRawMessage(requestId, messageType).GetAwaiter().GetResult();
return _serialization.Deserialize<T>(rawMessage)!;
} }
public async Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default) public async Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default)
@@ -83,8 +85,9 @@ namespace mROA.Implementation
Console.WriteLine($"Posting message: {id} - {messageType}"); Console.WriteLine($"Posting message: {id} - {messageType}");
var serialized = _serialization.Serialize(payload, payloadType);
await _interaction.PostMessage(new NetworkMessage await _interaction.PostMessage(new NetworkMessage
{ Id = id, SchemaId = messageType, Data = _serialization.Serialize(payload, payloadType) }); { Id = id, SchemaId = messageType, Data = serialized });
} }
public void PostCallMessage<T>(Guid id, MessageType messageType, T payload) where T : notnull public void PostCallMessage<T>(Guid id, MessageType messageType, T payload) where T : notnull
+1 -1
View File
@@ -73,7 +73,7 @@ namespace mROA.Implementation
set set
{ {
_contextId = value; _contextId = value;
Value = GetDefaultContextRepository().GetObjectBySharedObject<T>(this)!; Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
} }
} }