ReadFile and WriteFile really want to report how many bytes were read/written.

This commit is contained in:
Chris Marsh 2017-09-07 16:16:34 -07:00
parent 19abe80449
commit fb87e7c193

View File

@ -5,6 +5,7 @@
#define NOSERVICE
#define NOIME
#include <windows.h>
#include <assert.h>
int GetProcessId()
{
@ -75,22 +76,44 @@ bool BaseConnection::Write(const void* data, size_t length)
return true;
}
auto self = reinterpret_cast<BaseConnectionWin*>(this);
assert(self);
if (!self) {
return false;
}
if (self->pipe == INVALID_HANDLE_VALUE) {
return false;
}
return ::WriteFile(self->pipe, data, (DWORD)length, nullptr, nullptr) == TRUE;
assert(data);
if (!data) {
return false;
}
const DWORD bytesLength = (DWORD)length;
DWORD bytesWritten = 0;
return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE &&
bytesWritten == bytesLength;
}
bool BaseConnection::Read(void* data, size_t length)
{
assert(data);
if (!data) {
return false;
}
auto self = reinterpret_cast<BaseConnectionWin*>(this);
assert(self);
if (!self) {
return false;
}
if (self->pipe == INVALID_HANDLE_VALUE) {
return false;
}
DWORD bytesAvailable = 0;
if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
if (bytesAvailable >= length) {
if (::ReadFile(self->pipe, data, (DWORD)length, nullptr, nullptr) == TRUE) {
DWORD bytesToRead = (DWORD)length;
DWORD bytesRead = 0;
if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) {
assert(bytesToRead == bytesRead);
return true;
}
else {
@ -102,4 +125,4 @@ bool BaseConnection::Read(void* data, size_t length)
Close();
}
return false;
}
}