mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Improved packet handling to deal better with empty packets and errors.
This commit is contained in:
@@ -314,59 +314,86 @@ namespace GHelper.Peripherals.Mouse
|
|||||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||||
protected virtual byte[]? WriteForResponse(byte[] packet)
|
protected virtual byte[]? WriteForResponse(byte[] packet)
|
||||||
{
|
{
|
||||||
|
Array.Resize(ref packet, ASUS_MOUSE_PACKET_SIZE);
|
||||||
|
|
||||||
|
|
||||||
byte[] response = new byte[ASUS_MOUSE_PACKET_SIZE];
|
byte[] response = new byte[ASUS_MOUSE_PACKET_SIZE];
|
||||||
|
|
||||||
try
|
int retries = 3;
|
||||||
|
|
||||||
|
while (retries > 0)
|
||||||
{
|
{
|
||||||
if (IsPacketLoggerEnabled())
|
response = new byte[ASUS_MOUSE_PACKET_SIZE];
|
||||||
Logger.WriteLine(GetDisplayName() + ": Sending packet: " + ByteArrayToString(packet));
|
|
||||||
|
|
||||||
long time = MeasuredIO(Write, packet);
|
try
|
||||||
Logger.WriteLine(GetDisplayName() + ": Write took " + time + "ms");
|
|
||||||
|
|
||||||
time = MeasuredIO(Read, response);
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Read took " + time + "ms");
|
|
||||||
|
|
||||||
if (IsPacketLoggerEnabled())
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Read packet: " + ByteArrayToString(response));
|
|
||||||
|
|
||||||
if (IsMouseError(response))
|
|
||||||
{
|
{
|
||||||
Logger.WriteLine(GetDisplayName() + ": Mouse returned error (FF AA). Packet probably not supported by mouse firmware.");
|
if (IsPacketLoggerEnabled())
|
||||||
//Error. Mouse could not understand or process the sent packet
|
Logger.WriteLine(GetDisplayName() + ": Sending packet: " + ByteArrayToString(packet)
|
||||||
return response;
|
+ " Try " + (retries - 2) + " of 3");
|
||||||
}
|
|
||||||
|
long time = MeasuredIO(Write, packet);
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Write took " + time + "ms");
|
||||||
|
|
||||||
//Not the response we were looking for, continue reading
|
|
||||||
while (response[0] != packet[0] || response[1] != packet[1] || response[2] != packet[2])
|
|
||||||
{
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Read wrong packet left in buffer: " + ByteArrayToString(response) + ". Retrying...");
|
|
||||||
//Read again
|
|
||||||
time = MeasuredIO(Read, response);
|
time = MeasuredIO(Read, response);
|
||||||
Logger.WriteLine(GetDisplayName() + ": Read took " + time + "ms");
|
Logger.WriteLine(GetDisplayName() + ": Read took " + time + "ms");
|
||||||
|
|
||||||
|
|
||||||
|
if (IsMouseError(response))
|
||||||
|
{
|
||||||
|
if (IsPacketLoggerEnabled())
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Read packet: " + ByteArrayToString(response));
|
||||||
|
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Mouse returned error (FF AA). Packet probably not supported by mouse firmware.");
|
||||||
|
//Error. Mouse could not understand or process the sent packet
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response[1] == 0 && response[2] == 0 && response[3] == 0)
|
||||||
|
{
|
||||||
|
if (IsPacketLoggerEnabled())
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Read packet: " + ByteArrayToString(response));
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Received empty packet. Stopping here.");
|
||||||
|
//Empty packet
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Not the response we were looking for, continue reading
|
||||||
|
while (response[0] != packet[0] || response[1] != packet[1] || response[2] != packet[2])
|
||||||
|
{
|
||||||
|
if (IsPacketLoggerEnabled())
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Read wrong packet left in buffer: " + ByteArrayToString(response) + ". Retrying...");
|
||||||
|
//Read again
|
||||||
|
time = MeasuredIO(Read, response);
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Read took " + time + "ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsPacketLoggerEnabled())
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Read packet: " + ByteArrayToString(response));
|
||||||
|
|
||||||
|
|
||||||
|
return response;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Failed to read packet " + e.Message);
|
||||||
|
OnDisconnect();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (TimeoutException e)
|
||||||
|
{
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Timeout reading packet " + e.Message + " Trying again.");
|
||||||
|
retries--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
Logger.WriteLine(GetDisplayName() + ": Channel closed ");
|
||||||
|
OnDisconnect();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
return null;
|
||||||
{
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Failed to read packet " + e.Message);
|
|
||||||
OnDisconnect();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch (TimeoutException e)
|
|
||||||
{
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Timeout reading packet " + e.Message);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
Logger.WriteLine(GetDisplayName() + ": Channel closed ");
|
|
||||||
OnDisconnect();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
public abstract string GetDisplayName();
|
public abstract string GetDisplayName();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user