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