Skip to content

Commit

Permalink
Split into two sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
HakanL committed Dec 2, 2024
1 parent db3fb72 commit 04fac9b
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/Haukcode.sACN/SACNClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Haukcode.HighPerfComm;

Check failure on line 13 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HighPerfComm' does not exist in the namespace 'Haukcode' (are you missing an assembly reference?)

Check failure on line 13 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HighPerfComm' does not exist in the namespace 'Haukcode' (are you missing an assembly reference?)
using Haukcode.sACN.Model;
using HdrHistogram;

namespace Haukcode.sACN
{
public class SACNClient : Client<SACNClient.SendData, SocketReceiveMessageFromResult>

Check failure on line 18 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Client<,>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 18 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Client<,>' could not be found (are you missing a using directive or an assembly reference?)
{
public class SendData : HighPerfComm.SendData

Check failure on line 20 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HighPerfComm' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Haukcode.sACN/SACNClient.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HighPerfComm' could not be found (are you missing a using directive or an assembly reference?)
{
public ushort UniverseId;
public ushort UniverseId { get; set; }

public IPEndPoint? Destination;
public IPEndPoint? Destination { get; set; }
}

private const int ReceiveBufferSize = 20480;
Expand All @@ -39,7 +37,6 @@ public class SendData : HighPerfComm.SendData
private readonly HashSet<ushort> dmxUniverses = [];
private readonly Dictionary<IPAddress, IPEndPoint> endPointCache = [];
private readonly IPEndPoint localEndPoint;
private readonly HashSet<(IPAddress? Destination, ushort UniverseId)> usedDestinations = [];
private readonly Dictionary<ushort, IPEndPoint> universeMulticastEndpoints = [];

public SACNClient(Guid senderId, string senderName, IPAddress localAddress, int port = 5568)
Expand All @@ -53,15 +50,12 @@ public SACNClient(Guid senderId, string senderName, IPAddress localAddress, int

if (port <= 0)
throw new ArgumentException("Invalid port", nameof(port));
Port = port;
this.localEndPoint = new IPEndPoint(localAddress, port);

this.packetSubject = new Subject<ReceiveDataPacket>();

this.listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

//this.sendSocket.SendBufferSize = 5 * 1024 * 1024;
//this.listenSocket.ReceiveBufferSize = 5 * 1024 * 1024;
this.listenSocket.ReceiveBufferSize = ReceiveBufferSize;

// Set the SIO_UDP_CONNRESET ioctl to true for this UDP socket. If this UDP socket
// ever sends a UDP packet to a remote destination that exists but there is
Expand Down Expand Up @@ -138,7 +132,7 @@ private void ConfigureSendSocket(Socket socket)
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 1);
}

public int Port { get; }
public IPEndPoint LocalEndPoint => this.localEndPoint;

public Guid SenderId { get; }

Expand Down Expand Up @@ -241,13 +235,12 @@ await base.QueuePacket(packet.Length, important, (newSendData, memory) =>
{
if (!this.endPointCache.TryGetValue(destination, out var ipEndPoint))
{
ipEndPoint = new IPEndPoint(destination, Port);
ipEndPoint = new IPEndPoint(destination, this.localEndPoint.Port);
this.endPointCache.Add(destination, ipEndPoint);
}

newSendData.Destination = ipEndPoint;
}
this.usedDestinations.Add((destination, universeId));

newSendData.UniverseId = universeId;

Expand Down Expand Up @@ -323,7 +316,7 @@ protected override ValueTask<int> SendPacketAsync(SendData sendData, ReadOnlyMem
{
if (!universeMulticastEndpoints.TryGetValue(sendData.UniverseId, out destination))
{
destination = new IPEndPoint(SACNCommon.GetMulticastAddress(sendData.UniverseId), Port);
destination = new IPEndPoint(SACNCommon.GetMulticastAddress(sendData.UniverseId), this.localEndPoint.Port);
universeMulticastEndpoints.Add(sendData.UniverseId, destination);
}
}
Expand Down Expand Up @@ -353,7 +346,7 @@ protected override void ParseReceiveData(ReadOnlyMemory<byte> memory, SocketRece

if (!this.endPointCache.TryGetValue(result.PacketInformation.Address, out var ipEndPoint))
{
ipEndPoint = new IPEndPoint(result.PacketInformation.Address, Port);
ipEndPoint = new IPEndPoint(result.PacketInformation.Address, this.localEndPoint.Port);
this.endPointCache.Add(result.PacketInformation.Address, ipEndPoint);
}

Expand Down

0 comments on commit 04fac9b

Please sign in to comment.