Getting better
This commit is contained in:
parent
96c694f78d
commit
a781ef1453
1 changed files with 21 additions and 27 deletions
48
terminkey.h
48
terminkey.h
|
|
@ -86,12 +86,10 @@ int termwidth() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int ipc_init() {
|
int ipc_init() {
|
||||||
// Remove PIPE_NOWAIT so the pipe handles state transmissions reliably.
|
|
||||||
// We will handle non-blocking checks manually using PeekNamedPipe.
|
|
||||||
hServerPipe = CreateNamedPipe(
|
hServerPipe = CreateNamedPipe(
|
||||||
"\\\\.\\pipe\\cimp_ipc",
|
"\\\\.\\pipe\\cimp_ipc",
|
||||||
PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
|
PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
|
||||||
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
|
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, // Enable non-blocking wait mode
|
||||||
1,
|
1,
|
||||||
4096,
|
4096,
|
||||||
4096,
|
4096,
|
||||||
|
|
@ -113,34 +111,30 @@ 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;
|
||||||
DWORD bytesAvail = 0;
|
|
||||||
|
// In non-blocking mode, ConnectNamedPipe returns immediately.
|
||||||
// Use PeekNamedPipe to see if a client has connected and written data
|
// If a client is connected, it returns non-zero, or zero with ERROR_PIPE_CONNECTED.
|
||||||
// without blocking the main QB64 execution loop.
|
BOOL connected = ConnectNamedPipe(hServerPipe, NULL) ?
|
||||||
if (PeekNamedPipe(hServerPipe, NULL, 0, NULL, &bytesAvail, NULL)) {
|
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
|
||||||
if (bytesAvail > 0) {
|
|
||||||
// Data is waiting! Safely read it.
|
if (connected) {
|
||||||
BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL);
|
DWORD bytesAvail = 0;
|
||||||
if (success && bytesRead > 0) {
|
// Verify that data has safely populated the buffer before reading
|
||||||
buffer[bytesRead] = '\0';
|
if (PeekNamedPipe(hServerPipe, NULL, 0, NULL, &bytesAvail, NULL)) {
|
||||||
|
if (bytesAvail > 0) {
|
||||||
// Disconnect and immediately reconnect to reset the pipe state for the next client
|
BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL);
|
||||||
|
if (success && bytesRead > 0) {
|
||||||
|
buffer[bytesRead] = '\0';
|
||||||
|
DisconnectNamedPipe(hServerPipe); // Reset pipe state for the next command
|
||||||
|
return (int)bytesRead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (GetLastError() == ERROR_BROKEN_PIPE) {
|
||||||
DisconnectNamedPipe(hServerPipe);
|
DisconnectNamedPipe(hServerPipe);
|
||||||
return (int)bytesRead;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// If Peek fails because a client connected but broke the pipe/disconnected
|
|
||||||
DWORD err = GetLastError();
|
|
||||||
if (err == ERROR_BROKEN_PIPE) {
|
|
||||||
DisconnectNamedPipe(hServerPipe);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-blocking fallback: try to listen for the next incoming connection choice
|
|
||||||
// strictly if the pipe is currently completely idle.
|
|
||||||
ConnectNamedPipe(hServerPipe, NULL);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue