diff options
-rw-r--r-- | src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/client.c | 6 | ||||
-rw-r--r-- | src/cmd.h | 21 | ||||
-rw-r--r-- | src/msg.c (renamed from src/cmd.c) | 64 | ||||
-rw-r--r-- | src/msg.h | 21 | ||||
-rw-r--r-- | src/server.c | 10 |
6 files changed, 63 insertions, 63 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 61fdfa6..dcf1ba1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,5 +12,5 @@ add_compile_definitions(_GNU_SOURCE) add_compile_definitions(VERSION="${PROJECT_VERSION}") -add_executable(server server_main.c server.c cmd.c net.c) -add_executable(client client_main.c client.c cmd.c net.c) +add_executable(server server_main.c server.c msg.c net.c) +add_executable(client client_main.c client.c msg.c net.c) diff --git a/src/client.c b/src/client.c index 644cb13..0391784 100644 --- a/src/client.c +++ b/src/client.c @@ -1,5 +1,5 @@ #include "client.h" -#include "cmd.h" +#include "msg.h" #include "net.h" #include <unistd.h> @@ -21,9 +21,9 @@ void client_destroy(const struct client *client) int client_main(const struct client *client, int argc, char *argv[]) { int result, ret = 0; - struct cmd cmd = {argc, argv}; + struct msg msg = {argc, argv}; - ret = cmd_send_and_wait_for_result(client->fd, &cmd, &result); + ret = msg_send_and_wait_for_result(client->fd, &msg, &result); if (ret < 0) return ret; diff --git a/src/cmd.h b/src/cmd.h deleted file mode 100644 index 9f519f6..0000000 --- a/src/cmd.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __CMD_H__ -#define __CMD_H__ - -struct cmd { - int argc; - char **argv; -}; - -int cmd_from_argv(struct cmd *, const char *argv[]); - -int cmd_send(int fd, const struct cmd *); -int cmd_send_and_wait_for_result(int fd, const struct cmd *, int *result); - -typedef int (*cmd_handler)(const struct cmd *, void *arg); - -int cmd_recv(int fd, struct cmd *); -int cmd_recv_and_send_result(int fd, cmd_handler, void *arg); - -void cmd_free(const struct cmd *); - -#endif @@ -1,28 +1,28 @@ -#include "cmd.h" +#include "msg.h" #include "log.h" #include "net.h" #include <stdlib.h> #include <string.h> -int cmd_from_argv(struct cmd *cmd, const char *argv[]) +int msg_from_argv(struct msg *msg, const char *argv[]) { int argc = 0; for (const char **s = argv; *s; ++s) ++argc; - cmd->argc = argc; - cmd->argv = calloc(argc, sizeof(char *)); + msg->argc = argc; + msg->argv = calloc(argc, sizeof(char *)); - if (!cmd->argv) { + if (!msg->argv) { print_errno("calloc"); return -1; } for (int i = 0; i < argc; ++i) { - cmd->argv[i] = strdup(argv[i]); - if (!cmd->argv[i]) { + msg->argv[i] = strdup(argv[i]); + if (!msg->argv[i]) { print_errno("strdup"); goto free; } @@ -32,12 +32,12 @@ int cmd_from_argv(struct cmd *cmd, const char *argv[]) free: for (int i = 0; i < argc; ++i) - if (cmd->argv[i]) - free(cmd->argv[i]); + if (msg->argv[i]) + free(msg->argv[i]); else break; - free(cmd->argv); + free(msg->argv); return -1; } @@ -92,17 +92,17 @@ free: return -1; } -int cmd_send(int fd, const struct cmd *cmd) +int msg_send(int fd, const struct msg *msg) { int ret = 0; - size_t len = calc_buf_len(cmd->argc, cmd->argv); + size_t len = calc_buf_len(msg->argc, msg->argv); char *buf = malloc(len); if (!buf) { print_errno("malloc"); return -1; } - arr_pack(buf, cmd->argc, cmd->argv); + arr_pack(buf, msg->argc, msg->argv); ret = send_buf(fd, buf, len); if (ret < 0) @@ -114,11 +114,11 @@ free_buf: return ret; } -int cmd_send_and_wait_for_result(int fd, const struct cmd *cmd, int *result) +int msg_send_and_wait_for_result(int fd, const struct msg *msg, int *result) { int ret = 0; - ret = cmd_send(fd, cmd); + ret = msg_send(fd, msg); if (ret < 0) return ret; @@ -129,7 +129,7 @@ int cmd_send_and_wait_for_result(int fd, const struct cmd *cmd, int *result) return ret; } -int cmd_recv(int fd, struct cmd *cmd) +int msg_recv(int fd, struct msg *msg) { void *buf; size_t len; @@ -139,22 +139,22 @@ int cmd_recv(int fd, struct cmd *cmd) if (ret < 0) return ret; - cmd->argc = calc_arr_len(buf, len); - cmd->argv = calloc(cmd->argc, sizeof(char *)); - if (!cmd->argv) { + msg->argc = calc_arr_len(buf, len); + msg->argv = calloc(msg->argc, sizeof(char *)); + if (!msg->argv) { print_errno("calloc"); ret = -1; goto free_buf; } - ret = arr_unpack(cmd->argv, cmd->argc, buf); + ret = arr_unpack(msg->argv, msg->argc, buf); if (ret < 0) goto free_argv; goto free_buf; free_argv: - free(cmd->argv); + free(msg->argv); free_buf: free(buf); @@ -162,31 +162,31 @@ free_buf: return ret; } -int cmd_recv_and_send_result(int fd, cmd_handler handler, void *arg) +int msg_recv_and_send_result(int fd, msg_handler handler, void *arg) { - struct cmd cmd; + struct msg msg; int result; int ret = 0; - ret = cmd_recv(fd, &cmd); + ret = msg_recv(fd, &msg); if (ret < 0) return ret; - result = handler(&cmd, arg); + result = handler(&msg, arg); ret = send_buf(fd, &result, sizeof(result)); if (ret < 0) - goto free_cmd; + goto free_msg; -free_cmd: - cmd_free(&cmd); +free_msg: + msg_free(&msg); return ret; } -void cmd_free(const struct cmd *cmd) +void msg_free(const struct msg *msg) { - for (int i = 0; i < cmd->argc; ++i) - free(cmd->argv[i]); - free(cmd->argv); + for (int i = 0; i < msg->argc; ++i) + free(msg->argv[i]); + free(msg->argv); } diff --git a/src/msg.h b/src/msg.h new file mode 100644 index 0000000..028aaa0 --- /dev/null +++ b/src/msg.h @@ -0,0 +1,21 @@ +#ifndef __MSG_H__ +#define __MSG_H__ + +struct msg { + int argc; + char **argv; +}; + +int msg_from_argv(struct msg *, const char *argv[]); + +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 *); + +#endif diff --git a/src/server.c b/src/server.c index 91f3924..035adbe 100644 --- a/src/server.c +++ b/src/server.c @@ -1,5 +1,5 @@ #include "server.h" -#include "cmd.h" +#include "msg.h" #include "net.h" #include <stdio.h> @@ -19,16 +19,16 @@ void server_destroy(const struct server *server) close(server->fd); } -static int cmd_handle(const struct cmd *cmd, void *) +static int msg_handle(const struct msg *msg, void *) { - for (int i = 0; i < cmd->argc; ++i) - printf("cmd[%d]: %s\n", i, cmd->argv[i]); + for (int i = 0; i < msg->argc; ++i) + printf("msg[%d]: %s\n", i, msg->argv[i]); return 0; } static int server_handle(int fd, void *) { - return cmd_recv_and_send_result(fd, cmd_handle, NULL); + return msg_recv_and_send_result(fd, msg_handle, NULL); } static int server_accept(const struct server *server) |