Skip to content

Commit 047a43d

Browse files
authored
Refresh UDP clients on init
Safeguard against reusing disposed clients across reloads.
1 parent 1e16ce1 commit 047a43d

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

VRCFTPicoModule/VRCFTPicoModule.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace VRCFTPicoModule;
1111
public class VRCFTPicoModule : ExtTrackingModule
1212
{
1313
private static readonly int[] Ports = [29765, 29763];
14-
private static readonly UdpClient[] Clients = Ports.Select(port => new UdpClient(port) { Client = { ReceiveTimeout = 100 } }).ToArray();
14+
private UdpClient[] _clients = [];
1515
private static UdpClient _udpClient = new();
1616
private static int _port;
1717
private Updater? _updater;
@@ -117,7 +117,12 @@ private async Task<int> ListenOnPorts()
117117
{
118118
try
119119
{
120-
var tasks = Clients.Select(client => client.ReceiveAsync()).ToArray();
120+
_clients = Ports.Select(port => new UdpClient(port)
121+
{
122+
Client = { ReceiveTimeout = 100 }
123+
}).ToArray();
124+
125+
var tasks = _clients.Select(client => client.ReceiveAsync()).ToArray();
121126

122127
if (tasks.Length == 0)
123128
{
@@ -126,7 +131,7 @@ private async Task<int> ListenOnPorts()
126131

127132
var completedTask = await Task.WhenAny(tasks);
128133

129-
foreach (var client in Clients) client.Dispose();
134+
foreach (var client in _clients) client.Dispose();
130135

131136
return Array.IndexOf(tasks, completedTask);
132137
}
@@ -143,7 +148,7 @@ public override void Update()
143148
if (_shuttingDown) return;
144149

145150
try
146-
{
151+
{
147152
_updater?.Update(Status);
148153
}
149154
catch (AggregateException ex) when (ex.InnerException is ObjectDisposedException) { }
@@ -154,7 +159,7 @@ public override void Teardown()
154159
{
155160
_shuttingDown = true;
156161

157-
foreach (var client in Clients)
162+
foreach (var client in _clients)
158163
client.Dispose();
159164
_udpClient.Dispose();
160165
}

0 commit comments

Comments
 (0)