Untrusted channel works, but it breaks trusted channel
This commit is contained in:
@@ -18,6 +18,8 @@ namespace mROA.Implementation.Backend
|
||||
private ISerializationToolkit? _serialization;
|
||||
private Dictionary<int, CancellationTokenSource> _extractorsCTS = new();
|
||||
|
||||
|
||||
|
||||
public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType,
|
||||
IInjectableModule[] injectableModules)
|
||||
{
|
||||
@@ -124,7 +126,7 @@ namespace mROA.Implementation.Backend
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ThrowIfNotInjected()
|
||||
{
|
||||
if (_hub is null)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation.Backend
|
||||
{
|
||||
public class UdpGateway : IUntrustedGateway
|
||||
{
|
||||
private IConnectionHub _hub;
|
||||
private UdpClient _client;
|
||||
private Dictionary<IPEndPoint, int> _reservedPorts = new();
|
||||
private CancellationTokenSource _tokenSource = new();
|
||||
private ISerializationToolkit _serializationToolkit;
|
||||
|
||||
public UdpGateway(IPEndPoint listeningEndpoint)
|
||||
{
|
||||
_client = new UdpClient(listeningEndpoint);
|
||||
}
|
||||
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
switch (dependency)
|
||||
{
|
||||
case IConnectionHub hub:
|
||||
_hub = hub;
|
||||
break;
|
||||
case ISerializationToolkit serializationToolkit:
|
||||
_serializationToolkit = serializationToolkit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
_client.Close();
|
||||
}
|
||||
|
||||
public Task Start()
|
||||
{
|
||||
var token = _tokenSource.Token;
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
while (token.IsCancellationRequested == false)
|
||||
{
|
||||
var incoming = await _client.ReceiveAsync();
|
||||
var parsed = _serializationToolkit.Deserialize<NetworkMessageHeader>(incoming.Buffer);
|
||||
try
|
||||
{
|
||||
int channelId;
|
||||
switch (parsed.MessageType)
|
||||
{
|
||||
case EMessageType.UntrustedConnect:
|
||||
channelId = BitConverter.ToInt32(parsed.Data);
|
||||
_reservedPorts[incoming.RemoteEndPoint] = channelId;
|
||||
_ = UntrustedSend(_hub.GetInteraction(channelId), incoming.RemoteEndPoint);
|
||||
break;
|
||||
default:
|
||||
channelId = _reservedPorts[incoming.RemoteEndPoint];
|
||||
var interaction = _hub.GetInteraction(channelId);
|
||||
await interaction.ReceiveChanel.Writer.WriteAsync(parsed, token);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
private Task UntrustedSend(IChannelInteractionModule interaction, IPEndPoint endpoint)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
await foreach (var post in interaction.UntrustedPostChanel.ReadAllAsync())
|
||||
{
|
||||
var parsed = _serializationToolkit.Serialize(post);
|
||||
await _client.SendAsync(parsed, parsed.Length, endpoint);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,6 @@ namespace mROA.Implementation
|
||||
ClientRecovery,
|
||||
ClientConnect,
|
||||
ClientDisconnect,
|
||||
UntrustedConnect,
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ namespace mROA.Implementation
|
||||
private ISerializationToolkit _serializationToolkit;
|
||||
private IChannelInteractionModule _channelInteractionModule;
|
||||
private CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
@@ -35,19 +36,36 @@ namespace mROA.Implementation
|
||||
{
|
||||
var message = new Memory<byte>((await udpClient.ReceiveAsync()).Buffer);
|
||||
var parsed = _serializationToolkit.Deserialize<NetworkMessageHeader>(message.Span)!;
|
||||
|
||||
await writer.WriteAsync(parsed, token);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Posting(UdpClient udpClient, CancellationToken token)
|
||||
{
|
||||
var initMessage = new NetworkMessageHeader
|
||||
{
|
||||
MessageType = EMessageType.UntrustedConnect, Id = Guid.NewGuid(),
|
||||
Data = BitConverter.GetBytes(Math.Abs(_channelInteractionModule.ConnectionId))
|
||||
};
|
||||
|
||||
var initParsed = _serializationToolkit.Serialize(initMessage);
|
||||
|
||||
await udpClient.SendAsync(initParsed, initParsed.Length);
|
||||
|
||||
await foreach (var post in _channelInteractionModule.UntrustedPostChanel.ReadAllAsync(token))
|
||||
{
|
||||
var serialized = _serializationToolkit.Serialize(post);
|
||||
#if TRACE
|
||||
Console.WriteLine("Untrusted write start");
|
||||
#endif
|
||||
await udpClient.SendAsync(serialized, serialized.Length);
|
||||
#if TRACE
|
||||
Console.WriteLine("Untrusted write finished");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
switch (dependency)
|
||||
|
||||
Reference in New Issue
Block a user