Fix udp bug
This commit is contained in:
@@ -51,12 +51,8 @@ class Program
|
|||||||
builder.Services.Configure<DistributionOptions>(o => o.DistributionType = EDistributionType.ExtractorFirst);
|
builder.Services.Configure<DistributionOptions>(o => o.DistributionType = EDistributionType.ExtractorFirst);
|
||||||
|
|
||||||
var host = builder.Build();
|
var host = builder.Build();
|
||||||
//
|
|
||||||
new RemoteTypeBinder();
|
new RemoteTypeBinder();
|
||||||
//
|
|
||||||
|
|
||||||
// var contextualSerializationToolKit = host.Services.GetService<IContextualSerializationToolKit>();
|
|
||||||
// contextualSerializationToolKit.Deserialize<NetworkMessage>(contextualSerializationToolKit.Serialize(new NetworkMessage(), null), null);
|
|
||||||
_ = host.Services.GetService<IUntrustedGateway>()!.Start();
|
_ = host.Services.GetService<IUntrustedGateway>()!.Start();
|
||||||
var gateway = host.Services.GetService<IGatewayModule>();
|
var gateway = host.Services.GetService<IGatewayModule>();
|
||||||
gateway.Run();
|
gateway.Run();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class Program
|
|||||||
builder.Services.AddSingleton<IChannelInteractionModule, ChannelInteractionModule>();
|
builder.Services.AddSingleton<IChannelInteractionModule, ChannelInteractionModule>();
|
||||||
builder.Services.AddSingleton<IUntrustedInteractionModule, UdpUntrustedInteraction>();
|
builder.Services.AddSingleton<IUntrustedInteractionModule, UdpUntrustedInteraction>();
|
||||||
builder.Services.AddSingleton<IRepresentationModule, RepresentationModule>();
|
builder.Services.AddSingleton<IRepresentationModule, RepresentationModule>();
|
||||||
var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 9000);
|
var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 4567);
|
||||||
builder.Services.AddSingleton<IFrontendBridge, NetworkFrontendBridge>();
|
builder.Services.AddSingleton<IFrontendBridge, NetworkFrontendBridge>();
|
||||||
|
|
||||||
builder.Services.AddOptions();
|
builder.Services.AddOptions();
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Formats.Cbor;
|
using System.Formats.Cbor;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Threading;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using mROA.Abstract;
|
using mROA.Abstract;
|
||||||
using mROA.Implementation;
|
using mROA.Implementation;
|
||||||
@@ -15,13 +15,14 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
public class CborSerializationToolkit : IContextualSerializationToolKit
|
public class CborSerializationToolkit : IContextualSerializationToolKit
|
||||||
{
|
{
|
||||||
private readonly CborWriter _writer = new(initialCapacity: 2048);
|
private readonly ThreadLocal<CborWriter> _writer = new(() => new CborWriter(initialCapacity: 2048));
|
||||||
private readonly int _offset;
|
private readonly int _offset;
|
||||||
|
|
||||||
public CborSerializationToolkit(IOptions<SerializationBufferOffset> offsetOptions) : this(offsetOptions.Value.Offset)
|
public CborSerializationToolkit(IOptions<SerializationBufferOffset> offsetOptions) : this(offsetOptions.Value
|
||||||
|
.Offset)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly IOrdinaryStructureParser[] _parsers =
|
private readonly IOrdinaryStructureParser[] _parsers =
|
||||||
{
|
{
|
||||||
new CallRequestParser(), new FinalCommandExecutionParser(),
|
new CallRequestParser(), new FinalCommandExecutionParser(),
|
||||||
@@ -63,27 +64,24 @@ namespace mROA.Cbor
|
|||||||
|
|
||||||
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
||||||
{
|
{
|
||||||
byte[] result;
|
var writer = _writer.Value;
|
||||||
lock (_writer)
|
writer.Reset();
|
||||||
{
|
WriteData(objectToSerialize, writer, context);
|
||||||
_writer.Reset();
|
var result = new byte[_offset + writer.BytesWritten];
|
||||||
WriteData(objectToSerialize, _writer, context);
|
var span = result.AsSpan();
|
||||||
result = new byte[_offset + _writer.BytesWritten];
|
writer.Encode(span[_offset..]);
|
||||||
var span = result.AsSpan();
|
|
||||||
_writer.Encode(span[_offset..]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Serialize(object objectToSerialize, Span<byte> destination, IEndPointContext context)
|
public int Serialize(object objectToSerialize, Span<byte> destination, IEndPointContext context)
|
||||||
{
|
{
|
||||||
lock (_writer)
|
var writer = _writer.Value;
|
||||||
{
|
writer.Reset();
|
||||||
_writer.Reset();
|
WriteData(objectToSerialize, writer, context);
|
||||||
WriteData(objectToSerialize, _writer, context);
|
return writer.Encode(destination);
|
||||||
return _writer.Encode(destination);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Deserialize<T>(byte[] rawData, IEndPointContext? context)
|
public T Deserialize<T>(byte[] rawData, IEndPointContext? context)
|
||||||
@@ -110,7 +108,8 @@ namespace mROA.Cbor
|
|||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Bad deserialization for type {type}. Bytes: {BitConverter.ToString(rawMemory.ToArray())}");
|
Console.WriteLine(
|
||||||
|
$"Bad deserialization for type {type}. Bytes: {BitConverter.ToString(rawMemory.ToArray())}");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +141,7 @@ namespace mROA.Cbor
|
|||||||
|
|
||||||
public IContextualSerializationToolKit Clone()
|
public IContextualSerializationToolKit Clone()
|
||||||
{
|
{
|
||||||
return new CborSerializationToolkit(_offset) ;
|
return new CborSerializationToolkit(_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
|
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ namespace mROA.Codegen
|
|||||||
<!T eventBinderTemplate>
|
<!T eventBinderTemplate>
|
||||||
(instance as <!L type>).<!L eventName> += (<!L parametersDeclaration>) =>
|
(instance as <!L type>).<!L eventName> += (<!L parametersDeclaration>) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Try to send to {ownerId} with hash code {context.GetHashCode()}");
|
|
||||||
<!I callFilter>
|
<!I callFilter>
|
||||||
Console.WriteLine("Sending event...");
|
Console.WriteLine("Sending event...");
|
||||||
var request = new CallRequest
|
var request = new CallRequest
|
||||||
|
|||||||
@@ -14,15 +14,17 @@ namespace mROA.Implementation.Backend
|
|||||||
{
|
{
|
||||||
private readonly IConnectionHub _hub;
|
private readonly IConnectionHub _hub;
|
||||||
private readonly UdpClient _client;
|
private readonly UdpClient _client;
|
||||||
private readonly Dictionary<IPEndPoint, int> _reservedPorts = new();
|
private readonly Dictionary<IPEndPoint, Action<NetworkMessage>> _distributionActions = new();
|
||||||
private readonly CancellationTokenSource _tokenSource = new();
|
private readonly CancellationTokenSource _tokenSource = new();
|
||||||
private readonly IContextualSerializationToolKit _serializationToolkit;
|
private readonly IContextualSerializationToolKit _serializationToolkit;
|
||||||
|
private readonly IDistributionModule _distribution;
|
||||||
|
|
||||||
public UdpGateway(IOptions<GatewayOptions> options, IConnectionHub hub,
|
public UdpGateway(IOptions<GatewayOptions> options, IConnectionHub hub,
|
||||||
IContextualSerializationToolKit serializationToolkit)
|
IContextualSerializationToolKit serializationToolkit, IDistributionModule distribution)
|
||||||
{
|
{
|
||||||
_hub = hub;
|
_hub = hub;
|
||||||
_serializationToolkit = serializationToolkit;
|
_serializationToolkit = serializationToolkit;
|
||||||
|
_distribution = distribution;
|
||||||
_client = new UdpClient(options.Value.Endpoint);
|
_client = new UdpClient(options.Value.Endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,13 +51,12 @@ namespace mROA.Implementation.Backend
|
|||||||
{
|
{
|
||||||
case UntrustedConnect:
|
case UntrustedConnect:
|
||||||
channelId = BitConverter.ToInt32(parsed.Data);
|
channelId = BitConverter.ToInt32(parsed.Data);
|
||||||
_reservedPorts[incoming.RemoteEndPoint] = channelId;
|
_distributionActions[incoming.RemoteEndPoint] = _distribution.GetDistributionAction(channelId);
|
||||||
_ = UntrustedSend(_hub.GetInteraction(channelId), incoming.RemoteEndPoint);
|
_ = UntrustedSend(_hub.GetInteraction(channelId), incoming.RemoteEndPoint);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
channelId = _reservedPorts[incoming.RemoteEndPoint];
|
_distributionActions[incoming.RemoteEndPoint].Invoke(parsed);
|
||||||
var interaction = _hub.GetInteraction(channelId);
|
|
||||||
await interaction.ReceiveChanel.Writer.WriteAsync(parsed, token);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ namespace mROA.Implementation.Frontend
|
|||||||
var initMessage = new NetworkMessage
|
var initMessage = new NetworkMessage
|
||||||
{
|
{
|
||||||
MessageType = EMessageType.UntrustedConnect, Id = RequestId.Generate(),
|
MessageType = EMessageType.UntrustedConnect, Id = RequestId.Generate(),
|
||||||
Data = BitConverter.GetBytes(_channelInteractionModule.ConnectionId)
|
Data = BitConverter.GetBytes(-_channelInteractionModule.ConnectionId)
|
||||||
};
|
};
|
||||||
|
|
||||||
var initParsed = _serializationToolkit.Serialize(initMessage, _context);
|
var initParsed = _serializationToolkit.Serialize(initMessage, _context);
|
||||||
|
|||||||
Reference in New Issue
Block a user