From 1ce4acb85a3bcb953c0f9cc91d2edc855969948b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 22 Aug 2022 13:40:27 +0200 Subject: add some code A basic client-server app, the client sends commands as an array of strings. Hopefully I didn't mess up, and hopefully it'll be useful. --- src/server.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/server.c (limited to 'src/server.c') diff --git a/src/server.c b/src/server.c new file mode 100644 index 0000000..91f3924 --- /dev/null +++ b/src/server.c @@ -0,0 +1,48 @@ +#include "server.h" +#include "cmd.h" +#include "net.h" + +#include +#include + +int server_create(struct server *server, const struct settings *settings) +{ + server->fd = bind_to_port(settings->port); + if (server->fd < 0) + return server->fd; + + return 0; +} + +void server_destroy(const struct server *server) +{ + close(server->fd); +} + +static int cmd_handle(const struct cmd *cmd, void *) +{ + for (int i = 0; i < cmd->argc; ++i) + printf("cmd[%d]: %s\n", i, cmd->argv[i]); + return 0; +} + +static int server_handle(int fd, void *) +{ + return cmd_recv_and_send_result(fd, cmd_handle, NULL); +} + +static int server_accept(const struct server *server) +{ + return accept_connection(server->fd, server_handle, NULL); +} + +int server_main(const struct server *server) +{ + int ret = 0; + + while (1) { + ret = server_accept(server); + if (ret < 0) + return ret; + } +} -- cgit v1.2.3