Simplify, simplify, simplify. Kill code that was unloved and unused
This commit is contained in:
@@ -1444,154 +1444,3 @@ DN_API DN_OSPosixProcSelfStatus DN_OS_PosixProcSelfStatus()
|
||||
DN_OS_FileClose(&file);
|
||||
return result;
|
||||
}
|
||||
|
||||
// NOTE: DN_OSHttp /////////////////////////////////////////////////////////////////////////////////
|
||||
#if 0 // TODO(doyle): Implement websockets for Windows and Emscripten
|
||||
static EM_BOOL EMWebSocketOnOpenCallback(int type, const EmscriptenWebSocketOpenEvent *event, void *user_context)
|
||||
{
|
||||
(void)user_context;
|
||||
(void)type;
|
||||
(void)event;
|
||||
// EMSCRIPTEN_RESULT result = emscripten_websocket_send_utf8_text(event->socket, R"({"jsonrpc":"2.0","id":1,"method": "eth_subscribe","params":["newHeads"]})");
|
||||
// if (result)
|
||||
// DN_LogInfoF("Failed to emscripten_websocket_send_utf8_text(): %d\n", result);
|
||||
return EM_TRUE;
|
||||
}
|
||||
|
||||
static EM_BOOL EMWebSocketOnMsgCallback(int type, const EmscriptenWebSocketMessageEvent *event __attribute__((nonnull)), void *user_context)
|
||||
{
|
||||
(void)type;
|
||||
(void)user_context;
|
||||
(void)event;
|
||||
if (event->isText) {
|
||||
DN_LogInfoF("Received: %.*s", event->numBytes, event->data);
|
||||
} else {
|
||||
DN_LogInfoF("Received: %d bytes", event->numBytes);
|
||||
}
|
||||
return EM_TRUE;
|
||||
}
|
||||
|
||||
static EM_BOOL EMWebSocketOnErrorCallback(int type, const EmscriptenWebSocketErrorEvent *event, void *user_context)
|
||||
{
|
||||
(void)user_context;
|
||||
(void)type;
|
||||
(void)event;
|
||||
return EM_TRUE;
|
||||
}
|
||||
|
||||
static EM_BOOL EMWebSocketOnCloseCallback(int type, const EmscriptenWebSocketCloseEvent *event, void *user_context)
|
||||
{
|
||||
(void)user_context;
|
||||
(void)type;
|
||||
(void)event;
|
||||
return EM_TRUE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
static void DN_OS_HttpRequestEMFetchOnSuccessCallback(emscripten_fetch_t *fetch)
|
||||
{
|
||||
DN_OSHttpResponse *response = DN_Cast(DN_OSHttpResponse *) fetch->userData;
|
||||
if (!DN_Check(response))
|
||||
return;
|
||||
|
||||
response->http_status = DN_Cast(DN_U32) fetch->status;
|
||||
response->body = DN_Str8AllocArena(fetch->numBytes, DN_ZMem_No, response->arena);
|
||||
if (response->body.data)
|
||||
DN_Memcpy(response->body.data, fetch->data, fetch->numBytes);
|
||||
|
||||
DN_OS_SemaphoreIncrement(&response->on_complete_semaphore, 1);
|
||||
DN_AtomicAddU32(&response->done, 1);
|
||||
}
|
||||
|
||||
static void DN_OS_HttpRequestEMFetchOnErrorCallback(emscripten_fetch_t *fetch)
|
||||
{
|
||||
DN_OSHttpResponse *response = DN_Cast(DN_OSHttpResponse *) fetch->userData;
|
||||
if (!DN_Check(response))
|
||||
return;
|
||||
|
||||
response->http_status = DN_Cast(DN_U32) fetch->status;
|
||||
response->body = DN_Str8AllocArena(fetch->numBytes, DN_ZMem_No, response->arena);
|
||||
if (response->body.size)
|
||||
DN_Memcpy(response->body.data, fetch->data, fetch->numBytes);
|
||||
|
||||
DN_OS_SemaphoreIncrement(&response->on_complete_semaphore, 1);
|
||||
DN_AtomicAddU32(&response->done, 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
|
||||
DN_Arena *arena,
|
||||
DN_Str8 host,
|
||||
DN_Str8 path,
|
||||
DN_OSHttpRequestSecure secure,
|
||||
DN_Str8 method,
|
||||
DN_Str8 body,
|
||||
DN_Str8 headers)
|
||||
{
|
||||
if (!response || !arena)
|
||||
return;
|
||||
|
||||
response->arena = arena;
|
||||
response->builder.arena = response->scratch_arena.mem ? &response->scratch_arena : &response->tmp_arena;
|
||||
|
||||
DN_Arena *scratch = &response->scratch_arena;
|
||||
DN_TCScratch scratch_ = DN_TCScratchBeginArena(&arena, 1);
|
||||
DN_DEFER { DN_TCScratchEnd(&scratch_); };
|
||||
if (!scratch)
|
||||
scratch = &scratch_.arena;
|
||||
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
emscripten_fetch_attr_t fetch_attribs = {};
|
||||
emscripten_fetch_attr_init(&fetch_attribs);
|
||||
|
||||
if (method.size >= sizeof(fetch_attribs.requestMethod)) {
|
||||
response->error_msg =
|
||||
DN_Str8FromFmtArena(arena,
|
||||
"Request method in EM has a size limit of 31 characters, method was "
|
||||
"'%.*s' which is %zu characters long",
|
||||
DN_Str8PrintFmt(method),
|
||||
method.size);
|
||||
DN_CheckF(method.size < sizeof(fetch_attribs.requestMethod),
|
||||
"%.*s",
|
||||
DN_Str8PrintFmt(response->error_msg));
|
||||
response->error_code = DN_Cast(DN_U32) - 1;
|
||||
DN_AtomicAddU32(&response->done, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
DN_Memcpy(fetch_attribs.requestMethod, method.data, method.size);
|
||||
|
||||
fetch_attribs.requestData = body.data;
|
||||
fetch_attribs.requestDataSize = body.size;
|
||||
fetch_attribs.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
||||
fetch_attribs.onsuccess = DN_OS_HttpRequestEMFetchOnSuccessCallback;
|
||||
fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback;
|
||||
fetch_attribs.userData = response;
|
||||
|
||||
DN_Str8 url = DN_Str8FromFmtArena(scratch, "%.*s%.*s", DN_Str8PrintFmt(host), DN_Str8PrintFmt(path));
|
||||
DN_LogInfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'",
|
||||
fetch_attribs.requestMethod,
|
||||
DN_Str8PrintFmt(url),
|
||||
DN_Str8PrintFmt(body));
|
||||
response->on_complete_semaphore = DN_OS_SemaphoreInit(0);
|
||||
response->em_handle = emscripten_fetch(&fetch_attribs, url.data);
|
||||
#else // #elif defined(DN_OS_WIN32)
|
||||
DN_InvalidCodePathF("Unimplemented function");
|
||||
#endif
|
||||
}
|
||||
|
||||
DN_API void DN_OS_HttpRequestFree(DN_OSHttpResponse *response)
|
||||
{
|
||||
// NOTE: Cleanup
|
||||
#if defined(DN_PLATFORM_EMSCRIPTEN)
|
||||
if (response->em_handle) {
|
||||
emscripten_fetch_close(response->em_handle);
|
||||
response->em_handle = nullptr;
|
||||
}
|
||||
#endif // #elif defined(DN_OS_WIN32)
|
||||
|
||||
DN_MemListDeinit(response->tmp_arena.mem);
|
||||
DN_OS_SemaphoreDeinit(&response->on_complete_semaphore);
|
||||
*response = {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user