From 96c694f78ddb79ea0fab8e25e6ed34d478b4c450 Mon Sep 17 00:00:00 2001 From: visionmercer <62051836+visionmercer@users.noreply.github.com> Date: Tue, 23 Jun 2026 12:33:49 +0200 Subject: [PATCH] A better fix maybe? --- terminkey.h | 56 +++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/terminkey.h b/terminkey.h index 99945a2..d6b9d9a 100644 --- a/terminkey.h +++ b/terminkey.h @@ -86,10 +86,12 @@ int termwidth() { } int ipc_init() { + // Remove PIPE_NOWAIT so the pipe handles state transmissions reliably. + // We will handle non-blocking checks manually using PeekNamedPipe. hServerPipe = CreateNamedPipe( "\\\\.\\pipe\\cimp_ipc", PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 4096, 4096, @@ -99,12 +101,11 @@ int ipc_init() { if (hServerPipe == INVALID_HANDLE_VALUE) { DWORD err = GetLastError(); if (err == ERROR_ACCESS_DENIED || err == ERROR_PIPE_BUSY) { - return 0; // Already running + return 0; // Already running (Client Mode) } return -1; // Other error } - ConnectNamedPipe(hServerPipe, NULL); - return 1; + return 1; // Server mode successfully started } int ipc_check_message(uintptr_t buffer_ptr, int max_len) { @@ -112,33 +113,34 @@ 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) { - 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) { + DWORD bytesAvail = 0; + + // Use PeekNamedPipe to see if a client has connected and written data + // without blocking the main QB64 execution loop. + if (PeekNamedPipe(hServerPipe, NULL, 0, NULL, &bytesAvail, NULL)) { + if (bytesAvail > 0) { + // Data is waiting! Safely read it. + BOOL success = ReadFile(hServerPipe, buffer, max_len - 1, &bytesRead, NULL); + if (success && bytesRead > 0) { + buffer[bytesRead] = '\0'; + + // Disconnect and immediately reconnect to reset the pipe state for the next client DisconnectNamedPipe(hServerPipe); - ConnectNamedPipe(hServerPipe, NULL); + 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; }