diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-14 18:02:25 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-14 18:08:24 +0200 |
commit | b09122e98c0156db0da4eac6d8819f1c41ecd9e1 (patch) | |
tree | 672531e000990214fc692c37d71e1ad82c8584f9 | |
parent | msg: enforce at least one word (diff) | |
download | cimple-b09122e98c0156db0da4eac6d8819f1c41ecd9e1.tar.gz cimple-b09122e98c0156db0da4eac6d8819f1c41ecd9e1.zip |
msg: add functions for one-off communication
Diffstat (limited to '')
-rw-r--r-- | src/client.c | 22 | ||||
-rw-r--r-- | src/client.h | 4 | ||||
-rw-r--r-- | src/client_main.c | 4 | ||||
-rw-r--r-- | src/msg.c | 59 | ||||
-rw-r--r-- | src/msg.h | 4 | ||||
-rw-r--r-- | src/server.c | 2 |
6 files changed, 58 insertions, 37 deletions
diff --git a/src/client.c b/src/client.c index f0c0d3b..8cfb1d1 100644 --- a/src/client.c +++ b/src/client.c @@ -6,18 +6,18 @@ */ #include "client.h" +#include "compiler.h" #include "log.h" #include "msg.h" #include "net.h" #include <stdlib.h> -#include <unistd.h> struct client { - int fd; + int dummy; }; -int client_create(struct client **_client, const struct settings *settings) +int client_create(struct client **_client) { int ret = 0; @@ -27,27 +27,17 @@ int client_create(struct client **_client, const struct settings *settings) return -1; } - ret = net_connect(settings->host, settings->port); - if (ret < 0) - goto free; - client->fd = ret; - *_client = client; return ret; - -free: - free(client); - - return ret; } void client_destroy(struct client *client) { - log_errno_if(close(client->fd), "close"); free(client); } -int client_main(const struct client *client, const char **argv) +int client_main(UNUSED const struct client *client, const struct settings *settings, + const char **argv) { struct msg *request = NULL, *response = NULL; int ret = 0; @@ -56,7 +46,7 @@ int client_main(const struct client *client, const char **argv) if (ret < 0) return ret; - ret = msg_send_and_wait(client->fd, request, &response); + ret = msg_connect_and_communicate(settings->host, settings->port, request, &response); if (ret < 0) goto free_request; diff --git a/src/client.h b/src/client.h index 5bc3f5c..471a7be 100644 --- a/src/client.h +++ b/src/client.h @@ -15,9 +15,9 @@ struct settings { struct client; -int client_create(struct client **, const struct settings *); +int client_create(struct client **); void client_destroy(struct client *); -int client_main(const struct client *, const char **argv); +int client_main(const struct client *, const struct settings *, const char **argv); #endif diff --git a/src/client_main.c b/src/client_main.c index 160bb7e..287a5cb 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -70,11 +70,11 @@ int main(int argc, char *argv[]) if (ret < 0) return ret; - ret = client_create(&client, &settings); + ret = client_create(&client); if (ret < 0) return ret; - ret = client_main(client, (const char **)argv + optind); + ret = client_main(client, &settings, (const char **)argv + optind); if (ret < 0) goto destroy_client; @@ -12,6 +12,7 @@ #include <stdint.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> struct msg { size_t argc; @@ -169,21 +170,6 @@ destroy_buf: return ret; } -int msg_send_and_wait(int fd, const struct msg *request, struct msg **response) -{ - int ret = 0; - - ret = msg_send(fd, request); - if (ret < 0) - return ret; - - ret = msg_recv(fd, response); - if (ret < 0) - return ret; - - return ret; -} - int msg_recv(int fd, struct msg **_msg) { struct buf *buf = NULL; @@ -221,6 +207,49 @@ destroy_buf: return ret; } +int msg_communicate(int fd, const struct msg *request, struct msg **response) +{ + int ret = 0; + + if (!request && !response) { + log_err("For communication, there must be at least a request and/or response\n"); + return -1; + } + + if (request) { + ret = msg_send(fd, request); + if (ret < 0) + return ret; + } + + if (response) { + ret = msg_recv(fd, response); + if (ret < 0) + return ret; + } + + return ret; +} + +int msg_connect_and_communicate(const char *host, const char *port, const struct msg *request, + struct msg **response) +{ + int fd = -1, ret = 0; + + fd = net_connect(host, port); + if (fd < 0) + return fd; + + ret = msg_communicate(fd, request, response); + if (ret < 0) + goto close; + +close: + log_errno_if(close(fd), "close"); + + return ret; +} + void msg_dump(const struct msg *msg) { log("Message[%zu]:\n", msg->argc); @@ -30,7 +30,9 @@ int msg_is_error(const struct msg *); int msg_recv(int fd, struct msg **); int msg_send(int fd, const struct msg *); -int msg_send_and_wait(int fd, const struct msg *, struct msg **response); +int msg_communicate(int fd, const struct msg *, struct msg **response); +int msg_connect_and_communicate(const char *host, const char *port, const struct msg *, + struct msg **); void msg_dump(const struct msg *); diff --git a/src/server.c b/src/server.c index 04a6847..9d4e872 100644 --- a/src/server.c +++ b/src/server.c @@ -120,7 +120,7 @@ static int worker_ci_run(int fd, const struct run_queue_entry *ci_run) if (ret < 0) return ret; - ret = msg_send_and_wait(fd, request, &response); + ret = msg_communicate(fd, request, &response); msg_free(request); if (ret < 0) return ret; |