добавлена асинхронность

This commit is contained in:
2025-02-03 10:42:47 +03:00
parent 1f2df53c12
commit b2f16b27f4
9 changed files with 49 additions and 32 deletions
+35 -19
View File
@@ -95,9 +95,8 @@ public class TransmissionTestController : ITestController
public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest() public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest()
{ {
TransmittedSharedObject<ITestController> x = new TestController(); TransmittedSharedObject<ITestController> x = new TestController();
return new TestController { ReturnIfNotNull = this }; return new TestController { ReturnIfNotNull = this };
} }
@@ -118,47 +117,64 @@ public class TestParameter
public TransmittedSharedObject<ITestParameter> LinkedObject { get; set; } public TransmittedSharedObject<ITestParameter> LinkedObject { get; set; }
} }
public class TestControllerRemoteEndoint : ITestController, IRemoteObject public class TestControllerRemoteEndoint(int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule)
: ITestController, IRemoteObject
{ {
public TestControllerRemoteEndoint(int id)
{
_id = id;
}
public void A() public void A()
{ {
serialisationModule.PostCallRequest(new JsonCallRequest { CommandId = 0, ObjectId = id });
} }
public Task AAsync(CancellationToken cancellationToken) public async Task AAsync(CancellationToken cancellationToken)
{ {
var request = new JsonCallRequest { CommandId = 1, ObjectId = id };
serialisationModule.PostCallRequest(request);
await serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId);
} }
public int B() public int B()
{ {
var request = new JsonCallRequest { CommandId = 2, ObjectId = id };
serialisationModule.PostCallRequest(request);
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
.GetAwaiter().GetResult();
return (int)response.Result!;
} }
public Task<int> BAsync(CancellationToken cancellationToken) public async Task<int> BAsync(CancellationToken cancellationToken)
{ {
var request = new JsonCallRequest { CommandId = 3, ObjectId = id };
serialisationModule.PostCallRequest(request);
var response = await serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId);
return (int)response.Result!;
} }
public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest() public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest()
{ {
var request = new JsonCallRequest { CommandId = 4, ObjectId = id };
serialisationModule.PostCallRequest(request);
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
.GetAwaiter().GetResult();
return (TransmittedSharedObject<ITestController>)response.Result!;
} }
public int Parametrized(TestParameter parameter) public int Parametrized(TestParameter parameter)
{ {
var request = new JsonCallRequest { CommandId = 5, ObjectId = id, Parameter = parameter };
serialisationModule.PostCallRequest(request);
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
.GetAwaiter().GetResult();
return (int)response.Result!;
} }
public TransmittedSharedObject<ITestParameter> GetTestParameter() public TransmittedSharedObject<ITestParameter> GetTestParameter()
{ {
var request = new JsonCallRequest { CommandId = 6, ObjectId = id };
serialisationModule.PostCallRequest(request);
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
.GetAwaiter().GetResult();
return (TransmittedSharedObject<ITestParameter>)response.Result!;
} }
private int _id; public int Id => id;
public int Id => _id;
} }
+1 -1
View File
@@ -52,7 +52,7 @@ public class StreamTest
var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 }; var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 };
_frontendSerialisationModule.PostCallRequest(req); _frontendSerialisationModule.PostCallRequest(req);
var res = ((JsonElement)_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result).Deserialize<MockResult>(); var res = ((JsonElement)_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).GetAwaiter().GetResult().Result!).Deserialize<MockResult>();
isTestNotFinished = false; isTestNotFinished = false;
Assert.That(res.A == "wqer" && res.B == 5); Assert.That(res.A == "wqer" && res.B == 5);
} }
+1 -1
View File
@@ -6,7 +6,7 @@ public interface IInteractionModule
void SendTo(int clientId, byte[] message); void SendTo(int clientId, byte[] message);
public interface IFrontendInteractionModule public interface IFrontendInteractionModule
{ {
public byte[] ReceiveMessage(); public Task<byte[]> ReceiveMessage();
public void PostMessage(byte[] message); public void PostMessage(byte[] message);
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ public interface ISerialisationModule
void SetExecuteModule(IExecuteModule executeModule); void SetExecuteModule(IExecuteModule executeModule);
public interface IFrontendSerialisationModule public interface IFrontendSerialisationModule
{ {
T GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution; Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
void PostCallRequest(ICallRequest callRequest); void PostCallRequest(ICallRequest callRequest);
} }
} }
@@ -2,10 +2,10 @@
namespace mROA.Implementation; namespace mROA.Implementation;
public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes) : IContextRepository public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes, ISerialisationModule serialisationModule) : IContextRepository
{ {
private FrozenDictionary<Type, Type> _remoteTypes = remoteTypes.ToFrozenDictionary(); private FrozenDictionary<Type, Type> _remoteTypes = remoteTypes.ToFrozenDictionary();
public int ResisterObject(object o) public int ResisterObject(object o)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
@@ -25,7 +25,7 @@ public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes) : ICo
{ {
if (_remoteTypes.TryGetValue(typeof(T), out var remoteType)) if (_remoteTypes.TryGetValue(typeof(T), out var remoteType))
{ {
var remote = (T)Activator.CreateInstance(remoteType, id)!; var remote = (T)Activator.CreateInstance(remoteType, id, serialisationModule)!;
return remote; return remote;
} }
throw new NotSupportedException(); throw new NotSupportedException();
+1 -1
View File
@@ -2,5 +2,5 @@
public interface IRemoteObject public interface IRemoteObject
{ {
internal int Id { get; } public int Id { get; }
} }
@@ -6,13 +6,13 @@ namespace mROA.Implementation;
public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInteractionModule interactionModule) public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInteractionModule interactionModule)
: ISerialisationModule.IFrontendSerialisationModule : ISerialisationModule.IFrontendSerialisationModule
{ {
public T GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution public async Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution
{ {
var receiveMessage = interactionModule.ReceiveMessage(); var receiveMessage = await interactionModule.ReceiveMessage();
var parsed = JsonSerializer.Deserialize<T>(receiveMessage); var parsed = JsonSerializer.Deserialize<T>(receiveMessage);
while (parsed.CallRequestId != requestId) while (parsed.CallRequestId != requestId)
{ {
receiveMessage = interactionModule.ReceiveMessage(); receiveMessage = await interactionModule.ReceiveMessage();
parsed = JsonSerializer.Deserialize<T>(receiveMessage); parsed = JsonSerializer.Deserialize<T>(receiveMessage);
} }
return parsed; return parsed;
@@ -4,16 +4,16 @@ public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontend
{ {
public Stream ServerStream { get; set; } public Stream ServerStream { get; set; }
public byte[] ReceiveMessage() public async Task<byte[]> ReceiveMessage()
{ {
const int bufferSize = ushort.MaxValue; const int bufferSize = ushort.MaxValue;
byte[] buffer = new byte[bufferSize]; byte[] buffer = new byte[bufferSize];
if (!ServerStream.CanRead) throw new IOException("Server is not connected."); if (!ServerStream.CanRead) throw new IOException("Server is not connected.");
ServerStream.ReadExactly(buffer, 0, 2); await ServerStream.ReadExactlyAsync(buffer, 0, 2);
var len = BitConverter.ToUInt16(buffer, 0); var len = BitConverter.ToUInt16(buffer, 0);
ServerStream.ReadExactly(buffer, 0, len); await ServerStream.ReadExactlyAsync(buffer, 0, len);
return buffer[..len]; return buffer[..len];
} }
@@ -21,4 +21,5 @@ public class TransmittedSharedObject<T>
public static implicit operator TransmittedSharedObject<T>(T value) => public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(value) }; new() { ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(value) };
} }