опять добавлены токены в обработку сообщений

This commit is contained in:
2025-02-20 22:23:52 +03:00
parent 50fd3e7b73
commit ca2a04b88e
7 changed files with 26 additions and 16 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
+4 -2
View File
@@ -1,5 +1,7 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
using mROA.Implementation; using mROA.Implementation;
using mROA.Implementation.CommandExecution; using mROA.Implementation.CommandExecution;
@@ -22,9 +24,9 @@ namespace mROA.Abstract
public interface IRepresentationModule : IInjectableModule public interface IRepresentationModule : IInjectableModule
{ {
int Id { get; } int Id { get; }
Task<T> GetMessageAsync<T>(Guid? requestId = null, MessageType? messageType = null); Task<T> GetMessageAsync<T>(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default);
T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null); T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null);
Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null); Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default);
Task PostCallMessageAsync<T>(Guid id, MessageType messageType, T payload) where T : notnull; Task PostCallMessageAsync<T>(Guid id, MessageType messageType, T payload) where T : notnull;
Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType); Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType);
@@ -85,7 +85,7 @@ namespace mROA.Implementation.Frontend
} }
catch catch
{ {
multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id); multiClientOwnershipRepository?.FreeOwnership();
} }
} }
} }
@@ -34,7 +34,7 @@ namespace mROA.Implementation
public Task<NetworkMessage> GetNextMessageReceiving() public Task<NetworkMessage> GetNextMessageReceiving()
{ {
if (_currentReceiving != null) return _currentReceiving; if (_currentReceiving != null) return _currentReceiving;
_currentReceiving = Task.Run(GetNextMessage); _currentReceiving = Task.Run(async () => await GetNextMessage());
return _currentReceiving; return _currentReceiving;
} }
@@ -87,7 +87,7 @@ namespace mROA.Implementation
var message = _serialization.Deserialize<NetworkMessage>(localSpan.Span); var message = _serialization.Deserialize<NetworkMessage>(localSpan.Span);
_messageBuffer.Add(message!); _messageBuffer.Add(message!);
_currentReceiving = GetNextMessage(); _currentReceiving = Task.Run(async () => await GetNextMessage());
return message!; return message!;
} }
+12 -5
View File
@@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading;
using System.Threading.Tasks;
using mROA.Abstract; using mROA.Abstract;
using mROA.Implementation.CommandExecution; using mROA.Implementation.CommandExecution;
@@ -26,16 +27,22 @@ namespace mROA.Implementation
{ 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 successResponse = var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution<T>>( _representationModule.GetMessageAsync<FinalCommandExecution<T>>(request.Id,
messageType: MessageType.FinishedCommandExecution, requestId: request.Id); MessageType.FinishedCommandExecution,
localTokenSource.Token);
var errorResponse = var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>( _representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
messageType: MessageType.ExceptionCommandExecution, requestId: request.Id); MessageType.ExceptionCommandExecution, localTokenSource.Token);
Task.WaitAny(successResponse, errorResponse); Task.WaitAny(successResponse, errorResponse);
if (successResponse.IsCompletedSuccessfully) if (successResponse.IsCompletedSuccessfully)
{
return successResponse.Result.Result!; return successResponse.Result.Result!;
}
throw errorResponse.Result.GetException(); throw errorResponse.Result.GetException();
} }
+5 -4
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using mROA.Abstract; using mROA.Abstract;
@@ -24,12 +25,12 @@ namespace mROA.Implementation
public int Id => (_interaction ?? throw new NullReferenceException("Interaction is not initialized")).ConnectionId; public int Id => (_interaction ?? throw new NullReferenceException("Interaction is not initialized")).ConnectionId;
public async Task<T> GetMessageAsync<T>(Guid? requestId, MessageType? messageType) public async Task<T> GetMessageAsync<T>(Guid? requestId, MessageType? messageType, CancellationToken token = default)
{ {
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))!; return _serialization.Deserialize<T>(await GetRawMessage(requestId, messageType, token))!;
} }
public T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null) public T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null)
@@ -40,7 +41,7 @@ namespace mROA.Implementation
return _serialization.Deserialize<T>(GetRawMessage(requestId, messageType).GetAwaiter().GetResult())!; return _serialization.Deserialize<T>(GetRawMessage(requestId, messageType).GetAwaiter().GetResult())!;
} }
public async Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null) public async Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default)
{ {
if (_interaction == null) if (_interaction == null)
throw new NullReferenceException("Interaction toolkit is not initialized"); throw new NullReferenceException("Interaction toolkit is not initialized");
@@ -52,7 +53,7 @@ namespace mROA.Implementation
if (fromBuffer == null) if (fromBuffer == null)
{ {
while (true) while (token.IsCancellationRequested == false)
{ {
var message = await _interaction.GetNextMessageReceiving(); var message = await _interaction.GetNextMessageReceiving();
if ((requestId is not null && message.Id != requestId) || if ((requestId is not null && message.Id != requestId) ||