diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2022-01-21 14:29:32 +0100 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2022-01-21 15:18:47 +0100 |
commit | 20301007a15deede9995da5e6cab3d18ecccedf8 (patch) | |
tree | 57816db0ef654fff8813fcf85d2b9134cc3d08f8 | |
parent | b8b3839bba75a96acc852d83b02434d921b0e065 (diff) |
#915 release existing client on reconnections
Trasient network problems might result in duplicated clients, drop
client object if it already exists before creating a new one.
-rw-r--r-- | src/core.c | 19 | ||||
-rw-r--r-- | src/rest.c | 24 | ||||
-rw-r--r-- | src/rest.h | 1 |
3 files changed, 32 insertions, 12 deletions
@@ -323,10 +323,16 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) struct og_client_wol *cli_wol; struct og_client *cli; int client_sd; + bool agent; if (events & EV_ERROR) return; + if (io->fd == socket_agent_rest) + agent = true; + else + agent = false; + client_sd = accept(io->fd, (struct sockaddr *)&client_addr, &addrlen); if (client_sd < 0) { syslog(LOG_ERR, "cannot accept client connection\n"); @@ -342,6 +348,12 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) if (cli_wol) og_client_wol_destroy(cli_wol); + if (agent) { + cli = __og_client_find(&client_addr.sin_addr); + if (cli) + og_client_release(loop, cli); + } + cli = (struct og_client *)calloc(1, sizeof(struct og_client)); if (!cli) { close(client_sd); @@ -349,7 +361,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) } memcpy(&cli->addr, &client_addr, sizeof(client_addr)); - if (io->fd == socket_agent_rest) { + if (agent) { cli->agent = true; ev_io_init(&cli->io, og_agent_read_cb, client_sd, EV_READ); } else { @@ -358,7 +370,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) ev_io_start(loop, &cli->io); ev_init(&cli->timer, og_client_timer_cb); - if (io->fd == socket_agent_rest) + if (agent) cli->timer.repeat = OG_AGENT_CLIENT_TIMEOUT; else cli->timer.repeat = OG_CLIENT_TIMEOUT; @@ -366,9 +378,8 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) ev_timer_again(loop, &cli->timer); og_client_add(cli); - if (io->fd == socket_agent_rest) { + if (agent) og_agent_send_refresh(cli); - } } int og_socket_server_init(const char *port) @@ -86,9 +86,23 @@ void og_client_add(struct og_client *cli) list_add(&cli->list, &client_list); } -static struct og_client *og_client_find(const char *ip) +struct og_client *__og_client_find(const struct in_addr *addr) { struct og_client *client; + + list_for_each_entry(client, &client_list, list) { + if (!client->agent) + continue; + + if (client->addr.sin_addr.s_addr == addr->s_addr) + return client; + } + + return NULL; +} + +static struct og_client *og_client_find(const char *ip) +{ struct in_addr addr; int res; @@ -98,13 +112,7 @@ static struct og_client *og_client_find(const char *ip) return NULL; } - list_for_each_entry(client, &client_list, list) { - if (client->addr.sin_addr.s_addr == addr.s_addr && client->agent) { - return client; - } - } - - return NULL; + return __og_client_find(&addr); } static const char *og_client_status(const struct og_client *cli) @@ -65,6 +65,7 @@ struct og_client { }; void og_client_add(struct og_client *cli); +struct og_client *__og_client_find(const struct in_addr *addr); static inline int og_client_socket(const struct og_client *cli) { |