Windows IPC fix, maybe?
This commit is contained in:
parent
301a6423db
commit
c2d86b6f46
1 changed files with 21 additions and 8 deletions
29
terminkey.h
29
terminkey.h
|
|
@ -112,19 +112,32 @@ int ipc_check_message(uintptr_t buffer_ptr, int max_len) {
|
|||
|
||||
char* buffer = (char*)buffer_ptr;
|
||||
DWORD bytesRead = 0;
|
||||
|
||||
// Check if a client is connected (or already connected)
|
||||
BOOL connected = ConnectNamedPipe(hServerPipe, NULL) ?
|
||||
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
|
||||
|
||||
if (connected) {
|
||||
BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL);
|
||||
if (success && bytesRead > 0) {
|
||||
buffer[bytesRead] = '\0';
|
||||
DisconnectNamedPipe(hServerPipe);
|
||||
ConnectNamedPipe(hServerPipe, NULL);
|
||||
return (int)bytesRead;
|
||||
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);
|
||||
if (success && bytesRead > 0) {
|
||||
buffer[bytesRead] = '\0';
|
||||
DisconnectNamedPipe(hServerPipe);
|
||||
ConnectNamedPipe(hServerPipe, NULL); // Re-arm for next connection
|
||||
return (int)bytesRead;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If Peek fails because the client dropped out without sending anything
|
||||
if (GetLastError() == ERROR_BROKEN_PIPE) {
|
||||
DisconnectNamedPipe(hServerPipe);
|
||||
ConnectNamedPipe(hServerPipe, NULL);
|
||||
}
|
||||
}
|
||||
DisconnectNamedPipe(hServerPipe);
|
||||
ConnectNamedPipe(hServerPipe, NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue