// this thread works in a fire-and-forget fashion, it's started and then just runs static DWORD WINAPI read_named_pipe(void *opaque) { const char *pipe_name = "\\\\.\\pipe\\tinkerforge-brick-daemon-debug-log"; HANDLE hpipe; DWORD mode = PIPE_READMODE_MESSAGE; LogPipeMessage pipe_message; DWORD bytes_read; (void)opaque; append_debug_meta_message("Connecting to Brick Daemon..."); for (;;) { _debug_connected = 0; update_status_bar(); for (;;) { hpipe = CreateFile(pipe_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hpipe != INVALID_HANDLE_VALUE) { break; } Sleep(250); } if (!SetNamedPipeHandleState(hpipe, &mode, NULL, NULL)) { CloseHandle(hpipe); continue; } _debug_connected = 1; update_status_bar(); append_debug_meta_message("Connected to Brick Daemon"); for (;;) { if (!ReadFile(hpipe, &pipe_message, sizeof(pipe_message), &bytes_read, NULL)) { append_debug_meta_message("Disconnected from Brick Daemon, reconnecting..."); CloseHandle(hpipe); break; } if (bytes_read == sizeof(pipe_message) && pipe_message.length == sizeof(pipe_message)) { // enforce that strings are NUL-terminated pipe_message.file[sizeof(pipe_message.file) - 1] = '\0'; pipe_message.function[sizeof(pipe_message.function) - 1] = '\0'; pipe_message.message[sizeof(pipe_message.message) - 1] = '\0'; append_debug_pipe_message(&pipe_message); } } } return 0; }
// this thread works in a fire-and-forget fashion, it's started and then just runs static DWORD WINAPI read_named_pipe(void *opaque) { const char *pipe_name = "\\\\.\\pipe\\tinkerforge-brick-daemon-debug-log"; HANDLE hpipe; DWORD current_error; DWORD last_error; LogPipeMessage pipe_message; DWORD bytes_read; char buffer[256]; (void)opaque; append_debug_meta_message("Connecting to Brick Daemon..."); for (;;) { _debug_connected = 0; last_error = ERROR_SUCCESS; update_status_bar(); for (;;) { hpipe = CreateFile(pipe_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (hpipe != INVALID_HANDLE_VALUE) { break; } current_error = GetLastError(); if (current_error != last_error && current_error != ERROR_FILE_NOT_FOUND) { _snprintf(buffer, sizeof(buffer), "Error while connecting to Brick Daemon, trying again: %s (%d)", get_error_name(current_error), current_error); append_debug_meta_message(buffer); } last_error = current_error; Sleep(1000); } _debug_connected = 1; update_status_bar(); append_debug_meta_message("Connected to Brick Daemon"); for (;;) { if (!ReadFile(hpipe, &pipe_message, sizeof(pipe_message), &bytes_read, NULL)) { append_debug_meta_message("Disconnected from Brick Daemon, reconnecting..."); CloseHandle(hpipe); break; } if (bytes_read == sizeof(pipe_message) && pipe_message.length == sizeof(pipe_message)) { // enforce that strings are NUL-terminated pipe_message.source[sizeof(pipe_message.source) - 1] = '\0'; pipe_message.message[sizeof(pipe_message.message) - 1] = '\0'; append_debug_pipe_message(&pipe_message); } } } }