Skip to content

Commit

Permalink
fix broken UDP plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
compujuckel committed Dec 20, 2023
1 parent 2b74e96 commit 45b3433
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
7 changes: 1 addition & 6 deletions AssettoServer/Network/Udp/ACUdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Log.Information("Starting UDP server on port {Port}", _port);

// https://stackoverflow.com/questions/5199026/c-sharp-async-udp-listener-socketexception
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_socket.IOControl(-1744830452 /* SIO_UDP_CONNRESET */, new byte[] { 0, 0, 0, 0 }, null);
}

_socket.DisableUdpIcmpExceptions();
_socket.ReceiveTimeout = 1000;
_socket.Bind(new IPEndPoint(IPAddress.Any, _port));
await Task.Factory.StartNew(() => ReceiveLoop(stoppingToken), TaskCreationOptions.LongRunning);
Expand Down
14 changes: 9 additions & 5 deletions AssettoServer/Network/Udp/UdpPluginServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using AssettoServer.Shared.Network.Packets.Shared;
using AssettoServer.Shared.Network.Packets.UdpPlugin;
using AssettoServer.Shared.Services;
using AssettoServer.Utils;
using Microsoft.Extensions.Hosting;
using Serilog;
using ClientEvent = AssettoServer.Shared.Network.Packets.UdpPlugin.ClientEvent;
Expand Down Expand Up @@ -63,23 +64,25 @@ public UdpPluginServer(
{
throw new ConfigurationException("UDP_PLUGIN_ADDRESS is invalid, needs to be in format 0.0.0.0:10000");
}
var ip = addressSplit[0];
var ip = IPAddress.Parse(addressSplit[0]);

if (_configuration.Server.UdpPluginLocalPort == outPort)
{
throw new ConfigurationException("UDP_PLUGIN_ADDRESS port needs to be different from UDP_PLUGIN_LOCAL_PORT");
}

_outAddress = new IPEndPoint(IPAddress.Parse(ip), outPort).Serialize();
_inAddress = new IPEndPoint(IPAddress.Any, _configuration.Server.UdpPluginLocalPort).Serialize();
_outAddress = new IPEndPoint(ip, outPort).Serialize();
_inAddress = new IPEndPoint(ip, _configuration.Server.UdpPluginLocalPort).Serialize();

_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}

protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
Log.Information("Starting UDP plugin server on port {Port}", _configuration.Server.UdpPluginLocalPort);


_socket.DisableUdpIcmpExceptions();
_socket.ReceiveTimeout = 1000;
_socket.Bind(new IPEndPoint(IPAddress.Any, _configuration.Server.UdpPluginLocalPort));

Task realtimeTask = Task.Run(async () =>
Expand Down Expand Up @@ -140,7 +143,7 @@ private void ReceiveLoop(CancellationToken stoppingToken)
try
{
var bytesRead = _socket.ReceiveFrom(buffer, SocketFlags.None, address);
if (address.Equals(_inAddress))
if (address.IpEquals(_inAddress))
{
OnReceived(buffer, bytesRead);
}
Expand All @@ -149,6 +152,7 @@ private void ReceiveLoop(CancellationToken stoppingToken)
Log.Information("Ignoring UDP Plugin packet from address {Address}", address);
}
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut) { }
catch (Exception ex)
{
Log.Error(ex, "Error in UDP plugin receive loop");
Expand Down
9 changes: 8 additions & 1 deletion AssettoServer/Utils/SocketAddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;

namespace AssettoServer.Utils;

Expand All @@ -10,4 +11,10 @@ public static SocketAddress Clone(this SocketAddress address)
address.Buffer.CopyTo(clone.Buffer);
return clone;
}

public static bool IpEquals(this SocketAddress address, SocketAddress other)
{
// This works for IPv4 only. First two bytes = port, next 4 bytes = address
return address.Buffer.Span[2..6].SequenceEqual(other.Buffer.Span[2..6]);
}
}
16 changes: 16 additions & 0 deletions AssettoServer/Utils/SocketExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace AssettoServer.Utils;

public static class SocketExtensions
{
public static void DisableUdpIcmpExceptions(this Socket socket)
{
// https://stackoverflow.com/questions/5199026/c-sharp-async-udp-listener-socketexception
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
socket.IOControl(-1744830452 /* SIO_UDP_CONNRESET */, new byte[] { 0, 0, 0, 0 }, null);
}
}
}

0 comments on commit 45b3433

Please sign in to comment.