diff options
author | turret <turret@duck.com> | 2024-03-30 16:04:45 -0500 |
---|---|---|
committer | turret <turret@duck.com> | 2024-03-30 16:04:45 -0500 |
commit | 80a67b7d20393a29aa5d2cb92197f3381be7fd96 (patch) | |
tree | f0c313da5ef509e79e5067972edd91976513ce0e /net/ws.c | |
parent | f01745a2ee84f11b8cc54e37c5f7f596184ab785 (diff) | |
download | discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.tar.gz discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.tar.bz2 discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.zip |
*: directory changes
since this project is a skeleton and not meant to clutter up the code
that will actually consume the bot, i've opted to consolidate the
majority of files under a single directory and minimise extra files
*: move code to util/
*: move include files to include/dbs/
net: consolidate net functions into single file
config: remove config
Diffstat (limited to 'net/ws.c')
-rw-r--r-- | net/ws.c | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/net/ws.c b/net/ws.c deleted file mode 100644 index 3a8d512..0000000 --- a/net/ws.c +++ /dev/null @@ -1,67 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <sys/time.h> -#include <unistd.h> - -#include <cJSON.h> -#include <curl/curl.h> - -#include <log.h> - -extern CURL *ws_handle; -long last_sequence = -1; -struct timeval heartbeat_time; - -void ws_send_heartbeat() -{ - char buf[128] = "{\"op\":1,\"d\":null}"; - if(last_sequence > 0) - snprintf(buf, 128, "{\"op\":1,\"d\":%ld}", last_sequence); - 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) { - itimer.it_value = heartbeat_time; - setitimer(ITIMER_REAL, &itimer, NULL); - } -} - -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(data, - "heartbeat_interval")->valueint; - float jitter = (float)rand() / (RAND_MAX * 1.0f); - - heartbeat_time.tv_sec = heartbeat_wait / 1000; - heartbeat_time.tv_usec = (heartbeat_wait % 1000) * 1000; - struct timeval jitter_time = { - .tv_sec = heartbeat_time.tv_sec * jitter, - .tv_usec = heartbeat_time.tv_usec * jitter, - }; - struct itimerval new_itimer = { - .it_interval = heartbeat_time, - .it_value = jitter_time - }; - setitimer(ITIMER_REAL, &new_itimer, NULL); - break; - case 11: /* Heartbeat ACK */ - break; - default: - print(LOG_ERR "ws: received unknown WS opcode %d", op); - break; - } -} |