Skip to content

Commit dfbf596

Browse files
committed
🐛 Fix V1 server crash when toggling filter on and off repeatedly
1 parent f0c9328 commit dfbf596

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

OTDIPC/OTDIPC.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace OTDIPC
1313
{
1414
[PluginName("OpenKneeboard (OTD-IPC)")]
15-
public class OTDIPC : IPositionedPipelineElement<IDeviceReport>, IDriver
15+
public sealed class OTDIPC : IPositionedPipelineElement<IDeviceReport>, IDriver, IDisposable
1616
{
1717
State _state = new();
1818
private DeviceInfo? _deviceInfo;
@@ -46,6 +46,14 @@ public OTDIPC()
4646
}
4747
}
4848

49+
public void Dispose()
50+
{
51+
foreach (var server in _servers)
52+
{
53+
server.Dispose();
54+
}
55+
}
56+
4957
public void Consume(IDeviceReport deviceReport)
5058
{
5159
if (!_servers.Any(s => s.HaveClient))

OTDIPC/ServerBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace OTDIPC;
1313

14-
public abstract class ServerBase : IDisposable, IServer
14+
public abstract class ServerBase : IServer
1515
{
1616
protected readonly IDriver _driver;
1717
protected Timer? _timer;
@@ -31,6 +31,7 @@ protected ServerBase(IDriver driver)
3131

3232
public virtual void Dispose()
3333
{
34+
GC.SuppressFinalize(this);
3435
_timer?.Dispose();
3536
_driver.TabletChanged -= Driver_TabletChanged;
3637
_driver.StateChanged -= Driver_StateChanged;

OTDIPC/V1/Server.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ protected override async void RunServerAsync()
115115
_connection = pipe;
116116

117117
Log.Write("otd-ipc", "Waiting for connection");
118-
await pipe.WaitForConnectionAsync();
118+
try
119+
{
120+
await pipe.WaitForConnectionAsync();
121+
}
122+
catch (IOException e)
123+
{
124+
_waitingForConnection = false;
125+
Log.Write("otd-ipc", "Waiting for connection failed: " + e.Message);
126+
return;
127+
}
128+
119129
Log.Write("otd-ipc", "Client connected");
120130
_waitingForConnection = false;
121131
_connected = true;
@@ -129,6 +139,15 @@ void OnClientConnected()
129139
this.SendMessage(_deviceInfo);
130140
}
131141

142+
public override void Dispose()
143+
{
144+
var conn = Interlocked.Exchange(ref _connection, null);
145+
conn?.Dispose();
146+
147+
base.Dispose();
148+
Log.Write("otd-ipc", "V1 server disposed");
149+
}
150+
132151
protected override void Ping()
133152
{
134153
if (_waitingForConnection)

0 commit comments

Comments
 (0)