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

Fix usbmux to return any received data. #3134

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Changes from 1 commit
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
28 changes: 14 additions & 14 deletions src/Tools/dotnet-dsrouter/USBMuxTcpClientRouterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ public override Task FlushAsync(CancellationToken cancellationToken)

public override int Read(byte[] buffer, int offset, int count)
{
bool continueRead = true;
int bytesToRead = count;
int totalBytesRead = 0;
int currentBytesRead = 0;
int bytesRead = 0;

while (continueRead && bytesToRead - totalBytesRead > 0)
if (offset + count > buffer.Length)
throw new InvalidOperationException ($"Potential write beyond end of buffer");
hoyosjs marked this conversation as resolved.
Show resolved Hide resolved

if (offset < 0)
throw new InvalidOperationException ($"Write before beginning of buffer");

if (count < 0)
throw new InvalidOperationException ($"Negative read count");

while (true)
{
if (!IsOpen)
throw new EndOfStreamException();
Expand All @@ -132,21 +138,15 @@ public override int Read(byte[] buffer, int offset, int count)
{
fixed (byte* fixedBuffer = buffer)
{
currentBytesRead = USBMuxInterop.recv(_handle, fixedBuffer + totalBytesRead, new IntPtr(bytesToRead - totalBytesRead), 0);
bytesRead = USBMuxInterop.recv(_handle, fixedBuffer + offset, new IntPtr (count), 0);
}
}

if (currentBytesRead == -1 && Marshal.GetLastWin32Error() == USBMuxInterop.EINTR)
if (bytesRead == -1 && Marshal.GetLastWin32Error() == USBMuxInterop.EINTR)
continue;

continueRead = currentBytesRead > 0;
if (!continueRead)
break;

totalBytesRead += currentBytesRead;
return bytesRead;
}

return totalBytesRead;
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Expand Down