Commit: 19c2860c80d7773395dab3a00ed3edeaf1bdf375
Parent: 84e39fb7d5a0e6495699a37afbe15c3146dcca1d
Author: tkhenry-uofa
Date: Fri, 21 Feb 2025 09:00:52 -0700
w32: reload pipe when its in a bad state
Diffstat:
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/main_w32.c b/main_w32.c
@@ -108,6 +108,36 @@ clear_io_queue(Platform *platform, BeamformerInput *input, Arena arena)
}
}
+static b32
+poll_pipe(Pipe *p, Stream *e)
+{
+ u8 data;
+ i32 total_read = 0;
+ b32 result = ReadFile(p->file, &data, 0, &total_read, 0);
+ if (!result) {
+ i32 error = GetLastError();
+ /* NOTE: These errors mean nothing's been sent yet, otherwise pipe is busted
+ * and needs to be recreated. */
+ if (error != ERROR_NO_DATA &&
+ error != ERROR_PIPE_LISTENING &&
+ error != ERROR_PIPE_NOT_CONNECTED)
+ {
+ DisconnectNamedPipe(p->file);
+ CloseHandle(p->file);
+ *p = os_open_named_pipe(p->name);
+
+ if (p->file == INVALID_FILE) {
+ stream_append_s8(e, s8("poll_pipe: failed to reopen pipe: error: "));
+ stream_append_i64(e, GetLastError());
+ stream_append_byte(e, '\n');
+ os_write_err_msg(stream_to_s8(e));
+ e->widx = 0;
+ }
+ }
+ }
+ return result;
+}
+
int
main(void)
{
@@ -139,9 +169,7 @@ main(void)
input.last_mouse = input.mouse;
input.mouse.rl = GetMousePosition();
- i32 bytes_available = 0;
- input.pipe_data_available = PeekNamedPipe(data_pipe.file, 0, 1 * MEGABYTE, 0,
- &bytes_available, 0) && bytes_available;
+ input.pipe_data_available = poll_pipe(&data_pipe, &ctx.error_stream);
beamformer_frame_step(&ctx, &temp_memory, &input);
diff --git a/os_win32.c b/os_win32.c
@@ -12,6 +12,9 @@
#define GENERIC_WRITE 0x40000000
#define GENERIC_READ 0x80000000
+#define PIPE_WAIT 0x00
+#define PIPE_NOWAIT 0x01
+
#define PIPE_TYPE_BYTE 0x00
#define PIPE_ACCESS_INBOUND 0x01
@@ -27,6 +30,10 @@
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
+#define ERROR_NO_DATA 232L
+#define ERROR_PIPE_NOT_CONNECTED 233L
+#define ERROR_PIPE_LISTENING 536L
+
typedef struct {
u16 wProcessorArchitecture;
u16 _pad1;
@@ -82,6 +89,7 @@ W32(iptr) CreateFileMappingA(iptr, void *, u32, u32, u32, c8 *);
W32(iptr) CreateIoCompletionPort(iptr, iptr, uptr, u32);
W32(iptr) CreateNamedPipeA(c8 *, u32, u32, u32, u32, u32, u32, void *);
W32(b32) DeleteFileA(c8 *);
+W32(b32) DisconnectNamedPipe(iptr);
W32(void) ExitProcess(i32);
W32(b32) FreeLibrary(void *);
W32(b32) GetFileInformationByHandle(iptr, w32_file_info *);
@@ -92,7 +100,6 @@ W32(iptr) GetStdHandle(i32);
W32(void) GetSystemInfo(void *);
W32(void *) LoadLibraryA(c8 *);
W32(void *) MapViewOfFile(iptr, u32, u32, u32, u64);
-W32(b32) PeekNamedPipe(iptr, u8 *, i32, i32 *, i32 *, i32 *);
W32(b32) ReadDirectoryChangesW(iptr, u8 *, u32, b32, u32, u32 *, void *, void *);
W32(b32) ReadFile(iptr, u8 *, i32, i32 *, void *);
W32(b32) WriteFile(iptr, u8 *, i32, i32 *, void *);
@@ -219,7 +226,7 @@ os_get_file_stats(char *fname)
static Pipe
os_open_named_pipe(char *name)
{
- iptr h = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, 1,
+ iptr h = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE|PIPE_NOWAIT, 1,
0, 1 * MEGABYTE, 0, 0);
return (Pipe){.file = h, .name = name};
}