upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/cvm_server.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/main/cvm_server.c b/main/cvm_server.c
index 96ce7d3..b93e176 100644
--- a/main/cvm_server.c
+++ b/main/cvm_server.c
@@ -11,6 +11,7 @@
11#include "esp_tls.h" 11#include "esp_tls.h"
12#include "esp_crt_bundle.h" 12#include "esp_crt_bundle.h"
13#include "esp_random.h" 13#include "esp_random.h"
14#include "esp_timer.h"
14#include "freertos/FreeRTOS.h" 15#include "freertos/FreeRTOS.h"
15#include "freertos/task.h" 16#include "freertos/task.h"
16#include <string.h> 17#include <string.h>
@@ -30,6 +31,8 @@ static void publish_announcements_via_ws(esp_tls_t *tls);
30#define CVM_WS_BUF_SIZE 8192 31#define CVM_WS_BUF_SIZE 8192
31#define CVM_MAX_RESPONSE_SIZE 4096 32#define CVM_MAX_RESPONSE_SIZE 4096
32#define CVM_RECONNECT_DELAY_MS 5000 33#define CVM_RECONNECT_DELAY_MS 5000
34#define CVM_WS_READ_TIMEOUT_MS 60000
35#define CVM_WS_PING_INTERVAL_S 30
33 36
34static char *parse_ws_text_frame(const uint8_t *buf, int len) 37static char *parse_ws_text_frame(const uint8_t *buf, int len)
35{ 38{
@@ -148,7 +151,7 @@ static esp_err_t ws_connect(const char *relay_url, esp_tls_t **tls_out)
148 151
149 esp_tls_cfg_t tls_cfg = { 152 esp_tls_cfg_t tls_cfg = {
150 .crt_bundle_attach = esp_crt_bundle_attach, 153 .crt_bundle_attach = esp_crt_bundle_attach,
151 .timeout_ms = 15000, 154 .timeout_ms = CVM_WS_READ_TIMEOUT_MS,
152 }; 155 };
153 esp_tls_t *tls = esp_tls_init(); 156 esp_tls_t *tls = esp_tls_init();
154 if (!tls) return ESP_ERR_NO_MEM; 157 if (!tls) return ESP_ERR_NO_MEM;
@@ -363,9 +366,9 @@ static esp_err_t publish_kind_25910_response_ws(esp_tls_t *tls,
363 return ESP_ERR_NO_MEM; 366 return ESP_ERR_NO_MEM;
364 } 367 }
365 snprintf(msg, msg_len, "[\"EVENT\",%s]", event_json); 368 snprintf(msg, msg_len, "[\"EVENT\",%s]", event_json);
366 ESP_LOGI(TAG, "Sending WS response (%d bytes)", (int)strlen(msg)); 369 ESP_LOGD(TAG, "Sending WS response (%d bytes)", (int)strlen(msg));
367 int rc = ws_send_text(tls, msg); 370 int rc = ws_send_text(tls, msg);
368 ESP_LOGI(TAG, "WS send result: %d", rc); 371 ESP_LOGD(TAG, "WS send result: %d", rc);
369 free(msg); 372 free(msg);
370 free(event_json); 373 free(event_json);
371 return ESP_OK; 374 return ESP_OK;
@@ -613,6 +616,8 @@ static void cvm_relay_task(void *arg)
613 return; 616 return;
614 } 617 }
615 618
619 int64_t last_ping_time = 0;
620
616 while (g_running) { 621 while (g_running) {
617 int rlen = esp_tls_conn_read(tls, buf, CVM_WS_BUF_SIZE - 1); 622 int rlen = esp_tls_conn_read(tls, buf, CVM_WS_BUF_SIZE - 1);
618 if (rlen < 0) { 623 if (rlen < 0) {
@@ -631,6 +636,16 @@ static void cvm_relay_task(void *arg)
631 } 636 }
632 free(text); 637 free(text);
633 } 638 }
639 } else if ((buf[0] & 0x0F) == 0x09) {
640 uint8_t pong[2] = {0x8A, 0x00};
641 esp_tls_conn_write(tls, pong, 2);
642 }
643
644 int64_t now = (int64_t)esp_timer_get_time() / 1000000;
645 if (now - last_ping_time >= CVM_WS_PING_INTERVAL_S) {
646 uint8_t ping[2] = {0x89, 0x00};
647 esp_tls_conn_write(tls, ping, 2);
648 last_ping_time = now;
634 } 649 }
635 } 650 }
636 651