Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/Baballonia/Services/OscSendService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,32 @@ public abstract class OscSendService(
public event Action<int> OnMessagesDispatched = _ => { };
protected readonly IOscTarget OscTarget = oscTarget;
private Socket _sendSocket;
private bool _connected;
private IPEndPoint? _sendEndpoint;

protected void UpdateTarget(IPEndPoint endpoint)
{
_sendSocket?.Close();
OscTarget.IsConnected = false;
_connected = false;
_sendEndpoint = null;

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

try
{
_sendSocket.Connect(endpoint);
OscTarget.IsConnected = true;
}
catch (SocketException ex)
{
logger.LogWarning("Failed to bind to sender endpoint: {IpEndPoint}. {ExMessage}", endpoint, ex.Message);
}
catch (Exception ex)
{
logger.LogError("Unexpected Exception while binding to sender endpoint: {IpEndPoint}. {ExMessage}", endpoint, ex.Message);
}
_sendEndpoint = endpoint;
_connected = true;
OscTarget.IsConnected = true;
}

public async Task Send(OscMessage message, CancellationToken ct)
{
if (_sendSocket is not { Connected: true })
if (!_connected || _sendEndpoint is null)
{
return;
}

try
{
var ip = IPEndPoint.Parse(OscTarget.DestinationAddress);
await _sendSocket.SendToAsync(message.ToByteArray(), SocketFlags.None, ip, ct);
await _sendSocket.SendToAsync(message.ToByteArray(), SocketFlags.None, _sendEndpoint, ct);
OnMessagesDispatched(1);
}
catch (Exception ex)
Expand All @@ -63,7 +55,7 @@ public async Task Send(OscMessage message, CancellationToken ct)

public async Task Send(OscMessage[] messages, CancellationToken ct)
{
if (_sendSocket is not { Connected: true })
if (!_connected || _sendEndpoint is null)
{
return;
}
Expand All @@ -72,7 +64,7 @@ public async Task Send(OscMessage[] messages, CancellationToken ct)
{
foreach (var message in messages)
{
await _sendSocket.SendAsync(message.ToByteArray(), SocketFlags.None, ct);
await _sendSocket.SendToAsync(message.ToByteArray(), SocketFlags.None, _sendEndpoint, ct);
}

OnMessagesDispatched(messages.Length);
Expand Down
Loading