diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-16 10:23:30 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-16 10:23:30 +0200 |
commit | e5d833027f7fcdef4d77d26b31b5907873331d0e (patch) | |
tree | 5526ba7c848000691b98fb4a773b23f58a1a8a7e | |
parent | bbe3648e2108393f3132ede777c19e00a9d5a547 (diff) |
Add msghdr and iovec to task buffers
-rw-r--r-- | announce.c | 13 | ||||
-rw-r--r-- | main.h | 2 | ||||
-rw-r--r-- | server.c | 5 | ||||
-rw-r--r-- | server.h | 4 | ||||
-rw-r--r-- | uring.c | 22 | ||||
-rw-r--r-- | uring.h | 3 |
6 files changed, 30 insertions, 19 deletions
@@ -39,20 +39,18 @@ mcast_send(struct cfg *cfg, struct announce *aev, struct server *server) if (!server || !server->pretty_name || server->announce_port < 1) return; - len = snprintf(server->mcast_buf, sizeof(server->mcast_buf), + len = snprintf(server->mcast_buf.buf, sizeof(server->mcast_buf.buf), "[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]", server->pretty_name, server->announce_port); - if (len < 1 || len >= sizeof(server->mcast_buf)) { + if (len < 1 || len >= sizeof(server->mcast_buf.buf)) { error("%s: snprintf returned %i\n", __func__, len); return; } - server->mcast_msg.msg_name = &aev->mcast_task.addr; - server->mcast_msg.msg_namelen = sizeof(aev->mcast_task.addr); - server->mcast_iov.iov_len = len; - - uring_sendmsg(cfg, &aev->mcast_task, &server->mcast_msg, mcast_sent); + server->mcast_buf.len = len; + uring_task_set_buf(&aev->mcast_task, &server->mcast_buf); + uring_tbuf_sendmsg(cfg, &aev->mcast_task, mcast_sent); } static void @@ -182,6 +180,7 @@ announce_init(struct cfg *cfg) aev->mcast_task.addr.in4.sin_family = AF_INET; aev->mcast_task.addr.in4.sin_addr.s_addr = inet_addr("224.0.2.60"); aev->mcast_task.addr.in4.sin_port = htons(4445); + aev->mcast_task.addr.addrlen = sizeof(aev->mcast_task.addr.in4); cfg->aev = aev; uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb); @@ -31,6 +31,8 @@ struct uring_task_buf { char buf[4096]; size_t len; size_t done; + struct iovec iov; + struct msghdr msg; }; struct uring_task { @@ -741,11 +741,6 @@ server_new(struct cfg *cfg, const char *name) list_init(&scfg->locals); list_init(&scfg->proxys); list_init(&scfg->rcons); - memset(&scfg->mcast_iov, 0, sizeof(scfg->mcast_iov)); - scfg->mcast_iov.iov_base = scfg->mcast_buf; - memset(&scfg->mcast_msg, 0, sizeof(scfg->mcast_msg)); - scfg->mcast_msg.msg_iov = &scfg->mcast_iov; - scfg->mcast_msg.msg_iovlen = 1; scfg->idle_timeout = 0; list_add(&scfg->list, &cfg->servers); @@ -48,9 +48,7 @@ struct server { char *rcon_password; /* For announce messages */ - struct iovec mcast_iov; - struct msghdr mcast_msg; - char mcast_buf[4096]; + struct uring_task_buf mcast_buf; /* For checking idle status */ struct idle *idle; @@ -159,6 +159,16 @@ uring_task_get(struct cfg *cfg, struct uring_task *task) void uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf) { + if (tbuf) { + /* iov_len and msg_namelen are set at send/receive time */ + tbuf->iov.iov_base = tbuf->buf; + tbuf->msg.msg_name = &task->addr.storage; + tbuf->msg.msg_iov = &tbuf->iov; + tbuf->msg.msg_iovlen = 1; + tbuf->msg.msg_control = NULL; + tbuf->msg.msg_controllen = 0; + tbuf->msg.msg_flags = 0; + } task->tbuf = tbuf; } @@ -419,13 +429,21 @@ uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callbac } void -uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, callback_t callback) +uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback) { struct io_uring_sqe *sqe; + if (!task->tbuf) { + error("%s: called with no tbuf set\n", __func__); + return; + } + sqe = get_sqe(cfg, task); + task->tbuf->done = 0; + task->tbuf->iov.iov_len = task->tbuf->len; + task->tbuf->msg.msg_namelen = task->addr.addrlen; task->callback = callback; - io_uring_prep_sendmsg(sqe, task->fd, msg, 0); + io_uring_prep_sendmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); } @@ -51,8 +51,7 @@ uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, call void uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callback_t callback); -void uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, - callback_t callback); +void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback); void uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback); |