From b5980c321f83dd6cb57302becd72642b462fb68f Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 25 Aug 2022 11:31:34 +0200 Subject: msg: refactoring --- src/client.c | 2 +- src/msg.c | 18 +++++++++--------- src/msg.h | 11 +++++------ src/server.c | 4 ++-- 4 files changed, 17 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index 972ebbd..e42019c 100644 --- a/src/client.c +++ b/src/client.c @@ -23,7 +23,7 @@ int client_main(const struct client *client, int argc, char *argv[]) int result, ret = 0; struct msg msg = {argc, argv}; - ret = msg_send_and_wait_for_result(client->fd, &msg, &result); + ret = msg_send_and_wait(client->fd, &msg, &result); if (ret < 0) return ret; diff --git a/src/msg.c b/src/msg.c index fc0b93c..9262f70 100644 --- a/src/msg.c +++ b/src/msg.c @@ -5,6 +5,13 @@ #include #include +void msg_free(const struct msg *msg) +{ + for (int i = 0; i < msg->argc; ++i) + free(msg->argv[i]); + free(msg->argv); +} + int msg_from_argv(struct msg *msg, const char *argv[]) { int argc = 0; @@ -114,7 +121,7 @@ free_buf: return ret; } -int msg_send_and_wait_for_result(int fd, const struct msg *msg, int *result) +int msg_send_and_wait(int fd, const struct msg *msg, int *result) { int ret = 0; @@ -162,7 +169,7 @@ free_buf: return ret; } -int msg_recv_and_send_result(int fd, msg_handler handler, void *arg) +int msg_recv_and_handle(int fd, msg_handler handler, void *arg) { struct msg msg; int result; @@ -184,13 +191,6 @@ free_msg: return ret; } -void msg_free(const struct msg *msg) -{ - for (int i = 0; i < msg->argc; ++i) - free(msg->argv[i]); - free(msg->argv); -} - int msg_dump_unknown(const struct msg *msg) { print_log("Received an unknown message:\n"); diff --git a/src/msg.h b/src/msg.h index fca230e..03e5f89 100644 --- a/src/msg.h +++ b/src/msg.h @@ -6,17 +6,16 @@ struct msg { char **argv; }; +void msg_free(const struct msg *); + int msg_from_argv(struct msg *, const char *argv[]); +int msg_recv(int fd, struct msg *); int msg_send(int fd, const struct msg *); -int msg_send_and_wait_for_result(int fd, const struct msg *, int *result); typedef int (*msg_handler)(const struct msg *, void *arg); - -int msg_recv(int fd, struct msg *); -int msg_recv_and_send_result(int fd, msg_handler, void *arg); - -void msg_free(const struct msg *); +int msg_send_and_wait(int fd, const struct msg *, int *result); +int msg_recv_and_handle(int fd, msg_handler, void *arg); int msg_dump_unknown(const struct msg *); diff --git a/src/server.c b/src/server.c index 00789d5..3e9f080 100644 --- a/src/server.c +++ b/src/server.c @@ -22,14 +22,14 @@ void server_destroy(const struct server *server) tcp_server_destroy(&server->tcp_server); } -static int msg_handle(const struct msg *msg, void *) +static int server_msg_handler(const struct msg *msg, void *) { return msg_dump_unknown(msg); } static int server_conn_handler(int fd, void *) { - return msg_recv_and_send_result(fd, msg_handle, NULL); + return msg_recv_and_handle(fd, server_msg_handler, NULL); } int server_main(const struct server *server) -- cgit v1.2.3