Практически работает

This commit is contained in:
2025-03-10 17:35:16 +03:00
parent b430f1c572
commit c33d282a5c
4 changed files with 17 additions and 17 deletions
@@ -65,7 +65,7 @@ namespace mROA.Implementation.Frontend
while (true) while (true)
{ {
#if TRACE #if TRACE
Console.WriteLine("Waiting for request..."); Console.WriteLine("Waiting for request...");
#endif #endif
var tokenSource = new CancellationTokenSource(); var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token; var token = tokenSource.Token;
@@ -80,12 +80,12 @@ namespace mROA.Implementation.Frontend
messageType: MessageType.EventRequest, token: token); messageType: MessageType.EventRequest, token: token);
Task.WaitAny(defaultRequest, cancelRequest, eventRequest); Task.WaitAny(defaultRequest, cancelRequest, eventRequest);
#if TRACE #if TRACE
Console.WriteLine("Request received"); Console.WriteLine("Request received");
#endif #endif
if (cancelRequest.IsCompleted) if (cancelRequest.IsCompleted)
{ {
#if TRACE #if TRACE
Console.WriteLine("Cancelling request"); Console.WriteLine("Cancelling request");
#endif #endif
var req = cancelRequest.Result; var req = cancelRequest.Result;
tokenSource.Cancel(); tokenSource.Cancel();
@@ -118,7 +118,7 @@ namespace mROA.Implementation.Frontend
else else
{ {
tokenSource.Cancel(); tokenSource.Cancel();
var request = defaultRequest.Result; var request = eventRequest.Result;
_executeModule.Execute(request, _contextRepository, _representationModule); _executeModule.Execute(request, _contextRepository, _representationModule);
} }
} }
@@ -9,13 +9,13 @@ namespace mROA.Implementation
{ {
public class NextGenerationInteractionModule : INextGenerationInteractionModule public class NextGenerationInteractionModule : INextGenerationInteractionModule
{ {
private const int BufferSize = ushort.MaxValue;
private readonly Memory<byte> _buffer = new byte[BufferSize];
private readonly List<NetworkMessage> _messageBuffer = new(128);
private Task<NetworkMessage>? _currentReceiving;
private ISerializationToolkit? _serialization; private ISerializationToolkit? _serialization;
public int ConnectionId { get; private set; } public int ConnectionId { get; private set; }
public Stream? BaseStream { get; set; } public Stream? BaseStream { get; set; }
private Task<NetworkMessage>? _currentReceiving;
private const int BufferSize = ushort.MaxValue;
private readonly Memory<byte> _buffer = new byte[BufferSize];
private readonly List<NetworkMessage> _messageBuffer = new (128);
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
@@ -36,7 +36,6 @@ namespace mROA.Implementation
if (_currentReceiving != null) return _currentReceiving; if (_currentReceiving != null) return _currentReceiving;
_currentReceiving = Task.Run(async () => await GetNextMessage()); _currentReceiving = Task.Run(async () => await GetNextMessage());
return _currentReceiving; return _currentReceiving;
} }
public async Task PostMessage(NetworkMessage message) public async Task PostMessage(NetworkMessage message)
@@ -46,7 +45,7 @@ namespace mROA.Implementation
if (_serialization == null) if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is not initialized"); throw new NullReferenceException("Serialization toolkit is not initialized");
// Console.WriteLine("Sending {0}", JsonSerializer.Serialize(message)); // Console.WriteLine("Sending {0}", JsonSerializer.Serialize(message));
@@ -62,6 +61,7 @@ namespace mROA.Implementation
} }
public NetworkMessage[] UnhandledMessages => _messageBuffer.ToArray(); public NetworkMessage[] UnhandledMessages => _messageBuffer.ToArray();
public NetworkMessage? FirstByFilter(Predicate<NetworkMessage> predicate) public NetworkMessage? FirstByFilter(Predicate<NetworkMessage> predicate)
{ {
return _messageBuffer.FirstOrDefault(m => predicate(m)); return _messageBuffer.FirstOrDefault(m => predicate(m));
@@ -79,23 +79,23 @@ namespace mROA.Implementation
// Console.WriteLine("Receiving message"); // Console.WriteLine("Receiving message");
var firstBit = (byte)BaseStream.ReadByte(); var firstBit = (byte)BaseStream.ReadByte();
var secondBit = (byte)BaseStream.ReadByte(); var secondBit = (byte)BaseStream.ReadByte();
var len = BitConverter.ToUInt16(new[] { firstBit, secondBit}); var len = BitConverter.ToUInt16(new[] { firstBit, secondBit });
var localSpan = _buffer.Slice(0, len); var localSpan = _buffer.Slice(0, len);
await BaseStream.ReadExactlyAsync(localSpan); await BaseStream.ReadExactlyAsync(localSpan);
// Console.WriteLine("Receiving {0}", Encoding.Default.GetString(_buffer[..len])); // Console.WriteLine("Receiving {0}", Encoding.Default.GetString(_buffer[..len]));
var message = _serialization.Deserialize<NetworkMessage>(localSpan.Span); var message = _serialization.Deserialize<NetworkMessage>(localSpan.Span);
#if TRACE #if TRACE
Console.WriteLine($"Received Message {message.SchemaId} - {message.Id}"); Console.WriteLine($"Received Message {message.Id} - {message.SchemaId}");
TransmissionConfig.TotalTransmittedBytes += len; TransmissionConfig.TotalTransmittedBytes += len;
Console.WriteLine($"Total recieced bytes are {TransmissionConfig.TotalTransmittedBytes}"); Console.WriteLine($"Total recieced bytes are {TransmissionConfig.TotalTransmittedBytes}");
#endif #endif
_messageBuffer.Add(message); _messageBuffer.Add(message);
_currentReceiving = Task.Run(async () => await GetNextMessage()); _currentReceiving = Task.Run(async () => await GetNextMessage());
return message; return message;
} }
} }
+1 -1
View File
@@ -91,7 +91,7 @@ namespace mROA.Implementation
if (_serialization == null) if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is not initialized"); throw new NullReferenceException("Serialization toolkit is not initialized");
#if TRACE #if TRACE
Console.WriteLine($"Posting message: {id} - {messageType}"); Console.WriteLine($"Posting message: {id} - {messageType} to {Id}");
#endif #endif
var serialized = _serialization.Serialize(payload, payloadType); var serialized = _serialization.Serialize(payload, payloadType);
+1 -1
View File
@@ -21,7 +21,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants></DefineConstants> <DefineConstants>TRACE;</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>