Skip to content
Merged
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
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");

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