aboutsummaryrefslogtreecommitdiffstats
path: root/net/ws.c
diff options
context:
space:
mode:
authorturret <turret@duck.com>2024-01-17 19:21:54 +0000
committerturret <turret@duck.com>2024-01-17 19:21:54 +0000
commit9a30ecfb12ca3a2775f64fead593de0040ac020b (patch)
treebb167fbb7526701dd57e70db0a83c7fdadc0d573 /net/ws.c
parentd5c873a780357928fcccb27f4378b456f0e45dd2 (diff)
downloaddiscord-bot-skeleton-9a30ecfb12ca3a2775f64fead593de0040ac020b.tar.gz
discord-bot-skeleton-9a30ecfb12ca3a2775f64fead593de0040ac020b.tar.bz2
discord-bot-skeleton-9a30ecfb12ca3a2775f64fead593de0040ac020b.zip
net: minor refactor
enforce 78 character limit add comments for reasoning and description purposes
Diffstat (limited to 'net/ws.c')
-rw-r--r--net/ws.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/net/ws.c b/net/ws.c
index 9ad857d..8f3a588 100644
--- a/net/ws.c
+++ b/net/ws.c
@@ -21,6 +21,10 @@ void ws_send_heartbeat()
size_t sent;
curl_ws_send(ws_handle, buf, strnlen(buf, 128), &sent, 0, CURLWS_TEXT);
+ /* if we receive a heartbeat request from discord, we need to fix
+ the itimer so we don't send another one before the desired
+ heartbeat interval. if our itimer is off more than 2 seconds
+ then we fix it up and reset it */
struct itimerval itimer;
getitimer(ITIMER_REAL, &itimer);
if(itimer.it_value.tv_sec < heartbeat_time.tv_sec - 2) {
@@ -32,12 +36,14 @@ void ws_send_heartbeat()
void ws_handle_event(cJSON *event)
{
int op = cJSON_GetObjectItem(event, "op")->valueint;
+ cJSON *data = cJSON_GetObjectItem(event, "d")
switch(op) {
case 1: /* Heartbeat request */
ws_send_heartbeat();
break;
case 10: ; /* Hello */
- int heartbeat_wait = cJSON_GetObjectItem(cJSON_GetObjectItem(event, "d"), "heartbeat_interval")->valueint;
+ int heartbeat_wait = cJSON_GetObjectItem(data,
+ "heartbeat_interval")->valueint;
float jitter = (float)rand() / (RAND_MAX * 1.0f);
heartbeat_time.tv_sec = heartbeat_wait / 1000;
@@ -52,7 +58,7 @@ void ws_handle_event(cJSON *event)
};
setitimer(ITIMER_REAL, &new_itimer, NULL);
break;
- case 11:
+ case 11: /* Heartbeat ACK */
break;
default:
print(LOG_ERR "ws: received unknown WS opcode %d", op);