Windows IPC fix, maybe?
This commit is contained in:
parent
301a6423db
commit
c2d86b6f46
1 changed files with 21 additions and 8 deletions
15
terminkey.h
15
terminkey.h
|
|
@ -112,20 +112,33 @@ int ipc_check_message(uintptr_t buffer_ptr, int max_len) {
|
||||||
|
|
||||||
char* buffer = (char*)buffer_ptr;
|
char* buffer = (char*)buffer_ptr;
|
||||||
DWORD bytesRead = 0;
|
DWORD bytesRead = 0;
|
||||||
|
|
||||||
|
// Check if a client is connected (or already connected)
|
||||||
BOOL connected = ConnectNamedPipe(hServerPipe, NULL) ?
|
BOOL connected = ConnectNamedPipe(hServerPipe, NULL) ?
|
||||||
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
|
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
|
||||||
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
|
DWORD bytesAvail = 0;
|
||||||
|
// Sneak peek to see if data has safely arrived in the buffer yet
|
||||||
|
if (PeekNamedPipe(hServerPipe, NULL, 0, NULL, &bytesAvail, NULL)) {
|
||||||
|
if (bytesAvail > 0) {
|
||||||
|
// Data is ready, perform the read safely
|
||||||
BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL);
|
BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL);
|
||||||
if (success && bytesRead > 0) {
|
if (success && bytesRead > 0) {
|
||||||
buffer[bytesRead] = '\0';
|
buffer[bytesRead] = '\0';
|
||||||
DisconnectNamedPipe(hServerPipe);
|
DisconnectNamedPipe(hServerPipe);
|
||||||
ConnectNamedPipe(hServerPipe, NULL);
|
ConnectNamedPipe(hServerPipe, NULL); // Re-arm for next connection
|
||||||
return (int)bytesRead;
|
return (int)bytesRead;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If Peek fails because the client dropped out without sending anything
|
||||||
|
if (GetLastError() == ERROR_BROKEN_PIPE) {
|
||||||
DisconnectNamedPipe(hServerPipe);
|
DisconnectNamedPipe(hServerPipe);
|
||||||
ConnectNamedPipe(hServerPipe, NULL);
|
ConnectNamedPipe(hServerPipe, NULL);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue