Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust stream connection thread #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
99 changes: 62 additions & 37 deletions MFIGamepadShared/Gamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Gamepad: IDisposable
private readonly HidDeviceLoader _hidDeviceLoader;
private readonly VGenWrapper _vGenWrapper;
private Thread _gamepadUpdateThread;
private Thread _gamepadAliveThread;
private IDictionary<XInputGamepadButtons, XInputGamepadButtons> _virtualMappings;

public Gamepad(GamepadConfiguration config, VGenWrapper vGenWrapper, HidDeviceLoader hidDeviceLoader)
Expand Down Expand Up @@ -107,6 +108,49 @@ private void Log(string message)
ErrorOccuredEvent?.Invoke(this, message);
}

private void _deviceStream(HidDevice device, HidStream stream)
{
Thread.CurrentThread.IsBackground = true;

try
{
using (stream)
{
while (true)
{
if (!Thread.CurrentThread.IsAlive)
{
break;
}

var bytes = new byte[device.MaxInputReportLength];
int count;
try
{
count = stream.Read(bytes, 0, bytes.Length);
}
catch (TimeoutException)
{
continue;
}
catch (Exception ex)
{
Log(ex.Message);
break;
}

if (count > 0)
{
UpdateState(bytes);
}
}
}
}
catch (Exception ex)
{
Log(ex.Message);
}
}
private bool PlugInToHidDeviceAndStartLoop()
{
var device =
Expand Down Expand Up @@ -135,50 +179,31 @@ private bool PlugInToHidDeviceAndStartLoop()

_gamepadUpdateThread?.Abort();
_gamepadUpdateThread = new Thread(() =>
{
_deviceStream(device, stream);
});
_gamepadUpdateThread.Start();
_gamepadAliveThread?.Abort();
_gamepadAliveThread = new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;

try
while (_gamepadUpdateThread.IsAlive)
{
using (stream)
_gamepadUpdateThread.Join();
Log($"Lost connection. Waiting for the device...");
while (!device.TryOpen(out stream))
{
while (true)
{
if (!Thread.CurrentThread.IsAlive)
{
break;
}

var bytes = new byte[device.MaxInputReportLength];
int count;
try
{
count = stream.Read(bytes, 0, bytes.Length);
}
catch (TimeoutException)
{
continue;
}
catch (Exception ex)
{
Log(ex.Message);
break;
}

if (count > 0)
{
UpdateState(bytes);
}
}
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Log(ex.Message);
_gamepadUpdateThread = new Thread(() =>
{
Log($"Stream was resumed.");
_deviceStream(device, stream);
});
_gamepadUpdateThread.Start();
}
});
_gamepadUpdateThread.Start();

_gamepadAliveThread.Start();
return true;
}

Expand Down