Small change of message type assigning

This commit is contained in:
2025-03-31 10:34:04 +03:00
parent 62dde889d8
commit 2851a85d1c
38 changed files with 78 additions and 711 deletions
@@ -198,7 +198,7 @@ namespace mROA.Implementation.Backend
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
representationModule.PostCallMessage(command.Id, MessageType.FinishedCommandExecution, payload);
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, payload);
multiClientOwnershipRepository?.FreeOwnership();
});
@@ -240,7 +240,7 @@ namespace mROA.Implementation.Backend
var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
representationModule.PostCallMessage(command.Id, MessageType.FinishedCommandExecution,
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution,
payload);
multiClientOwnershipRepository?.FreeOwnership();
});
@@ -75,9 +75,9 @@ namespace mROA.Implementation.Backend
interaction.BaseStream = client.GetStream();
interaction.PostMessage(new NetworkMessage
interaction.PostMessage(new NetworkMessageHeader
{
Id = Guid.NewGuid(), SchemaId = MessageType.IdAssigning,
Id = Guid.NewGuid(), EMessageType = EMessageType.IdAssigning,
Data = _serialization!.Serialize(new IdAssignment { Id = -interaction.ConnectionId })
});
_hub!.RegisterInteraction(interaction);
@@ -6,5 +6,6 @@ namespace mROA.Implementation.CommandExecution
public class AsyncCommandExecution : ICommandExecution
{
public Guid Id { get; set; }
public EMessageType MessageType => EMessageType.Unknown;
}
}
@@ -7,6 +7,7 @@ namespace mROA.Implementation.CommandExecution
public class ExceptionCommandExecution : ICommandExecution
{
public Guid Id { get; set; }
public EMessageType MessageType => EMessageType.ExceptionCommandExecution;
public string Exception { get; set; }
public RemoteException GetException()
@@ -8,6 +8,7 @@ namespace mROA.Implementation.CommandExecution
public class FinalCommandExecution : ICommandExecution
{
public Guid Id { get; set; }
public EMessageType MessageType => EMessageType.FinishedCommandExecution;
}
public class FinalCommandExecution<T> : FinalCommandExecution
@@ -40,10 +40,10 @@ namespace mROA.Implementation.Frontend
_tcpClient.Connect(_ipEndPoint);
_interactionModule.BaseStream = _tcpClient.GetStream();
var welcomeMessage = _interactionModule.GetNextMessageReceiving().GetAwaiter().GetResult();
if (welcomeMessage.SchemaId != MessageType.IdAssigning)
if (welcomeMessage.EMessageType != EMessageType.IdAssigning)
{
throw new Exception(
$"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.SchemaId.ToString()}");
$"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.EMessageType.ToString()}");
}
@@ -73,13 +73,13 @@ namespace mROA.Implementation.Frontend
var token = tokenSource.Token;
var defaultRequest =
_representationModule!.GetMessageAsync<DefaultCallRequest>(
messageType: MessageType.CallRequest, token: token);
messageType: EMessageType.CallRequest, token: token);
var cancelRequest =
_representationModule!.GetMessageAsync<CancelRequest>(
messageType: MessageType.CancelRequest, token: token);
messageType: EMessageType.CancelRequest, token: token);
var eventRequest =
_representationModule!.GetMessageAsync<DefaultCallRequest>(
messageType: MessageType.EventRequest, token: token);
messageType: EMessageType.EventRequest, token: token);
Task.WaitAny(defaultRequest, cancelRequest, eventRequest);
#if TRACE
Console.WriteLine("Request received");
@@ -135,13 +135,9 @@ namespace mROA.Implementation.Frontend
var result = _executeModule!.Execute(request, _realContextRepository!, _representationModule!);
var resultType = result switch
{
FinalCommandExecution => MessageType.FinishedCommandExecution,
ExceptionCommandExecution => MessageType.ExceptionCommandExecution,
_ => MessageType.Unknown
};
if (resultType == MessageType.Unknown)
var resultType = result.MessageType;
if (resultType == EMessageType.Unknown)
{
return;
}
+3 -1
View File
@@ -1,7 +1,9 @@
namespace mROA.Implementation
{
public class IdAssignment
public class IdAssignment : INetworkMessage
{
public int Id { get; set; }
public EMessageType MessageType => EMessageType.IdAssigning;
}
}
@@ -1,21 +1,27 @@
using System;
using System.Text.Json.Serialization;
using mROA.Implementation.Attributes;
// ReSharper disable UnusedMember.Global
namespace mROA.Implementation
{
public class NetworkMessage
public interface INetworkMessage
{
[SerializationIgnore]
public EMessageType MessageType { get; }
}
public class NetworkMessageHeader
{
public Guid Id { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public MessageType SchemaId { get; set; }
public EMessageType EMessageType { get; set; }
public byte[] Data { get; set; }
}
public enum MessageType
public enum EMessageType
{
Unknown,
FinishedCommandExecution,
@@ -23,6 +29,7 @@ namespace mROA.Implementation
CallRequest,
IdAssigning,
CancelRequest,
EventRequest
EventRequest,
ClientRecovery
}
}
@@ -11,8 +11,8 @@ namespace mROA.Implementation
{
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 readonly List<NetworkMessageHeader> _messageBuffer = new(128);
private Task<NetworkMessageHeader>? _currentReceiving;
private ISerializationToolkit? _serialization;
public int ConnectionId { get; set; }
public Stream? BaseStream { get; set; }
@@ -31,14 +31,14 @@ namespace mROA.Implementation
}
}
public Task<NetworkMessage> GetNextMessageReceiving()
public Task<NetworkMessageHeader> GetNextMessageReceiving()
{
if (_currentReceiving != null) return _currentReceiving;
_currentReceiving = Task.Run(async () => await GetNextMessage());
return _currentReceiving;
}
public async Task PostMessage(NetworkMessage message)
public async Task PostMessage(NetworkMessageHeader messageHeader)
{
if (BaseStream == null)
throw new NullReferenceException("BaseStream is null");
@@ -49,26 +49,26 @@ namespace mROA.Implementation
// Console.WriteLine("Sending {0}", JsonSerializer.Serialize(message));
var rawMessage = _serialization.Serialize(message);
var rawMessage = _serialization.Serialize(messageHeader);
var header = BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort));
await BaseStream.WriteAsync(header);
await BaseStream.WriteAsync(rawMessage);
}
public void HandleMessage(NetworkMessage message)
public void HandleMessage(NetworkMessageHeader messageHeader)
{
_messageBuffer.Remove(message);
_messageBuffer.Remove(messageHeader);
}
public NetworkMessage[] UnhandledMessages => _messageBuffer.ToArray();
public NetworkMessageHeader[] UnhandledMessages => _messageBuffer.ToArray();
public NetworkMessage? FirstByFilter(Predicate<NetworkMessage> predicate)
public NetworkMessageHeader? FirstByFilter(Predicate<NetworkMessageHeader> predicate)
{
return _messageBuffer.FirstOrDefault(m => predicate(m));
}
private async Task<NetworkMessage> GetNextMessage()
private async Task<NetworkMessageHeader> GetNextMessage()
{
if (BaseStream == null)
throw new NullReferenceException("BaseStream is null");
@@ -88,7 +88,7 @@ namespace mROA.Implementation
// Console.WriteLine("Receiving {0}", Encoding.Default.GetString(_buffer[..len]));
var message = _serialization.Deserialize<NetworkMessage>(localSpan.Span);
var message = _serialization.Deserialize<NetworkMessageHeader>(localSpan.Span);
#if TRACE
Console.WriteLine($"{DateTime.Now.TimeOfDay} Received Message {message.Id} - {message.SchemaId}");
TransmissionConfig.TotalTransmittedBytes += len;
+8 -8
View File
@@ -56,24 +56,24 @@ namespace mROA.Implementation
CommandId = methodId, ObjectId = _identifier, Parameters = parameters
};
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
await _representationModule.PostCallMessageAsync(request.Id, EMessageType.CallRequest, request);
var localTokenSource = new CancellationTokenSource();
var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution<T>>(request.Id,
MessageType.FinishedCommandExecution,
EMessageType.FinishedCommandExecution,
localTokenSource.Token);
var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
MessageType.ExceptionCommandExecution, localTokenSource.Token);
EMessageType.ExceptionCommandExecution, localTokenSource.Token);
cancellationToken.Register(async () =>
{
#if TRACE
Console.WriteLine("Cancelling task");
#endif
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest,
await _representationModule.PostCallMessageAsync(request.Id, EMessageType.CancelRequest,
new CancelRequest
{
Id = request.Id
@@ -103,24 +103,24 @@ namespace mROA.Implementation
{
CommandId = methodId, ObjectId = _identifier, Parameters = parameters
};
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
await _representationModule.PostCallMessageAsync(request.Id, EMessageType.CallRequest, request);
var localTokenSource = new CancellationTokenSource();
var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution>(request.Id,
MessageType.FinishedCommandExecution,
EMessageType.FinishedCommandExecution,
localTokenSource.Token);
var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
MessageType.ExceptionCommandExecution, localTokenSource.Token);
EMessageType.ExceptionCommandExecution, localTokenSource.Token);
cancellationToken.Register(async () =>
{
#if TRACE
Console.WriteLine("Cancelling task");
#endif
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest,
await _representationModule.PostCallMessageAsync(request.Id, EMessageType.CancelRequest,
new CancelRequest
{
Id = request.Id
+14 -14
View File
@@ -28,7 +28,7 @@ namespace mROA.Implementation
public int Id => (_interaction ?? throw new NullReferenceException("Interaction is not initialized"))
.ConnectionId;
public async Task<T> GetMessageAsync<T>(Guid? requestId, MessageType? messageType,
public async Task<T> GetMessageAsync<T>(Guid? requestId, EMessageType? messageType,
CancellationToken token = default)
{
if (_serialization == null)
@@ -38,7 +38,7 @@ namespace mROA.Implementation
return _serialization.Deserialize<T>(rawMessage)!;
}
public T GetMessage<T>(Guid? requestId = null, MessageType? messageType = null)
public T GetMessage<T>(Guid? requestId = null, EMessageType? messageType = null)
{
if (_serialization == null)
throw new NullReferenceException("Serialization toolkit is not initialized");
@@ -47,7 +47,7 @@ namespace mROA.Implementation
return _serialization.Deserialize<T>(rawMessage)!;
}
public async Task<byte[]> GetRawMessage(Guid? requestId = null, MessageType? messageType = null,
public async Task<byte[]> GetRawMessage(Guid? requestId = null, EMessageType? messageType = null,
CancellationToken token = default)
{
if (_interaction == null)
@@ -56,7 +56,7 @@ namespace mROA.Implementation
var fromBuffer =
_interaction.FirstByFilter(message =>
(requestId is null || message.Id == requestId) &&
(messageType is null || message.SchemaId == messageType));
(messageType is null || message.EMessageType == messageType));
if (fromBuffer == null)
{
@@ -64,7 +64,7 @@ namespace mROA.Implementation
{
var message = await _interaction.GetNextMessageReceiving();
if ((requestId is not null && message.Id != requestId) ||
(messageType is not null && message.SchemaId != messageType))
(messageType is not null && message.EMessageType != messageType))
continue;
_interaction.HandleMessage(message);
@@ -81,12 +81,12 @@ namespace mROA.Implementation
return fromBuffer.Data;
}
public async Task PostCallMessageAsync<T>(Guid id, MessageType messageType, T payload) where T : notnull
public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload) where T : notnull
{
await PostCallMessageAsync(id, messageType, payload, typeof(T));
await PostCallMessageAsync(id, eMessageType, payload, typeof(T));
}
public async Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType)
public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, object payload, Type payloadType)
{
if (_interaction == null)
throw new NullReferenceException("Interaction toolkit is not initialized");
@@ -97,18 +97,18 @@ namespace mROA.Implementation
#endif
var serialized = _serialization.Serialize(payload, payloadType);
await _interaction.PostMessage(new NetworkMessage
{ Id = id, SchemaId = messageType, Data = serialized });
await _interaction.PostMessage(new NetworkMessageHeader
{ Id = id, EMessageType = eMessageType, Data = serialized });
}
public void PostCallMessage<T>(Guid id, MessageType messageType, T payload) where T : notnull
public void PostCallMessage<T>(Guid id, EMessageType eMessageType, T payload) where T : notnull
{
PostCallMessageAsync(id, messageType, payload).GetAwaiter().GetResult();
PostCallMessageAsync(id, eMessageType, payload).GetAwaiter().GetResult();
}
public void PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType)
public void PostCallMessage(Guid id, EMessageType eMessageType, object payload, Type payloadType)
{
PostCallMessageAsync(id, messageType, payload, payloadType).GetAwaiter().GetResult();
PostCallMessageAsync(id, eMessageType, payload, payloadType).GetAwaiter().GetResult();
}
}
}