diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 10:05:10 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 10:05:10 +0200 |
commit | 2288866ba616148d6d3dbe74ee8f269ba9132149 (patch) | |
tree | 8c59ae13647d08042a249175c482c03bef9b69e4 /src/client.c | |
parent | make struct worker opaque (diff) | |
download | cimple-2288866ba616148d6d3dbe74ee8f269ba9132149.tar.gz cimple-2288866ba616148d6d3dbe74ee8f269ba9132149.zip |
make struct client opaque
Diffstat (limited to 'src/client.c')
-rw-r--r-- | src/client.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/client.c b/src/client.c index 7c47ae6..739c612 100644 --- a/src/client.c +++ b/src/client.c @@ -12,18 +12,39 @@ #include <unistd.h> -int client_create(struct client *client, const struct settings *settings) +struct client { + int fd; +}; + +int client_create(struct client **_client, const struct settings *settings) { - client->fd = net_connect(settings->host, settings->port); - if (client->fd < 0) - return client->fd; + struct client *client; + int ret = 0; + + *_client = malloc(sizeof(struct client)); + if (!*_client) { + log_errno("malloc"); + return -1; + } + client = *_client; + + ret = net_connect(settings->host, settings->port); + if (ret < 0) + goto free; + client->fd = ret; + + return ret; - return 0; +free: + free(client); + + return ret; } -void client_destroy(const struct client *client) +void client_destroy(struct client *client) { log_errno_if(close(client->fd), "close"); + free(client); } int client_main(const struct client *client, int argc, char *argv[]) |