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 NOSERVICE
#define NOIME #define NOIME
#include <windows.h> #include <windows.h>
#include <assert.h>
int GetProcessId() int GetProcessId()
{ {
@ -75,22 +76,44 @@ bool BaseConnection::Write(const void* data, size_t length)
return true; return true;
} }
auto self = reinterpret_cast<BaseConnectionWin*>(this); auto self = reinterpret_cast<BaseConnectionWin*>(this);
assert(self);
if (!self) {
return false;
}
if (self->pipe == INVALID_HANDLE_VALUE) { if (self->pipe == INVALID_HANDLE_VALUE) {
return false; 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) bool BaseConnection::Read(void* data, size_t length)
{ {
assert(data);
if (!data) {
return false;
}
auto self = reinterpret_cast<BaseConnectionWin*>(this); auto self = reinterpret_cast<BaseConnectionWin*>(this);
assert(self);
if (!self) {
return false;
}
if (self->pipe == INVALID_HANDLE_VALUE) { if (self->pipe == INVALID_HANDLE_VALUE) {
return false; return false;
} }
DWORD bytesAvailable = 0; DWORD bytesAvailable = 0;
if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) { if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
if (bytesAvailable >= length) { 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; return true;
} }
else { else {