diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-20 10:56:23 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-20 10:56:23 +0200 |
commit | c405a7d93341d2c13167e8ab834c68f7b035a8bb (patch) | |
tree | 67bb1f0f3e5cbeee3fc4cf66c24d0f7bd2b38fff | |
parent | 031b79e3c66de6151dd40cc5067d3fac76e1e7f4 (diff) |
Some callback naming consistency
-rw-r--r-- | cfgdir.c | 4 | ||||
-rw-r--r-- | config.h | 4 | ||||
-rw-r--r-- | main.c | 7 | ||||
-rw-r--r-- | main.h | 20 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | server.h | 6 | ||||
-rw-r--r-- | uring.c | 84 | ||||
-rw-r--r-- | uring.h | 31 |
8 files changed, 83 insertions, 75 deletions
@@ -187,7 +187,7 @@ struct cfg_key_value_map scfg_key_map[] = { static bool handle_dns(struct cfg *cfg, struct server *scfg, const char *type, - struct cfg_value *value, dns_callback_t *async_cb, + struct cfg_value *value, dns_cb_t *async_cb, bool (*sync_cb)(struct cfg *, struct server *, struct saddr *)) { struct saddr *saddr, *tmp; @@ -208,7 +208,7 @@ handle_dns(struct cfg *cfg, struct server *scfg, const char *type, type, value->dns_async); dns = value->dns_async; - dns->callback = async_cb; + dns->cb = async_cb; dns->priv = scfg; list_add(&dns->list, &scfg->dnslookups); uring_task_get(cfg, &scfg->task); @@ -18,7 +18,7 @@ enum cfg_value_type { struct dns_async; -typedef void (dns_callback_t)(struct dns_async *); +typedef void (dns_cb_t)(struct dns_async *); struct dns_async { char name[FQDN_STR_LEN + 1]; @@ -26,7 +26,7 @@ struct dns_async { struct addrinfo req; struct gaicb gcb; struct sigevent sev; - dns_callback_t *callback; + dns_cb_t *cb; void *priv; struct list_head list; }; @@ -603,15 +603,14 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) } else { debug(DBG_DNS, "DNS lookup complete, dns: %p, dns->cb: %p\n", hack_dns, - hack_dns ? hack_dns->callback : NULL); + hack_dns ? hack_dns->cb : NULL); - if (!hack_dns || !hack_dns->callback) { + if (!hack_dns || !hack_dns->cb) { error("DNS callback not set\n"); goto out; } - if (hack_dns && hack_dns->callback) - hack_dns->callback(hack_dns); + hack_dns->cb(hack_dns); } out: @@ -62,8 +62,8 @@ void __die(const char *fmt, ...) __attribute__((format(printf, 1, 2))); struct uring_task; /* To save typing in all the function definitions below */ -typedef void (*callback_t)(struct cfg *, struct uring_task *, int res); -typedef int (*rcallback_t)(struct cfg *, struct uring_task *, int res); +typedef void (*utask_cb_t)(struct cfg *, struct uring_task *, int res); +typedef int (*rutask_cb_t)(struct cfg *, struct uring_task *, int res); struct uring_task_buf { char buf[4096]; @@ -81,10 +81,18 @@ struct uring_task { void (*free)(struct uring_task *); bool dead; struct uring_task_buf *tbuf; - callback_t callback; - rcallback_t complete_callback; /* to check if tbuf processing is done */ - callback_t final_callback; /* once tbuf processing is done */ - struct saddr saddr; /* used for recvmsg/sendmsg */ + + /* called once or repeatedly until is_complete_cb is satisfied */ + utask_cb_t cb; + + /* returns: 0 = not complete; < 0 = error; > 0 = complete */ + rutask_cb_t is_complete_cb; + + /* called once tbuf processing is done */ + utask_cb_t final_cb; + + /* used for recvmsg/sendmsg */ + struct saddr saddr; void *priv; }; diff --git a/meson.build b/meson.build index 74da814..fa9199e 100644 --- a/meson.build +++ b/meson.build @@ -18,8 +18,6 @@ mcproxy_sources = [ 'systemd.c', 'utils.c'] -executable('ctest', 'ctest.c') -executable('stest', 'stest.c') executable('mcproxy', mcproxy_sources, link_args: [ '-lanl' ], @@ -4,20 +4,20 @@ enum server_type { SERVER_TYPE_UNDEFINED, SERVER_TYPE_ANNOUNCE, - SERVER_TYPE_PROXY + SERVER_TYPE_PROXY, }; enum server_stop_method { SERVER_STOP_METHOD_UNDEFINED, SERVER_STOP_METHOD_RCON, SERVER_STOP_METHOD_SYSTEMD, - SERVER_STOP_METHOD_EXEC + SERVER_STOP_METHOD_EXEC, }; enum server_start_method { SERVER_START_METHOD_UNDEFINED, SERVER_START_METHOD_SYSTEMD, - SERVER_START_METHOD_EXEC + SERVER_START_METHOD_EXEC, }; struct server { @@ -259,7 +259,7 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) { int r; - if (!task || !task->tbuf || !task->final_callback) + if (!task || !task->tbuf || !task->final_cb) die("missing parameters"); debug(DBG_UR, "task %s (%p), fd %i, refcount %u\n", @@ -283,12 +283,11 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) return; finished: - task->final_callback(cfg, task, r); - return; + task->final_cb(cfg, task, r); } void -uring_tbuf_write(struct cfg *cfg, struct uring_task *task, callback_t callback) +uring_tbuf_write(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) { if (!task || task->fd < 0 || !task->tbuf || task->tbuf->len < 0) { error("invalid parameters\n"); @@ -299,12 +298,12 @@ uring_tbuf_write(struct cfg *cfg, struct uring_task *task, callback_t callback) task->name, task, task->fd, task->refcount); task->tbuf->done = 0; - task->final_callback = callback; + task->final_cb = final_cb; uring_write(cfg, task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb); } void -uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, callback_t callback) +uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -317,7 +316,7 @@ uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, cal task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - task->callback = callback; + task->cb = cb; io_uring_prep_write(sqe, task->fd, buf, len, 0); io_uring_sqe_set_data(sqe, task); } @@ -327,7 +326,7 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) { int r; - if (!task || !task->tbuf || !task->final_callback || !task->complete_callback) { + if (!task || !task->tbuf || !task->final_cb || !task->is_complete_cb) { error("invalid parameters\n"); return; } @@ -341,7 +340,7 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) } task->tbuf->len += res; - r = task->complete_callback(cfg, task, res); + r = task->is_complete_cb(cfg, task, res); if (r < 0) { r = res; goto finished; @@ -362,15 +361,14 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) return; finished: - task->final_callback(cfg, task, r); - return; + task->final_cb(cfg, task, r); } void uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, - rcallback_t complete, callback_t callback) + rutask_cb_t is_complete_cb, utask_cb_t final_cb) { - if (!task || task->fd < 0 || !task->tbuf || !complete) { + if (!task || task->fd < 0 || !task->tbuf || !is_complete_cb || !final_cb) { error("%s: invalid parameters\n", __func__); return; } @@ -379,9 +377,10 @@ uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, task->name, task, task->fd, task->refcount); task->tbuf->len = 0; - task->complete_callback = complete; - task->final_callback = callback; - uring_read(cfg, task, &task->tbuf->buf, sizeof(task->tbuf->buf), uring_tbuf_read_until_cb); + task->is_complete_cb = is_complete_cb; + task->final_cb = final_cb; + uring_read(cfg, task, &task->tbuf->buf, sizeof(task->tbuf->buf), + uring_tbuf_read_until_cb); } static int @@ -399,9 +398,10 @@ uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, callback_t callback) +uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, + utask_cb_t final_cb) { - uring_tbuf_read_until(cfg, task, uring_tbuf_eof, callback); + uring_tbuf_read_until(cfg, task, uring_tbuf_eof, final_cb); } static int @@ -414,13 +414,13 @@ uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read(struct cfg *cfg, struct uring_task *task, callback_t callback) +uring_tbuf_read(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) { - uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, callback); + uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, final_cb); } void -uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, callback_t callback) +uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -433,13 +433,13 @@ uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t le task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - task->callback = callback; + task->cb = cb; io_uring_prep_read(sqe, task->fd, buf, len, offset); io_uring_sqe_set_data(sqe, task); } void -uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callback_t callback) +uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -447,13 +447,13 @@ uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callbac task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - task->callback = callback; + task->cb = cb; io_uring_prep_openat(sqe, AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0); io_uring_sqe_set_data(sqe, task); } void -uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t callback) +uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -470,13 +470,13 @@ uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t callback task->tbuf->len = 0; task->tbuf->iov.iov_len = sizeof(task->tbuf->buf); task->tbuf->msg.msg_namelen = task->saddr.addrlen; - task->callback = callback; + task->cb = cb; io_uring_prep_recvmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); } void -uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback) +uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -492,13 +492,13 @@ uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback task->tbuf->done = 0; task->tbuf->iov.iov_len = task->tbuf->len; task->tbuf->msg.msg_namelen = task->saddr.addrlen; - task->callback = callback; + task->cb = cb; io_uring_prep_sendmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); } void -uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, callback_t callback) +uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -511,13 +511,13 @@ uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, cal task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - task->callback = callback; + task->cb = cb; io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->storage, saddr->addrlen); io_uring_sqe_set_data(sqe, task); } void -uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, callback_t callback) +uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -531,13 +531,13 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, call sqe = get_sqe(cfg, task); saddr->addrlen = sizeof(saddr->storage); - task->callback = callback; + task->cb = cb; io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->storage, &saddr->addrlen, SOCK_CLOEXEC); io_uring_sqe_set_data(sqe, task); } void -uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t callback) +uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, utask_cb_t cb) { struct io_uring_sqe *sqe; @@ -550,7 +550,7 @@ uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - task->callback = callback; + task->cb = cb; io_uring_prep_poll_add(sqe, task->fd, poll_mask); io_uring_sqe_set_data(sqe, task); } @@ -636,7 +636,7 @@ uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, task->name ? task->name : "<none>", task, task->fd, - task->callback); + task->cb); } void @@ -659,7 +659,7 @@ uring_event_loop(struct cfg *cfg) nr = 0; io_uring_for_each_cqe(&cfg->uev->uring, head, cqe) { struct uring_task *task = io_uring_cqe_get_data(cqe); - bool do_callback; + bool do_cb; enum cqe_type cqe_type; cqe_count++; @@ -673,30 +673,30 @@ uring_event_loop(struct cfg *cfg) switch (cqe_type) { case CQE_TYPE_CANCEL: uring_print_cqe(cfg, "cancel", task, cqe); - do_callback = false; + do_cb = false; break; case CQE_TYPE_CLOSE: uring_print_cqe(cfg, "close", task, cqe); - do_callback = false; + do_cb = false; break; case CQE_TYPE_POLL_CANCEL: uring_print_cqe(cfg, "poll_cancel", task, cqe); - do_callback = false; + do_cb = false; break; case CQE_TYPE_NORMAL: uring_print_cqe(cfg, "standard", task, cqe); - do_callback = true; + do_cb = true; break; default: die("unknown CQE type"); } - if (do_callback && task->callback) - task->callback(cfg, task, cqe->res); + if (do_cb && task->cb) + task->cb(cfg, task, cqe->res); uring_task_put(cfg, task); @@ -26,43 +26,46 @@ void uring_task_init(struct uring_task *task, const char *name, void uring_close(struct cfg *cfg, struct uring_task *task, int fd); -void uring_tbuf_write(struct cfg *cfg, struct uring_task *task, callback_t callback); +void uring_tbuf_write(struct cfg *cfg, struct uring_task *task, + utask_cb_t final_cb); void uring_write(struct cfg *cfg, struct uring_task *task, void *buf, - size_t len, callback_t callback); + size_t len, utask_cb_t cb); void uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, - rcallback_t complete, callback_t callback); + rutask_cb_t is_complete_cb, utask_cb_t final_cb); void uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, - callback_t callback); + utask_cb_t final_cb); -void uring_tbuf_read(struct cfg *cfg, struct uring_task *task, callback_t callback); +void uring_tbuf_read(struct cfg *cfg, struct uring_task *task, + utask_cb_t final_cb); void uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, - size_t len, off_t offset, callback_t callback); + size_t len, off_t offset, utask_cb_t cb); static inline void -uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, callback_t callback) +uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, + utask_cb_t cb) { - uring_read_offset(cfg, task, buf, len, 0, callback); + uring_read_offset(cfg, task, buf, len, 0, cb); } void uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, - callback_t callback); + utask_cb_t cb); -void uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t callback); +void uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb); -void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback); +void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb); void uring_connect(struct cfg *cfg, struct uring_task *task, - struct saddr *saddr, callback_t callback); + struct saddr *saddr, utask_cb_t cb); void uring_accept(struct cfg *cfg, struct uring_task *task, - struct saddr *saddr, callback_t callback); + struct saddr *saddr, utask_cb_t cb); void uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, - callback_t callback); + utask_cb_t cb); void uring_poll_cancel(struct cfg *cfg, struct uring_task *task); |