From de6f6090f94b14ed98dac71956ff1bede48f1d8e Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 31 Mar 2025 19:03:55 -0400 Subject: Apply style changes, abide by strict aliasing - Remove internal sending step in packet pool. Signed-off-by: Andrew Opalach --- src/curl/curl.c | 30 +++++++----------------- src/curl/http.c | 66 +++++++++++++++++----------------------------------- src/curl/websocket.c | 15 ++++-------- 3 files changed, 34 insertions(+), 77 deletions(-) (limited to 'src/curl') diff --git a/src/curl/curl.c b/src/curl/curl.c index 568f82e..5a5ac01 100644 --- a/src/curl/curl.c +++ b/src/curl/curl.c @@ -1,3 +1,4 @@ +#define AL_LOG_SECTION "curl" #include #include "curl.h" @@ -10,11 +11,11 @@ static void curl_socket_action_callback(struct ev_loop *loop, ev_io *w, s32 reve { (void)loop; struct nn_curl *curl = (struct nn_curl *)w->data; - s32 action = (revents & EV_READ ? CURL_CSELECT_IN : 0) | (revents & EV_WRITE ? CURL_CSELECT_OUT : 0); + s32 action = ((revents & EV_READ) ? CURL_CSELECT_IN : 0) | ((revents & EV_WRITE) ? CURL_CSELECT_OUT : 0); s32 running; CURLMcode mc = curl_multi_socket_action(curl->multi_handle, curl->sock, action, &running); if (mc != CURLM_OK) { - al_log_error("curl", "curl_multi_socket_action() failed (%s).", curl_multi_strerror(mc)); + error("curl_multi_socket_action() failed (%s).", curl_multi_strerror(mc)); return; } curl->handle_events(curl->userdata, curl); @@ -26,8 +27,8 @@ static void set_sock(struct nn_curl *curl, s32 what) if (curl->started) { ev_io_stop(curl->loop->ev, &curl->event); } - s32 action = (what & CURL_POLL_IN ? EV_READ : 0) | (what & CURL_POLL_OUT ? EV_WRITE : 0); - ev_io_init(&curl->event, curl_socket_action_callback, curl->sock, action); + s32 action = ((what & CURL_POLL_IN) ? EV_READ : 0) | ((what & CURL_POLL_OUT) ? EV_WRITE : 0); + ev_io_init_n(&curl->event, curl_socket_action_callback, curl->sock, action); ev_io_start(curl->loop->ev, &curl->event); curl->started = true; } @@ -55,12 +56,10 @@ static void timeout_callback(struct ev_loop *loop, ev_timer *w, s32 revents) (void)loop; struct nn_curl *curl = (struct nn_curl *)w->data; (void)revents; - if (curl->timer_started) { ev_timer_stop(curl->loop->ev, w); curl->timer_started = false; } - s32 running; curl_multi_socket_action(curl->multi_handle, CURL_SOCKET_TIMEOUT, 0, &running); curl->handle_events(curl->userdata, curl); @@ -70,22 +69,15 @@ static s32 timer_callback(CURLM *multi, s64 timeout_ms, void *userp) { (void)multi; struct nn_curl *curl = (struct nn_curl *)userp; - if (curl->timer_started) { ev_timer_stop(curl->loop->ev, &curl->timer); curl->timer_started = false; } - - if (timeout_ms == -1) { - return 0; - } - + if (timeout_ms == -1) return 0; curl->timer.data = curl; - ev_timer_init(&curl->timer, timeout_callback, timeout_ms / 1000.0, 0.0); - + ev_timer_init_n(&curl->timer, timeout_callback, timeout_ms / 1000.0, 0.0); curl->timer_started = true; ev_timer_start(curl->loop->ev, &curl->timer); - return 0; } @@ -95,24 +87,20 @@ bool nn_curl_init(struct nn_curl *curl) if (!curl->handle) goto err; //curl_easy_setopt(curl->handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl->handle, CURLOPT_NOPROGRESS, 1L); - curl->multi_handle = curl_multi_init(); if (!curl->multi_handle) goto err; curl_multi_setopt(curl->multi_handle, CURLMOPT_SOCKETFUNCTION, sock_callback); curl_multi_setopt(curl->multi_handle, CURLMOPT_SOCKETDATA, curl); curl_multi_setopt(curl->multi_handle, CURLMOPT_TIMERFUNCTION, timer_callback); curl_multi_setopt(curl->multi_handle, CURLMOPT_TIMERDATA, curl); - curl->loop = NULL; curl->event.data = curl; curl->started = false; curl->timer_started = false; curl->added = false; - return true; err: nn_curl_close(curl); - return false; } @@ -130,7 +118,7 @@ bool nn_curl_add_handle(struct nn_curl *curl) // a callback is running on another thread. CURLMcode mc = curl_multi_add_handle(curl->multi_handle, curl->handle); if (mc != CURLM_OK) { - al_log_error("curl", "curl_multi_add_handle() failed (%s).", curl_multi_strerror(mc)); + error("curl_multi_add_handle() failed (%s).", curl_multi_strerror(mc)); return false; } curl->added = true; @@ -142,7 +130,7 @@ bool nn_curl_remove_handle(struct nn_curl *curl) al_assert(curl->added); CURLMcode mc = curl_multi_remove_handle(curl->multi_handle, curl->handle); if (mc != CURLM_OK) { - al_log_error("curl", "curl_multi_remove_handle() failed (%s).", curl_multi_strerror(mc)); + error("curl_multi_remove_handle() failed (%s).", curl_multi_strerror(mc)); return false; } curl->added = false; diff --git a/src/curl/http.c b/src/curl/http.c index ad2a156..993aef7 100644 --- a/src/curl/http.c +++ b/src/curl/http.c @@ -1,3 +1,4 @@ +#define AL_LOG_SECTION "curl_http" #include #include "http.h" @@ -5,17 +6,15 @@ static void check_status_codes(struct nn_curl *curl) { struct nn_http *http = (struct nn_http *)curl; - - if (http->status_code <= 0L) { + if (http->status_code <= 0) { curl_easy_getinfo(curl->handle, CURLINFO_RESPONSE_CODE, &http->status_code); - if (http->status_code > 0L) { + if (http->status_code > 0) { http->callback(http->userdata, NNWT_HTTP_RESPONSE_CODE, NULL, &http->status_code); } } - - if (http->content_length < 0L && http->status_code == 200L) { + if (http->content_length < 0) { curl_easy_getinfo(curl->handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &http->content_length); - if (http->content_length >= 0L) { + if (http->content_length >= 0) { http->callback(http->userdata, NNWT_HTTP_CONTENT_LENGTH, NULL, &http->content_length); } } @@ -31,26 +30,19 @@ static void http_handle_events(void *userdata, struct nn_curl *curl) switch (msg->msg) { case CURLMSG_DONE: { al_assert(msg->easy_handle == curl->handle); - CURLcode code = msg->data.result; if (code != CURLE_OK) { - al_log_error("http", "Curl error: %s.", curl_easy_strerror(code)); - http->status_code = -1L; + error("Curl error: %s.", curl_easy_strerror(code)); + http->status_code = -1; http->callback(http->userdata, NNWT_HTTP_ERROR, NULL, &http->status_code); return; } - - if (!nn_curl_remove_handle(curl)) { - return; - } - + if (!nn_curl_remove_handle(curl)) return; check_status_codes(curl); - - if (http->status_code == 200L) { + if (http->status_code == 200) { http->callback(http->userdata, NNWT_HTTP_FINISHED, NULL, &http->status_code); - } else if (http->status_code / 100L == 3L) { + } else if (http->status_code / 100 == 3) { http->callback(http->userdata, NNWT_HTTP_REDIRECT, NULL, &http->status_code); - char *redirect_url = NULL; curl_easy_getinfo(curl->handle, CURLINFO_REDIRECT_URL, &redirect_url); if (!redirect_url) { @@ -58,23 +50,19 @@ static void http_handle_events(void *userdata, struct nn_curl *curl) return; } nn_curl_set_url(curl, &al_str_cr(redirect_url)); - - http->status_code = -1L; - http->content_length = -1L; - if (!nn_curl_add_handle(curl)) { - return; - } + http->status_code = -1; + http->content_length = -1; + if (!nn_curl_add_handle(curl)) return; } else { http->callback(http->userdata, NNWT_HTTP_ERROR, NULL, &http->status_code); } - return; } case CURLMSG_LAST: - al_log_debug("http", "Unhandled CURLMSG_LAST."); + debug("Unhandled CURLMSG_LAST."); break; case CURLMSG_NONE: - al_log_debug("http", "Unhandled CURLMSG_NONE."); + debug("Unhandled CURLMSG_NONE."); break; } } @@ -82,8 +70,8 @@ static void http_handle_events(void *userdata, struct nn_curl *curl) bool nn_http_init(struct nn_http *http) { - http->status_code = -1L; - http->content_length = -1L; + http->status_code = -1; + http->content_length = -1; http->headers = NULL; return nn_curl_init(&http->curl); } @@ -148,12 +136,9 @@ static void request_callback(void *userdata, u8 op, u8 *buf, void *opaque) if (request->pointer + n > size) { n = size - request->pointer; } - nn_buffer_read(&request->payload, buf, request->pointer, n); request->pointer += n; - *(size_t *)opaque = n; - break; } case NNWT_HTTP_WRITE: { @@ -182,30 +167,25 @@ static void request_callback(void *userdata, u8 op, u8 *buf, void *opaque) static bool init_request_internal(struct nn_http *http, u8 method) { struct nn_curl *curl = &http->curl; - switch (method) { case NNWT_HTTP_HEAD: - curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L); + curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1); break; case NNWT_HTTP_GET: - curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1); break; case NNWT_HTTP_POST: - curl_easy_setopt(curl->handle, CURLOPT_POST, 1L); + curl_easy_setopt(curl->handle, CURLOPT_POST, 1); break; } - curl_easy_setopt(curl->handle, CURLOPT_WRITEFUNCTION, stream_write_callback); curl_easy_setopt(curl->handle, CURLOPT_WRITEDATA, http); curl_easy_setopt(curl->handle, CURLOPT_READFUNCTION, stream_read_callback); curl_easy_setopt(curl->handle, CURLOPT_READDATA, http); - if (http->headers) { curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, http->headers); } - curl->handle_events = http_handle_events; - return nn_curl_add_handle(curl); } @@ -235,18 +215,14 @@ bool nn_http_request(struct nn_http_request *request, u8 method, struct nn_event struct nn_curl *curl = &request->http.curl; al_assert(curl->loop == NULL); curl->loop = loop; - request->http.callback = request_callback; request->http.userdata = request; - long payload = (long)nn_buffer_get_size(&request->payload); - if (payload > 0L) { + if (payload > 0) { curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, payload); } - request->callback = callback; request->userdata = userdata; - return init_request_internal(&request->http, method); } diff --git a/src/curl/websocket.c b/src/curl/websocket.c index 0030d16..8995221 100644 --- a/src/curl/websocket.c +++ b/src/curl/websocket.c @@ -1,3 +1,4 @@ +#define AL_LOG_SECTION "curl_ws" #include #include "../socket/net.h" @@ -30,18 +31,14 @@ static size_t stream_write_callback(char *buffer, size_t size, size_t nmemb, voi (void)size; (void)nmemb; struct nn_websocket *ws = (struct nn_websocket *)userdata; - const struct curl_ws_frame *m = curl_ws_meta(ws->curl.handle); size_t frame_size = m->offset + m->len + m->bytesleft; - al_log_debug("ws", "frame_size: %zu, bytesleft: %zu.", frame_size, m->bytesleft); - + debug("frame_size: %zu, bytesleft: %zu.", frame_size, m->bytesleft); nn_buffer_ensure_space(&ws->frame, frame_size); al_memcpy(nn_buffer_get_ptr(&ws->frame, m->offset), buffer, m->len); - if (m->bytesleft == 0) { ws->callback(ws->userdata, m, nn_buffer_get_ptr(&ws->frame, 0), frame_size); } - return m->len; } @@ -62,17 +59,13 @@ bool nn_websocket_connect(struct nn_websocket *ws, struct nn_event_loop *loop, struct nn_curl *curl = &ws->curl; al_assert(curl->loop == NULL); curl->loop = loop; - - curl_easy_setopt(curl->handle, CURLOPT_CONNECT_ONLY, 0L); + curl_easy_setopt(curl->handle, CURLOPT_CONNECT_ONLY, 0); ws->callback = callback; ws->userdata = userdata; - curl_easy_setopt(curl->handle, CURLOPT_WRITEFUNCTION, stream_write_callback); curl_easy_setopt(curl->handle, CURLOPT_WRITEDATA, ws); - curl->handle_events = websocket_handle_events; curl->userdata = ws; - return nn_curl_add_handle(&ws->curl); } @@ -89,7 +82,7 @@ bool nn_websocket_send_frame(struct nn_websocket *ws, u8 *data, size_t size) size_t sent; 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)); + error("curl_ws_send() failed (%s).", curl_easy_strerror(code)); return false; } al_assert(sent == size); -- cgit v1.2.3-101-g0448