сетевой вызов метода работает
This commit is contained in:
+31
-3
@@ -1,28 +1,56 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using mROA.Implementation;
|
using mROA.Implementation;
|
||||||
|
using mROA.Implementation.Test;
|
||||||
|
|
||||||
namespace mROA.Test;
|
namespace mROA.Test;
|
||||||
|
|
||||||
public class StreamTest
|
public class StreamTest
|
||||||
{
|
{
|
||||||
private StreamBasedInteractionModule _interactionModule;
|
private StreamBasedInteractionModule _interactionModule;
|
||||||
|
private StreamBasedFrontendInteractionModule _frontendInteractionModule;
|
||||||
|
private JsonFrontendSerialisationModule _frontendSerialisationModule;
|
||||||
private ISerialisationModule _serialisationModule;
|
private ISerialisationModule _serialisationModule;
|
||||||
private IExecuteModule _executeModule;
|
private IExecuteModule _executeModule;
|
||||||
private IMethodRepository _methodRepository;
|
|
||||||
private IContextRepository _contextRepository;
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
_interactionModule = new StreamBasedInteractionModule();
|
_interactionModule = new StreamBasedInteractionModule();
|
||||||
|
|
||||||
_serialisationModule = new JsonSerialisationModule(_interactionModule, _methodRepository);
|
_serialisationModule = new JsonSerialisationModule(_interactionModule, new MockMethodRepository());
|
||||||
|
|
||||||
_executeModule = new MockExecModule();
|
_executeModule = new MockExecModule();
|
||||||
_serialisationModule.SetExecuteModule(_executeModule);
|
_serialisationModule.SetExecuteModule(_executeModule);
|
||||||
|
|
||||||
|
_frontendInteractionModule = new StreamBasedFrontendInteractionModule();
|
||||||
|
_frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void StreamingTest()
|
public void StreamingTest()
|
||||||
{
|
{
|
||||||
|
bool isTestNotFinished = true;
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
TcpListener listener = new TcpListener(IPAddress.Loopback, 4567);
|
||||||
|
listener.Start();
|
||||||
|
|
||||||
|
var stream = listener.AcceptTcpClient().GetStream();
|
||||||
|
|
||||||
|
Console.WriteLine("Client connected");
|
||||||
|
|
||||||
|
_interactionModule.RegisterClient(stream);
|
||||||
|
|
||||||
|
while (isTestNotFinished) ;
|
||||||
|
});
|
||||||
|
var tcpClient = new TcpClient();
|
||||||
|
tcpClient.Connect(IPAddress.Loopback, 4567);
|
||||||
|
_frontendInteractionModule.ServerStream = tcpClient.GetStream();
|
||||||
|
|
||||||
|
var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 };
|
||||||
|
_frontendSerialisationModule.PostCallRequest(req);
|
||||||
|
var res =_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result;
|
||||||
|
isTestNotFinished = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,6 @@ namespace mROA;
|
|||||||
public interface ICommandExecution
|
public interface ICommandExecution
|
||||||
{
|
{
|
||||||
Guid CallRequestId { get; init; }
|
Guid CallRequestId { get; init; }
|
||||||
int ClientId { get; }
|
int ClientId { get; set; }
|
||||||
int CommandId { get; }
|
int CommandId { get; }
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ public interface ISerialisationModule
|
|||||||
void SetExecuteModule(IExecuteModule executeModule);
|
void SetExecuteModule(IExecuteModule executeModule);
|
||||||
public interface IFrontendSerialisationModule
|
public interface IFrontendSerialisationModule
|
||||||
{
|
{
|
||||||
ICommandExecution GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
|
T GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
|
||||||
void PostCallRequest(ICallRequest callRequest);
|
void PostCallRequest(ICallRequest callRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
public interface ICallRequest
|
public interface ICallRequest
|
||||||
{
|
{
|
||||||
Guid CallRequestId { get; }
|
Guid CallRequestId { get; internal set; }
|
||||||
int CommandId { get; }
|
int CommandId { get; }
|
||||||
int ClientId { get; }
|
|
||||||
int ObjectId { get; }
|
int ObjectId { get; }
|
||||||
object Parameter { get; }
|
object Parameter { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JsonCallRequest : ICallRequest
|
public class JsonCallRequest : ICallRequest
|
||||||
{
|
{
|
||||||
public Guid CallRequestId { get; } = Guid.NewGuid();
|
public Guid CallRequestId { get; set; } = Guid.NewGuid();
|
||||||
public int CommandId { get; set; }
|
public int CommandId { get; set; }
|
||||||
public int ClientId { get; set; }
|
|
||||||
public int ObjectId { get; set; } = -1;
|
public int ObjectId { get; set; } = -1;
|
||||||
public object? Parameter { get; set; }
|
public object? Parameter { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace mROA.Implementation;
|
|||||||
public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInteractionModule interactionModule)
|
public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInteractionModule interactionModule)
|
||||||
: ISerialisationModule.IFrontendSerialisationModule
|
: ISerialisationModule.IFrontendSerialisationModule
|
||||||
{
|
{
|
||||||
public ICommandExecution GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution
|
public T GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution
|
||||||
{
|
{
|
||||||
var receiveMessage = interactionModule.ReceiveMessage();
|
var receiveMessage = interactionModule.ReceiveMessage();
|
||||||
var parsed = JsonSerializer.Deserialize<T>(receiveMessage);
|
var parsed = JsonSerializer.Deserialize<T>(receiveMessage);
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ public class JsonSerialisationModule : ISerialisationModule
|
|||||||
public void HandleIncomingRequest(int clientId, byte[] command)
|
public void HandleIncomingRequest(int clientId, byte[] command)
|
||||||
{
|
{
|
||||||
JsonCallRequest request = JsonSerializer.Deserialize<JsonCallRequest>(command);
|
JsonCallRequest request = JsonSerializer.Deserialize<JsonCallRequest>(command);
|
||||||
request.ClientId = clientId;
|
|
||||||
|
|
||||||
if (request.Parameter is not null)
|
if (request.Parameter is not null)
|
||||||
{
|
{
|
||||||
@@ -30,6 +29,7 @@ public class JsonSerialisationModule : ISerialisationModule
|
|||||||
}
|
}
|
||||||
|
|
||||||
var response = _executeModule.Execute(request);
|
var response = _executeModule.Execute(request);
|
||||||
|
response.ClientId = clientId;
|
||||||
PostResponse(response);
|
PostResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class LaunchReadyExecutionModule : IExecuteModule
|
|||||||
var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]);
|
var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]);
|
||||||
return new FinalCommandExecution
|
return new FinalCommandExecution
|
||||||
{
|
{
|
||||||
CommandId = command.CommandId, Result = finalResult, ClientId = command.ClientId,
|
CommandId = command.CommandId, Result = finalResult,
|
||||||
CallRequestId = command.CallRequestId
|
CallRequestId = command.CallRequestId
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ public class LaunchReadyExecutionModule : IExecuteModule
|
|||||||
|
|
||||||
var result = (Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!;
|
var result = (Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!;
|
||||||
var exec = new AsyncCommandExecution(tokenSource)
|
var exec = new AsyncCommandExecution(tokenSource)
|
||||||
{ CommandId = command.CommandId, ClientId = command.ClientId, CallRequestId = command.CallRequestId };
|
{ CommandId = command.CommandId, CallRequestId = command.CallRequestId };
|
||||||
|
|
||||||
result.ContinueWith(_ => { PostFinalizedCallback(exec, null); }, token);
|
result.ContinueWith(_ => { PostFinalizedCallback(exec, null); }, token);
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ public class LaunchReadyExecutionModule : IExecuteModule
|
|||||||
var result =
|
var result =
|
||||||
(Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!;
|
(Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!;
|
||||||
var exec = new AsyncCommandExecution(tokenSource)
|
var exec = new AsyncCommandExecution(tokenSource)
|
||||||
{ CommandId = command.CommandId, ClientId = command.ClientId, CallRequestId = command.CallRequestId };
|
{ CommandId = command.CommandId, CallRequestId = command.CallRequestId };
|
||||||
|
|
||||||
result.ContinueWith(task =>
|
result.ContinueWith(task =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,30 +2,25 @@ namespace mROA.Implementation;
|
|||||||
|
|
||||||
public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule
|
public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule
|
||||||
{
|
{
|
||||||
private Stream _serverStream;
|
public Stream ServerStream { get; set; }
|
||||||
|
|
||||||
public StreamBasedFrontendInteractionModule(Stream serverStream)
|
|
||||||
{
|
|
||||||
_serverStream = serverStream;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] ReceiveMessage()
|
public 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);
|
ServerStream.ReadExactly(buffer, 0, 2);
|
||||||
var len = BitConverter.ToUInt16(buffer, 0);
|
var len = BitConverter.ToUInt16(buffer, 0);
|
||||||
_serverStream.ReadExactly(buffer, 0, len);
|
ServerStream.ReadExactly(buffer, 0, len);
|
||||||
|
|
||||||
return buffer[..len];
|
return buffer[..len];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PostMessage(byte[] message)
|
public void PostMessage(byte[] message)
|
||||||
{
|
{
|
||||||
_serverStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
|
ServerStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
|
||||||
_serverStream.Write(message, 0, message.Length);
|
ServerStream.Write(message, 0, message.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace mROA.Implementation;
|
|
||||||
|
|
||||||
public class ConsoleFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule
|
|
||||||
{
|
|
||||||
public byte[] ReceiveMessage()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void PostMessage(byte[] message)
|
|
||||||
{
|
|
||||||
Console.WriteLine(Encoding.UTF8.GetString(message));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,6 @@ public class MockExecModule : IExecuteModule
|
|||||||
public ICommandExecution Execute(ICallRequest command)
|
public ICommandExecution Execute(ICallRequest command)
|
||||||
{
|
{
|
||||||
return new FinalCommandExecution
|
return new FinalCommandExecution
|
||||||
{ ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId };
|
{ CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace mROA.Implementation.Test;
|
||||||
|
|
||||||
|
public class MockMethodRepository : IMethodRepository
|
||||||
|
{
|
||||||
|
public MethodInfo GetMethod(int id)
|
||||||
|
{
|
||||||
|
return GetType().GetMethod("MockMethod")!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int RegisterMethod(MethodInfo method)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<MethodInfo> GetMethods()
|
||||||
|
{
|
||||||
|
return [GetType().GetMethod("MockMethod")!];
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MockMethod(object x)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user