diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-25 10:47:06 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-25 10:56:03 +0200 |
commit | 08ee3cab641f367b642bc29f6540de6be85226bd (patch) | |
tree | 887f517cbec643ab90c5647d76c26e7fb024b8cc /src/server.c | |
parent | msg: add msg_dump_unknown (diff) | |
download | cimple-08ee3cab641f367b642bc29f6540de6be85226bd.tar.gz cimple-08ee3cab641f367b642bc29f6540de6be85226bd.zip |
net: rework API
First, rename all API functions so that they start with net_.
Second, abstract the basic TCP server functionality into tcp_server.c.
This includes reworking net_accept so that it's a simple blocking
operation, and putting the callback stuff to tcp_server.c. Also, the
server now uses detached threads instead of fork(), since I want
connection handlers to share memory.
Diffstat (limited to 'src/server.c')
-rw-r--r-- | src/server.c | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/server.c b/src/server.c index 9604bd6..00789d5 100644 --- a/src/server.c +++ b/src/server.c @@ -1,22 +1,25 @@ #include "server.h" #include "msg.h" #include "net.h" +#include "tcp_server.h" #include <stdio.h> #include <unistd.h> int server_create(struct server *server, const struct settings *settings) { - server->fd = bind_to_port(settings->port); - if (server->fd < 0) - return server->fd; + int ret = 0; + + ret = tcp_server_create(&server->tcp_server, settings->port); + if (ret < 0) + return ret; return 0; } void server_destroy(const struct server *server) { - close(server->fd); + tcp_server_destroy(&server->tcp_server); } static int msg_handle(const struct msg *msg, void *) @@ -24,22 +27,17 @@ static int msg_handle(const struct msg *msg, void *) return msg_dump_unknown(msg); } -static int server_handle(int fd, void *) +static int server_conn_handler(int fd, void *) { return msg_recv_and_send_result(fd, msg_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); + ret = tcp_server_accept(&server->tcp_server, server_conn_handler, NULL); if (ret < 0) return ret; } |