From b8a239524825e2c3adc9b62721e23e5b2eaa11ca Mon Sep 17 00:00:00 2001 From: nekobit Date: Mon, 31 Oct 2022 16:59:01 +0000 Subject: [PATCH] Poll until response FossilOrigin-Name: d4b97ddeddacb24765014384e6a29e66469fd3528b0ff924ed4a8c329c8dae97 --- include/mastodont_fetch.h | 20 +++++++++++++++- include/mastodont_types.h | 3 +++ src/fetch.c | 50 +++++++++++++++++++++++++++++---------- src/notification.c | 14 +++++------ 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/include/mastodont_fetch.h b/include/mastodont_fetch.h index 5055b80..ebf957a 100644 --- a/include/mastodont_fetch.h +++ b/include/mastodont_fetch.h @@ -30,6 +30,21 @@ struct mstdnt_fetch_data void* callback_args; }; +struct mstdnt_fd +{ + int fd; + short events; + short revents; +}; + +/* Note: Don't quite rely on these values being equal to curl's! */ +enum mstdnt_fds_events +{ + MSTDNT_POLLIN = CURL_WAIT_POLLIN, + MSTDNT_POLLPRI = CURL_WAIT_POLLPRI, + MSTDNT_POLLOUT = CURL_WAIT_POLLOUT, +}; + enum mstdnt_fetch_await { MSTDNT_AWAIT_ALL, @@ -68,6 +83,9 @@ int mstdnt_fetch_curl_async(mastodont_t* mstdnt, * there are no more transfers. MSTDNT_AWAIT_ONCE will run * @return 1 on error */ -int mstdnt_await(mastodont_t* mstdnt, enum mstdnt_fetch_await opt); +int mstdnt_poll(mastodont_t* mstdnt, + enum mstdnt_fetch_await opt, + struct mstdnt_fd extra_fds[], + size_t nfds); #endif /* MASTODONT_FETCH_H */ diff --git a/include/mastodont_types.h b/include/mastodont_types.h index e266585..8ec8855 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -28,6 +28,9 @@ typedef int8_t mstdnt_bool; #define MSTDNT_FALSE 1 #define MSTDNT_BOOL_UNSET 0 +typedef struct mstdnt_request_cb_data { + +} mstdnt_request_cb_data_t; typedef void (*mstdnt_request_cb_t)(void* data, void* args); #define MSTDNT_FLAG_NO_URI_SANITIZE (1<<0) diff --git a/src/fetch.c b/src/fetch.c index 20f4bbc..5d63117 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -36,6 +36,8 @@ size_t mstdnt_curl_write_callback(char* ptr, size_t _size, size_t nmemb, void* _ memcpy(&(res->response[res->size]), ptr, size); res->size += size; res->response[res->size] = 0; + + res->callback(NULL, res->callback_args); return size; } @@ -106,42 +108,64 @@ int mstdnt_fetch_curl_async(mastodont_t* mstdnt, curl_easy_setopt(curl, request_t, 1); // Add curl handle to multi, then run - curl_multi_add_handle(mstdnt->curl, curl); + res = curl_multi_add_handle(mstdnt->curl, curl); // No docs on this? int running; res = curl_multi_perform(mstdnt->curl, &running); - if (!res) + if (res != CURLM_OK) + { + printf("error %s\n", curl_multi_strerror(res)); return -1; + } return running; } -int mstdnt_await(mastodont_t* mstdnt, enum mstdnt_fetch_await opt) +int mstdnt_poll(mastodont_t* mstdnt, + enum mstdnt_fetch_await opt, + struct mstdnt_fd extra_fds[], + size_t nfds) { CURLMsg* msg; int msgs_left = 1; struct mstdnt_fetch_data* data; int res; - - // TODO + struct curl_waitfd* fds = NULL; - res = curl_multi_poll(mstdnt->curl, NULL, 0, 1000, NULL); + // Any other fds can go here + if (extra_fds) + { + fds = calloc(nfds, sizeof(struct curl_waitfd)); + for (int i = 0; i < nfds; ++i) + { + fds[i].fd = extra_fds[i].fd; + fds[i].events = extra_fds[i].events; + fds[i].revents = extra_fds[i].revents; + } + } + + res = curl_multi_poll(mstdnt->curl, fds, nfds, 0, NULL); // Check if our socket is done - while ((msg = curl_multi_info_read(mstdnt->curl, &msgs_left))) + do { - if (msg->msg == CURLMSG_DONE) + while ((msg = curl_multi_info_read(mstdnt->curl, &msgs_left))) { - // Get easy info - curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data); - mstdnt_fetch_data_cleanup(data); + if (msg->msg == CURLMSG_DONE) + { + // Get easy info + curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data); + mstdnt_fetch_data_cleanup(data); - curl_multi_remove_handle(mstdnt->curl, msg->easy_handle); - curl_easy_cleanup(msg->easy_handle); + curl_multi_remove_handle(mstdnt->curl, msg->easy_handle); + curl_easy_cleanup(msg->easy_handle); + } } } while (opt == MSTDNT_AWAIT_ALL && msgs_left); + free(fds); + return res; } diff --git a/src/notification.c b/src/notification.c index 1aa1f21..71eca15 100644 --- a/src/notification.c +++ b/src/notification.c @@ -115,13 +115,13 @@ int mstdnt_notifications_json_callback(cJSON* json, void* _args) } int mstdnt_get_notifications(mastodont_t* data, - struct mstdnt_args* m_args, -mstdnt_request_cb_t cb_request, -void* cb_args, - struct mstdnt_notifications_args* args, - struct mstdnt_storage* storage, - struct mstdnt_notification** notifs, - size_t* size) + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + struct mstdnt_notifications_args* args, + struct mstdnt_storage* storage, + struct mstdnt_notification** notifs, + size_t* size) { struct _mstdnt_notifications_result_cb_args req_cb_args = { notifs, size };