Poll until response

FossilOrigin-Name: d4b97ddeddacb24765014384e6a29e66469fd3528b0ff924ed4a8c329c8dae97
This commit is contained in:
nekobit 2022-10-31 16:59:01 +00:00
parent 3357bd476e
commit b8a2395248
4 changed files with 66 additions and 21 deletions

View file

@ -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 */

View file

@ -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)

View file

@ -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;
}

View file

@ -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 };