improve: ping to see if idle client still alive
All checks were successful
gitea-sync/antd-wvnc-plugin/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-07-21 23:02:12 +02:00
parent bff0fb40c6
commit 604aab8f0b

21
wvnc.c
View File

@ -10,6 +10,8 @@
#include <antd/ws.h>
#include <sys/time.h>
#define PING_INTERVAL 10u // 10s
#define get_user_data(x) ((wvnc_user_data_t *)x)
/*
Vnc to web socket using the
@ -241,6 +243,16 @@ void *process(void *data, int wait)
}
//buff = NULL;
}
else if (h->opcode == WS_PONG)
{
buff = (uint8_t *)malloc(h->plen + 1);
if (buff)
{
ws_read_data(user_data->wscl->client, h, h->plen, buff);
LOG("Receive pong message from client: %s. Client Alive", buff);
free(buff);
}
}
else
{
vnc_fatal(user_data, "Unknow opcode");
@ -537,6 +549,15 @@ void waitfor(void *data)
timeout.tv_usec = 500;
while (user_data->status != DISCONNECTED)
{
// check whether we need to send ping message to client
if (difftime(time(NULL), user_data->wscl->client->last_io) > (double)PING_INTERVAL)
{
if (ws_ping(user_data->wscl->client, "WVNC", 0) != 0)
{
ERROR("Unable to ping client, close the connection: %d", user_data->wscl->client->sock);
return;
}
}
FD_ZERO(&fd_in);
FD_SET(user_data->wscl->client->sock, &fd_in);
int rc = select(user_data->wscl->client->sock + 1, &fd_in, NULL, NULL, &timeout);