feat: 长保持连接,控制端可重启

This commit is contained in:
Mock committed 2026-04-10 11:11:03 +08:00
1 parent 2033db7268
commit 79dba2a664
10 files changed
+930 -105

No files matched your search

+325 -44
View File
@@ -3,17 +3,29 @@
#include "cJSON.h"
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define KCP_RELAY_MAX_DATAGRAM_SIZE (60 * 1024)
#define KCP_HUB_MAINTENANCE_INTERVAL_MS 250
#define KCP_HUB_DEFAULT_TELEMETRY_INTERVAL_MS 500
#define KCP_HUB_DEFAULT_HEARTBEAT_INTERVAL_MS 1000
#define KCP_HUB_DEFAULT_LEASE_TIMEOUT_MS 4000
#define KCP_HUB_TELEMETRY_NODE_ID "hub-telemetry"
#define KCP_HUB_DEFAULT_NODE_ID "hub"
#define KCP_HUB_CTRL_REGISTER_OK "{\"type\":\"server_register_ok\"}"
#define KCP_HUB_CTRL_PEER_REPLACED "{\"type\":\"server_peer_replaced\",\"reason\":\"new_instance_wins\"}"
#define KCP_HUB_CTRL_HEARTBEAT "{\"type\":\"server_heartbeat\"}"
#define KCP_HUB_CTRL_HEARTBEAT_ACK "{\"type\":\"server_heartbeat_ack\"}"
typedef struct kcp_peer_entry {
struct kcp_peer_entry *next;
char peer_id[OMNI_MAX_PEER_ID];
kcp_conn_t *conn;
uint32_t last_seen_ms;
uint32_t last_heartbeat_sent_ms;
} kcp_peer_entry_t;
typedef struct kcp_session_thread_ctx {
@@ -21,6 +33,12 @@ typedef struct kcp_session_thread_ctx {
kcp_conn_t *conn;
} kcp_session_thread_ctx_t;
typedef struct kcp_hub_pending_action {
struct kcp_hub_pending_action *next;
char peer_id[OMNI_MAX_PEER_ID];
kcp_conn_t *conn;
} kcp_hub_pending_action_t;
struct kcp_hub {
pthread_rwlock_t lock;
kcp_peer_entry_t *peers;
@@ -29,6 +47,8 @@ struct kcp_hub {
int stats_interval_ms;
char telemetry_peer_id[OMNI_MAX_PEER_ID];
int telemetry_interval_ms;
int heartbeat_interval_ms;
int lease_timeout_ms;
pthread_t telemetry_thread;
int telemetry_thread_started;
int relay_fd;
@@ -41,11 +61,65 @@ struct kcp_hub {
static int kcp_hub_peer_id_has_suffix(const char *peer_id, const char *suffix);
static int kcp_hub_deliver_to_local_peer(kcp_hub_t *hub, const message_t *msg);
static int kcp_hub_send_server_text(kcp_conn_t *conn, const char *to, const char *payload);
static void kcp_hub_touch_peer(kcp_hub_t *hub, const char *peer_id, kcp_conn_t *conn);
static void kcp_hub_run_maintenance(kcp_hub_t *hub);
static uint32_t kcp_hub_now_ms(void) {
return omni_now_millis32();
}
static uint32_t kcp_hub_elapsed_ms(uint32_t now_ms, uint32_t then_ms) {
return now_ms - then_ms;
}
static int kcp_hub_text_body_equals(const message_t *msg, const char *payload) {
size_t expected_len;
if (msg == NULL || payload == NULL) {
return 0;
}
expected_len = strlen(payload);
return msg->body_len == expected_len && msg->body != NULL && memcmp(msg->body, payload, expected_len) == 0;
}
static int kcp_hub_append_pending_action(kcp_hub_pending_action_t **head, const char *peer_id, kcp_conn_t *conn) {
kcp_hub_pending_action_t *action;
if (head == NULL || peer_id == NULL || conn == NULL) {
errno = EINVAL;
return -1;
}
action = (kcp_hub_pending_action_t *) calloc(1, sizeof(*action));
if (action == NULL) {
return -1;
}
snprintf(action->peer_id, sizeof(action->peer_id), "%s", peer_id);
action->conn = conn;
action->next = *head;
*head = action;
return 0;
}
static void kcp_hub_free_pending_actions(kcp_hub_pending_action_t *head) {
while (head != NULL) {
kcp_hub_pending_action_t *next = head->next;
free(head);
head = next;
}
}
static int kcp_hub_peer_is_telemetry(const char *peer_id) {
return kcp_hub_peer_id_has_suffix(peer_id, "-telemetry");
}
static int kcp_hub_peer_uses_server_lease(const char *peer_id) {
if (peer_id == NULL || peer_id[0] == '\0') {
return 0;
}
return kcp_hub_peer_id_has_suffix(peer_id, "-ctrl") || kcp_hub_peer_is_telemetry(peer_id);
}
static const char *kcp_hub_peer_node_id(const char *peer_id) {
return kcp_hub_peer_is_telemetry(peer_id) ? KCP_HUB_TELEMETRY_NODE_ID : KCP_HUB_DEFAULT_NODE_ID;
}
@@ -118,6 +192,27 @@ static int kcp_hub_configure_peer_transport(kcp_conn_t *conn, const char *peer_i
return 0;
}
static void kcp_hub_touch_peer_locked(kcp_hub_t *hub, const char *peer_id, kcp_conn_t *conn) {
kcp_peer_entry_t *entry;
if (hub == NULL || peer_id == NULL || peer_id[0] == '\0') {
return;
}
entry = kcp_hub_find_peer(hub, peer_id);
if (entry != NULL && (conn == NULL || entry->conn == conn)) {
entry->last_seen_ms = kcp_hub_now_ms();
}
}
static void kcp_hub_touch_peer(kcp_hub_t *hub, const char *peer_id, kcp_conn_t *conn) {
if (hub == NULL || peer_id == NULL || peer_id[0] == '\0') {
return;
}
pthread_rwlock_wrlock(&hub->lock);
kcp_hub_touch_peer_locked(hub, peer_id, conn);
pthread_rwlock_unlock(&hub->lock);
}
static int kcp_hub_add_runtime_stats_json(cJSON *object, const kcp_runtime_stats_t *stats) {
if (object == NULL || stats == NULL) {
errno = EINVAL;
@@ -287,25 +382,53 @@ static int kcp_hub_push_telemetry_snapshot(kcp_hub_t *hub) {
static void *kcp_hub_telemetry_thread_main(void *arg) {
kcp_hub_t *hub = (kcp_hub_t *) arg;
uint32_t last_telemetry_push_ms = 0;
while (!atomic_load(&hub->closed)) {
int interval_ms = KCP_HUB_DEFAULT_TELEMETRY_INTERVAL_MS;
uint32_t now_ms = kcp_hub_now_ms();
int telemetry_enabled = 0;
pthread_rwlock_rdlock(&hub->lock);
if (hub->telemetry_interval_ms > 0) {
telemetry_enabled = hub->telemetry_peer_id[0] != '\0';
if (telemetry_enabled && hub->telemetry_interval_ms > 0) {
interval_ms = hub->telemetry_interval_ms;
}
pthread_rwlock_unlock(&hub->lock);
(void) kcp_hub_push_telemetry_snapshot(hub);
if (telemetry_enabled && (last_telemetry_push_ms == 0 || kcp_hub_elapsed_ms(now_ms, last_telemetry_push_ms) >= (uint32_t) interval_ms)) {
(void) kcp_hub_push_telemetry_snapshot(hub);
last_telemetry_push_ms = now_ms;
}
kcp_hub_run_maintenance(hub);
if (atomic_load(&hub->closed)) {
break;
}
usleep((useconds_t) interval_ms * 1000U);
usleep((useconds_t) KCP_HUB_MAINTENANCE_INTERVAL_MS * 1000U);
}
return NULL;
}
static int kcp_hub_send_server_text(kcp_conn_t *conn, const char *to, const char *payload) {
message_t msg;
protocol_message_init(&msg);
msg.type = MSG_TYPE_TEXT;
snprintf(msg.from, sizeof(msg.from), "%s", SERVER_PEER_ID);
snprintf(msg.to, sizeof(msg.to), "%s", (to == NULL || to[0] == '\0') ? "unknown" : to);
msg.body = (uint8_t *) omni_strdup(payload == NULL ? "" : payload);
if (msg.body == NULL) {
return -1;
}
msg.body_len = strlen((const char *) msg.body);
if (kcp_conn_send(conn, &msg) != 0) {
protocol_message_clear(&msg);
return -1;
}
protocol_message_clear(&msg);
return 0;
}
static int kcp_hub_send_server_error(kcp_conn_t *conn, const char *to, const char *message) {
message_t msg;
protocol_message_init(&msg);
@@ -495,8 +618,56 @@ static int kcp_hub_handle_peer_message(kcp_hub_t *hub, const char *peer_id, kcp_
char *error_text = NULL;
int relay_status = 0;
kcp_hub_touch_peer(hub, peer_id, conn);
switch (msg->type) {
case MSG_TYPE_TEXT:
if (strcmp(msg->to, SERVER_PEER_ID) == 0) {
if (kcp_hub_text_body_equals(msg, KCP_HUB_CTRL_HEARTBEAT_ACK)) {
return 0;
}
if (kcp_hub_send_server_error(conn, peer_id, "unsupported server control message") != 0) {
return -1;
}
errno = EPROTO;
return -1;
}
snprintf(msg->from, sizeof(msg->from), "%s", peer_id);
if (kcp_hub_deliver_to_local_peer(hub, msg) == 0) {
return 0;
}
if (errno != ENOENT) {
error_text = omni_strdup_printf("failed to forward to %s", msg->to);
if (error_text == NULL) {
return -1;
}
if (kcp_hub_send_server_error(conn, peer_id, error_text) != 0) {
free(error_text);
return -1;
}
free(error_text);
return 0;
}
if (kcp_hub_forward_to_relay(hub, msg, &relay_status) == 0) {
return 0;
}
if (relay_status == 1) {
error_text = omni_strdup_printf("unknown target: %s", msg->to);
} else if (relay_status == 2) {
error_text = omni_strdup("failed to relay to remote peer");
} else if (relay_status == 3) {
error_text = omni_strdup("message too large for relay udp");
} else {
error_text = omni_strdup("failed to relay to remote peer");
}
if (error_text == NULL) {
return -1;
}
if (kcp_hub_send_server_error(conn, peer_id, error_text) != 0) {
free(error_text);
return -1;
}
free(error_text);
return 0;
case MSG_TYPE_FILE:
case MSG_TYPE_BINARY:
snprintf(msg->from, sizeof(msg->from), "%s", peer_id);
@@ -558,9 +729,55 @@ static int kcp_hub_handle_peer_message(kcp_hub_t *hub, const char *peer_id, kcp_
}
}
static int kcp_hub_commit_registered_conn(
kcp_hub_t *hub,
const char *peer_id,
kcp_conn_t *conn,
uint32_t now_ms,
kcp_conn_t **out_old_conn
) {
kcp_peer_entry_t *entry;
if (hub == NULL || peer_id == NULL || peer_id[0] == '\0' || conn == NULL) {
errno = EINVAL;
return -1;
}
if (out_old_conn != NULL) {
*out_old_conn = NULL;
}
pthread_rwlock_wrlock(&hub->lock);
entry = kcp_hub_find_peer(hub, peer_id);
if (entry != NULL) {
if (out_old_conn != NULL) {
*out_old_conn = entry->conn;
}
entry->conn = conn;
entry->last_seen_ms = now_ms;
entry->last_heartbeat_sent_ms = 0;
pthread_rwlock_unlock(&hub->lock);
return 0;
}
entry = (kcp_peer_entry_t *) calloc(1, sizeof(*entry));
if (entry == NULL) {
pthread_rwlock_unlock(&hub->lock);
return -1;
}
snprintf(entry->peer_id, sizeof(entry->peer_id), "%s", peer_id);
entry->conn = conn;
entry->last_seen_ms = now_ms;
entry->last_heartbeat_sent_ms = 0;
entry->next = hub->peers;
hub->peers = entry;
pthread_rwlock_unlock(&hub->lock);
return 0;
}
static int kcp_hub_register_conn(kcp_hub_t *hub, kcp_conn_t *conn, char *peer_id, size_t peer_id_len) {
message_t msg;
kcp_peer_entry_t *entry;
kcp_conn_t *old_conn = NULL;
uint32_t now_ms;
protocol_message_init(&msg);
if (kcp_conn_receive(conn, &msg) != 0) {
@@ -574,34 +791,22 @@ static int kcp_hub_register_conn(kcp_hub_t *hub, kcp_conn_t *conn, char *peer_id
return -1;
}
pthread_rwlock_wrlock(&hub->lock);
entry = kcp_hub_find_peer(hub, msg.from);
if (entry != NULL) {
char *error_text;
pthread_rwlock_unlock(&hub->lock);
error_text = omni_strdup_printf("duplicate peer id: %s", msg.from);
if (error_text != NULL) {
(void) kcp_hub_send_server_error(conn, msg.from, error_text);
free(error_text);
}
protocol_message_clear(&msg);
errno = EEXIST;
return -1;
}
entry = (kcp_peer_entry_t *) calloc(1, sizeof(*entry));
if (entry == NULL) {
pthread_rwlock_unlock(&hub->lock);
protocol_message_clear(&msg);
return -1;
}
snprintf(entry->peer_id, sizeof(entry->peer_id), "%s", msg.from);
entry->conn = conn;
entry->next = hub->peers;
hub->peers = entry;
pthread_rwlock_unlock(&hub->lock);
snprintf(peer_id, peer_id_len, "%s", msg.from);
if (kcp_hub_send_server_text(conn, msg.from, KCP_HUB_CTRL_REGISTER_OK) != 0) {
protocol_message_clear(&msg);
return -1;
}
now_ms = kcp_hub_now_ms();
if (kcp_hub_commit_registered_conn(hub, msg.from, conn, now_ms, &old_conn) != 0) {
protocol_message_clear(&msg);
return -1;
}
if (old_conn != NULL && old_conn != conn) {
(void) kcp_hub_send_server_text(old_conn, msg.from, KCP_HUB_CTRL_PEER_REPLACED);
kcp_conn_close(old_conn);
}
protocol_message_clear(&msg);
return 0;
}
@@ -623,8 +828,16 @@ kcp_hub_t *kcp_hub_new(latency_logger_t *logger, kcp_session_stats_logger_t *sta
hub->stats_logger = stats_logger;
hub->stats_interval_ms = stats_interval_ms > 0 ? stats_interval_ms : KCP_DEFAULT_STATS_INTERVAL_MS;
hub->telemetry_interval_ms = KCP_HUB_DEFAULT_TELEMETRY_INTERVAL_MS;
hub->heartbeat_interval_ms = KCP_HUB_DEFAULT_HEARTBEAT_INTERVAL_MS;
hub->lease_timeout_ms = KCP_HUB_DEFAULT_LEASE_TIMEOUT_MS;
hub->relay_fd = -1;
atomic_init(&hub->closed, 0);
if (pthread_create(&hub->telemetry_thread, NULL, kcp_hub_telemetry_thread_main, hub) != 0) {
pthread_rwlock_destroy(&hub->lock);
free(hub);
return NULL;
}
hub->telemetry_thread_started = 1;
return hub;
}
@@ -732,8 +945,6 @@ int kcp_hub_set_relay(kcp_hub_t *hub, int relay_fd, const struct sockaddr *peer_
}
int kcp_hub_set_telemetry(kcp_hub_t *hub, const char *peer_id, int interval_ms) {
int start_thread = 0;
if (hub == NULL || peer_id == NULL) {
errno = EINVAL;
return -1;
@@ -741,22 +952,92 @@ int kcp_hub_set_telemetry(kcp_hub_t *hub, const char *peer_id, int interval_ms)
pthread_rwlock_wrlock(&hub->lock);
snprintf(hub->telemetry_peer_id, sizeof(hub->telemetry_peer_id), "%s", peer_id);
hub->telemetry_interval_ms = interval_ms > 0 ? interval_ms : KCP_HUB_DEFAULT_TELEMETRY_INTERVAL_MS;
if (!hub->telemetry_thread_started && hub->telemetry_peer_id[0] != '\0') {
start_thread = 1;
hub->telemetry_thread_started = 1;
pthread_rwlock_unlock(&hub->lock);
return 0;
}
static void kcp_hub_run_maintenance(kcp_hub_t *hub) {
kcp_hub_pending_action_t *heartbeat_actions = NULL;
kcp_hub_pending_action_t *close_actions = NULL;
uint32_t now_ms;
int heartbeat_interval_ms;
int lease_timeout_ms;
if (hub == NULL) {
return;
}
now_ms = kcp_hub_now_ms();
heartbeat_interval_ms = KCP_HUB_DEFAULT_HEARTBEAT_INTERVAL_MS;
lease_timeout_ms = KCP_HUB_DEFAULT_LEASE_TIMEOUT_MS;
pthread_rwlock_wrlock(&hub->lock);
if (hub->heartbeat_interval_ms > 0) {
heartbeat_interval_ms = hub->heartbeat_interval_ms;
}
if (hub->lease_timeout_ms > 0) {
lease_timeout_ms = hub->lease_timeout_ms;
}
{
kcp_peer_entry_t *prev = NULL;
kcp_peer_entry_t *entry = hub->peers;
while (entry != NULL) {
kcp_peer_entry_t *next = entry->next;
uint32_t idle_ms = kcp_hub_elapsed_ms(now_ms, entry->last_seen_ms);
int uses_server_lease = kcp_hub_peer_uses_server_lease(entry->peer_id);
if (entry->conn == NULL || entry->peer_id[0] == '\0') {
prev = entry;
entry = next;
continue;
}
if (uses_server_lease && lease_timeout_ms > 0 && idle_ms >= (uint32_t) lease_timeout_ms) {
if (prev == NULL) {
hub->peers = next;
} else {
prev->next = next;
}
(void) kcp_hub_append_pending_action(&close_actions, entry->peer_id, entry->conn);
free(entry);
entry = next;
continue;
}
if (
uses_server_lease
&&
heartbeat_interval_ms > 0
&& idle_ms >= (uint32_t) heartbeat_interval_ms
&& (entry->last_heartbeat_sent_ms == 0 || kcp_hub_elapsed_ms(now_ms, entry->last_heartbeat_sent_ms) >= (uint32_t) heartbeat_interval_ms)
) {
entry->last_heartbeat_sent_ms = now_ms;
(void) kcp_hub_append_pending_action(&heartbeat_actions, entry->peer_id, entry->conn);
}
prev = entry;
entry = next;
}
}
pthread_rwlock_unlock(&hub->lock);
if (start_thread) {
if (pthread_create(&hub->telemetry_thread, NULL, kcp_hub_telemetry_thread_main, hub) != 0) {
pthread_rwlock_wrlock(&hub->lock);
hub->telemetry_thread_started = 0;
hub->telemetry_peer_id[0] = '\0';
pthread_rwlock_unlock(&hub->lock);
return -1;
while (heartbeat_actions != NULL) {
kcp_hub_pending_action_t *next = heartbeat_actions->next;
if (kcp_hub_send_server_text(heartbeat_actions->conn, heartbeat_actions->peer_id, KCP_HUB_CTRL_HEARTBEAT) != 0) {
kcp_hub_unregister(hub, heartbeat_actions->peer_id, heartbeat_actions->conn);
kcp_conn_close(heartbeat_actions->conn);
}
free(heartbeat_actions);
heartbeat_actions = next;
}
return 0;
while (close_actions != NULL) {
kcp_hub_pending_action_t *next = close_actions->next;
kcp_conn_close(close_actions->conn);
free(close_actions);
close_actions = next;
}
kcp_hub_free_pending_actions(heartbeat_actions);
kcp_hub_free_pending_actions(close_actions);
}
int kcp_hub_serve_relay(kcp_hub_t *hub) {