Stop using Async methods for FileStream instance. Reduce CPU usage. Initial thread action queue.

No longer uses an application set timeout but it looks like
a timeout will be handled by the system
This commit is contained in:
Travis Nickles 2017-05-13 15:01:43 -07:00
parent 7a1b382c5a
commit bca7850a36
3 changed files with 63 additions and 6 deletions

View File

@ -516,7 +516,20 @@ namespace DS4Windows
if (removingStatus)
{
CurrentState[ind].Battery = PreviousState[ind].Battery = 0; // Reset for the next connection's initial status change.
x360Bus.Unplug(ind);
if (!useDInputOnly[ind])
{
bool unplugResult = x360Bus.Unplug(ind);
int xinputIndex = x360Bus.FirstController + ind;
if (unplugResult)
{
LogDebug("X360 Controller # " + xinputIndex + " unplugged");
}
else
{
LogDebug("X360 Controller # " + xinputIndex + " failed to unplug");
}
}
string removed = Properties.Resources.ControllerWasRemoved.Replace("*Mac address*", (ind + 1).ToString());
if (device.getBattery() <= 20 &&
device.getConnectionType() == ConnectionType.BT && !device.isCharging())

View File

@ -352,6 +352,8 @@ namespace DS4Windows
}
private SynchronizationContext uiContext = null;
private Queue<Action> eventQueue = new Queue<Action>();
private object eventQueueLock = new object();
public DS4Device(HidDevice hidDevice)
{
@ -479,7 +481,8 @@ namespace DS4Windows
}
else
{
return hDevice.WriteAsyncOutputReportViaInterrupt(outputReport);
return hDevice.WriteOutputReportViaInterrupt(outputReport, READ_STREAM_TIMEOUT);
//return hDevice.WriteAsyncOutputReportViaInterrupt(outputReport);
}
}
@ -567,14 +570,15 @@ namespace DS4Windows
if (conType == ConnectionType.BT)
{
//HidDevice.ReadStatus res = hDevice.ReadFile(btInputReport);
HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(btInputReport, READ_STREAM_TIMEOUT);
//HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(btInputReport, READ_STREAM_TIMEOUT);
HidDevice.ReadStatus res = hDevice.ReadWithFileStream(btInputReport);
//HidDevice.ReadStatus res = hDevice.ReadFileOverlapped(btInputReport, READ_STREAM_TIMEOUT);
if (res == HidDevice.ReadStatus.Success)
{
Array.Copy(btInputReport, 2, inputReport, 0, inputReport.Length);
}
else
{
if (res == HidDevice.ReadStatus.WaitTimedOut)
{
Log.LogToGui(Mac.ToString() + " disconnected due to timeout", true);
@ -595,14 +599,15 @@ namespace DS4Windows
}), null);
return;
}
}
else
{
//HidDevice.ReadStatus res = hDevice.ReadFile(inputReport);
//Array.Clear(inputReport, 0, inputReport.Length);
HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(inputReport, READ_STREAM_TIMEOUT);
//HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(inputReport, READ_STREAM_TIMEOUT);
HidDevice.ReadStatus res = hDevice.ReadWithFileStream(inputReport);
//HidDevice.ReadStatus res = hDevice.ReadFileOverlapped(inputReport, READ_STREAM_TIMEOUT);
if (res != HidDevice.ReadStatus.Success)
{
if (res == HidDevice.ReadStatus.WaitTimedOut)
@ -797,6 +802,18 @@ namespace DS4Windows
error = currerror;
cState.CopyTo(pState);
/*lock (eventQueueLock)
{
Action tempAct = null;
//while (eventQueue.TryDequeue(out tempAct))
for (int actInd = 0, actLen = eventQueue.Count; actInd < actLen; actInd++)
{
tempAct = eventQueue.Dequeue();
tempAct.Invoke();
}
}
*/
}
}
@ -1117,5 +1134,13 @@ namespace DS4Windows
{
this.Report = null;
}
public void queueEvent(Action act)
{
lock (eventQueueLock)
{
eventQueue.Enqueue(act);
}
}
}
}

View File

@ -242,6 +242,25 @@ namespace DS4Windows
}
}
public ReadStatus ReadWithFileStream(byte[] inputBuffer)
{
try
{
if (fileStream.Read(inputBuffer, 0, inputBuffer.Length) > 0)
{
return ReadStatus.Success;
}
else
{
return ReadStatus.NoDataRead;
}
}
catch (Exception)
{
return ReadStatus.ReadError;
}
}
public ReadStatus ReadWithFileStream(byte[] inputBuffer, int timeout)
{
try