diff --git a/mROA.Test/NextGenTest.cs b/mROA.Test/NextGenTest.cs index f57dc5c..7293877 100644 --- a/mROA.Test/NextGenTest.cs +++ b/mROA.Test/NextGenTest.cs @@ -46,6 +46,7 @@ public class NextGenTest Task.WaitAll(tasks.ToArray()); Assert.Pass(); + } private async Task ReadStream(Guid current) diff --git a/mROA/Abstract/IInteractionModule.cs b/mROA/Abstract/IInteractionModule.cs index 5d49bf1..5ccc8e6 100644 --- a/mROA/Abstract/IInteractionModule.cs +++ b/mROA/Abstract/IInteractionModule.cs @@ -2,25 +2,10 @@ using mROA.Implementation; namespace mROA.Abstract; -public interface IInteractionModule : IInjectableModule -{ - void SendTo(int clientId, byte[] message); - void RegisterSource(Stream stream); - Stream GetSource(int clientId); - public interface IFrontendInteractionModule : IInjectableModule - { - int ClientId { get; } - public Task ReceiveMessage(); - public void PostMessage(byte[] message); - } -} - public interface INextGenerationInteractionModule : IInjectableModule { int ConntectionId { get; } public Stream? BaseStream { get; set; } Task GetNextMessageReceiving(); - void PostMessage(NetworkMessage message); - NetworkMessage[] UnhandledMessages { get; } - void HandleMessage(NetworkMessage msg); + Task PostMessage(NetworkMessage message); } \ No newline at end of file diff --git a/mROA/Abstract/ISerialisationModule.cs b/mROA/Abstract/ISerialisationModule.cs index 7ce2124..883265d 100644 --- a/mROA/Abstract/ISerialisationModule.cs +++ b/mROA/Abstract/ISerialisationModule.cs @@ -1,3 +1,4 @@ +using System.Windows.Input; using mROA.Implementation; namespace mROA.Abstract; @@ -12,7 +13,14 @@ public interface ISerialisationModule : IInjectableModule int ClientId { get; } Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution; Task> GetFinalCommandExecution(Guid requestId); - void PostCallRequest(ICallRequest callRequest); } +} + +public interface IRepresentationModule : IInjectableModule +{ + int Id { get; } + Task GetMessage(Guid? requestId, MessageType? messageType); + Task PostCallMessage(Guid id, MessageType messageType, T payload); + Task PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType); } \ No newline at end of file diff --git a/mROA/Implementation/AsyncNetworkInteractionModule.cs b/mROA/Implementation/AsyncNetworkInteractionModule.cs deleted file mode 100644 index d3c4a96..0000000 --- a/mROA/Implementation/AsyncNetworkInteractionModule.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace mROA.Implementation; - -public class AsyncNetworkInteractionModule -{ - private ISerializationToolkit _iSerializationToolkit; - private const int BufferSize = ushort.MaxValue; - private Stream? _stream; - private List _unhandledMessages = new(8); - private byte[] _buffer = new byte[BufferSize]; - private Task? _currentReadTask; - - - public Task ReceiveNext() - { - _currentReadTask ??= StartReceiving(); - return _currentReadTask; - } - - private async Task StartReceiving() - { - await _stream.ReadExactlyAsync(_buffer, 0, 2); - var len = BitConverter.ToUInt16(_buffer, 0); - await _stream.ReadExactlyAsync(_buffer, 0, len); - - } - - public NetworkMessage GetLastMessage() - { - return _unhandledMessages.Last(); - } - - -} \ No newline at end of file diff --git a/mROA/Implementation/NetworkMessage.cs b/mROA/Implementation/NetworkMessage.cs index 4a08543..bd70245 100644 --- a/mROA/Implementation/NetworkMessage.cs +++ b/mROA/Implementation/NetworkMessage.cs @@ -1,4 +1,5 @@ -using mROA.Abstract; +using System.Text.Json; +using mROA.Abstract; namespace mROA.Implementation; diff --git a/mROA/NextGenerationInteractionModule.cs b/mROA/Implementation/NextGenerationInteractionModule.cs similarity index 58% rename from mROA/NextGenerationInteractionModule.cs rename to mROA/Implementation/NextGenerationInteractionModule.cs index 07404da..e8657ad 100644 --- a/mROA/NextGenerationInteractionModule.cs +++ b/mROA/Implementation/NextGenerationInteractionModule.cs @@ -1,17 +1,15 @@ using mROA.Abstract; -using mROA.Implementation; -namespace mROA; +namespace mROA.Implementation; public class NextGenerationInteractionModule : INextGenerationInteractionModule { - private ISerializationToolkit _serialization; + private ISerializationToolkit? _serialization; public int ConntectionId { get; set; } - public Stream BaseStream { get; set; } + public Stream? BaseStream { get; set; } private Task? _currentReceiving; private const int BufferSize = ushort.MaxValue; - private byte[] _buffer = new byte[BufferSize]; - private List _unhandledMessages = new(8); + private readonly byte[] _buffer = new byte[BufferSize]; public void Inject(T dependency) { @@ -29,22 +27,24 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule return _currentReceiving; } - public void PostMessage(NetworkMessage message) + public async Task PostMessage(NetworkMessage message) { + if (BaseStream == null) + throw new NullReferenceException("BaseStream is null"); + var rawMessage = _serialization.Serialize(message); - BaseStream.Write(BitConverter.GetBytes((ushort)rawMessage.Length), 0, sizeof(ushort)); - BaseStream.Write(rawMessage, 0, rawMessage.Length); - } - - public NetworkMessage[] UnhandledMessages => _unhandledMessages.ToArray(); - - public void HandleMessage(NetworkMessage msg) - { - _unhandledMessages.Remove(msg); + await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort))); + await BaseStream.WriteAsync(rawMessage); } private async Task GetNextMessage() { + if (BaseStream == null) + throw new NullReferenceException("BaseStream is null"); + + if (_serialization == null) + throw new NullReferenceException("Serialization toolkit is null"); + await BaseStream.ReadExactlyAsync(_buffer, 0, 2); var len = BitConverter.ToUInt16(_buffer, 0); await BaseStream.ReadExactlyAsync(_buffer, 0, len); diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 8c30743..78faf9e 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -1,5 +1,4 @@ using System.Collections.Frozen; -using System.Collections.Immutable; using mROA.Abstract; namespace mROA.Implementation; diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs new file mode 100644 index 0000000..477f411 --- /dev/null +++ b/mROA/Implementation/RepresentationModule.cs @@ -0,0 +1,44 @@ +using mROA.Abstract; + +namespace mROA.Implementation; + +public class RepresentationModule : IRepresentationModule +{ + private ISerializationToolkit _serialization; + private INextGenerationInteractionModule _interaction; + + public void Inject(T dependency) + { + switch (dependency) + { + case ISerializationToolkit toolkit: + _serialization = toolkit; + break; + case INextGenerationInteractionModule interactionModule: + _interaction = interactionModule; + break; + } + } + + public int Id { get; set; } + + public async Task GetMessage(Guid? requestId, MessageType? messageType) + { + while (true) + { + var message = await _interaction.GetNextMessageReceiving(); + if ((requestId is null || message.Id == requestId) && (messageType is null || message.SchemaId == messageType)) + return _serialization.Deserialize(message.Data)!; + } + } + + public async Task PostCallMessage(Guid id, MessageType messageType, T payload) + { + await PostCallMessage(id, messageType, payload, typeof(T)); + } + + public async Task PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType) + { + await _interaction.PostMessage(new NetworkMessage {Id = id, SchemaId = messageType, Data = _serialization.Serialize(payload, payloadType)}); + } +} \ No newline at end of file