diff options
-rw-r--r-- | src/server.c | 4 | ||||
-rw-r--r-- | src/server.h | 2 | ||||
-rw-r--r-- | src/tcp_server.c | 33 | ||||
-rw-r--r-- | src/tcp_server.h | 8 |
4 files changed, 33 insertions, 14 deletions
diff --git a/src/server.c b/src/server.c index a158b81..933a3b7 100644 --- a/src/server.c +++ b/src/server.c @@ -74,7 +74,7 @@ void server_destroy(struct server *server) log("Shutting down\n"); ci_queue_destroy(&server->ci_queue); - tcp_server_destroy(&server->tcp_server); + tcp_server_destroy(server->tcp_server); storage_destroy(&server->storage); pthread_errno_if(pthread_cond_destroy(&server->server_cv), "pthread_cond_destroy"); pthread_errno_if(pthread_mutex_destroy(&server->server_mtx), "pthread_mutex_destroy"); @@ -363,7 +363,7 @@ int server_main(struct server *server) while (!global_stop_flag) { log("Waiting for new connections\n"); - ret = tcp_server_accept(&server->tcp_server, server_conn_handler, server); + ret = tcp_server_accept(server->tcp_server, server_conn_handler, server); if (ret < 0) break; } diff --git a/src/server.h b/src/server.h index 16dfb5c..0e5c030 100644 --- a/src/server.h +++ b/src/server.h @@ -28,7 +28,7 @@ struct server { struct storage storage; - struct tcp_server tcp_server; + struct tcp_server *tcp_server; struct ci_queue ci_queue; }; diff --git a/src/tcp_server.c b/src/tcp_server.c index 87613a5..acd3320 100644 --- a/src/tcp_server.c +++ b/src/tcp_server.c @@ -15,18 +15,39 @@ #include <stdlib.h> #include <unistd.h> -int tcp_server_create(struct tcp_server *server, const char *port) +struct tcp_server { + int fd; +}; + +int tcp_server_create(struct tcp_server **_server, const char *port) { - server->fd = net_bind(port); - if (server->fd < 0) - return server->fd; + struct tcp_server *server; + int ret = 0; + + *_server = malloc(sizeof(struct tcp_server)); + if (!*_server) { + log_errno("malloc"); + return -1; + } + server = *_server; - return 0; + ret = net_bind(port); + if (ret < 0) + goto free; + server->fd = ret; + + return ret; + +free: + free(server); + + return ret; } -void tcp_server_destroy(const struct tcp_server *server) +void tcp_server_destroy(struct tcp_server *server) { log_errno_if(close(server->fd), "close"); + free(server); } struct child_context { diff --git a/src/tcp_server.h b/src/tcp_server.h index 55fb1fa..0f377d8 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -8,12 +8,10 @@ #ifndef __TCP_SERVER_H__ #define __TCP_SERVER_H__ -struct tcp_server { - int fd; -}; +struct tcp_server; -int tcp_server_create(struct tcp_server *, const char *port); -void tcp_server_destroy(const struct tcp_server *); +int tcp_server_create(struct tcp_server **, const char *port); +void tcp_server_destroy(struct tcp_server *); typedef int (*tcp_server_conn_handler)(int conn_fd, void *arg); int tcp_server_accept(const struct tcp_server *, tcp_server_conn_handler, void *arg); |