Skip to content

Commit

Permalink
Fixed a bug where VRCFT would crash on close, freezing the app
Browse files Browse the repository at this point in the history
  • Loading branch information
dfgHiatus committed May 30, 2023
1 parent a997124 commit 7ee2d29
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions BabbleOSC.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net;
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using VRCFaceTracking.Core.OSC;
Expand All @@ -7,6 +7,7 @@ namespace VRCFaceTracking.Babble;
public partial class BabbleOSC
{
private Socket _receiver;
public bool _loop = true;
private readonly Thread _thread;
private readonly ILogger _logger;
private readonly int _resolvedPort;
Expand All @@ -25,9 +26,10 @@ public BabbleOSC(ILogger iLogger, int? port = null)

_receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_resolvedPort = port ?? DEFAULT_PORT;
_receiver.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.ReceiveTimeout = TIMEOUT_MS;

_loop = true;
_thread = new Thread(new ThreadStart(ListenLoop));
_thread.Start();
}
Expand All @@ -37,11 +39,11 @@ private void ListenLoop()
OscMessageMeta oscMeta = new OscMessageMeta();
var buffer = new byte[4096];

while (!MainStandalone.MasterCancellationTokenSource.IsCancellationRequested)
while (_loop)
{
try
{
if (_receiver.Connected)
if (_receiver.IsBound)
{
var length = _receiver.Receive(buffer);
if (SROSCLib.parse_osc(buffer, length, ref oscMeta))
Expand All @@ -60,20 +62,17 @@ private void ListenLoop()
_receiver.Close();
_receiver.Dispose();
_receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_receiver.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _resolvedPort));
_receiver.ReceiveTimeout = TIMEOUT_MS;
}
}
catch (Exception e)
{
if (_receiver.Connected)
_logger.LogError(e.Message);
}
catch (Exception) { }
}
}

public void Teardown()
{
_loop = false;
_receiver.Close();
_receiver.Dispose();
_thread.Join();
Expand Down

0 comments on commit 7ee2d29

Please sign in to comment.