улучшен новый модуль взаимодействия и написан новый модуль репрезентации
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class AsyncNetworkInteractionModule
|
||||
{
|
||||
private ISerializationToolkit _iSerializationToolkit;
|
||||
private const int BufferSize = ushort.MaxValue;
|
||||
private Stream? _stream;
|
||||
private List<NetworkMessage> _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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using mROA.Abstract;
|
||||
using System.Text.Json;
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation;
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class NextGenerationInteractionModule : INextGenerationInteractionModule
|
||||
{
|
||||
private ISerializationToolkit? _serialization;
|
||||
public int ConntectionId { get; set; }
|
||||
public Stream? BaseStream { get; set; }
|
||||
private Task<NetworkMessage>? _currentReceiving;
|
||||
private const int BufferSize = ushort.MaxValue;
|
||||
private readonly byte[] _buffer = new byte[BufferSize];
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
if (dependency is ISerializationToolkit toolkit)
|
||||
_serialization = toolkit;
|
||||
}
|
||||
|
||||
|
||||
public Task<NetworkMessage> GetNextMessageReceiving()
|
||||
{
|
||||
if (_currentReceiving is { IsCompleted: false })
|
||||
return _currentReceiving;
|
||||
|
||||
_currentReceiving = GetNextMessage();
|
||||
return _currentReceiving;
|
||||
}
|
||||
|
||||
public async Task PostMessage(NetworkMessage message)
|
||||
{
|
||||
if (BaseStream == null)
|
||||
throw new NullReferenceException("BaseStream is null");
|
||||
|
||||
var rawMessage = _serialization.Serialize(message);
|
||||
await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort)));
|
||||
await BaseStream.WriteAsync(rawMessage);
|
||||
}
|
||||
|
||||
private async Task<NetworkMessage> 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);
|
||||
|
||||
return _serialization.Deserialize<NetworkMessage>(_buffer[..len])!;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Immutable;
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation;
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class RepresentationModule : IRepresentationModule
|
||||
{
|
||||
private ISerializationToolkit _serialization;
|
||||
private INextGenerationInteractionModule _interaction;
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
switch (dependency)
|
||||
{
|
||||
case ISerializationToolkit toolkit:
|
||||
_serialization = toolkit;
|
||||
break;
|
||||
case INextGenerationInteractionModule interactionModule:
|
||||
_interaction = interactionModule;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public int Id { get; set; }
|
||||
|
||||
public async Task<T> GetMessage<T>(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<T>(message.Data)!;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PostCallMessage<T>(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)});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user