diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index f99c1a34a..66c359f02 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory(w32-pthreads) add_subdirectory(glad) +add_subdirectory(ipc-util) find_package(Jansson 2.5 QUIET) diff --git a/deps/ipc-util/CMakeLists.txt b/deps/ipc-util/CMakeLists.txt new file mode 100644 index 000000000..917311d11 --- /dev/null +++ b/deps/ipc-util/CMakeLists.txt @@ -0,0 +1,30 @@ +# TODO: Add posix support +if(NOT WIN32) + return() +endif() + +project(ipc-util) + +set(ipc-util_HEADERS + ipc-util/pipe.h) + +if(WIN32) + set(ipc-util_HEADERS + ${ipc-util_HEADERS} + ipc-util/pipe-windows.h) + set(ipc-util_SOURCES + ipc-util/pipe-windows.c) +else() + set(ipc-util_HEADERS + ${ipc-util_HEADERS} + ipc-util/pipe-posix.h) + set(ipc-util_SOURCES + ipc-util/pipe-posix.c) +endif() + +add_library(ipc-util STATIC + ${ipc-util_SOURCES} + ${ipc-util_HEADERS}) +target_include_directories(ipc-util + PUBLIC .) +target_link_libraries(ipc-util) diff --git a/deps/ipc-util/ipc-util/pipe-posix.c b/deps/ipc-util/ipc-util/pipe-posix.c new file mode 100644 index 000000000..1a2978a61 --- /dev/null +++ b/deps/ipc-util/ipc-util/pipe-posix.c @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2014 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* TODO */ diff --git a/deps/ipc-util/ipc-util/pipe-posix.h b/deps/ipc-util/ipc-util/pipe-posix.h new file mode 100644 index 000000000..757ebceb0 --- /dev/null +++ b/deps/ipc-util/ipc-util/pipe-posix.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2014 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +#include + +/* TODO */ diff --git a/deps/ipc-util/ipc-util/pipe-windows.c b/deps/ipc-util/ipc-util/pipe-windows.c new file mode 100644 index 000000000..96f828061 --- /dev/null +++ b/deps/ipc-util/ipc-util/pipe-windows.c @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2014 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "pipe.h" + +#define IPC_PIPE_BUF_SIZE 1024 + +static inline bool ipc_pipe_internal_create_events(ipc_pipe_server_t *pipe) +{ + pipe->ready_event = CreateEvent(NULL, false, false, NULL); + return !!pipe->ready_event; +} + +static inline bool ipc_pipe_internal_create_pipe(ipc_pipe_server_t *pipe, + const char *name) +{ + char new_name[512]; + const DWORD access = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED; + const DWORD flags = PIPE_TYPE_MESSAGE | + PIPE_READMODE_MESSAGE | + PIPE_WAIT; + + strcpy_s(new_name, sizeof(new_name), "\\\\.\\pipe\\"); + strcat_s(new_name, sizeof(new_name), name); + + pipe->handle = CreateNamedPipeA(new_name, access, flags, 1, + IPC_PIPE_BUF_SIZE, IPC_PIPE_BUF_SIZE, 0, NULL); + + return pipe->handle != INVALID_HANDLE_VALUE; +} + +static inline void ipc_pipe_internal_ensure_capacity(ipc_pipe_server_t *pipe, + size_t new_size) +{ + if (pipe->capacity >= new_size) { + return; + } + + pipe->read_data = realloc(pipe->read_data, new_size); + pipe->capacity = new_size; +} + +static inline void ipc_pipe_internal_append_bytes(ipc_pipe_server_t *pipe, + uint8_t *bytes, size_t size) +{ + size_t new_size = pipe->size + size; + ipc_pipe_internal_ensure_capacity(pipe, new_size); + memcpy(pipe->read_data + pipe->size, bytes, size); + pipe->size = new_size; +} + +static inline bool ipc_pipe_internal_io_pending(void) +{ + return GetLastError() == ERROR_IO_PENDING; +} + +static DWORD CALLBACK ipc_pipe_internal_server_thread(LPVOID param) +{ + ipc_pipe_server_t *pipe = param; + uint8_t buf[IPC_PIPE_BUF_SIZE]; + + /* wait for connection */ + DWORD wait = WaitForSingleObject(pipe->ready_event, INFINITE); + if (wait != WAIT_OBJECT_0) { + pipe->read_callback(pipe->param, NULL, 0); + return 0; + } + + for (;;) { + DWORD bytes = 0; + bool success; + + success = !!ReadFile(pipe->handle, buf, IPC_PIPE_BUF_SIZE, NULL, + &pipe->overlap); + if (!success && !ipc_pipe_internal_io_pending()) { + break; + } + + DWORD wait = WaitForSingleObject(pipe->ready_event, INFINITE); + if (wait != WAIT_OBJECT_0) { + break; + } + + success = !!GetOverlappedResult(pipe->handle, &pipe->overlap, + &bytes, true); + if (!success || !bytes) { + break; + } + + ipc_pipe_internal_append_bytes(pipe, buf, (size_t)bytes); + + if (success) { + pipe->read_callback(pipe->param, pipe->read_data, + pipe->size); + pipe->size = 0; + } + } + + pipe->read_callback(pipe->param, NULL, 0); + return 0; +} + +static inline bool ipc_pipe_internal_start_server_thread( + ipc_pipe_server_t *pipe) +{ + pipe->thread = CreateThread(NULL, 0, ipc_pipe_internal_server_thread, + pipe, 0, NULL); + return pipe->thread != NULL; +} + +static inline bool ipc_pipe_internal_wait_for_connection( + ipc_pipe_server_t *pipe) +{ + bool success; + + pipe->overlap.hEvent = pipe->ready_event; + success = !!ConnectNamedPipe(pipe->handle, &pipe->overlap); + return success || (!success && ipc_pipe_internal_io_pending()); +} + +static inline bool ipc_pipe_internal_open_pipe(ipc_pipe_client_t *pipe, + const char *name) +{ + DWORD mode = PIPE_READMODE_MESSAGE; + char new_name[512]; + + strcpy_s(new_name, sizeof(new_name), "\\\\.\\pipe\\"); + strcat_s(new_name, sizeof(new_name), name); + + pipe->handle = CreateFileA(new_name, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, 0, NULL); + if (pipe->handle == INVALID_HANDLE_VALUE) { + return false; + } + + return !!SetNamedPipeHandleState(pipe->handle, &mode, NULL, NULL); +} + +/* ------------------------------------------------------------------------- */ + +bool ipc_pipe_server_start(ipc_pipe_server_t *pipe, const char *name, + ipc_pipe_read_t read_callback, void *param) +{ + pipe->read_callback = read_callback; + pipe->param = param; + + if (!ipc_pipe_internal_create_events(pipe)) { + goto error; + } + if (!ipc_pipe_internal_create_pipe(pipe, name)) { + goto error; + } + if (!ipc_pipe_internal_wait_for_connection(pipe)) { + goto error; + } + if (!ipc_pipe_internal_start_server_thread(pipe)) { + goto error; + } + + return true; + +error: + ipc_pipe_server_free(pipe); + return false; +} + +void ipc_pipe_server_free(ipc_pipe_server_t *pipe) +{ + if (!pipe) + return; + + if (pipe->thread) { + CancelIoEx(pipe->handle, &pipe->overlap); + SetEvent(pipe->ready_event); + WaitForSingleObject(pipe->thread, INFINITE); + CloseHandle(pipe->thread); + } + if (pipe->ready_event) + CloseHandle(pipe->ready_event); + if (pipe->handle) + CloseHandle(pipe->handle); + + free(pipe->read_data); + memset(pipe, 0, sizeof(*pipe)); +} + +bool ipc_pipe_client_open(ipc_pipe_client_t *pipe, const char *name) +{ + if (!ipc_pipe_internal_open_pipe(pipe, name)) { + ipc_pipe_client_free(pipe); + return false; + } + + return true; +} + +void ipc_pipe_client_free(ipc_pipe_client_t *pipe) +{ + if (!pipe) + return; + + if (pipe->handle) + CloseHandle(pipe->handle); + + memset(pipe, 0, sizeof(*pipe)); +} + +bool ipc_pipe_client_write(ipc_pipe_client_t *pipe, const void *data, + size_t size) +{ + DWORD bytes; + + if (!pipe) { + return false; + } + + if (!pipe->handle || pipe->handle == INVALID_HANDLE_VALUE) { + return false; + } + + return !!WriteFile(pipe->handle, data, (DWORD)size, &bytes, NULL); +} diff --git a/deps/ipc-util/ipc-util/pipe-windows.h b/deps/ipc-util/ipc-util/pipe-windows.h new file mode 100644 index 000000000..683c50ad2 --- /dev/null +++ b/deps/ipc-util/ipc-util/pipe-windows.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +#include + +struct ipc_pipe_server { + OVERLAPPED overlap; + HANDLE handle; + HANDLE ready_event; + HANDLE thread; + + uint8_t *read_data; + size_t size; + size_t capacity; + + ipc_pipe_read_t read_callback; + void *param; +}; + +struct ipc_pipe_client { + HANDLE handle; +}; + +static inline bool ipc_pipe_client_valid(ipc_pipe_client_t *pipe) +{ + return pipe->handle != NULL && pipe->handle != INVALID_HANDLE_VALUE; +} diff --git a/deps/ipc-util/ipc-util/pipe.h b/deps/ipc-util/ipc-util/pipe.h new file mode 100644 index 000000000..4f67216b1 --- /dev/null +++ b/deps/ipc-util/ipc-util/pipe.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014 Hugh Bailey + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#elif _MSC_VER +#ifndef inline +#define inline __inline +#endif +#endif + +struct ipc_pipe_server; +struct ipc_pipe_client; +typedef struct ipc_pipe_server ipc_pipe_server_t; +typedef struct ipc_pipe_client ipc_pipe_client_t; + +typedef void (*ipc_pipe_read_t)(void *param, uint8_t *data, size_t size); + +bool ipc_pipe_server_start(ipc_pipe_server_t *pipe, const char *name, + ipc_pipe_read_t read_callback, void *param); +void ipc_pipe_server_free(ipc_pipe_server_t *pipe); + +bool ipc_pipe_client_open(ipc_pipe_client_t *pipe, const char *name); +void ipc_pipe_client_free(ipc_pipe_client_t *pipe); +bool ipc_pipe_client_write(ipc_pipe_client_t *pipe, const void *data, + size_t size); +static inline bool ipc_pipe_client_valid(ipc_pipe_client_t *pipe); + +#ifdef _WIN32 +#include "pipe-windows.h" +#else /* assume posix */ +#include "pipe-posix.h" +#endif + +#ifdef __cplusplus +} +#endif