From a887b614c76f9bfeff4d3eef4a6e993be147dc7c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Fri, 21 Feb 2025 18:59:02 +0300 Subject: [PATCH] =?UTF-8?q?=D1=84=D1=80=D0=BE=D0=BD=D1=82=D0=B5=D0=BD?= =?UTF-8?q?=D0=B4=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D1=82=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Frontend/Program.cs | 28 +++++++-------- mROA/Implementation/NetworkMessage.cs | 2 +- mROA/Implementation/RemoteObjectBase.cs | 47 +++++++++++++++++++------ 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index ca2ac99..b9bf3a7 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -69,19 +69,19 @@ class Program var data = page.Value.GetData(); Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data)); - var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest; - - 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"); + // var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest; + // + // 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 diff --git a/mROA/Implementation/NetworkMessage.cs b/mROA/Implementation/NetworkMessage.cs index 2ccda17..6cb302b 100644 --- a/mROA/Implementation/NetworkMessage.cs +++ b/mROA/Implementation/NetworkMessage.cs @@ -15,6 +15,6 @@ namespace mROA.Implementation public enum MessageType { - Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning + Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning, CancelRequest } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index 2da6cbe..62bcf76 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -20,8 +20,9 @@ namespace mROA.Implementation public int Id => _id; public int OwnerId => _representationModule.Id; - - protected async Task GetResultAsync(int methodId, object? parameter = default) + + protected async Task GetResultAsync(int methodId, object? parameter = default, + CancellationToken cancellationToken = default) { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() }; @@ -37,30 +38,54 @@ namespace mROA.Implementation _representationModule.GetMessageAsync(requestId: request.Id, MessageType.ExceptionCommandExecution, localTokenSource.Token); - Task.WaitAny(successResponse, errorResponse); + Task.WaitAny(new Task[] + { + successResponse, errorResponse + }, cancellationToken); + + if (cancellationToken.IsCancellationRequested) + { + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id); + cancellationToken.ThrowIfCancellationRequested(); + } if (successResponse.IsCompletedSuccessfully) { + localTokenSource.Cancel(); return successResponse.Result.Result!; } + localTokenSource.Cancel(); throw errorResponse.Result.GetException(); } - protected async Task CallAsync(int methodId, object? parameter = default) + protected async Task CallAsync(int methodId, object? parameter = default, + CancellationToken cancellationToken = default) { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() }; await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); - var successResponse = - _representationModule.GetMessageAsync( - messageType: MessageType.FinishedCommandExecution, requestId: request.Id); - var errorResponse = - _representationModule.GetMessageAsync( - messageType: MessageType.ExceptionCommandExecution, requestId: request.Id); + var localTokenSource = new CancellationTokenSource(); - Task.WaitAny(successResponse, errorResponse); + var successResponse = + _representationModule.GetMessageAsync(request.Id, + MessageType.FinishedCommandExecution, + localTokenSource.Token); + var errorResponse = + _representationModule.GetMessageAsync(requestId: request.Id, + MessageType.ExceptionCommandExecution, localTokenSource.Token); + + Task.WaitAny(new Task[] + { + successResponse, errorResponse + }, cancellationToken); + + if (cancellationToken.IsCancellationRequested) + { + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id); + cancellationToken.ThrowIfCancellationRequested(); + } if (successResponse.IsCompletedSuccessfully) return;