фикс номер 1: кажется дело было в выполненности таска приема

This commit is contained in:
2025-02-18 14:11:28 +03:00
parent bb994f6761
commit 6023f47212
9 changed files with 53 additions and 26 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ public class ClientBasedPrinter : IPrinter
public string GetName() public string GetName()
{ {
Console.WriteLine("ClientBasedPrinter called from server!!!!!!!!!!! Vova likes that:)"); Console.WriteLine("ClientBasedPrinter called from server!!!!!!!!!!! Vova likes that:)");
return "ClientBasedPrinter"; return "ClientBasedPrinter from mroa";
} }
public async Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken) public async Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken)
+12 -3
View File
@@ -28,7 +28,7 @@ TransmissionConfig.RealContextRepository = builder.GetModule<ContextRepository>(
TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule<RemoteContextRepository>(); TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule<RemoteContextRepository>();
builder.GetModule<NetworkFrontendBridge>()!.Connect(); builder.GetModule<NetworkFrontendBridge>()!.Connect();
_ = builder.GetModule<RequestExtractor>()!.StartExtraction(); // _ = builder.GetModule<RequestExtractor>()!.StartExtraction();
Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId()); Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId());
var context = builder.GetModule<RemoteContextRepository>(); var context = builder.GetModule<RemoteContextRepository>();
@@ -36,18 +36,27 @@ var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactor
//правильный порядок команд 8-5-10-7 //правильный порядок команд 8-5-10-7
var printer = factory.Create("Test"); var printer = factory.Create("Test");
Thread.Sleep(100);
var name = printer.Value.GetName(); var name = printer.Value.GetName();
Console.WriteLine("Printer name : {0}", name); Console.WriteLine("Printer name : {0}", name);
Thread.Sleep(100);
factory.Register(new SharedObject<IPrinter>(new ClientBasedPrinter())); factory.Register(new SharedObject<IPrinter>(new ClientBasedPrinter()));
Thread.Sleep(100);
Console.WriteLine("Registered printer"); Console.WriteLine("Registered printer");
var registred = factory.GetFirstPrinter(); var registred = factory.GetFirstPrinter();
Thread.Sleep(100);
Console.WriteLine("First printer");
Console.WriteLine(registred.Value); Console.WriteLine(registred.Value);
Console.WriteLine("Collecting all printers"); Console.WriteLine("Collecting all printers");
var names = factory.CollectAllNames(); var names = factory.CollectAllNames();
Console.WriteLine(names); Thread.Sleep(100);
Console.WriteLine(string.Join(", ", names));
Console.ReadLine(); Console.ReadLine();
// var page = await printer.Value.Print("Test Page", new CancellationToken()); // var page = await printer.Value.Print("Test Page", new CancellationToken());
@@ -48,7 +48,7 @@ public class BasicExecutionModule : IExecuteModule
return new TypedFinalCommandExecution return new TypedFinalCommandExecution
{ {
CommandId = command.CommandId, Result = finalResult, CommandId = command.CommandId, Result = finalResult,
Id = command.CallRequestId, Id = command.Id,
Type = currentCommand.ReturnType Type = currentCommand.ReturnType
}; };
} }
@@ -56,7 +56,7 @@ public class BasicExecutionModule : IExecuteModule
{ {
return new ExceptionCommandExecution return new ExceptionCommandExecution
{ {
Id = command.CallRequestId, CommandId = command.CommandId, Id = command.Id, CommandId = command.CommandId,
Exception = e.ToString() Exception = e.ToString()
}; };
} }
@@ -75,13 +75,13 @@ public class BasicExecutionModule : IExecuteModule
result.Wait(token); result.Wait(token);
return new FinalCommandExecution { CommandId = command.CommandId, Id = command.CallRequestId }; return new FinalCommandExecution { CommandId = command.CommandId, Id = command.Id };
} }
catch (Exception e) catch (Exception e)
{ {
return new ExceptionCommandExecution return new ExceptionCommandExecution
{ {
Id = command.CallRequestId, CommandId = command.CommandId, Id = command.Id, CommandId = command.CommandId,
Exception = e.ToString() Exception = e.ToString()
}; };
} }
@@ -102,7 +102,7 @@ public class BasicExecutionModule : IExecuteModule
var finalResult = result.GetType().GetProperty("Result")?.GetValue(result); var finalResult = result.GetType().GetProperty("Result")?.GetValue(result);
return new TypedFinalCommandExecution return new TypedFinalCommandExecution
{ {
Id = command.CallRequestId, Id = command.Id,
Result = finalResult, Result = finalResult,
CommandId = command.CommandId, CommandId = command.CommandId,
Type = finalResult?.GetType() Type = finalResult?.GetType()
@@ -112,7 +112,7 @@ public class BasicExecutionModule : IExecuteModule
{ {
return new ExceptionCommandExecution return new ExceptionCommandExecution
{ {
Id = command.CallRequestId, CommandId = command.CommandId, Id = command.Id, CommandId = command.CommandId,
Exception = e.ToString() Exception = e.ToString()
}; };
} }
+2 -2
View File
@@ -4,7 +4,7 @@ namespace mROA.Implementation;
public interface ICallRequest public interface ICallRequest
{ {
Guid CallRequestId { get; } Guid Id { get; }
int CommandId { get; } int CommandId { get; }
int ObjectId { get; } int ObjectId { get; }
object? Parameter { get; } object? Parameter { get; }
@@ -12,7 +12,7 @@ public interface ICallRequest
public class DefaultCallRequest : ICallRequest public class DefaultCallRequest : ICallRequest
{ {
public Guid CallRequestId { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
public int CommandId { get; init; } public int CommandId { get; init; }
public int ObjectId { get; init; } = -1; public int ObjectId { get; init; } = -1;
@@ -61,6 +61,8 @@ public class RequestExtractor : IRequestExtractor
var request = var request =
_representationModule!.GetMessage<DefaultCallRequest>(messageType: MessageType.CallRequest); _representationModule!.GetMessage<DefaultCallRequest>(messageType: MessageType.CallRequest);
Console.WriteLine("Executing {0}", request.Id);
if (request.Parameter is not null) if (request.Parameter is not null)
{ {
var parameterType = _methodRepository!.GetMethod(request.CommandId).GetParameters().First() var parameterType = _methodRepository!.GetMethod(request.CommandId).GetParameters().First()
@@ -75,7 +77,7 @@ public class RequestExtractor : IRequestExtractor
? MessageType.FinishedCommandExecution ? MessageType.FinishedCommandExecution
: MessageType.ExceptionCommandExecution; : MessageType.ExceptionCommandExecution;
_representationModule.PostCallMessage(request.CallRequestId, resultType, result, result.GetType()); _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType());
} }
} }
catch catch
+2
View File
@@ -1,4 +1,5 @@
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using mROA.Abstract; using mROA.Abstract;
namespace mROA.Implementation; namespace mROA.Implementation;
@@ -6,6 +7,7 @@ namespace mROA.Implementation;
public class NetworkMessage public class NetworkMessage
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public MessageType SchemaId { get; set; } public MessageType SchemaId { get; set; }
public byte[] Data { get; set; } public byte[] Data { get; set; }
} }
@@ -1,4 +1,6 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using mROA.Abstract; using mROA.Abstract;
namespace mROA.Implementation; namespace mROA.Implementation;
@@ -11,7 +13,9 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule
private Task<NetworkMessage>? _currentReceiving; private Task<NetworkMessage>? _currentReceiving;
private const int BufferSize = ushort.MaxValue; private const int BufferSize = ushort.MaxValue;
private readonly byte[] _buffer = new byte[BufferSize]; private readonly byte[] _buffer = new byte[BufferSize];
private List<NetworkMessage> _messageBuffer = [];
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
{ {
switch (dependency) switch (dependency)
@@ -28,10 +32,11 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule
public Task<NetworkMessage> GetNextMessageReceiving() public Task<NetworkMessage> GetNextMessageReceiving()
{ {
if (_currentReceiving is { IsCompleted: false }) Console.WriteLine(_currentReceiving?.Status);
if (_currentReceiving is { Status: TaskStatus.Running })
return _currentReceiving; return _currentReceiving;
_currentReceiving = GetNextMessage(); _currentReceiving = Task.Run(GetNextMessage);
return _currentReceiving; return _currentReceiving;
} }
@@ -40,12 +45,15 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule
if (BaseStream == null) if (BaseStream == null)
throw new NullReferenceException("BaseStream is null"); throw new NullReferenceException("BaseStream is null");
Console.WriteLine("Sending {0}", JsonSerializer.Serialize(message));
var rawMessage = _serialization.Serialize(message); var rawMessage = _serialization.Serialize(message);
await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort))); await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort)));
await BaseStream.WriteAsync(rawMessage); await BaseStream.WriteAsync(rawMessage);
} }
private async Task<NetworkMessage> GetNextMessage() private NetworkMessage GetNextMessage()
{ {
if (BaseStream == null) if (BaseStream == null)
throw new NullReferenceException("BaseStream is null"); throw new NullReferenceException("BaseStream is null");
@@ -53,10 +61,17 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule
if (_serialization == null) if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is null"); throw new NullReferenceException("Serialization toolkit is null");
await BaseStream.ReadExactlyAsync(_buffer, 0, 2);
Console.WriteLine("Receiving message");
BaseStream.ReadExactly(_buffer, 0, 2);
var len = BitConverter.ToUInt16(_buffer, 0); var len = BitConverter.ToUInt16(_buffer, 0);
await BaseStream.ReadExactlyAsync(_buffer, 0, len); BaseStream.ReadExactly(_buffer, 0, len);
Console.WriteLine("Receiving {0}", Encoding.Default.GetString(_buffer[..len]));
return _serialization.Deserialize<NetworkMessage>(_buffer[..len])!; var message = JsonSerializer.Deserialize<NetworkMessage>(Encoding.Default.GetString(_buffer[..len]));
_messageBuffer.Add(message!);
return message!;
} }
} }
+6 -6
View File
@@ -20,14 +20,14 @@ public abstract class RemoteObjectBase
{ {
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.CallRequestId, MessageType.CallRequest, request); await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
var successResponse = var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution<T>>( _representationModule.GetMessageAsync<FinalCommandExecution<T>>(
messageType: MessageType.FinishedCommandExecution, requestId: request.CallRequestId); messageType: MessageType.FinishedCommandExecution, requestId: request.Id);
var errorResponse = var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>( _representationModule.GetMessageAsync<ExceptionCommandExecution>(
messageType: MessageType.ExceptionCommandExecution, requestId: request.CallRequestId); messageType: MessageType.ExceptionCommandExecution, requestId: request.Id);
Task.WaitAny(successResponse, errorResponse); Task.WaitAny(successResponse, errorResponse);
if (successResponse.IsCompletedSuccessfully) if (successResponse.IsCompletedSuccessfully)
@@ -40,14 +40,14 @@ public abstract class RemoteObjectBase
{ {
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.CallRequestId, MessageType.CallRequest, request); await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
var successResponse = var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution>( _representationModule.GetMessageAsync<FinalCommandExecution>(
messageType: MessageType.FinishedCommandExecution, requestId: request.CallRequestId); messageType: MessageType.FinishedCommandExecution, requestId: request.Id);
var errorResponse = var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>( _representationModule.GetMessageAsync<ExceptionCommandExecution>(
messageType: MessageType.ExceptionCommandExecution, requestId: request.CallRequestId); messageType: MessageType.ExceptionCommandExecution, requestId: request.Id);
Task.WaitAny(successResponse, errorResponse); Task.WaitAny(successResponse, errorResponse);
@@ -38,7 +38,6 @@ public class RepresentationModule : IRepresentationModule
while (true) while (true)
{ {
var message = await _interaction.GetNextMessageReceiving(); var message = await _interaction.GetNextMessageReceiving();
Console.WriteLine("Message received {0}", JsonSerializer.Serialize(message));
if ((requestId is null || message.Id == requestId) && if ((requestId is null || message.Id == requestId) &&
(messageType is null || message.SchemaId == messageType)) (messageType is null || message.SchemaId == messageType))
return message.Data; return message.Data;