From 9e63fb59e7b8506183172869ea44b68cdec29be5 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 4 Oct 2024 13:36:54 -0400 Subject: Tons of changes Mainly for things that came up in other projects. Signed-off-by: Andrew Opalach --- src/curl/curl.c | 4 ++-- src/curl/curl.h | 3 ++- src/curl/http.c | 3 ++- src/curl/websocket.c | 20 ++++++++++++++------ src/curl/websocket.h | 4 +++- 5 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src/curl') diff --git a/src/curl/curl.c b/src/curl/curl.c index 3f4cb9e..5560487 100644 --- a/src/curl/curl.c +++ b/src/curl/curl.c @@ -13,7 +13,7 @@ static void curl_socket_action_callback(struct ev_loop *loop, ev_io *w, s32 reve al_log_error("curl", "curl_multi_socket_action() failed (%s).", curl_multi_strerror(mc)); return; } - curl->handle_events(curl); + curl->handle_events(curl->userdata, curl); } static void set_sock(struct aki_curl *curl, s32 what) @@ -53,7 +53,7 @@ static void timeout_callback(struct ev_loop *loop, ev_timer *w, s32 revents) curl->timer_started = false; s32 running; curl_multi_socket_action(curl->multi_handle, CURL_SOCKET_TIMEOUT, 0, &running); - curl->handle_events(curl); + curl->handle_events(curl->userdata, curl); } static s32 timer_callback(CURLM *multi, s64 timeout_ms, void *userp) diff --git a/src/curl/curl.h b/src/curl/curl.h index 1850dcf..5bb38c0 100644 --- a/src/curl/curl.h +++ b/src/curl/curl.h @@ -13,7 +13,8 @@ struct aki_curl { CURL *handle; CURLM *multi_handle; curl_socket_t sock; - void (*handle_events)(struct aki_curl *); + void (*handle_events)(void *, struct aki_curl *); + void *userdata; }; bool aki_curl_init(struct aki_curl *curl); diff --git a/src/curl/http.c b/src/curl/http.c index c58d5a6..cdd740b 100644 --- a/src/curl/http.c +++ b/src/curl/http.c @@ -19,8 +19,9 @@ static void check_status_codes(struct aki_curl *curl) } } -static void http_handle_events(struct aki_curl *curl) +static void http_handle_events(void *userdata, struct aki_curl *curl) { + (void)userdata; s32 pending; CURLMsg *msg; struct aki_http *http = (struct aki_http *)curl; diff --git a/src/curl/websocket.c b/src/curl/websocket.c index 4d480c3..aede3de 100644 --- a/src/curl/websocket.c +++ b/src/curl/websocket.c @@ -3,8 +3,9 @@ #include "websocket.h" -static void websocket_handle_events(struct aki_curl *curl) +static void websocket_handle_events(void *userdata, struct aki_curl *curl) { + struct aki_websocket *ws = (struct aki_websocket *)userdata; s32 pending; CURLMsg *msg; while ((msg = curl_multi_info_read(curl->multi_handle, &pending))) { @@ -12,6 +13,8 @@ static void websocket_handle_events(struct aki_curl *curl) case CURLMSG_DONE: aki_curl_remove_handle(curl); aki_curl_close(curl); + aki_buffer_free(&ws->frame); + ws->callback(ws->userdata, NULL, NULL, 0); return; case CURLMSG_LAST: break; @@ -51,13 +54,17 @@ void aki_websocket_set_url(struct aki_websocket *ws, str *url) bool aki_websocket_connect(struct aki_websocket *ws, struct aki_event_loop *loop, void (*callback)(void *, const struct curl_ws_frame *, u8 *, size_t), void *userdata) { - al_assert(ws->curl.loop == NULL); + // Connect blocking. + //curl_easy_setopt(ws->curl.handle, CURLOPT_CONNECT_ONLY, 2L); + //curl_easy_perform(ws->curl.handle); + curl_easy_setopt(ws->curl.handle, CURLOPT_CONNECT_ONLY, 0L); ws->curl.loop = loop; ws->callback = callback; ws->userdata = userdata; curl_easy_setopt(ws->curl.handle, CURLOPT_WRITEFUNCTION, stream_write_callback); curl_easy_setopt(ws->curl.handle, CURLOPT_WRITEDATA, ws); ws->curl.handle_events = websocket_handle_events; + ws->curl.userdata = ws; return aki_curl_add_handle(&ws->curl); } @@ -69,13 +76,14 @@ void aki_websocket_disconnect(struct aki_websocket *ws, u16 status) al_assert(sent == sizeof(u16)); } -//aki_websocket_close(); -//aki_buffer_free(&ws->frame); - bool aki_websocket_send_frame(struct aki_websocket *ws, u8 *data, size_t size) { size_t sent; - curl_ws_send(ws->curl.handle, data, size, &sent, 0, CURLWS_TEXT); + CURLcode code = curl_ws_send(ws->curl.handle, data, size, &sent, 0, CURLWS_TEXT); + if (code != CURLE_OK) { + al_log_error("curl", "curl_ws_send() failed (%s).", curl_easy_strerror(code)); + return false; + } al_assert(sent == size); return true; } diff --git a/src/curl/websocket.h b/src/curl/websocket.h index 3dc740a..276f6bc 100644 --- a/src/curl/websocket.h +++ b/src/curl/websocket.h @@ -1,6 +1,8 @@ #pragma once -#include "http.h" +#include "../util/buffer.h" + +#include "curl.h" struct aki_websocket { struct aki_curl curl; -- cgit v1.2.3-101-g0448