diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 4a614c3..1a32a1f 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -51,12 +51,8 @@ class Program builder.Services.Configure(o => o.DistributionType = EDistributionType.ExtractorFirst); var host = builder.Build(); -// new RemoteTypeBinder(); -// - // var contextualSerializationToolKit = host.Services.GetService(); - // contextualSerializationToolKit.Deserialize(contextualSerializationToolKit.Serialize(new NetworkMessage(), null), null); _ = host.Services.GetService()!.Start(); var gateway = host.Services.GetService(); gateway.Run(); diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 5b5f26a..406dec2 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -39,7 +39,7 @@ class Program builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 9000); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 4567); builder.Services.AddSingleton(); builder.Services.AddOptions(); diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index 571bbef..317d654 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -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,13 +15,14 @@ namespace mROA.Cbor { public class CborSerializationToolkit : IContextualSerializationToolKit { - private readonly CborWriter _writer = new(initialCapacity: 2048); + private readonly ThreadLocal _writer = new(() => new CborWriter(initialCapacity: 2048)); private readonly int _offset; - public CborSerializationToolkit(IOptions offsetOptions) : this(offsetOptions.Value.Offset) + public CborSerializationToolkit(IOptions offsetOptions) : this(offsetOptions.Value + .Offset) { } - + private readonly IOrdinaryStructureParser[] _parsers = { new CallRequestParser(), new FinalCommandExecutionParser(), @@ -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 span = result.AsSpan(); - _writer.Encode(span[_offset..]); - } + 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..]); + return result; } - + public int Serialize(object objectToSerialize, Span 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(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) diff --git a/mROA.Codegen/RemoteTypeBinder.cstmpl b/mROA.Codegen/RemoteTypeBinder.cstmpl index adb82c9..e206a10 100644 --- a/mROA.Codegen/RemoteTypeBinder.cstmpl +++ b/mROA.Codegen/RemoteTypeBinder.cstmpl @@ -26,7 +26,6 @@ namespace mROA.Codegen (instance as ). += () => { - Console.WriteLine($"Try to send to {ownerId} with hash code {context.GetHashCode()}"); Console.WriteLine("Sending event..."); var request = new CallRequest diff --git a/mROA/Implementation/Backend/UdpGateway.cs b/mROA/Implementation/Backend/UdpGateway.cs index 9c775d5..66b625d 100644 --- a/mROA/Implementation/Backend/UdpGateway.cs +++ b/mROA/Implementation/Backend/UdpGateway.cs @@ -14,15 +14,17 @@ namespace mROA.Implementation.Backend { private readonly IConnectionHub _hub; private readonly UdpClient _client; - private readonly Dictionary _reservedPorts = new(); + private readonly Dictionary> _distributionActions = new(); private readonly CancellationTokenSource _tokenSource = new(); private readonly IContextualSerializationToolKit _serializationToolkit; - + private readonly IDistributionModule _distribution; + public UdpGateway(IOptions 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; } } diff --git a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs index c3a63ff..07c88c1 100644 --- a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs +++ b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs @@ -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);