Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions Drone/Interop/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,19 @@ public static bool CreatePipe(out IntPtr hReadPipe, out IntPtr hWritePipe, ref S
return result;
}

public static bool PeekNamedPipe(IntPtr hPipe)
public static bool PeekNamedPipe(IntPtr hPipe, ref uint nbBytesAvailable)
{
object[] parameters = { hPipe, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)0, IntPtr.Zero };
return (bool)Generic.DynamicApiInvoke(
object[] parameters = { hPipe, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, nbBytesAvailable, IntPtr.Zero };

var result = (bool)Generic.DynamicApiInvoke(
"kernel32.dll",
"PeekNamedPipe",
typeof(PeekNamedPipe),
ref parameters);

nbBytesAvailable = (uint)parameters[4];

return result;
}

public static bool FreeLibrary(IntPtr hModule)
Expand Down
7 changes: 6 additions & 1 deletion Drone/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public static bool DataAvailable(this TcpClient client)
public static bool DataAvailable(this PipeStream pipe)
{
var hPipe = pipe.SafePipeHandle.DangerousGetHandle();
return Interop.Methods.PeekNamedPipe(hPipe);
uint nbBytesAvailable = 0;
bool result = Interop.Methods.PeekNamedPipe(hPipe, ref nbBytesAvailable);
if (result == false)
throw new System.ComponentModel.Win32Exception("Named Pipe is not available.");

return nbBytesAvailable > 0;
}

public static async Task<byte[]> ReadStream(this Stream stream)
Expand Down