fix:更新客户端功能
This commit is contained in:
567
src/apps/bridge_main.c
Normal file
567
src/apps/bridge_main.c
Normal file
@@ -0,0 +1,567 @@
|
||||
/*
|
||||
* bridge_main.c
|
||||
* 固定多跳代理:
|
||||
* - 上游作为一个 peer 主动连接远端 hub
|
||||
* - 下游作为一个轻量 hub 接入本地 peer
|
||||
* - 将 bind / tunnel / status 在上下游之间原样转发
|
||||
*
|
||||
* 适用场景:
|
||||
* - A 连接 C(hub)
|
||||
* - B 连接 D(bridge)
|
||||
* - D 再连接 C
|
||||
* - C 只看见 bridge 暴露出来的逻辑 client_id
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define BRIDGE_MAX_PAYLOAD (PEER_TUNNEL_META_SIZE + 65536u)
|
||||
|
||||
typedef struct BridgeRuntime {
|
||||
int listen_fd;
|
||||
int upstream_fd;
|
||||
int downstream_fd;
|
||||
pthread_mutex_t upstream_mu;
|
||||
pthread_mutex_t downstream_mu;
|
||||
pthread_mutex_t state_mu;
|
||||
atomic_int running;
|
||||
char client_id[OMNI_PEER_ID_SIZE];
|
||||
} BridgeRuntime;
|
||||
|
||||
static volatile sig_atomic_t g_stop = 0;
|
||||
|
||||
static void on_signal(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
g_stop = 1;
|
||||
}
|
||||
|
||||
static void install_signal_handlers(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = on_signal;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
(void)sigaction(SIGINT, &sa, NULL);
|
||||
(void)sigaction(SIGTERM, &sa, NULL);
|
||||
(void)signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage:\n"
|
||||
" %s -H <upstream_hub_ip> -P <upstream_hub_port> -i <client_id> -L <listen_port> [-b <bind_ip>]\n",
|
||||
prog);
|
||||
}
|
||||
|
||||
static int peer_id_is_valid(const char *id)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
if (!id || !id[0]) {
|
||||
return 0;
|
||||
}
|
||||
for (len = 0; id[len] != '\0'; ++len) {
|
||||
unsigned char ch = (unsigned char)id[len];
|
||||
|
||||
if (len + 1u >= OMNI_PEER_ID_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
if (!((ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= 'A' && ch <= 'Z') ||
|
||||
(ch >= '0' && ch <= '9') ||
|
||||
ch == '_' || ch == '-' || ch == '.')) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ssize_t read_n(int fd, void *buf, size_t n)
|
||||
{
|
||||
uint8_t *p = (uint8_t *)buf;
|
||||
size_t done = 0;
|
||||
|
||||
while (done < n) {
|
||||
ssize_t rc = recv(fd, p + done, n - done, 0);
|
||||
if (rc == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (rc < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
done += (size_t)rc;
|
||||
}
|
||||
return (ssize_t)done;
|
||||
}
|
||||
|
||||
static ssize_t write_n(int fd, const void *buf, size_t n)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)buf;
|
||||
size_t done = 0;
|
||||
|
||||
while (done < n) {
|
||||
ssize_t rc = send(fd, p + done, n - done, 0);
|
||||
if (rc < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
done += (size_t)rc;
|
||||
}
|
||||
return (ssize_t)done;
|
||||
}
|
||||
|
||||
static int recv_app_message(int fd, MsgHeader *out_hdr, uint8_t *payload_buf, size_t payload_cap)
|
||||
{
|
||||
MsgHeader net_hdr;
|
||||
ssize_t n;
|
||||
|
||||
n = read_n(fd, &net_hdr, MSG_HEADER_SIZE);
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (n != (ssize_t)MSG_HEADER_SIZE) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
omni_msg_header_decode(&net_hdr, out_hdr);
|
||||
if (out_hdr->len > payload_cap) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
if (out_hdr->len > 0) {
|
||||
n = read_n(fd, payload_buf, out_hdr->len);
|
||||
if (n != (ssize_t)out_hdr->len) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
}
|
||||
|
||||
logger_on_recv(MSG_HEADER_SIZE + out_hdr->len);
|
||||
logger_maybe_print_performance_log("bridge_recv");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int send_app_message_fd_locked(int fd,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
MsgHeader hdr;
|
||||
uint8_t header_buf[MSG_HEADER_SIZE];
|
||||
|
||||
omni_msg_header_encode(&hdr, type, payload_len, omni_now_ms());
|
||||
memcpy(header_buf, &hdr, sizeof(header_buf));
|
||||
|
||||
if (write_n(fd, header_buf, sizeof(header_buf)) != (ssize_t)sizeof(header_buf)) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
if (payload_len > 0 && payload) {
|
||||
if (write_n(fd, payload, payload_len) != (ssize_t)payload_len) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
}
|
||||
|
||||
logger_on_send(MSG_HEADER_SIZE + payload_len);
|
||||
logger_maybe_print_performance_log("bridge_send");
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
static int bridge_send_upstream(BridgeRuntime *rt,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
int rc;
|
||||
|
||||
pthread_mutex_lock(&rt->upstream_mu);
|
||||
rc = send_app_message_fd_locked(rt->upstream_fd, type, payload, payload_len);
|
||||
pthread_mutex_unlock(&rt->upstream_mu);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int bridge_send_downstream(BridgeRuntime *rt,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
int rc = OMNI_ERR_IO;
|
||||
|
||||
pthread_mutex_lock(&rt->downstream_mu);
|
||||
if (rt->downstream_fd >= 0) {
|
||||
rc = send_app_message_fd_locked(rt->downstream_fd, type, payload, payload_len);
|
||||
}
|
||||
pthread_mutex_unlock(&rt->downstream_mu);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int send_status_to_downstream(BridgeRuntime *rt,
|
||||
uint32_t code,
|
||||
const char *self_id,
|
||||
const char *peer_id,
|
||||
const char *detail)
|
||||
{
|
||||
PeerStatusMeta status_meta;
|
||||
|
||||
omni_peer_status_meta_encode(&status_meta, code, self_id, peer_id, detail);
|
||||
return bridge_send_downstream(rt,
|
||||
MSG_TYPE_PEER_STATUS,
|
||||
&status_meta,
|
||||
PEER_STATUS_META_SIZE);
|
||||
}
|
||||
|
||||
static int create_connected_socket(const char *host, uint16_t port)
|
||||
{
|
||||
int fd;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, host, &addr.sin_addr) != 1) {
|
||||
close(fd);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int create_listen_socket(const char *bind_ip, uint16_t port)
|
||||
{
|
||||
int fd;
|
||||
int reuse = 1;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
if (bind_ip && bind_ip[0] != '\0') {
|
||||
if (inet_pton(AF_INET, bind_ip, &addr.sin_addr) != 1) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (listen(fd, 8) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int bridge_send_upstream_register(BridgeRuntime *rt)
|
||||
{
|
||||
PeerRegisterMeta meta;
|
||||
|
||||
omni_peer_register_meta_encode(&meta, rt->client_id);
|
||||
return bridge_send_upstream(rt, MSG_TYPE_PEER_REGISTER, &meta, PEER_REGISTER_META_SIZE);
|
||||
}
|
||||
|
||||
static void *upstream_thread_main(void *arg)
|
||||
{
|
||||
BridgeRuntime *rt = (BridgeRuntime *)arg;
|
||||
uint8_t payload[BRIDGE_MAX_PAYLOAD];
|
||||
|
||||
while (atomic_load(&rt->running)) {
|
||||
MsgHeader hdr;
|
||||
int rc = recv_app_message(rt->upstream_fd, &hdr, payload, sizeof(payload));
|
||||
|
||||
if (rc == 0) {
|
||||
logger_log("INFO", "bridge", "upstream_closed");
|
||||
break;
|
||||
}
|
||||
if (rc < 0) {
|
||||
logger_log("ERROR", "bridge", "upstream_recv_failed rc=%d", rc);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (hdr.type) {
|
||||
case MSG_TYPE_PEER_STATUS:
|
||||
case MSG_TYPE_PEER_TUNNEL:
|
||||
if (bridge_send_downstream(rt, hdr.type, payload, hdr.len) != OMNI_OK) {
|
||||
logger_log("WARN", "bridge",
|
||||
"downstream_forward_skipped type=%u len=%u",
|
||||
(unsigned)hdr.type,
|
||||
(unsigned)hdr.len);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
logger_log("WARN", "bridge", "unexpected_upstream_type=%u len=%u",
|
||||
(unsigned)hdr.type, (unsigned)hdr.len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
atomic_store(&rt->running, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void clear_downstream_fd(BridgeRuntime *rt, int fd)
|
||||
{
|
||||
pthread_mutex_lock(&rt->downstream_mu);
|
||||
if (rt->downstream_fd == fd) {
|
||||
rt->downstream_fd = -1;
|
||||
}
|
||||
pthread_mutex_unlock(&rt->downstream_mu);
|
||||
}
|
||||
|
||||
static void handle_downstream_connection(BridgeRuntime *rt, int fd)
|
||||
{
|
||||
uint8_t payload[BRIDGE_MAX_PAYLOAD];
|
||||
int registered = 0;
|
||||
|
||||
while (atomic_load(&rt->running)) {
|
||||
MsgHeader hdr;
|
||||
int rc = recv_app_message(fd, &hdr, payload, sizeof(payload));
|
||||
|
||||
if (rc == 0) {
|
||||
logger_log("INFO", "bridge", "downstream_closed");
|
||||
break;
|
||||
}
|
||||
if (rc < 0) {
|
||||
logger_log("ERROR", "bridge", "downstream_recv_failed rc=%d", rc);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (hdr.type) {
|
||||
case MSG_TYPE_PEER_REGISTER: {
|
||||
PeerRegisterMeta register_meta;
|
||||
|
||||
if (hdr.len < PEER_REGISTER_META_SIZE) {
|
||||
(void)send_status_to_downstream(rt, PEER_STATUS_ERROR, NULL, NULL, "short_register_payload");
|
||||
break;
|
||||
}
|
||||
omni_peer_register_meta_decode((const PeerRegisterMeta *)payload, ®ister_meta);
|
||||
if (strcmp(register_meta.client_id, rt->client_id) != 0) {
|
||||
(void)send_status_to_downstream(rt,
|
||||
PEER_STATUS_ERROR,
|
||||
register_meta.client_id,
|
||||
NULL,
|
||||
"client_id_mismatch");
|
||||
logger_log("WARN", "bridge",
|
||||
"downstream_register_mismatch got=%s expect=%s",
|
||||
register_meta.client_id,
|
||||
rt->client_id);
|
||||
break;
|
||||
}
|
||||
registered = 1;
|
||||
(void)send_status_to_downstream(rt,
|
||||
PEER_STATUS_REGISTERED,
|
||||
rt->client_id,
|
||||
NULL,
|
||||
"bridge_register_ok");
|
||||
break;
|
||||
}
|
||||
case MSG_TYPE_PEER_BIND:
|
||||
case MSG_TYPE_PEER_TUNNEL:
|
||||
if (!registered) {
|
||||
(void)send_status_to_downstream(rt,
|
||||
PEER_STATUS_ERROR,
|
||||
rt->client_id,
|
||||
NULL,
|
||||
"register_first");
|
||||
break;
|
||||
}
|
||||
if (bridge_send_upstream(rt, hdr.type, payload, hdr.len) != OMNI_OK) {
|
||||
logger_log("ERROR", "bridge",
|
||||
"upstream_forward_failed type=%u len=%u",
|
||||
(unsigned)hdr.type,
|
||||
(unsigned)hdr.len);
|
||||
(void)send_status_to_downstream(rt,
|
||||
PEER_STATUS_ERROR,
|
||||
rt->client_id,
|
||||
NULL,
|
||||
"upstream_forward_failed");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
logger_log("WARN", "bridge", "unexpected_downstream_type=%u len=%u",
|
||||
(unsigned)hdr.type, (unsigned)hdr.len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
clear_downstream_fd(rt, fd);
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *upstream_ip = NULL;
|
||||
const char *bind_ip = NULL;
|
||||
const char *client_id = NULL;
|
||||
int upstream_port = 0;
|
||||
int listen_port = 0;
|
||||
int opt;
|
||||
BridgeRuntime rt;
|
||||
pthread_t upstream_tid;
|
||||
|
||||
while ((opt = getopt(argc, argv, "H:P:i:L:b:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'H':
|
||||
upstream_ip = optarg;
|
||||
break;
|
||||
case 'P':
|
||||
upstream_port = atoi(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
client_id = optarg;
|
||||
break;
|
||||
case 'L':
|
||||
listen_port = atoi(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
bind_ip = optarg;
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!upstream_ip || upstream_port <= 0 || listen_port <= 0 || !peer_id_is_valid(client_id)) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
logger_init();
|
||||
install_signal_handlers();
|
||||
|
||||
memset(&rt, 0, sizeof(rt));
|
||||
rt.listen_fd = -1;
|
||||
rt.upstream_fd = -1;
|
||||
rt.downstream_fd = -1;
|
||||
omni_copy_fixed_ascii(rt.client_id, sizeof(rt.client_id), client_id);
|
||||
atomic_init(&rt.running, 1);
|
||||
pthread_mutex_init(&rt.upstream_mu, NULL);
|
||||
pthread_mutex_init(&rt.downstream_mu, NULL);
|
||||
pthread_mutex_init(&rt.state_mu, NULL);
|
||||
|
||||
rt.upstream_fd = create_connected_socket(upstream_ip, (uint16_t)upstream_port);
|
||||
if (rt.upstream_fd < 0) {
|
||||
perror("bridge upstream connect");
|
||||
goto fail;
|
||||
}
|
||||
if (bridge_send_upstream_register(&rt) != OMNI_OK) {
|
||||
fprintf(stderr, "bridge upstream register failed\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
rt.listen_fd = create_listen_socket(bind_ip, (uint16_t)listen_port);
|
||||
if (rt.listen_fd < 0) {
|
||||
perror("bridge listen");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (pthread_create(&upstream_tid, NULL, upstream_thread_main, &rt) != 0) {
|
||||
perror("bridge pthread_create");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
logger_log("INFO", "bridge",
|
||||
"listening bind_ip=%s listen_port=%u upstream=%s:%u client_id=%s",
|
||||
bind_ip ? bind_ip : "0.0.0.0",
|
||||
(unsigned)listen_port,
|
||||
upstream_ip,
|
||||
(unsigned)upstream_port,
|
||||
rt.client_id);
|
||||
|
||||
while (atomic_load(&rt.running) && !g_stop) {
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len = sizeof(peer_addr);
|
||||
int cfd = accept(rt.listen_fd, (struct sockaddr *)&peer_addr, &peer_len);
|
||||
|
||||
if (cfd < 0) {
|
||||
if (errno == EINTR && !g_stop) {
|
||||
continue;
|
||||
}
|
||||
if (g_stop) {
|
||||
break;
|
||||
}
|
||||
perror("bridge accept");
|
||||
break;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&rt.downstream_mu);
|
||||
if (rt.downstream_fd >= 0) {
|
||||
pthread_mutex_unlock(&rt.downstream_mu);
|
||||
close(cfd);
|
||||
logger_log("WARN", "bridge", "reject_extra_downstream");
|
||||
continue;
|
||||
}
|
||||
rt.downstream_fd = cfd;
|
||||
pthread_mutex_unlock(&rt.downstream_mu);
|
||||
|
||||
logger_log("INFO", "bridge", "downstream_connected");
|
||||
handle_downstream_connection(&rt, cfd);
|
||||
}
|
||||
|
||||
atomic_store(&rt.running, 0);
|
||||
if (rt.listen_fd >= 0) {
|
||||
close(rt.listen_fd);
|
||||
}
|
||||
pthread_join(upstream_tid, NULL);
|
||||
if (rt.upstream_fd >= 0) {
|
||||
shutdown(rt.upstream_fd, SHUT_RDWR);
|
||||
close(rt.upstream_fd);
|
||||
}
|
||||
logger_print_performance_log("final");
|
||||
pthread_mutex_destroy(&rt.state_mu);
|
||||
pthread_mutex_destroy(&rt.downstream_mu);
|
||||
pthread_mutex_destroy(&rt.upstream_mu);
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
if (rt.listen_fd >= 0) {
|
||||
close(rt.listen_fd);
|
||||
}
|
||||
if (rt.upstream_fd >= 0) {
|
||||
close(rt.upstream_fd);
|
||||
}
|
||||
pthread_mutex_destroy(&rt.state_mu);
|
||||
pthread_mutex_destroy(&rt.downstream_mu);
|
||||
pthread_mutex_destroy(&rt.upstream_mu);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
/*
|
||||
* client_main.c
|
||||
* 客户端:读取大文件分片发送,同时后台接收服务端 ASCII 指令并打印
|
||||
* 客户端:读取大文件分片发送,同时后台接收服务端控制/确认消息
|
||||
*
|
||||
* 线程模型:
|
||||
* - 主线程:读取文件并发送 FILE_CHUNK / FILE_END
|
||||
* - 子线程:持续接收服务端 COMMAND 并打印
|
||||
*
|
||||
* 消息格式:
|
||||
* - 每条业务消息为 [MsgHeader(16B) + payload]
|
||||
* - MsgHeader 字段由 common.h 中的 encode/decode 统一处理
|
||||
* 整体模型:
|
||||
* 1) 主线程负责读文件、切 chunk、封装业务帧并发送。
|
||||
* 2) 接收线程负责监听服务端命令和最终 ACK。
|
||||
* 3) 两个线程通过原子变量共享“运行状态”和“ACK 结果”。
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
@@ -23,28 +20,43 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define CLIENT_FRAME_BUF_SIZE (MSG_HEADER_SIZE + 65536u)
|
||||
/* 接收线程的单帧缓冲区上限:业务头 + chunk 元数据 + 最大 payload。 */
|
||||
#define CLIENT_FRAME_BUF_SIZE (MSG_HEADER_SIZE + TRANSFER_CHUNK_META_SIZE + 65536u)
|
||||
#define OMNI_TIME_SYNC_PROBE_COUNT 5u
|
||||
#define OMNI_TIME_SYNC_REPLY_TIMEOUT_MS 500u
|
||||
|
||||
typedef struct ClientRuntime {
|
||||
/* 协议抽象层句柄。 */
|
||||
OmniContext *ctx;
|
||||
/* 线程共享运行标记:1=运行中,0=退出。 */
|
||||
atomic_int running;
|
||||
OmniContext *ctx; /* 底层协议上下文。 */
|
||||
atomic_int running; /* 共享退出标志,主线程和接收线程都会检查。 */
|
||||
atomic_int ack_received; /* 是否已经收到服务端的传输完成确认。 */
|
||||
atomic_ullong ack_rtt_ms; /* FILE_END -> TRANSFER_ACK 这一来一回的 RTT。 */
|
||||
atomic_ullong ack_bytes_written; /* 服务端实际写盘字节数。 */
|
||||
atomic_uint ack_transfer_id; /* 收到 ACK 对应的传输 ID。 */
|
||||
atomic_uint sync_expected_probe_id; /* 当前正在等的 probe。 */
|
||||
atomic_ullong sync_expected_client_send_ts_ms; /* 当前 probe 的客户端 t0。 */
|
||||
atomic_int sync_reply_ready; /* 后台线程是否已经收到匹配响应。 */
|
||||
atomic_ullong sync_reply_rtt_ms; /* 当前 probe 的 RTT。 */
|
||||
atomic_llong sync_reply_offset_ms; /* 当前 probe 的 offset。 */
|
||||
} ClientRuntime;
|
||||
|
||||
/*
|
||||
* 进程级停止标记:
|
||||
* - 收到 SIGINT/SIGTERM(例如 Ctrl+C)时置 1
|
||||
* - 主线程据此触发收尾逻辑,保证线程/连接能优雅退出
|
||||
*/
|
||||
typedef struct ClockSyncResult {
|
||||
int valid; /* 是否已拿到可用 offset。 */
|
||||
int64_t server_minus_client_offset_ms; /* server_time - client_time */
|
||||
uint64_t best_rtt_ms; /* 选中样本的 RTT。 */
|
||||
uint32_t sample_count; /* 实际成功样本数。 */
|
||||
} ClockSyncResult;
|
||||
|
||||
/* 信号处理只做最轻量的事情:设置停止标志,由主流程自己收尾。 */
|
||||
static volatile sig_atomic_t g_stop = 0;
|
||||
|
||||
/* SIGINT/SIGTERM 的处理函数:通知发送循环尽快退出。 */
|
||||
static void on_signal(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
g_stop = 1;
|
||||
}
|
||||
|
||||
/* 注册 Ctrl+C / kill 的处理逻辑,避免被粗暴中断后缺少收尾日志。 */
|
||||
static void install_signal_handlers(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
@@ -57,6 +69,7 @@ static void install_signal_handlers(void)
|
||||
(void)sigaction(SIGTERM, &sa, NULL);
|
||||
}
|
||||
|
||||
/* 打印客户端命令行帮助。 */
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
@@ -66,9 +79,9 @@ static void usage(const char *prog)
|
||||
prog);
|
||||
}
|
||||
|
||||
/* 将字符串协议名转换为内部枚举;非法输入默认回退 TCP。 */
|
||||
static OmniProtocol parse_proto(const char *s)
|
||||
{
|
||||
/* 输入非法时回退到 TCP,方便本地默认测试。 */
|
||||
if (!s) return OMNI_PROTO_TCP;
|
||||
if (strcmp(s, "tcp") == 0) return OMNI_PROTO_TCP;
|
||||
if (strcmp(s, "udp") == 0) return OMNI_PROTO_UDP;
|
||||
@@ -76,17 +89,16 @@ static OmniProtocol parse_proto(const char *s)
|
||||
return OMNI_PROTO_TCP;
|
||||
}
|
||||
|
||||
static int send_app_message(OmniContext *ctx,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
/*
|
||||
* 发送一个完整的业务帧。
|
||||
* 这里统一负责:拼 MsgHeader -> 拷贝 payload -> 调底层 omni_send。
|
||||
*/
|
||||
static int send_app_message_with_timestamp(OmniContext *ctx,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len,
|
||||
uint64_t timestamp_ms)
|
||||
{
|
||||
/*
|
||||
* 统一应用层发包:
|
||||
* 1) 组装业务头(网络字节序)
|
||||
* 2) 拼接 payload
|
||||
* 3) 通过 omni_send 一次发送整帧
|
||||
*/
|
||||
size_t total_len = MSG_HEADER_SIZE + (size_t)payload_len;
|
||||
uint8_t *frame = (uint8_t *)malloc(total_len);
|
||||
if (!frame) {
|
||||
@@ -95,7 +107,8 @@ static int send_app_message(OmniContext *ctx,
|
||||
}
|
||||
|
||||
MsgHeader hdr;
|
||||
omni_msg_header_encode(&hdr, type, payload_len, omni_now_ms());
|
||||
/* 业务层总是按“头 + 载荷”的统一格式发给对端。 */
|
||||
omni_msg_header_encode(&hdr, type, payload_len, timestamp_ms);
|
||||
memcpy(frame, &hdr, MSG_HEADER_SIZE);
|
||||
if (payload_len > 0 && payload) {
|
||||
memcpy(frame + MSG_HEADER_SIZE, payload, payload_len);
|
||||
@@ -113,16 +126,12 @@ static int send_app_message(OmniContext *ctx,
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 从接收到的一整帧中拆出应用层头和 payload 指针,并做长度一致性校验。 */
|
||||
static int decode_app_message(const uint8_t *frame,
|
||||
size_t frame_len,
|
||||
MsgHeader *out_hdr,
|
||||
const uint8_t **out_payload)
|
||||
{
|
||||
/*
|
||||
* 统一应用层解包:
|
||||
* - 至少要有 16B 头
|
||||
* - 头中 len 与总帧长度必须一致,避免越界/脏数据
|
||||
*/
|
||||
if (!frame || frame_len < MSG_HEADER_SIZE || !out_hdr || !out_payload) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
@@ -139,15 +148,250 @@ static int decode_app_message(const uint8_t *frame,
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 用四时间戳公式估算 server_time - client_time。 */
|
||||
static int64_t compute_server_minus_client_offset_ms(const TimeSyncReplyMeta *reply,
|
||||
uint64_t client_recv_ts_ms)
|
||||
{
|
||||
int64_t req_leg = (int64_t)reply->server_recv_ts_ms -
|
||||
(int64_t)reply->client_send_ts_ms;
|
||||
int64_t resp_leg = (int64_t)reply->server_send_ts_ms -
|
||||
(int64_t)client_recv_ts_ms;
|
||||
return (req_leg + resp_leg) / 2;
|
||||
}
|
||||
|
||||
/*
|
||||
* 在正式发文件前做几轮时钟探测:
|
||||
* - 主线程发 t0
|
||||
* - 服务端回 t1/t2
|
||||
* - 后台接收线程在 t3 处写回 RTT/offset
|
||||
* - 主线程带超时地等待,保证经由单向 relay 时不会卡死
|
||||
*/
|
||||
static int perform_time_sync(OmniContext *ctx,
|
||||
ClientRuntime *rt,
|
||||
ClockSyncResult *out_result)
|
||||
{
|
||||
uint64_t best_rtt_ms = UINT64_MAX;
|
||||
int64_t best_offset_ms = 0;
|
||||
uint32_t sample_count = 0;
|
||||
|
||||
if (!ctx || !rt || !out_result) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
|
||||
memset(out_result, 0, sizeof(*out_result));
|
||||
|
||||
for (uint32_t probe_id = 1; probe_id <= OMNI_TIME_SYNC_PROBE_COUNT; ++probe_id) {
|
||||
TimeSyncProbeMeta probe_meta;
|
||||
uint64_t client_send_ts_ms = omni_now_ms();
|
||||
int got_reply = 0;
|
||||
|
||||
atomic_store(&rt->sync_reply_ready, 0);
|
||||
atomic_store(&rt->sync_expected_probe_id, probe_id);
|
||||
atomic_store(&rt->sync_expected_client_send_ts_ms, client_send_ts_ms);
|
||||
|
||||
omni_time_sync_probe_meta_encode(&probe_meta, probe_id, client_send_ts_ms);
|
||||
if (send_app_message_with_timestamp(ctx,
|
||||
MSG_TYPE_TIME_SYNC_REQ,
|
||||
&probe_meta,
|
||||
TIME_SYNC_PROBE_META_SIZE,
|
||||
client_send_ts_ms) != OMNI_OK) {
|
||||
logger_log("ERROR", "client",
|
||||
"time_sync_send_failed probe_id=%u",
|
||||
(unsigned)probe_id);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
for (uint32_t waited_ms = 0;
|
||||
waited_ms < OMNI_TIME_SYNC_REPLY_TIMEOUT_MS;
|
||||
waited_ms += 5u) {
|
||||
if (atomic_load(&rt->sync_reply_ready)) {
|
||||
uint64_t rtt_ms = atomic_load(&rt->sync_reply_rtt_ms);
|
||||
int64_t offset_ms = atomic_load(&rt->sync_reply_offset_ms);
|
||||
|
||||
sample_count++;
|
||||
got_reply = 1;
|
||||
logger_on_rtt(rtt_ms);
|
||||
logger_log("INFO", "client",
|
||||
"time_sync_sample probe_id=%u rtt_ms=%llu offset_ms=%lld",
|
||||
(unsigned)probe_id,
|
||||
(unsigned long long)rtt_ms,
|
||||
(long long)offset_ms);
|
||||
if (rtt_ms < best_rtt_ms) {
|
||||
best_rtt_ms = rtt_ms;
|
||||
best_offset_ms = offset_ms;
|
||||
}
|
||||
break;
|
||||
}
|
||||
usleep(5 * 1000);
|
||||
}
|
||||
|
||||
atomic_store(&rt->sync_expected_probe_id, 0);
|
||||
atomic_store(&rt->sync_expected_client_send_ts_ms, 0);
|
||||
atomic_store(&rt->sync_reply_ready, 0);
|
||||
|
||||
if (!got_reply) {
|
||||
logger_log("WARN", "client",
|
||||
"time_sync_probe_timeout probe_id=%u timeout_ms=%u",
|
||||
(unsigned)probe_id,
|
||||
(unsigned)OMNI_TIME_SYNC_REPLY_TIMEOUT_MS);
|
||||
}
|
||||
}
|
||||
|
||||
if (sample_count == 0 || best_rtt_ms == UINT64_MAX) {
|
||||
logger_log("WARN", "client", "time_sync_no_valid_sample");
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
out_result->valid = 1;
|
||||
out_result->server_minus_client_offset_ms = best_offset_ms;
|
||||
out_result->best_rtt_ms = best_rtt_ms;
|
||||
out_result->sample_count = sample_count;
|
||||
|
||||
{
|
||||
TimeSyncReportMeta report_meta;
|
||||
uint64_t report_ts_ms = omni_now_ms();
|
||||
|
||||
omni_time_sync_report_meta_encode(&report_meta,
|
||||
best_offset_ms,
|
||||
best_rtt_ms,
|
||||
sample_count);
|
||||
if (send_app_message_with_timestamp(ctx,
|
||||
MSG_TYPE_TIME_SYNC_REPORT,
|
||||
&report_meta,
|
||||
TIME_SYNC_REPORT_META_SIZE,
|
||||
report_ts_ms) != OMNI_OK) {
|
||||
logger_log("ERROR", "client",
|
||||
"time_sync_report_send_failed offset_ms=%lld best_rtt_ms=%llu",
|
||||
(long long)best_offset_ms,
|
||||
(unsigned long long)best_rtt_ms);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
}
|
||||
|
||||
logger_log("INFO", "client",
|
||||
"time_sync_selected offset_ms=%lld best_rtt_ms=%llu samples=%u",
|
||||
(long long)best_offset_ms,
|
||||
(unsigned long long)best_rtt_ms,
|
||||
(unsigned)sample_count);
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 按需扩容“每秒发送多少片”的统计数组。 */
|
||||
static int ensure_window_capacity(uint64_t **counts, size_t *cap, uint32_t window_id)
|
||||
{
|
||||
size_t need = (size_t)window_id + 1u;
|
||||
size_t new_cap;
|
||||
uint64_t *new_counts;
|
||||
|
||||
if (need <= *cap) {
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
new_cap = (*cap == 0) ? 8u : *cap;
|
||||
while (new_cap < need) {
|
||||
new_cap *= 2u;
|
||||
}
|
||||
|
||||
new_counts = (uint64_t *)realloc(*counts, new_cap * sizeof(uint64_t));
|
||||
if (!new_counts) {
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
|
||||
memset(new_counts + *cap, 0, (new_cap - *cap) * sizeof(uint64_t));
|
||||
*counts = new_counts;
|
||||
*cap = new_cap;
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 将窗口分布统计转成 "0:12,1:15,3:9" 这类便于日志输出的字符串。 */
|
||||
static char *format_window_distribution(const uint64_t *counts, uint32_t total_windows)
|
||||
{
|
||||
size_t cap = 256;
|
||||
size_t len = 0;
|
||||
char *buf = (char *)malloc(cap);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
buf[0] = '\0';
|
||||
|
||||
for (uint32_t i = 0; i < total_windows; ++i) {
|
||||
char tmp[64];
|
||||
int n;
|
||||
if (counts[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
n = snprintf(tmp, sizeof(tmp), "%s%u:%llu",
|
||||
(len == 0) ? "" : ",",
|
||||
(unsigned)i,
|
||||
(unsigned long long)counts[i]);
|
||||
if (n <= 0) {
|
||||
continue;
|
||||
}
|
||||
while (len + (size_t)n + 1 > cap) {
|
||||
char *new_buf;
|
||||
cap *= 2u;
|
||||
new_buf = (char *)realloc(buf, cap);
|
||||
if (!new_buf) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
buf = new_buf;
|
||||
}
|
||||
memcpy(buf + len, tmp, (size_t)n);
|
||||
len += (size_t)n;
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
snprintf(buf, cap, "none");
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* 通过保存/恢复文件指针位置计算总文件大小,不影响后续顺序读取。 */
|
||||
static uint64_t compute_file_size(FILE *fp)
|
||||
{
|
||||
off_t cur = ftello(fp);
|
||||
off_t end;
|
||||
if (cur < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (fseeko(fp, 0, SEEK_END) != 0) {
|
||||
return 0;
|
||||
}
|
||||
end = ftello(fp);
|
||||
(void)fseeko(fp, cur, SEEK_SET);
|
||||
if (end < 0) {
|
||||
return 0;
|
||||
}
|
||||
return (uint64_t)end;
|
||||
}
|
||||
|
||||
/* 计算百分比时统一处理分母为 0 的情况。 */
|
||||
static double rate_percent(uint64_t numerator, uint64_t denominator)
|
||||
{
|
||||
if (denominator == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return ((double)numerator * 100.0) / (double)denominator;
|
||||
}
|
||||
|
||||
static uint64_t saturating_sub_u64(uint64_t total, uint64_t delta)
|
||||
{
|
||||
return (total > delta) ? (total - delta) : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 后台接收线程:
|
||||
* - 接收服务端主动下发的命令
|
||||
* - 接收传输结束 ACK,并把结果写入原子变量
|
||||
*/
|
||||
static void *recv_thread_main(void *arg)
|
||||
{
|
||||
ClientRuntime *rt = (ClientRuntime *)arg;
|
||||
uint8_t frame[CLIENT_FRAME_BUF_SIZE];
|
||||
|
||||
/*
|
||||
* 显式启用可取消:主线程收尾时通过 pthread_cancel 打断阻塞 recv,
|
||||
* 避免 UDP/KCP 场景下因长时间无回包导致 join 卡住。
|
||||
*/
|
||||
/* 允许主线程在退出时 cancel 本线程。 */
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
||||
|
||||
@@ -158,7 +402,6 @@ static void *recv_thread_main(void *arg)
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
/* 0 在不同协议下可能代表“暂时无数据”或“对端关闭”,做短暂退避避免空转。 */
|
||||
usleep(2 * 1000);
|
||||
continue;
|
||||
}
|
||||
@@ -172,15 +415,69 @@ static void *recv_thread_main(void *arg)
|
||||
}
|
||||
|
||||
if (hdr.type == MSG_TYPE_COMMAND) {
|
||||
/* COMMAND 约定为 ASCII 文本,做安全截断后打印。 */
|
||||
/* 命令消息只是做演示打印,不参与文件传输状态机。 */
|
||||
char cmd[2048];
|
||||
size_t cpy = hdr.len < (uint32_t)(sizeof(cmd) - 1) ? hdr.len : (sizeof(cmd) - 1);
|
||||
memcpy(cmd, payload, cpy);
|
||||
cmd[cpy] = '\0';
|
||||
printf("[server-cmd] %s\n", cmd);
|
||||
fflush(stdout);
|
||||
} else if (hdr.type == MSG_TYPE_TRANSFER_ACK) {
|
||||
TransferAckMeta ack_meta;
|
||||
uint64_t rtt_ms;
|
||||
|
||||
if (hdr.len < TRANSFER_ACK_META_SIZE) {
|
||||
logger_log("WARN", "client", "short_transfer_ack len=%u", (unsigned)hdr.len);
|
||||
continue;
|
||||
}
|
||||
|
||||
omni_transfer_ack_meta_decode((const TransferAckMeta *)payload, &ack_meta);
|
||||
/* 服务端把 FILE_END 的发送时间原样回显回来,因此客户端可直接测一轮 ACK RTT。 */
|
||||
rtt_ms = omni_now_ms() - ack_meta.echoed_end_ts_ms;
|
||||
atomic_store(&rt->ack_received, 1);
|
||||
atomic_store(&rt->ack_rtt_ms, rtt_ms);
|
||||
atomic_store(&rt->ack_bytes_written, ack_meta.bytes_written);
|
||||
atomic_store(&rt->ack_transfer_id, ack_meta.transfer_id);
|
||||
logger_on_rtt(rtt_ms);
|
||||
logger_log("INFO", "client",
|
||||
"transfer_ack transfer_id=%u bytes_written=%llu rtt_ms=%llu",
|
||||
(unsigned)ack_meta.transfer_id,
|
||||
(unsigned long long)ack_meta.bytes_written,
|
||||
(unsigned long long)rtt_ms);
|
||||
} else if (hdr.type == MSG_TYPE_TIME_SYNC_RESP) {
|
||||
TimeSyncReplyMeta reply_meta;
|
||||
uint32_t expected_probe_id;
|
||||
uint64_t expected_client_send_ts_ms;
|
||||
uint64_t client_recv_ts_ms;
|
||||
uint64_t rtt_ms;
|
||||
int64_t offset_ms;
|
||||
|
||||
if (hdr.len < TIME_SYNC_REPLY_META_SIZE) {
|
||||
logger_log("WARN", "client", "short_time_sync_resp len=%u", (unsigned)hdr.len);
|
||||
continue;
|
||||
}
|
||||
|
||||
omni_time_sync_reply_meta_decode((const TimeSyncReplyMeta *)payload, &reply_meta);
|
||||
expected_probe_id = atomic_load(&rt->sync_expected_probe_id);
|
||||
expected_client_send_ts_ms = atomic_load(&rt->sync_expected_client_send_ts_ms);
|
||||
if (expected_probe_id == 0 ||
|
||||
reply_meta.probe_id != expected_probe_id ||
|
||||
reply_meta.client_send_ts_ms != expected_client_send_ts_ms) {
|
||||
logger_log("WARN", "client",
|
||||
"unexpected_time_sync_resp probe_id=%u expected_probe=%u",
|
||||
(unsigned)reply_meta.probe_id,
|
||||
(unsigned)expected_probe_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
client_recv_ts_ms = omni_now_ms();
|
||||
rtt_ms = client_recv_ts_ms - reply_meta.client_send_ts_ms;
|
||||
offset_ms = compute_server_minus_client_offset_ms(&reply_meta,
|
||||
client_recv_ts_ms);
|
||||
atomic_store(&rt->sync_reply_rtt_ms, rtt_ms);
|
||||
atomic_store(&rt->sync_reply_offset_ms, offset_ms);
|
||||
atomic_store(&rt->sync_reply_ready, 1);
|
||||
} else {
|
||||
/* 客户端当前只消费 COMMAND,其它类型保留日志便于调试。 */
|
||||
logger_log("INFO", "client",
|
||||
"recv_non_command type=%u len=%u",
|
||||
(unsigned)hdr.type, (unsigned)hdr.len);
|
||||
@@ -191,20 +488,45 @@ static void *recv_thread_main(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 客户端主流程:
|
||||
* 1) 解析参数并建立连接
|
||||
* 2) 启动接收线程
|
||||
* 3) 循环读取文件并分片发送
|
||||
* 4) 发送 FILE_END
|
||||
* 5) 等待 ACK 或超时,然后输出汇总
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
install_signal_handlers();
|
||||
|
||||
/* 命令行参数默认值。 */
|
||||
const char *proto_str = "tcp";
|
||||
const char *server_ip = NULL;
|
||||
const char *file_path = NULL;
|
||||
OmniProtocol proto;
|
||||
FILE *fp = NULL;
|
||||
OmniContext *ctx = NULL;
|
||||
pthread_t recv_tid;
|
||||
ClientRuntime rt;
|
||||
uint8_t *chunk = NULL;
|
||||
uint64_t *window_counts = NULL;
|
||||
size_t window_cap = 0;
|
||||
uint32_t total_windows = 0;
|
||||
uint64_t total_bytes = 0;
|
||||
uint32_t total_chunks = 0;
|
||||
uint32_t transfer_id;
|
||||
uint64_t total_sent = 0;
|
||||
uint64_t offset = 0;
|
||||
uint64_t transfer_start_send_ms = 0;
|
||||
uint64_t file_end_send_ts_ms = 0;
|
||||
ClockSyncResult clock_sync;
|
||||
int server_port = 0;
|
||||
int bind_port = 0;
|
||||
unsigned chunk_size = OMNI_DEFAULT_MTU;
|
||||
int wait_seconds = 2;
|
||||
|
||||
int opt;
|
||||
int exit_code = 0;
|
||||
|
||||
install_signal_handlers();
|
||||
|
||||
while ((opt = getopt(argc, argv, "p:H:P:f:b:m:w:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'p':
|
||||
@@ -239,66 +561,82 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
if (chunk_size == 0 || chunk_size > 65536u) {
|
||||
/* 约束 chunk 上限,避免一次申请/发送过大缓冲。 */
|
||||
fprintf(stderr, "invalid chunk size: %u\n", chunk_size);
|
||||
return 1;
|
||||
}
|
||||
FILE *fp = fopen(file_path, "rb");
|
||||
|
||||
fp = fopen(file_path, "rb");
|
||||
if (!fp) {
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
|
||||
OmniProtocol proto = parse_proto(proto_str);
|
||||
/* 客户端角色:对端地址由 -H/-P 指定。 */
|
||||
OmniContext *ctx = omni_init(OMNI_ROLE_CLIENT, proto,
|
||||
NULL, (uint16_t)bind_port,
|
||||
server_ip, (uint16_t)server_port);
|
||||
total_bytes = compute_file_size(fp);
|
||||
total_chunks = (chunk_size == 0) ? 0u :
|
||||
(uint32_t)((total_bytes + (uint64_t)chunk_size - 1u) / (uint64_t)chunk_size);
|
||||
transfer_id = (uint32_t)((omni_now_ms() ^ (uint64_t)getpid() ^ total_bytes) & 0xffffffffu);
|
||||
proto = parse_proto(proto_str);
|
||||
ctx = omni_init(OMNI_ROLE_CLIENT, proto,
|
||||
NULL, (uint16_t)bind_port,
|
||||
server_ip, (uint16_t)server_port);
|
||||
if (!ctx) {
|
||||
fclose(fp);
|
||||
fprintf(stderr, "omni_init failed\n");
|
||||
return 1;
|
||||
}
|
||||
logger_set_transfer_total(total_bytes);
|
||||
logger_set_progress(0);
|
||||
memset(&clock_sync, 0, sizeof(clock_sync));
|
||||
|
||||
ClientRuntime rt;
|
||||
/* 接收线程与主线程共享一个 runtime 结构。 */
|
||||
rt.ctx = ctx;
|
||||
atomic_init(&rt.running, 1);
|
||||
atomic_init(&rt.ack_received, 0);
|
||||
atomic_init(&rt.ack_rtt_ms, 0);
|
||||
atomic_init(&rt.ack_bytes_written, 0);
|
||||
atomic_init(&rt.ack_transfer_id, 0);
|
||||
atomic_init(&rt.sync_expected_probe_id, 0);
|
||||
atomic_init(&rt.sync_expected_client_send_ts_ms, 0);
|
||||
atomic_init(&rt.sync_reply_ready, 0);
|
||||
atomic_init(&rt.sync_reply_rtt_ms, 0);
|
||||
atomic_init(&rt.sync_reply_offset_ms, 0);
|
||||
|
||||
/* 启动异步接收线程(打印服务端指令)。 */
|
||||
pthread_t recv_tid;
|
||||
if (pthread_create(&recv_tid, NULL, recv_thread_main, &rt) != 0) {
|
||||
perror("pthread_create");
|
||||
atomic_store(&rt.running, 0);
|
||||
fclose(fp);
|
||||
omni_close(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t *chunk = (uint8_t *)malloc(chunk_size);
|
||||
if (perform_time_sync(ctx, &rt, &clock_sync) != OMNI_OK) {
|
||||
logger_log("WARN", "client",
|
||||
"time_sync_unavailable transfer_will_continue_without_compensated_server_metrics");
|
||||
}
|
||||
|
||||
chunk = (uint8_t *)malloc(chunk_size);
|
||||
if (!chunk) {
|
||||
logger_log("ERROR", "client", "malloc_chunk_failed size=%u", chunk_size);
|
||||
atomic_store(&rt.running, 0);
|
||||
pthread_cancel(recv_tid);
|
||||
pthread_join(recv_tid, NULL);
|
||||
omni_close(ctx);
|
||||
fclose(fp);
|
||||
omni_close(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t total_sent = 0;
|
||||
/*
|
||||
* 主发送循环:
|
||||
* - 每次读取 chunk_size 字节
|
||||
* - 发送 FILE_CHUNK
|
||||
* - EOF 后发送 FILE_END
|
||||
*/
|
||||
while (atomic_load(&rt.running)) {
|
||||
for (uint32_t seq = 1; atomic_load(&rt.running); ++seq) {
|
||||
uint64_t origin_ts_ms;
|
||||
size_t nread;
|
||||
|
||||
if (g_stop) {
|
||||
logger_log("INFO", "client", "signal_received_stop_sending");
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
break;
|
||||
}
|
||||
size_t nread = fread(chunk, 1, chunk_size, fp);
|
||||
|
||||
origin_ts_ms = omni_now_ms();
|
||||
nread = fread(chunk, 1, chunk_size, fp);
|
||||
if (nread == 0) {
|
||||
if (feof(fp)) {
|
||||
break;
|
||||
@@ -306,53 +644,201 @@ int main(int argc, char **argv)
|
||||
if (ferror(fp)) {
|
||||
logger_log("ERROR", "client", "fread_failed");
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nread > 0) {
|
||||
int rc = send_app_message(ctx, MSG_TYPE_FILE_CHUNK, chunk, (uint32_t)nread);
|
||||
if (rc != OMNI_OK) {
|
||||
uint64_t process_t0 = omni_now_ms();
|
||||
uint64_t send_ts_ms = omni_now_ms();
|
||||
uint32_t window_id;
|
||||
size_t payload_len = TRANSFER_CHUNK_META_SIZE + nread;
|
||||
uint8_t *payload = (uint8_t *)malloc(payload_len);
|
||||
TransferChunkMeta meta;
|
||||
int rc;
|
||||
|
||||
if (!payload) {
|
||||
logger_log("ERROR", "client", "malloc_payload_failed len=%zu", payload_len);
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (transfer_start_send_ms == 0) {
|
||||
transfer_start_send_ms = send_ts_ms;
|
||||
}
|
||||
/* 以“首次发送时间”为零点,将分片映射到按秒划分的发送窗口。 */
|
||||
window_id = (uint32_t)((send_ts_ms - transfer_start_send_ms) / 1000u);
|
||||
if (ensure_window_capacity(&window_counts, &window_cap, window_id) != OMNI_OK) {
|
||||
free(payload);
|
||||
logger_log("ERROR", "client", "window_counter_alloc_failed window=%u",
|
||||
(unsigned)window_id);
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
omni_transfer_chunk_meta_encode(&meta,
|
||||
transfer_id,
|
||||
seq,
|
||||
total_chunks,
|
||||
window_id,
|
||||
total_bytes,
|
||||
offset,
|
||||
(uint32_t)nread,
|
||||
origin_ts_ms);
|
||||
/* payload = chunk 元数据 + 实际文件数据。 */
|
||||
memcpy(payload, &meta, TRANSFER_CHUNK_META_SIZE);
|
||||
memcpy(payload + TRANSFER_CHUNK_META_SIZE, chunk, nread);
|
||||
logger_on_processing_latency((double)(omni_now_ms() - process_t0));
|
||||
|
||||
rc = send_app_message_with_timestamp(ctx, MSG_TYPE_FILE_CHUNK,
|
||||
payload, (uint32_t)payload_len,
|
||||
send_ts_ms);
|
||||
free(payload);
|
||||
|
||||
if (rc != OMNI_OK) {
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
window_counts[window_id]++;
|
||||
if (window_id + 1u > total_windows) {
|
||||
total_windows = window_id + 1u;
|
||||
}
|
||||
total_sent += nread;
|
||||
offset += nread;
|
||||
logger_set_progress(total_sent);
|
||||
}
|
||||
}
|
||||
|
||||
if (atomic_load(&rt.running)) {
|
||||
/* 正常结束时发送 FILE_END,通知服务端落盘完成。 */
|
||||
int rc = send_app_message(ctx, MSG_TYPE_FILE_END, NULL, 0);
|
||||
if (rc != OMNI_OK) {
|
||||
TransferEndMeta end_meta;
|
||||
file_end_send_ts_ms = omni_now_ms();
|
||||
/* FILE_END 表示“数据流已经发完”,不是文件内容本身。 */
|
||||
omni_transfer_end_meta_encode(&end_meta,
|
||||
transfer_id,
|
||||
total_chunks,
|
||||
total_bytes,
|
||||
total_windows);
|
||||
if (send_app_message_with_timestamp(ctx, MSG_TYPE_FILE_END,
|
||||
&end_meta, TRANSFER_END_META_SIZE,
|
||||
file_end_send_ts_ms) != OMNI_OK) {
|
||||
atomic_store(&rt.running, 0);
|
||||
exit_code = 1;
|
||||
}
|
||||
}
|
||||
|
||||
logger_log("INFO", "client", "file_transfer_done bytes=%llu",
|
||||
(unsigned long long)total_sent);
|
||||
free(chunk);
|
||||
fclose(fp);
|
||||
logger_log("INFO", "client",
|
||||
"file_transfer_done transfer_id=%u bytes=%llu total_chunks=%u",
|
||||
(unsigned)transfer_id,
|
||||
(unsigned long long)total_sent,
|
||||
(unsigned)total_chunks);
|
||||
|
||||
/*
|
||||
* 等待模式:
|
||||
* - wait_seconds >= 0: 发送完成后最多等待 N 秒
|
||||
* - wait_seconds < 0 : 常驻模式,直到 Ctrl+C(SIGINT)或连接异常
|
||||
*/
|
||||
if (wait_seconds < 0) {
|
||||
/* keepalive 模式:发送完成后不主动退出,便于继续观察控制消息。 */
|
||||
logger_log("INFO", "client", "keepalive_mode=on press_ctrl_c_to_exit");
|
||||
while (atomic_load(&rt.running) && !g_stop) {
|
||||
sleep(1);
|
||||
}
|
||||
} else {
|
||||
/* 普通模式:等待一小段时间给服务端回 ACK。 */
|
||||
for (int i = 0; i < wait_seconds && atomic_load(&rt.running) && !g_stop; ++i) {
|
||||
if (atomic_load(&rt.ack_received)) {
|
||||
break;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 收尾顺序:先停接收线程,再关闭网络上下文。 */
|
||||
{
|
||||
/* 输出客户端视角的最终汇总,包括发送窗口分布和 ACK 结果。 */
|
||||
OmniStats snapshot = logger_get_snapshot();
|
||||
char *window_dist = format_window_distribution(window_counts, total_windows);
|
||||
uint64_t tcp_original_bytes = saturating_sub_u64(snapshot.tcp_data_bytes_sent,
|
||||
snapshot.tcp_retrans_bytes);
|
||||
uint64_t kcp_original_bytes = saturating_sub_u64(snapshot.kcp_data_bytes_sent,
|
||||
snapshot.kcp_retrans_bytes);
|
||||
double tcp_retrans_rate = rate_percent(snapshot.tcp_retrans_bytes, tcp_original_bytes);
|
||||
double kcp_retrans_rate = rate_percent(snapshot.kcp_retrans_bytes, kcp_original_bytes);
|
||||
uint64_t ack_rtt_ms = atomic_load(&rt.ack_rtt_ms);
|
||||
uint64_t ack_bytes_written = atomic_load(&rt.ack_bytes_written);
|
||||
unsigned ack_received = (unsigned)atomic_load(&rt.ack_received);
|
||||
|
||||
logger_log("INFO", "summary",
|
||||
"event=transfer_summary role=client proto=%s transfer_id=%u "
|
||||
"total_bytes=%llu total_chunks=%u sent_bytes=%llu progress_bytes=%llu "
|
||||
"tx_avg_mbps=%.3f tx_current_mbps=%.3f rx_avg_mbps=%.3f "
|
||||
"processing_avg_ms=%.3f processing_max_ms=%.3f "
|
||||
"queue_avg_ms=%.3f transmission_avg_ms=%.3f propagation_avg_ms=%.3f "
|
||||
"last_rtt_ms=%llu min_rtt_ms=%llu "
|
||||
"send_buffer_avg_pct=%.2f recv_buffer_avg_pct=%.2f "
|
||||
"cwnd_avg=%.2f "
|
||||
"tcp_retrans=%llu tcp_data_segs_out=%llu tcp_original_bytes=%llu "
|
||||
"tcp_retrans_bytes=%llu tcp_retrans_rate_pct=%.2f "
|
||||
"kcp_retrans=%llu kcp_data_segs_out=%llu kcp_original_bytes=%llu "
|
||||
"kcp_retrans_bytes=%llu kcp_retrans_rate_pct=%.2f "
|
||||
"send_windows=%u send_window_distribution=%s "
|
||||
"ack_received=%u ack_rtt_ms=%llu ack_bytes_written=%llu "
|
||||
"clock_sync_ok=%u clock_offset_ms=%lld clock_sync_rtt_ms=%llu clock_sync_samples=%u",
|
||||
proto_str,
|
||||
(unsigned)transfer_id,
|
||||
(unsigned long long)total_bytes,
|
||||
(unsigned)total_chunks,
|
||||
(unsigned long long)total_sent,
|
||||
(unsigned long long)snapshot.progress_bytes,
|
||||
snapshot.tx_avg_mbps,
|
||||
snapshot.tx_current_mbps,
|
||||
snapshot.rx_avg_mbps,
|
||||
(snapshot.processing_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.processing_delay_ms.sum / (double)snapshot.processing_delay_ms.count,
|
||||
snapshot.processing_delay_ms.max,
|
||||
(snapshot.queue_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.queue_delay_ms.sum / (double)snapshot.queue_delay_ms.count,
|
||||
(snapshot.transmission_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.transmission_delay_ms.sum / (double)snapshot.transmission_delay_ms.count,
|
||||
(snapshot.propagation_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.propagation_delay_ms.sum / (double)snapshot.propagation_delay_ms.count,
|
||||
(unsigned long long)snapshot.last_rtt_ms,
|
||||
(unsigned long long)((snapshot.min_rtt_ms == UINT64_MAX) ? 0 : snapshot.min_rtt_ms),
|
||||
(snapshot.send_buffer_pct.count == 0) ? 0.0 :
|
||||
snapshot.send_buffer_pct.sum / (double)snapshot.send_buffer_pct.count,
|
||||
(snapshot.recv_buffer_pct.count == 0) ? 0.0 :
|
||||
snapshot.recv_buffer_pct.sum / (double)snapshot.recv_buffer_pct.count,
|
||||
(snapshot.cwnd.count == 0) ? 0.0 :
|
||||
snapshot.cwnd.sum / (double)snapshot.cwnd.count,
|
||||
(unsigned long long)snapshot.tcp_retrans,
|
||||
(unsigned long long)snapshot.tcp_data_segs_out,
|
||||
(unsigned long long)tcp_original_bytes,
|
||||
(unsigned long long)snapshot.tcp_retrans_bytes,
|
||||
tcp_retrans_rate,
|
||||
(unsigned long long)snapshot.kcp_retrans,
|
||||
(unsigned long long)snapshot.kcp_data_segs_out,
|
||||
(unsigned long long)kcp_original_bytes,
|
||||
(unsigned long long)snapshot.kcp_retrans_bytes,
|
||||
kcp_retrans_rate,
|
||||
(unsigned)total_windows,
|
||||
window_dist ? window_dist : "alloc_failed",
|
||||
ack_received,
|
||||
(unsigned long long)ack_rtt_ms,
|
||||
(unsigned long long)ack_bytes_written,
|
||||
(unsigned)clock_sync.valid,
|
||||
(long long)clock_sync.server_minus_client_offset_ms,
|
||||
(unsigned long long)clock_sync.best_rtt_ms,
|
||||
(unsigned)clock_sync.sample_count);
|
||||
free(window_dist);
|
||||
}
|
||||
|
||||
/* 主线程统一做收尾:停线程、释放缓冲、关闭文件和连接。 */
|
||||
atomic_store(&rt.running, 0);
|
||||
pthread_cancel(recv_tid);
|
||||
pthread_join(recv_tid, NULL);
|
||||
|
||||
free(window_counts);
|
||||
free(chunk);
|
||||
fclose(fp);
|
||||
omni_close(ctx);
|
||||
return 0;
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
754
src/apps/hub_main.c
Normal file
754
src/apps/hub_main.c
Normal file
@@ -0,0 +1,754 @@
|
||||
/*
|
||||
* hub_main.c
|
||||
* 云端 hub:维护 client_id -> 连接 的映射,并负责 register / bind / tunnel 路由
|
||||
*
|
||||
* 当前阶段只实现 TCP 控制面:
|
||||
* - 多个 peer 主动连接 hub
|
||||
* - peer 先 REGISTER 自己的逻辑 ID
|
||||
* - peer 可 BIND 默认目标
|
||||
* - peer 发送 TUNNEL 后,hub 根据 dst_id 转发给目标
|
||||
*
|
||||
* 后续文件/视频消息可以直接复用 MSG_TYPE_PEER_TUNNEL 的 inner_type。
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define HUB_MAX_PAYLOAD (PEER_TUNNEL_META_SIZE + 65536u)
|
||||
#define HUB_BACKLOG 64
|
||||
|
||||
typedef struct HubState HubState;
|
||||
|
||||
typedef struct HubClient {
|
||||
HubState *hub;
|
||||
int fd;
|
||||
pthread_t tid;
|
||||
pthread_mutex_t write_mu;
|
||||
atomic_int running;
|
||||
char client_id[OMNI_PEER_ID_SIZE];
|
||||
char bound_peer[OMNI_PEER_ID_SIZE];
|
||||
char remote_ip[64];
|
||||
uint16_t remote_port;
|
||||
struct HubClient *next;
|
||||
} HubClient;
|
||||
|
||||
struct HubState {
|
||||
int listen_fd;
|
||||
atomic_int running;
|
||||
pthread_mutex_t mu;
|
||||
HubClient *clients;
|
||||
};
|
||||
|
||||
static volatile sig_atomic_t g_stop = 0;
|
||||
|
||||
static void on_signal(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
g_stop = 1;
|
||||
}
|
||||
|
||||
static void install_signal_handlers(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = on_signal;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
(void)sigaction(SIGINT, &sa, NULL);
|
||||
(void)sigaction(SIGTERM, &sa, NULL);
|
||||
(void)signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage:\n"
|
||||
" %s -P <listen_port> [-b <bind_ip>] [-p tcp]\n",
|
||||
prog);
|
||||
}
|
||||
|
||||
static int peer_id_is_valid(const char *id)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
if (!id || !id[0]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (len = 0; id[len] != '\0'; ++len) {
|
||||
unsigned char ch = (unsigned char)id[len];
|
||||
|
||||
if (len + 1u >= OMNI_PEER_ID_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
if (!(isalnum(ch) || ch == '_' || ch == '-' || ch == '.')) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *safe_client_id(const HubClient *client)
|
||||
{
|
||||
if (!client || client->client_id[0] == '\0') {
|
||||
return "unregistered";
|
||||
}
|
||||
return client->client_id;
|
||||
}
|
||||
|
||||
static ssize_t read_n(int fd, void *buf, size_t n)
|
||||
{
|
||||
uint8_t *p = (uint8_t *)buf;
|
||||
size_t done = 0;
|
||||
|
||||
while (done < n) {
|
||||
ssize_t rc = recv(fd, p + done, n - done, 0);
|
||||
if (rc == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (rc < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
done += (size_t)rc;
|
||||
}
|
||||
return (ssize_t)done;
|
||||
}
|
||||
|
||||
static ssize_t write_n(int fd, const void *buf, size_t n)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)buf;
|
||||
size_t done = 0;
|
||||
|
||||
while (done < n) {
|
||||
ssize_t rc = send(fd, p + done, n - done, 0);
|
||||
if (rc < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
done += (size_t)rc;
|
||||
}
|
||||
return (ssize_t)done;
|
||||
}
|
||||
|
||||
static int recv_app_message(int fd, MsgHeader *out_hdr, uint8_t *payload_buf, size_t payload_cap)
|
||||
{
|
||||
MsgHeader net_hdr;
|
||||
ssize_t n;
|
||||
|
||||
if (!out_hdr || !payload_buf) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
|
||||
n = read_n(fd, &net_hdr, MSG_HEADER_SIZE);
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (n != (ssize_t)MSG_HEADER_SIZE) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
omni_msg_header_decode(&net_hdr, out_hdr);
|
||||
if (out_hdr->len > payload_cap) {
|
||||
logger_log("ERROR", "hub", "payload_too_large len=%u cap=%zu",
|
||||
(unsigned)out_hdr->len, payload_cap);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
if (out_hdr->len == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = read_n(fd, payload_buf, out_hdr->len);
|
||||
if (n != (ssize_t)out_hdr->len) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
logger_on_recv(MSG_HEADER_SIZE + out_hdr->len);
|
||||
logger_maybe_print_performance_log("hub_recv");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int send_app_message_locked(HubClient *client,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
MsgHeader hdr;
|
||||
uint8_t header_buf[MSG_HEADER_SIZE];
|
||||
|
||||
if (!client) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
|
||||
omni_msg_header_encode(&hdr, type, payload_len, omni_now_ms());
|
||||
memcpy(header_buf, &hdr, sizeof(header_buf));
|
||||
|
||||
if (write_n(client->fd, header_buf, sizeof(header_buf)) != (ssize_t)sizeof(header_buf)) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
if (payload_len > 0 && payload) {
|
||||
if (write_n(client->fd, payload, payload_len) != (ssize_t)payload_len) {
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
}
|
||||
|
||||
logger_on_send(MSG_HEADER_SIZE + payload_len);
|
||||
logger_maybe_print_performance_log("hub_send");
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
static int send_app_message(HubClient *client,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (!client) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&client->write_mu);
|
||||
rc = send_app_message_locked(client, type, payload, payload_len);
|
||||
pthread_mutex_unlock(&client->write_mu);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int send_status_locked(HubClient *client,
|
||||
uint32_t code,
|
||||
const char *self_id,
|
||||
const char *peer_id,
|
||||
const char *detail)
|
||||
{
|
||||
PeerStatusMeta status_meta;
|
||||
|
||||
omni_peer_status_meta_encode(&status_meta, code, self_id, peer_id, detail);
|
||||
return send_app_message_locked(client,
|
||||
MSG_TYPE_PEER_STATUS,
|
||||
&status_meta,
|
||||
PEER_STATUS_META_SIZE);
|
||||
}
|
||||
|
||||
static int send_status(HubClient *client,
|
||||
uint32_t code,
|
||||
const char *self_id,
|
||||
const char *peer_id,
|
||||
const char *detail)
|
||||
{
|
||||
PeerStatusMeta status_meta;
|
||||
|
||||
omni_peer_status_meta_encode(&status_meta, code, self_id, peer_id, detail);
|
||||
return send_app_message(client,
|
||||
MSG_TYPE_PEER_STATUS,
|
||||
&status_meta,
|
||||
PEER_STATUS_META_SIZE);
|
||||
}
|
||||
|
||||
static HubClient *find_client_locked(HubState *hub, const char *client_id)
|
||||
{
|
||||
HubClient *cur;
|
||||
|
||||
for (cur = hub->clients; cur != NULL; cur = cur->next) {
|
||||
if (cur->client_id[0] == '\0') {
|
||||
continue;
|
||||
}
|
||||
if (strcmp(cur->client_id, client_id) == 0) {
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void add_client_locked(HubState *hub, HubClient *client)
|
||||
{
|
||||
client->next = hub->clients;
|
||||
hub->clients = client;
|
||||
}
|
||||
|
||||
static void remove_client_locked(HubState *hub, HubClient *client)
|
||||
{
|
||||
HubClient **pp = &hub->clients;
|
||||
|
||||
while (*pp) {
|
||||
if (*pp == client) {
|
||||
*pp = client->next;
|
||||
client->next = NULL;
|
||||
return;
|
||||
}
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void unregister_client(HubClient *client)
|
||||
{
|
||||
HubState *hub;
|
||||
HubClient **notify = NULL;
|
||||
size_t notify_count = 0;
|
||||
size_t notify_cap = 0;
|
||||
HubClient *cur;
|
||||
char departed_id[OMNI_PEER_ID_SIZE];
|
||||
|
||||
if (!client || !client->hub) {
|
||||
return;
|
||||
}
|
||||
|
||||
hub = client->hub;
|
||||
memset(departed_id, 0, sizeof(departed_id));
|
||||
omni_copy_fixed_ascii(departed_id, sizeof(departed_id), client->client_id);
|
||||
|
||||
pthread_mutex_lock(&hub->mu);
|
||||
remove_client_locked(hub, client);
|
||||
|
||||
if (departed_id[0] != '\0') {
|
||||
for (cur = hub->clients; cur != NULL; cur = cur->next) {
|
||||
if (strcmp(cur->bound_peer, departed_id) != 0) {
|
||||
continue;
|
||||
}
|
||||
if (notify_count == notify_cap) {
|
||||
size_t new_cap = (notify_cap == 0) ? 4u : notify_cap * 2u;
|
||||
HubClient **new_notify =
|
||||
(HubClient **)realloc(notify, new_cap * sizeof(*new_notify));
|
||||
if (!new_notify) {
|
||||
break;
|
||||
}
|
||||
notify = new_notify;
|
||||
notify_cap = new_cap;
|
||||
}
|
||||
cur->bound_peer[0] = '\0';
|
||||
pthread_mutex_lock(&cur->write_mu);
|
||||
notify[notify_count++] = cur;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&hub->mu);
|
||||
|
||||
for (size_t i = 0; i < notify_count; ++i) {
|
||||
(void)send_status_locked(notify[i],
|
||||
PEER_STATUS_UNBOUND,
|
||||
notify[i]->client_id,
|
||||
departed_id,
|
||||
"peer_offline binding_cleared");
|
||||
pthread_mutex_unlock(¬ify[i]->write_mu);
|
||||
}
|
||||
free(notify);
|
||||
}
|
||||
|
||||
static void close_client(HubClient *client)
|
||||
{
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&client->write_mu);
|
||||
if (client->fd >= 0) {
|
||||
shutdown(client->fd, SHUT_RDWR);
|
||||
close(client->fd);
|
||||
client->fd = -1;
|
||||
}
|
||||
pthread_mutex_unlock(&client->write_mu);
|
||||
}
|
||||
|
||||
static int handle_register(HubClient *client, const uint8_t *payload, uint32_t payload_len)
|
||||
{
|
||||
PeerRegisterMeta register_meta;
|
||||
char detail[128];
|
||||
|
||||
logger_log("DEBUG", "hub",
|
||||
"handle_register remote=%s:%u payload_len=%u",
|
||||
client->remote_ip,
|
||||
(unsigned)client->remote_port,
|
||||
(unsigned)payload_len);
|
||||
|
||||
if (payload_len < PEER_REGISTER_META_SIZE) {
|
||||
return send_status(client, PEER_STATUS_ERROR, NULL, NULL, "short_register_payload");
|
||||
}
|
||||
|
||||
omni_peer_register_meta_decode((const PeerRegisterMeta *)payload, ®ister_meta);
|
||||
if (!peer_id_is_valid(register_meta.client_id)) {
|
||||
return send_status(client, PEER_STATUS_ERROR, NULL, NULL, "invalid_client_id");
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&client->hub->mu);
|
||||
if (client->client_id[0] != '\0') {
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, NULL, "already_registered");
|
||||
}
|
||||
if (find_client_locked(client->hub, register_meta.client_id) != NULL) {
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
return send_status(client, PEER_STATUS_ERROR, NULL, register_meta.client_id, "client_id_in_use");
|
||||
}
|
||||
omni_copy_fixed_ascii(client->client_id, sizeof(client->client_id), register_meta.client_id);
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
|
||||
snprintf(detail, sizeof(detail), "registered remote=%s:%u",
|
||||
client->remote_ip, (unsigned)client->remote_port);
|
||||
logger_log("INFO", "hub",
|
||||
"client_registered client_id=%s remote=%s:%u",
|
||||
client->client_id,
|
||||
client->remote_ip,
|
||||
(unsigned)client->remote_port);
|
||||
return send_status(client,
|
||||
PEER_STATUS_REGISTERED,
|
||||
client->client_id,
|
||||
NULL,
|
||||
detail);
|
||||
}
|
||||
|
||||
static int handle_bind(HubClient *client, const uint8_t *payload, uint32_t payload_len)
|
||||
{
|
||||
PeerBindMeta bind_meta;
|
||||
HubClient *target;
|
||||
|
||||
logger_log("DEBUG", "hub",
|
||||
"handle_bind client_id=%s payload_len=%u",
|
||||
safe_client_id(client),
|
||||
(unsigned)payload_len);
|
||||
|
||||
if (client->client_id[0] == '\0') {
|
||||
return send_status(client, PEER_STATUS_ERROR, NULL, NULL, "register_first");
|
||||
}
|
||||
if (payload_len < PEER_BIND_META_SIZE) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, NULL, "short_bind_payload");
|
||||
}
|
||||
|
||||
omni_peer_bind_meta_decode((const PeerBindMeta *)payload, &bind_meta);
|
||||
if (!peer_id_is_valid(bind_meta.peer_id)) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, NULL, "invalid_peer_id");
|
||||
}
|
||||
if (strcmp(bind_meta.peer_id, client->client_id) == 0) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, bind_meta.peer_id, "cannot_bind_self");
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&client->hub->mu);
|
||||
target = find_client_locked(client->hub, bind_meta.peer_id);
|
||||
if (!target) {
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, bind_meta.peer_id, "peer_not_online");
|
||||
}
|
||||
omni_copy_fixed_ascii(client->bound_peer, sizeof(client->bound_peer), bind_meta.peer_id);
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
|
||||
logger_log("INFO", "hub",
|
||||
"peer_bound client_id=%s peer_id=%s",
|
||||
client->client_id,
|
||||
bind_meta.peer_id);
|
||||
return send_status(client,
|
||||
PEER_STATUS_BOUND,
|
||||
client->client_id,
|
||||
bind_meta.peer_id,
|
||||
"bind_ok");
|
||||
}
|
||||
|
||||
static int handle_tunnel(HubClient *client, const uint8_t *payload, uint32_t payload_len)
|
||||
{
|
||||
PeerTunnelMeta tunnel_meta;
|
||||
HubClient *target = NULL;
|
||||
char effective_dst[OMNI_PEER_ID_SIZE];
|
||||
uint8_t *forward_payload = NULL;
|
||||
uint32_t inner_len;
|
||||
int rc = OMNI_OK;
|
||||
|
||||
logger_log("DEBUG", "hub",
|
||||
"handle_tunnel client_id=%s payload_len=%u",
|
||||
safe_client_id(client),
|
||||
(unsigned)payload_len);
|
||||
|
||||
if (client->client_id[0] == '\0') {
|
||||
return send_status(client, PEER_STATUS_ERROR, NULL, NULL, "register_first");
|
||||
}
|
||||
if (payload_len < PEER_TUNNEL_META_SIZE) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, NULL, "short_tunnel_payload");
|
||||
}
|
||||
|
||||
omni_peer_tunnel_meta_decode((const PeerTunnelMeta *)payload, &tunnel_meta);
|
||||
inner_len = payload_len - PEER_TUNNEL_META_SIZE;
|
||||
memset(effective_dst, 0, sizeof(effective_dst));
|
||||
|
||||
pthread_mutex_lock(&client->hub->mu);
|
||||
if (tunnel_meta.dst_id[0] != '\0') {
|
||||
omni_copy_fixed_ascii(effective_dst, sizeof(effective_dst), tunnel_meta.dst_id);
|
||||
} else {
|
||||
omni_copy_fixed_ascii(effective_dst, sizeof(effective_dst), client->bound_peer);
|
||||
}
|
||||
|
||||
if (effective_dst[0] != '\0') {
|
||||
target = find_client_locked(client->hub, effective_dst);
|
||||
if (target) {
|
||||
pthread_mutex_lock(&target->write_mu);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&client->hub->mu);
|
||||
|
||||
if (!peer_id_is_valid(effective_dst)) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, NULL, "missing_or_invalid_destination");
|
||||
}
|
||||
if (!target) {
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, effective_dst, "destination_not_online");
|
||||
}
|
||||
|
||||
forward_payload = (uint8_t *)malloc(payload_len);
|
||||
if (!forward_payload) {
|
||||
pthread_mutex_unlock(&target->write_mu);
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, effective_dst, "malloc_forward_payload_failed");
|
||||
}
|
||||
|
||||
{
|
||||
PeerTunnelMeta forward_meta;
|
||||
|
||||
omni_peer_tunnel_meta_encode(&forward_meta,
|
||||
client->client_id,
|
||||
effective_dst,
|
||||
tunnel_meta.inner_type);
|
||||
memcpy(forward_payload, &forward_meta, PEER_TUNNEL_META_SIZE);
|
||||
if (inner_len > 0) {
|
||||
memcpy(forward_payload + PEER_TUNNEL_META_SIZE,
|
||||
payload + PEER_TUNNEL_META_SIZE,
|
||||
inner_len);
|
||||
}
|
||||
}
|
||||
|
||||
rc = send_app_message_locked(target,
|
||||
MSG_TYPE_PEER_TUNNEL,
|
||||
forward_payload,
|
||||
payload_len);
|
||||
pthread_mutex_unlock(&target->write_mu);
|
||||
free(forward_payload);
|
||||
|
||||
if (rc != OMNI_OK) {
|
||||
logger_log("ERROR", "hub",
|
||||
"forward_failed src_id=%s dst_id=%s inner_type=%u",
|
||||
client->client_id,
|
||||
effective_dst,
|
||||
(unsigned)tunnel_meta.inner_type);
|
||||
return send_status(client, PEER_STATUS_ERROR, client->client_id, effective_dst, "forward_failed");
|
||||
}
|
||||
|
||||
logger_log("INFO", "hub",
|
||||
"forward_ok src_id=%s dst_id=%s inner_type=%u payload_bytes=%u",
|
||||
client->client_id,
|
||||
effective_dst,
|
||||
(unsigned)tunnel_meta.inner_type,
|
||||
(unsigned)inner_len);
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
static void *client_thread_main(void *arg)
|
||||
{
|
||||
HubClient *client = (HubClient *)arg;
|
||||
uint8_t payload[HUB_MAX_PAYLOAD];
|
||||
|
||||
while (atomic_load(&client->hub->running) && atomic_load(&client->running)) {
|
||||
MsgHeader hdr;
|
||||
int rc = recv_app_message(client->fd, &hdr, payload, sizeof(payload));
|
||||
|
||||
if (rc == 0) {
|
||||
logger_log("INFO", "hub",
|
||||
"client_closed client_id=%s remote=%s:%u",
|
||||
safe_client_id(client),
|
||||
client->remote_ip,
|
||||
(unsigned)client->remote_port);
|
||||
break;
|
||||
}
|
||||
if (rc < 0) {
|
||||
logger_log("ERROR", "hub",
|
||||
"client_recv_failed client_id=%s remote=%s:%u rc=%d",
|
||||
safe_client_id(client),
|
||||
client->remote_ip,
|
||||
(unsigned)client->remote_port,
|
||||
rc);
|
||||
break;
|
||||
}
|
||||
|
||||
logger_log("DEBUG", "hub",
|
||||
"message_recv client_id=%s type=%u len=%u",
|
||||
safe_client_id(client),
|
||||
(unsigned)hdr.type,
|
||||
(unsigned)hdr.len);
|
||||
|
||||
switch (hdr.type) {
|
||||
case MSG_TYPE_PEER_REGISTER:
|
||||
(void)handle_register(client, payload, hdr.len);
|
||||
break;
|
||||
case MSG_TYPE_PEER_BIND:
|
||||
(void)handle_bind(client, payload, hdr.len);
|
||||
break;
|
||||
case MSG_TYPE_PEER_TUNNEL:
|
||||
(void)handle_tunnel(client, payload, hdr.len);
|
||||
break;
|
||||
default:
|
||||
(void)send_status(client,
|
||||
PEER_STATUS_ERROR,
|
||||
client->client_id[0] ? client->client_id : NULL,
|
||||
NULL,
|
||||
"unsupported_message_type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
atomic_store(&client->running, 0);
|
||||
unregister_client(client);
|
||||
close_client(client);
|
||||
pthread_mutex_destroy(&client->write_mu);
|
||||
free(client);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int create_listen_socket(const char *bind_ip, uint16_t port)
|
||||
{
|
||||
int fd;
|
||||
int reuse = 1;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
if (bind_ip && bind_ip[0] != '\0') {
|
||||
if (inet_pton(AF_INET, bind_ip, &addr.sin_addr) != 1) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
}
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (listen(fd, HUB_BACKLOG) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *bind_ip = NULL;
|
||||
const char *proto_str = "tcp";
|
||||
int listen_port = 0;
|
||||
int opt;
|
||||
HubState hub;
|
||||
|
||||
while ((opt = getopt(argc, argv, "b:p:P:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'b':
|
||||
bind_ip = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
proto_str = optarg;
|
||||
break;
|
||||
case 'P':
|
||||
listen_port = atoi(optarg);
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (listen_port <= 0 || strcmp(proto_str, "tcp") != 0) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
logger_init();
|
||||
install_signal_handlers();
|
||||
|
||||
memset(&hub, 0, sizeof(hub));
|
||||
atomic_init(&hub.running, 1);
|
||||
pthread_mutex_init(&hub.mu, NULL);
|
||||
|
||||
hub.listen_fd = create_listen_socket(bind_ip, (uint16_t)listen_port);
|
||||
if (hub.listen_fd < 0) {
|
||||
perror("hub listen");
|
||||
pthread_mutex_destroy(&hub.mu);
|
||||
return 1;
|
||||
}
|
||||
|
||||
logger_log("INFO", "hub", "listening bind_ip=%s port=%u",
|
||||
bind_ip ? bind_ip : "0.0.0.0",
|
||||
(unsigned)listen_port);
|
||||
|
||||
while (atomic_load(&hub.running) && !g_stop) {
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len = sizeof(peer_addr);
|
||||
int cfd = accept(hub.listen_fd, (struct sockaddr *)&peer_addr, &peer_len);
|
||||
|
||||
if (cfd < 0) {
|
||||
if (errno == EINTR && !g_stop) {
|
||||
continue;
|
||||
}
|
||||
if (g_stop) {
|
||||
break;
|
||||
}
|
||||
perror("hub accept");
|
||||
break;
|
||||
}
|
||||
|
||||
HubClient *client = (HubClient *)calloc(1, sizeof(*client));
|
||||
if (!client) {
|
||||
close(cfd);
|
||||
continue;
|
||||
}
|
||||
|
||||
client->hub = &hub;
|
||||
client->fd = cfd;
|
||||
atomic_init(&client->running, 1);
|
||||
pthread_mutex_init(&client->write_mu, NULL);
|
||||
if (!inet_ntop(AF_INET, &peer_addr.sin_addr, client->remote_ip, sizeof(client->remote_ip))) {
|
||||
omni_copy_fixed_ascii(client->remote_ip, sizeof(client->remote_ip), "unknown");
|
||||
}
|
||||
client->remote_port = ntohs(peer_addr.sin_port);
|
||||
|
||||
pthread_mutex_lock(&hub.mu);
|
||||
add_client_locked(&hub, client);
|
||||
pthread_mutex_unlock(&hub.mu);
|
||||
|
||||
if (pthread_create(&client->tid, NULL, client_thread_main, client) != 0) {
|
||||
perror("hub pthread_create");
|
||||
unregister_client(client);
|
||||
close_client(client);
|
||||
pthread_mutex_destroy(&client->write_mu);
|
||||
free(client);
|
||||
continue;
|
||||
}
|
||||
pthread_detach(client->tid);
|
||||
|
||||
logger_log("INFO", "hub",
|
||||
"client_connected remote=%s:%u",
|
||||
client->remote_ip,
|
||||
(unsigned)client->remote_port);
|
||||
}
|
||||
|
||||
atomic_store(&hub.running, 0);
|
||||
if (hub.listen_fd >= 0) {
|
||||
close(hub.listen_fd);
|
||||
}
|
||||
logger_print_performance_log("final");
|
||||
return 0;
|
||||
}
|
||||
1051
src/apps/peer_main.c
Normal file
1051
src/apps/peer_main.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -27,7 +27,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define RELAY_BUF_SIZE (MSG_HEADER_SIZE + 65536u)
|
||||
#define RELAY_BUF_SIZE (MSG_HEADER_SIZE + TRANSFER_CHUNK_META_SIZE + 65536u)
|
||||
|
||||
typedef struct RelayState {
|
||||
/* 当前 relay 工作协议。 */
|
||||
@@ -53,6 +53,7 @@ static void usage(const char *prog)
|
||||
prog);
|
||||
}
|
||||
|
||||
/* 将命令行协议字符串映射为内部协议枚举。 */
|
||||
static OmniProtocol parse_proto(const char *s)
|
||||
{
|
||||
/* 非法输入回退 TCP。 */
|
||||
@@ -63,6 +64,7 @@ static OmniProtocol parse_proto(const char *s)
|
||||
return OMNI_PROTO_TCP;
|
||||
}
|
||||
|
||||
/* 在运行期安全地切换下游转发目标。 */
|
||||
static int relay_set_target(RelayState *st, const char *ip, uint16_t port)
|
||||
{
|
||||
/*
|
||||
@@ -95,6 +97,7 @@ static int relay_set_target(RelayState *st, const char *ip, uint16_t port)
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 控制线程:解析 stdin 指令并驱动 show / set / quit。 */
|
||||
static void *control_thread_main(void *arg)
|
||||
{
|
||||
/* 控制线程负责解析 stdin 命令。 */
|
||||
@@ -153,6 +156,12 @@ static void *control_thread_main(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* relay 主流程:
|
||||
* - 上游用 server 角色收数据
|
||||
* - 下游用 client 角色发数据
|
||||
* - 主线程搬运数据,控制线程负责改目标
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* 命令行参数默认值。 */
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
/*
|
||||
* server_main.c
|
||||
* 服务端:接收文件写盘;主线程监听键盘输入并发送 ASCII 指令到客户端
|
||||
*
|
||||
* 线程模型:
|
||||
* - 接收线程:持续收业务帧,写入文件,直到 FILE_END
|
||||
* - 主线程:在交互终端下读取 stdin,发送 COMMAND 给客户端
|
||||
*
|
||||
* 说明:
|
||||
* - 当 stdin 不是 TTY(例如被脚本后台拉起)时,主线程不做交互输入,
|
||||
* 仅等待接收线程完成传输,便于自动化测试稳定运行。
|
||||
* 服务端:接收文件写盘,输出结构化传输统计
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
@@ -22,23 +14,51 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SERVER_FRAME_BUF_SIZE (MSG_HEADER_SIZE + 65536u)
|
||||
#define SERVER_FRAME_BUF_SIZE (MSG_HEADER_SIZE + TRANSFER_CHUNK_META_SIZE + 65536u)
|
||||
|
||||
/*
|
||||
* 一次文件传输在服务端视角下的累计状态。
|
||||
* 它主要用来回答三个问题:
|
||||
* 1) 收到了多少唯一分片
|
||||
* 2) 有没有重复/乱序/缺失
|
||||
* 3) 最终写盘了多少字节
|
||||
*/
|
||||
typedef struct TransferState {
|
||||
uint32_t transfer_id; /* 当前传输 ID。 */
|
||||
uint32_t total_chunks; /* 期望收到的总分片数。 */
|
||||
uint32_t total_windows; /* 发送端告诉我们的总窗口数。 */
|
||||
uint64_t total_bytes; /* 期望收到的总字节数。 */
|
||||
uint32_t highest_seq_seen; /* 目前见过的最大序号,用于判断乱序。 */
|
||||
uint32_t unique_chunks; /* 成功去重后收到的分片数。 */
|
||||
uint32_t duplicate_chunks; /* 重复到达的分片数。 */
|
||||
uint32_t out_of_order_chunks; /* 序号回退的分片数,表示存在乱序。 */
|
||||
uint64_t unique_bytes_written; /* 实际写盘的唯一数据量。 */
|
||||
uint8_t *seen; /* 位图/标记数组:某个 seq 是否已收到。 */
|
||||
size_t seen_cap; /* seen 数组容量。 */
|
||||
uint64_t *recv_window_counts; /* 每个时间窗口收到的分片数。 */
|
||||
size_t recv_window_cap; /* recv_window_counts 容量。 */
|
||||
} TransferState;
|
||||
|
||||
typedef struct ClockSyncState {
|
||||
int valid; /* 是否已经拿到客户端确认的 offset。 */
|
||||
int64_t server_minus_client_offset_ms; /* server_time - client_time */
|
||||
uint64_t best_rtt_ms; /* 客户端选中的最优 RTT 样本。 */
|
||||
uint32_t sample_count; /* 参与选择的有效样本数。 */
|
||||
} ClockSyncState;
|
||||
|
||||
/* 服务端运行期上下文:连接、写盘目标、线程状态和传输统计都放在这里。 */
|
||||
typedef struct ServerRuntime {
|
||||
/* 协议抽象层句柄。 */
|
||||
OmniContext *ctx;
|
||||
/* 当前运行协议,用于区分 recv 返回 0 的语义(TCP=对端关闭)。 */
|
||||
OmniProtocol proto;
|
||||
/* 接收文件写入目标。 */
|
||||
FILE *out_fp;
|
||||
/* 全局运行标记。 */
|
||||
atomic_int running;
|
||||
/* 收到 FILE_END 后置 1。 */
|
||||
atomic_int transfer_done;
|
||||
/* 已成功写入的文件字节数。 */
|
||||
uint64_t bytes_written;
|
||||
TransferState transfer;
|
||||
ClockSyncState clock_sync;
|
||||
} ServerRuntime;
|
||||
|
||||
/* 打印服务端命令行帮助。 */
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
@@ -47,9 +67,9 @@ static void usage(const char *prog)
|
||||
prog);
|
||||
}
|
||||
|
||||
/* 将字符串协议名解析为内部枚举,非法值默认回退 TCP。 */
|
||||
static OmniProtocol parse_proto(const char *s)
|
||||
{
|
||||
/* 输入不合法时回退到 TCP。 */
|
||||
if (!s) return OMNI_PROTO_TCP;
|
||||
if (strcmp(s, "tcp") == 0) return OMNI_PROTO_TCP;
|
||||
if (strcmp(s, "udp") == 0) return OMNI_PROTO_UDP;
|
||||
@@ -57,12 +77,13 @@ static OmniProtocol parse_proto(const char *s)
|
||||
return OMNI_PROTO_TCP;
|
||||
}
|
||||
|
||||
static int send_app_message(OmniContext *ctx,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len)
|
||||
/* 与客户端对称:负责发送完整业务帧。 */
|
||||
static int send_app_message_with_timestamp(OmniContext *ctx,
|
||||
uint32_t type,
|
||||
const void *payload,
|
||||
uint32_t payload_len,
|
||||
uint64_t timestamp_ms)
|
||||
{
|
||||
/* 与客户端保持一致的统一发包函数。 */
|
||||
size_t total_len = MSG_HEADER_SIZE + (size_t)payload_len;
|
||||
uint8_t *frame = (uint8_t *)malloc(total_len);
|
||||
if (!frame) {
|
||||
@@ -71,7 +92,7 @@ static int send_app_message(OmniContext *ctx,
|
||||
}
|
||||
|
||||
MsgHeader hdr;
|
||||
omni_msg_header_encode(&hdr, type, payload_len, omni_now_ms());
|
||||
omni_msg_header_encode(&hdr, type, payload_len, timestamp_ms);
|
||||
memcpy(frame, &hdr, MSG_HEADER_SIZE);
|
||||
if (payload_len > 0 && payload) {
|
||||
memcpy(frame + MSG_HEADER_SIZE, payload, payload_len);
|
||||
@@ -89,12 +110,12 @@ static int send_app_message(OmniContext *ctx,
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 与客户端对称:拆包并校验应用层帧长度。 */
|
||||
static int decode_app_message(const uint8_t *frame,
|
||||
size_t frame_len,
|
||||
MsgHeader *out_hdr,
|
||||
const uint8_t **out_payload)
|
||||
{
|
||||
/* 与客户端一致的统一解包校验。 */
|
||||
if (!frame || frame_len < MSG_HEADER_SIZE || !out_hdr || !out_payload) {
|
||||
return OMNI_ERR_PARAM;
|
||||
}
|
||||
@@ -111,12 +132,463 @@ static int decode_app_message(const uint8_t *frame,
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 用客户端确认的 offset,把 origin_ts_ms 转成服务端时钟系并计算端到端时延。 */
|
||||
static double compute_compensated_end_to_end_ms(const ServerRuntime *rt,
|
||||
uint64_t server_recv_ts_ms,
|
||||
const TransferChunkMeta *meta)
|
||||
{
|
||||
int64_t e2e_ms;
|
||||
|
||||
if (!rt || !meta || !rt->clock_sync.valid) {
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
e2e_ms = (int64_t)server_recv_ts_ms -
|
||||
((int64_t)meta->origin_ts_ms + rt->clock_sync.server_minus_client_offset_ms);
|
||||
if (e2e_ms < 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return (double)e2e_ms;
|
||||
}
|
||||
|
||||
/* 扩容 seen 标记数组,下标直接使用 seq 值。 */
|
||||
static int ensure_seen_capacity(TransferState *st, uint32_t total_chunks)
|
||||
{
|
||||
size_t need = (size_t)total_chunks + 1u;
|
||||
uint8_t *new_seen;
|
||||
|
||||
if (need <= st->seen_cap) {
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
new_seen = (uint8_t *)realloc(st->seen, need);
|
||||
if (!new_seen) {
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
memset(new_seen + st->seen_cap, 0, need - st->seen_cap);
|
||||
st->seen = new_seen;
|
||||
st->seen_cap = need;
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 扩容按窗口聚合的接收计数数组。 */
|
||||
static int ensure_window_capacity(uint64_t **counts, size_t *cap, uint32_t window_id)
|
||||
{
|
||||
size_t need = (size_t)window_id + 1u;
|
||||
size_t new_cap;
|
||||
uint64_t *new_counts;
|
||||
|
||||
if (need <= *cap) {
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
new_cap = (*cap == 0) ? 8u : *cap;
|
||||
while (new_cap < need) {
|
||||
new_cap *= 2u;
|
||||
}
|
||||
|
||||
new_counts = (uint64_t *)realloc(*counts, new_cap * sizeof(uint64_t));
|
||||
if (!new_counts) {
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
|
||||
memset(new_counts + *cap, 0, (new_cap - *cap) * sizeof(uint64_t));
|
||||
*counts = new_counts;
|
||||
*cap = new_cap;
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 将接收窗口分布转成日志友好的字符串。 */
|
||||
static char *format_window_distribution(const uint64_t *counts, uint32_t total_windows)
|
||||
{
|
||||
size_t cap = 256;
|
||||
size_t len = 0;
|
||||
char *buf = (char *)malloc(cap);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
buf[0] = '\0';
|
||||
|
||||
for (uint32_t i = 0; i < total_windows; ++i) {
|
||||
char tmp[64];
|
||||
int n;
|
||||
if (counts[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
n = snprintf(tmp, sizeof(tmp), "%s%u:%llu",
|
||||
(len == 0) ? "" : ",",
|
||||
(unsigned)i,
|
||||
(unsigned long long)counts[i]);
|
||||
if (n <= 0) {
|
||||
continue;
|
||||
}
|
||||
while (len + (size_t)n + 1 > cap) {
|
||||
char *new_buf;
|
||||
cap *= 2u;
|
||||
new_buf = (char *)realloc(buf, cap);
|
||||
if (!new_buf) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
buf = new_buf;
|
||||
}
|
||||
memcpy(buf + len, tmp, (size_t)n);
|
||||
len += (size_t)n;
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
snprintf(buf, cap, "none");
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
static uint64_t saturating_sub_u64(uint64_t total, uint64_t delta)
|
||||
{
|
||||
return (total > delta) ? (total - delta) : 0;
|
||||
}
|
||||
|
||||
static double rate_percent(uint64_t numerator, uint64_t denominator)
|
||||
{
|
||||
if (denominator == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return ((double)numerator * 100.0) / (double)denominator;
|
||||
}
|
||||
|
||||
/*
|
||||
* 根据 seen 位图生成缺失区间字符串,例如 "3-5,9,12-14"。
|
||||
* 同时顺手统计缺失 chunk 数、突发丢包次数和最长突发长度。
|
||||
*/
|
||||
static char *format_missing_ranges(const TransferState *st,
|
||||
uint32_t *out_missing_chunks,
|
||||
uint32_t *out_burst_count,
|
||||
uint32_t *out_max_burst_len)
|
||||
{
|
||||
size_t cap = 256;
|
||||
size_t len = 0;
|
||||
char *buf = (char *)malloc(cap);
|
||||
uint32_t missing_chunks = 0;
|
||||
uint32_t burst_count = 0;
|
||||
uint32_t max_burst_len = 0;
|
||||
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
buf[0] = '\0';
|
||||
|
||||
if (st->total_chunks == 0 || !st->seen) {
|
||||
snprintf(buf, cap, "none");
|
||||
if (out_missing_chunks) *out_missing_chunks = 0;
|
||||
if (out_burst_count) *out_burst_count = 0;
|
||||
if (out_max_burst_len) *out_max_burst_len = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
for (uint32_t seq = 1; seq <= st->total_chunks; ++seq) {
|
||||
if (st->seen[seq] != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t start = seq;
|
||||
uint32_t end = seq;
|
||||
char tmp[64];
|
||||
int n;
|
||||
|
||||
/* 把连续缺失的 seq 合并成一个区间,便于读日志时看出 burst loss。 */
|
||||
while (end + 1u <= st->total_chunks && st->seen[end + 1u] == 0) {
|
||||
end++;
|
||||
}
|
||||
|
||||
missing_chunks += (end - start + 1u);
|
||||
burst_count++;
|
||||
if ((end - start + 1u) > max_burst_len) {
|
||||
max_burst_len = end - start + 1u;
|
||||
}
|
||||
|
||||
if (start == end) {
|
||||
n = snprintf(tmp, sizeof(tmp), "%s%u",
|
||||
(len == 0) ? "" : ",",
|
||||
(unsigned)start);
|
||||
} else {
|
||||
n = snprintf(tmp, sizeof(tmp), "%s%u-%u",
|
||||
(len == 0) ? "" : ",",
|
||||
(unsigned)start,
|
||||
(unsigned)end);
|
||||
}
|
||||
if (n > 0) {
|
||||
while (len + (size_t)n + 1 > cap) {
|
||||
char *new_buf;
|
||||
cap *= 2u;
|
||||
new_buf = (char *)realloc(buf, cap);
|
||||
if (!new_buf) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
buf = new_buf;
|
||||
}
|
||||
memcpy(buf + len, tmp, (size_t)n);
|
||||
len += (size_t)n;
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
seq = end;
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
snprintf(buf, cap, "none");
|
||||
}
|
||||
|
||||
if (out_missing_chunks) *out_missing_chunks = missing_chunks;
|
||||
if (out_burst_count) *out_burst_count = burst_count;
|
||||
if (out_max_burst_len) *out_max_burst_len = max_burst_len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* 用首个有效分片补齐传输的基础元数据,后续分片只做一致性延续。 */
|
||||
static void transfer_state_init(TransferState *st, const TransferChunkMeta *meta)
|
||||
{
|
||||
if (st->transfer_id == 0) {
|
||||
st->transfer_id = meta->transfer_id;
|
||||
}
|
||||
if (st->total_chunks == 0) {
|
||||
st->total_chunks = meta->total_chunks;
|
||||
}
|
||||
if (st->total_bytes == 0) {
|
||||
st->total_bytes = meta->total_bytes;
|
||||
}
|
||||
if (meta->window_id + 1u > st->total_windows) {
|
||||
st->total_windows = meta->window_id + 1u;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 处理一个 FILE_CHUNK:
|
||||
* - 校验元数据
|
||||
* - 去重 / 统计乱序
|
||||
* - 估算时延拆分
|
||||
* - 按 offset 随机写盘
|
||||
*/
|
||||
static int server_record_chunk(ServerRuntime *rt,
|
||||
const MsgHeader *hdr,
|
||||
const uint8_t *payload,
|
||||
uint32_t payload_len)
|
||||
{
|
||||
TransferChunkMeta meta;
|
||||
const uint8_t *chunk_data;
|
||||
uint64_t process_t0;
|
||||
|
||||
(void)hdr;
|
||||
|
||||
if (payload_len < TRANSFER_CHUNK_META_SIZE) {
|
||||
logger_log("ERROR", "server", "short_chunk_payload len=%u", (unsigned)payload_len);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
process_t0 = omni_now_ms();
|
||||
omni_transfer_chunk_meta_decode((const TransferChunkMeta *)payload, &meta);
|
||||
if (meta.chunk_bytes > payload_len - TRANSFER_CHUNK_META_SIZE) {
|
||||
logger_log("ERROR", "server",
|
||||
"invalid_chunk_meta transfer_id=%u seq=%u chunk_bytes=%u payload_len=%u",
|
||||
(unsigned)meta.transfer_id,
|
||||
(unsigned)meta.seq,
|
||||
(unsigned)meta.chunk_bytes,
|
||||
(unsigned)payload_len);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
transfer_state_init(&rt->transfer, &meta);
|
||||
if (ensure_seen_capacity(&rt->transfer, meta.total_chunks) != OMNI_OK) {
|
||||
logger_log("ERROR", "server", "seen_bitmap_alloc_failed total_chunks=%u",
|
||||
(unsigned)meta.total_chunks);
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
if (ensure_window_capacity(&rt->transfer.recv_window_counts,
|
||||
&rt->transfer.recv_window_cap,
|
||||
meta.window_id) != OMNI_OK) {
|
||||
logger_log("ERROR", "server", "recv_window_alloc_failed window=%u",
|
||||
(unsigned)meta.window_id);
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
|
||||
if (meta.seq == 0 || meta.seq > meta.total_chunks) {
|
||||
logger_log("WARN", "server", "invalid_seq transfer_id=%u seq=%u total_chunks=%u",
|
||||
(unsigned)meta.transfer_id,
|
||||
(unsigned)meta.seq,
|
||||
(unsigned)meta.total_chunks);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
/* seen 位图用于去重:重复片不再写盘,但会计入 duplicate_chunks。 */
|
||||
if (rt->transfer.seen[meta.seq] != 0) {
|
||||
rt->transfer.duplicate_chunks++;
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
rt->transfer.seen[meta.seq] = 1;
|
||||
rt->transfer.unique_chunks++;
|
||||
rt->transfer.recv_window_counts[meta.window_id]++;
|
||||
/* 若当前序号小于历史最大序号,说明该片到达顺序发生了回退。 */
|
||||
if (meta.seq < rt->transfer.highest_seq_seen) {
|
||||
rt->transfer.out_of_order_chunks++;
|
||||
}
|
||||
if (meta.seq > rt->transfer.highest_seq_seen) {
|
||||
rt->transfer.highest_seq_seen = meta.seq;
|
||||
}
|
||||
|
||||
{
|
||||
double e2e_ms = compute_compensated_end_to_end_ms(rt, process_t0, &meta);
|
||||
if (e2e_ms >= 0.0) {
|
||||
logger_on_end_to_end_latency(e2e_ms);
|
||||
}
|
||||
}
|
||||
|
||||
logger_set_transfer_total(meta.total_bytes);
|
||||
chunk_data = payload + TRANSFER_CHUNK_META_SIZE;
|
||||
/*
|
||||
* 必须按 offset 写盘,而不是简单 append。
|
||||
* 这样即便 UDP/KCP 发生乱序,输出文件仍能恢复到正确位置。
|
||||
*/
|
||||
if (fseeko(rt->out_fp, (off_t)meta.offset_bytes, SEEK_SET) != 0) {
|
||||
logger_log("ERROR", "server",
|
||||
"fseeko_failed transfer_id=%u seq=%u offset=%llu",
|
||||
(unsigned)meta.transfer_id,
|
||||
(unsigned)meta.seq,
|
||||
(unsigned long long)meta.offset_bytes);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
if (fwrite(chunk_data, 1, meta.chunk_bytes, rt->out_fp) != meta.chunk_bytes) {
|
||||
logger_log("ERROR", "server",
|
||||
"fwrite_failed transfer_id=%u seq=%u chunk_bytes=%u",
|
||||
(unsigned)meta.transfer_id,
|
||||
(unsigned)meta.seq,
|
||||
(unsigned)meta.chunk_bytes);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
logger_on_processing_latency((double)(omni_now_ms() - process_t0));
|
||||
|
||||
rt->transfer.unique_bytes_written += meta.chunk_bytes;
|
||||
rt->bytes_written = rt->transfer.unique_bytes_written;
|
||||
logger_set_progress(rt->bytes_written);
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
/* 输出服务端视角的最终汇总,包括丢包区间、乱序和时延统计。 */
|
||||
static void server_log_transfer_summary(ServerRuntime *rt)
|
||||
{
|
||||
OmniStats snapshot = logger_get_snapshot();
|
||||
char *missing_ranges = NULL;
|
||||
char *recv_window_dist = NULL;
|
||||
uint32_t missing_chunks = 0;
|
||||
uint32_t burst_count = 0;
|
||||
uint32_t max_burst_len = 0;
|
||||
double loss_rate = 0.0;
|
||||
double tcp_retrans_rate = 0.0;
|
||||
double kcp_retrans_rate = 0.0;
|
||||
uint64_t tcp_original_bytes = saturating_sub_u64(snapshot.tcp_data_bytes_sent,
|
||||
snapshot.tcp_retrans_bytes);
|
||||
uint64_t kcp_original_bytes = saturating_sub_u64(snapshot.kcp_data_bytes_sent,
|
||||
snapshot.kcp_retrans_bytes);
|
||||
|
||||
missing_ranges = format_missing_ranges(&rt->transfer,
|
||||
&missing_chunks,
|
||||
&burst_count,
|
||||
&max_burst_len);
|
||||
recv_window_dist = format_window_distribution(rt->transfer.recv_window_counts,
|
||||
rt->transfer.total_windows);
|
||||
if (rt->transfer.total_chunks > 0) {
|
||||
loss_rate = ((double)missing_chunks * 100.0) / (double)rt->transfer.total_chunks;
|
||||
}
|
||||
tcp_retrans_rate = rate_percent(snapshot.tcp_retrans_bytes, tcp_original_bytes);
|
||||
kcp_retrans_rate = rate_percent(snapshot.kcp_retrans_bytes, kcp_original_bytes);
|
||||
|
||||
logger_log("INFO", "summary",
|
||||
"event=transfer_summary role=server proto=%d transfer_id=%u "
|
||||
"expected_bytes=%llu written_bytes=%llu expected_chunks=%u unique_chunks=%u "
|
||||
"duplicate_chunks=%u out_of_order_chunks=%u "
|
||||
"loss_rate_pct=%.2f missing_chunks=%u missing_ranges=%s "
|
||||
"burst_loss_count=%u max_burst_loss_len=%u recv_windows=%u recv_window_distribution=%s "
|
||||
"rx_avg_mbps=%.3f rx_current_mbps=%.3f "
|
||||
"processing_avg_ms=%.3f queue_avg_ms=%.3f transmission_avg_ms=%.3f "
|
||||
"propagation_avg_ms=%.3f end_to_end_avg_ms=%.3f "
|
||||
"send_buffer_avg_pct=%.2f recv_buffer_avg_pct=%.2f "
|
||||
"cwnd_avg=%.2f "
|
||||
"local_tcp_retrans=%llu local_tcp_data_segs_out=%llu local_tcp_original_bytes=%llu "
|
||||
"local_tcp_retrans_bytes=%llu local_tcp_retrans_rate_pct=%.2f "
|
||||
"local_kcp_retrans=%llu local_kcp_data_segs_out=%llu local_kcp_original_bytes=%llu "
|
||||
"local_kcp_retrans_bytes=%llu local_kcp_retrans_rate_pct=%.2f "
|
||||
"clock_sync_ok=%u clock_offset_ms=%lld clock_sync_rtt_ms=%llu clock_sync_samples=%u",
|
||||
(int)rt->proto,
|
||||
(unsigned)rt->transfer.transfer_id,
|
||||
(unsigned long long)rt->transfer.total_bytes,
|
||||
(unsigned long long)rt->bytes_written,
|
||||
(unsigned)rt->transfer.total_chunks,
|
||||
(unsigned)rt->transfer.unique_chunks,
|
||||
(unsigned)rt->transfer.duplicate_chunks,
|
||||
(unsigned)rt->transfer.out_of_order_chunks,
|
||||
loss_rate,
|
||||
(unsigned)missing_chunks,
|
||||
missing_ranges ? missing_ranges : "alloc_failed",
|
||||
(unsigned)burst_count,
|
||||
(unsigned)max_burst_len,
|
||||
(unsigned)rt->transfer.total_windows,
|
||||
recv_window_dist ? recv_window_dist : "alloc_failed",
|
||||
snapshot.rx_avg_mbps,
|
||||
snapshot.rx_current_mbps,
|
||||
(snapshot.processing_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.processing_delay_ms.sum / (double)snapshot.processing_delay_ms.count,
|
||||
(snapshot.queue_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.queue_delay_ms.sum / (double)snapshot.queue_delay_ms.count,
|
||||
(snapshot.transmission_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.transmission_delay_ms.sum / (double)snapshot.transmission_delay_ms.count,
|
||||
(snapshot.propagation_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.propagation_delay_ms.sum / (double)snapshot.propagation_delay_ms.count,
|
||||
(snapshot.end_to_end_delay_ms.count == 0) ? 0.0 :
|
||||
snapshot.end_to_end_delay_ms.sum / (double)snapshot.end_to_end_delay_ms.count,
|
||||
(snapshot.send_buffer_pct.count == 0) ? 0.0 :
|
||||
snapshot.send_buffer_pct.sum / (double)snapshot.send_buffer_pct.count,
|
||||
(snapshot.recv_buffer_pct.count == 0) ? 0.0 :
|
||||
snapshot.recv_buffer_pct.sum / (double)snapshot.recv_buffer_pct.count,
|
||||
(snapshot.cwnd.count == 0) ? 0.0 :
|
||||
snapshot.cwnd.sum / (double)snapshot.cwnd.count,
|
||||
(unsigned long long)snapshot.tcp_retrans,
|
||||
(unsigned long long)snapshot.tcp_data_segs_out,
|
||||
(unsigned long long)tcp_original_bytes,
|
||||
(unsigned long long)snapshot.tcp_retrans_bytes,
|
||||
tcp_retrans_rate,
|
||||
(unsigned long long)snapshot.kcp_retrans,
|
||||
(unsigned long long)snapshot.kcp_data_segs_out,
|
||||
(unsigned long long)kcp_original_bytes,
|
||||
(unsigned long long)snapshot.kcp_retrans_bytes,
|
||||
kcp_retrans_rate,
|
||||
(unsigned)rt->clock_sync.valid,
|
||||
(long long)rt->clock_sync.server_minus_client_offset_ms,
|
||||
(unsigned long long)rt->clock_sync.best_rtt_ms,
|
||||
(unsigned)rt->clock_sync.sample_count);
|
||||
|
||||
free(missing_ranges);
|
||||
free(recv_window_dist);
|
||||
}
|
||||
|
||||
/* 释放 TransferState 持有的动态内存。 */
|
||||
static void transfer_state_free(TransferState *st)
|
||||
{
|
||||
free(st->seen);
|
||||
free(st->recv_window_counts);
|
||||
memset(st, 0, sizeof(*st));
|
||||
}
|
||||
|
||||
/*
|
||||
* 服务端接收线程:
|
||||
* - 接收 FILE_CHUNK 并写盘
|
||||
* - 接收 FILE_END 并输出汇总、回 ACK
|
||||
* - 接收 COMMAND 仅记录日志
|
||||
*/
|
||||
static void *recv_thread_main(void *arg)
|
||||
{
|
||||
ServerRuntime *rt = (ServerRuntime *)arg;
|
||||
uint8_t frame[SERVER_FRAME_BUF_SIZE];
|
||||
|
||||
/* 允许主线程在退出时取消本线程,避免阻塞 recv 导致无法收尾。 */
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
||||
|
||||
@@ -127,11 +599,6 @@ static void *recv_thread_main(void *arg)
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
/*
|
||||
* recv 返回 0 的语义依赖协议:
|
||||
* - TCP: 对端连接关闭,接收线程可退出
|
||||
* - UDP/KCP: 可能仅表示当前无可读数据,继续等待
|
||||
*/
|
||||
if (rt->proto == OMNI_PROTO_TCP) {
|
||||
logger_log("INFO", "server", "tcp_peer_closed");
|
||||
break;
|
||||
@@ -149,27 +616,112 @@ static void *recv_thread_main(void *arg)
|
||||
}
|
||||
|
||||
if (hdr.type == MSG_TYPE_FILE_CHUNK) {
|
||||
/* 文件分片:直接按顺序落盘。 */
|
||||
size_t nw = fwrite(payload, 1, hdr.len, rt->out_fp);
|
||||
if (nw != hdr.len) {
|
||||
logger_log("ERROR", "server", "fwrite_failed expect=%u got=%zu",
|
||||
(unsigned)hdr.len, nw);
|
||||
rc = server_record_chunk(rt, &hdr, payload, hdr.len);
|
||||
if (rc != OMNI_OK) {
|
||||
break;
|
||||
}
|
||||
rt->bytes_written += nw;
|
||||
} else if (hdr.type == MSG_TYPE_TIME_SYNC_REQ) {
|
||||
TimeSyncProbeMeta probe_meta;
|
||||
TimeSyncReplyMeta reply_meta;
|
||||
uint64_t server_recv_ts_ms;
|
||||
uint64_t server_send_ts_ms;
|
||||
|
||||
if (hdr.len < TIME_SYNC_PROBE_META_SIZE) {
|
||||
logger_log("WARN", "server",
|
||||
"short_time_sync_probe len=%u",
|
||||
(unsigned)hdr.len);
|
||||
continue;
|
||||
}
|
||||
|
||||
server_recv_ts_ms = omni_now_ms();
|
||||
omni_time_sync_probe_meta_decode((const TimeSyncProbeMeta *)payload, &probe_meta);
|
||||
server_send_ts_ms = omni_now_ms();
|
||||
omni_time_sync_reply_meta_encode(&reply_meta,
|
||||
probe_meta.probe_id,
|
||||
probe_meta.client_send_ts_ms,
|
||||
server_recv_ts_ms,
|
||||
server_send_ts_ms);
|
||||
if (send_app_message_with_timestamp(rt->ctx,
|
||||
MSG_TYPE_TIME_SYNC_RESP,
|
||||
&reply_meta,
|
||||
TIME_SYNC_REPLY_META_SIZE,
|
||||
server_send_ts_ms) != OMNI_OK) {
|
||||
logger_log("ERROR", "server",
|
||||
"time_sync_reply_failed probe_id=%u",
|
||||
(unsigned)probe_meta.probe_id);
|
||||
break;
|
||||
}
|
||||
logger_log("INFO", "server",
|
||||
"time_sync_reply probe_id=%u server_recv_ts_ms=%llu server_send_ts_ms=%llu",
|
||||
(unsigned)probe_meta.probe_id,
|
||||
(unsigned long long)server_recv_ts_ms,
|
||||
(unsigned long long)server_send_ts_ms);
|
||||
continue;
|
||||
} else if (hdr.type == MSG_TYPE_TIME_SYNC_REPORT) {
|
||||
TimeSyncReportMeta report_meta;
|
||||
|
||||
if (hdr.len < TIME_SYNC_REPORT_META_SIZE) {
|
||||
logger_log("WARN", "server",
|
||||
"short_time_sync_report len=%u",
|
||||
(unsigned)hdr.len);
|
||||
continue;
|
||||
}
|
||||
|
||||
omni_time_sync_report_meta_decode((const TimeSyncReportMeta *)payload, &report_meta);
|
||||
rt->clock_sync.valid = 1;
|
||||
rt->clock_sync.server_minus_client_offset_ms =
|
||||
report_meta.server_minus_client_offset_ms;
|
||||
rt->clock_sync.best_rtt_ms = report_meta.best_rtt_ms;
|
||||
rt->clock_sync.sample_count = report_meta.sample_count;
|
||||
logger_on_rtt(report_meta.best_rtt_ms);
|
||||
logger_log("INFO", "server",
|
||||
"time_sync_ready offset_ms=%lld best_rtt_ms=%llu samples=%u",
|
||||
(long long)rt->clock_sync.server_minus_client_offset_ms,
|
||||
(unsigned long long)rt->clock_sync.best_rtt_ms,
|
||||
(unsigned)rt->clock_sync.sample_count);
|
||||
continue;
|
||||
} else if (hdr.type == MSG_TYPE_FILE_END) {
|
||||
/*
|
||||
* 文件接收结束:
|
||||
* - 仅置位 transfer_done
|
||||
* - 不退出线程,让服务端在交互模式下继续保持长连接并可下发指令
|
||||
*/
|
||||
TransferEndMeta end_meta;
|
||||
TransferAckMeta ack_meta;
|
||||
|
||||
if (hdr.len >= TRANSFER_END_META_SIZE) {
|
||||
/* FILE_END 会补齐整次传输的最终期望规模信息。 */
|
||||
omni_transfer_end_meta_decode((const TransferEndMeta *)payload, &end_meta);
|
||||
if (rt->transfer.transfer_id == 0) {
|
||||
rt->transfer.transfer_id = end_meta.transfer_id;
|
||||
}
|
||||
if (rt->transfer.total_chunks == 0) {
|
||||
rt->transfer.total_chunks = end_meta.total_chunks;
|
||||
}
|
||||
if (rt->transfer.total_bytes == 0) {
|
||||
rt->transfer.total_bytes = end_meta.total_bytes;
|
||||
}
|
||||
if (end_meta.total_windows > rt->transfer.total_windows) {
|
||||
rt->transfer.total_windows = end_meta.total_windows;
|
||||
}
|
||||
logger_set_transfer_total(rt->transfer.total_bytes);
|
||||
}
|
||||
|
||||
/* 先确保文件内容刷到磁盘,再输出汇总并回 ACK。 */
|
||||
fflush(rt->out_fp);
|
||||
server_log_transfer_summary(rt);
|
||||
atomic_store(&rt->transfer_done, 1);
|
||||
logger_log("INFO", "server", "file_transfer_end bytes=%llu",
|
||||
(unsigned long long)rt->bytes_written);
|
||||
|
||||
omni_transfer_ack_meta_encode(&ack_meta,
|
||||
rt->transfer.transfer_id,
|
||||
rt->transfer.total_chunks,
|
||||
rt->transfer.total_bytes,
|
||||
rt->bytes_written,
|
||||
hdr.timestamp);
|
||||
(void)send_app_message_with_timestamp(rt->ctx,
|
||||
MSG_TYPE_TRANSFER_ACK,
|
||||
&ack_meta,
|
||||
TRANSFER_ACK_META_SIZE,
|
||||
omni_now_ms());
|
||||
continue;
|
||||
} else if (hdr.type == MSG_TYPE_COMMAND) {
|
||||
/* 当前服务端不处理“来自客户端”的 COMMAND,仅记录日志。 */
|
||||
logger_log("INFO", "server",
|
||||
"recv_command_from_peer len=%u (ignored)",
|
||||
(unsigned)hdr.len);
|
||||
@@ -184,15 +736,26 @@ static void *recv_thread_main(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 服务端主流程:
|
||||
* 1) 建立监听/接收上下文
|
||||
* 2) 启动后台接收线程
|
||||
* 3) 如果前台是交互终端,则允许输入命令发给客户端
|
||||
* 4) 等待传输完成后统一收尾
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* 命令行参数默认值。 */
|
||||
const char *proto_str = "tcp";
|
||||
const char *bind_ip = NULL;
|
||||
const char *output_path = NULL;
|
||||
OmniProtocol proto;
|
||||
FILE *out_fp = NULL;
|
||||
OmniContext *ctx = NULL;
|
||||
ServerRuntime rt;
|
||||
pthread_t recv_tid;
|
||||
int listen_port = 0;
|
||||
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, "p:b:P:o:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'p':
|
||||
@@ -218,33 +781,29 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *out_fp = fopen(output_path, "wb");
|
||||
out_fp = fopen(output_path, "wb+");
|
||||
if (!out_fp) {
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
|
||||
OmniProtocol proto = parse_proto(proto_str);
|
||||
/* 服务端角色:仅监听本地端口。 */
|
||||
OmniContext *ctx = omni_init(OMNI_ROLE_SERVER, proto,
|
||||
bind_ip, (uint16_t)listen_port,
|
||||
NULL, 0);
|
||||
proto = parse_proto(proto_str);
|
||||
ctx = omni_init(OMNI_ROLE_SERVER, proto,
|
||||
bind_ip, (uint16_t)listen_port,
|
||||
NULL, 0);
|
||||
if (!ctx) {
|
||||
fclose(out_fp);
|
||||
fprintf(stderr, "omni_init failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ServerRuntime rt;
|
||||
memset(&rt, 0, sizeof(rt));
|
||||
rt.ctx = ctx;
|
||||
rt.proto = proto;
|
||||
rt.out_fp = out_fp;
|
||||
rt.bytes_written = 0;
|
||||
atomic_init(&rt.running, 1);
|
||||
atomic_init(&rt.transfer_done, 0);
|
||||
|
||||
/* 启动接收线程处理文件写入主流程。 */
|
||||
pthread_t recv_tid;
|
||||
if (pthread_create(&recv_tid, NULL, recv_thread_main, &rt) != 0) {
|
||||
perror("pthread_create");
|
||||
omni_close(ctx);
|
||||
@@ -253,11 +812,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (isatty(STDIN_FILENO)) {
|
||||
/*
|
||||
* 交互模式:
|
||||
* - 每次回车读取一行
|
||||
* - 非空行封装为 COMMAND 发送给客户端
|
||||
*/
|
||||
/* 交互模式:允许服务端向客户端发送文本命令。 */
|
||||
char line[2048];
|
||||
while (atomic_load(&rt.running)) {
|
||||
if (!fgets(line, sizeof(line), stdin)) {
|
||||
@@ -273,31 +828,31 @@ int main(int argc, char **argv)
|
||||
continue;
|
||||
}
|
||||
if (strcmp(line, "quit") == 0) {
|
||||
/* 主动退出交互循环。 */
|
||||
break;
|
||||
}
|
||||
|
||||
int rc = send_app_message(ctx, MSG_TYPE_COMMAND, line, (uint32_t)len);
|
||||
if (rc != OMNI_OK) {
|
||||
if (send_app_message_with_timestamp(ctx,
|
||||
MSG_TYPE_COMMAND,
|
||||
line,
|
||||
(uint32_t)len,
|
||||
omni_now_ms()) != OMNI_OK) {
|
||||
logger_log("ERROR", "server", "send_command_failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* 非交互模式(如脚本后台):
|
||||
* 只等待接收线程将 transfer_done 置位,避免阻塞在 stdin。
|
||||
*/
|
||||
/* 非交互模式:例如脚本执行时,只等待传输结束即可。 */
|
||||
while (atomic_load(&rt.running) && !atomic_load(&rt.transfer_done)) {
|
||||
usleep(100 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
/* 收尾:取消接收线程 -> join -> 关闭网络 -> 关闭文件。 */
|
||||
/* 主线程统一收尾并释放接收线程持有的资源。 */
|
||||
atomic_store(&rt.running, 0);
|
||||
pthread_cancel(recv_tid);
|
||||
pthread_join(recv_tid, NULL);
|
||||
omni_close(ctx);
|
||||
fclose(out_fp);
|
||||
transfer_state_free(&rt.transfer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* 打印测试程序命令行帮助。 */
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
@@ -27,6 +28,7 @@ static void usage(const char *prog)
|
||||
prog, prog);
|
||||
}
|
||||
|
||||
/* 解析协议名,非法输入时默认回退 TCP。 */
|
||||
static OmniProtocol parse_proto(const char *s)
|
||||
{
|
||||
if (strcmp(s, "tcp") == 0) return OMNI_PROTO_TCP;
|
||||
@@ -35,6 +37,10 @@ static OmniProtocol parse_proto(const char *s)
|
||||
return OMNI_PROTO_TCP;
|
||||
}
|
||||
|
||||
/*
|
||||
* 测试服务端:
|
||||
* 持续收消息并原样 echo 回去,主要用于验证双向收发路径是否通畅。
|
||||
*/
|
||||
static void run_server(OmniProtocol proto, uint16_t port)
|
||||
{
|
||||
OmniContext *ctx = omni_init(OMNI_ROLE_SERVER, proto,
|
||||
@@ -56,6 +62,7 @@ static void run_server(OmniProtocol proto, uint16_t port)
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
/* KCP 在“暂时没拼出完整消息”时可能返回 0,因此这里不能立刻判定连接结束。 */
|
||||
if (proto == OMNI_PROTO_KCP) {
|
||||
usleep(10 * 1000);
|
||||
continue;
|
||||
@@ -73,6 +80,10 @@ static void run_server(OmniProtocol proto, uint16_t port)
|
||||
omni_close(ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* 测试客户端:
|
||||
* 连续发 100 条消息,每发一条就等一条 echo,用来观测协议往返行为。
|
||||
*/
|
||||
static void run_client(OmniProtocol proto, const char *host, uint16_t port)
|
||||
{
|
||||
if (!host) {
|
||||
@@ -111,6 +122,7 @@ static void run_client(OmniProtocol proto, const char *host, uint16_t port)
|
||||
break;
|
||||
}
|
||||
if (m == 0) {
|
||||
/* 对 KCP 来说,0 更像“此刻没取到完整消息”,而不是 socket 关闭。 */
|
||||
if (proto == OMNI_PROTO_KCP) {
|
||||
usleep(10 * 1000);
|
||||
--i;
|
||||
@@ -129,6 +141,7 @@ static void run_client(OmniProtocol proto, const char *host, uint16_t port)
|
||||
omni_close(ctx);
|
||||
}
|
||||
|
||||
/* 程序入口:根据角色参数派发到测试客户端或测试服务端。 */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *role_str = NULL;
|
||||
|
||||
1025
src/core/logger.c
1025
src/core/logger.c
File diff suppressed because it is too large
Load Diff
@@ -93,12 +93,11 @@ ssize_t omni_send(OmniContext *ctx, const void *buf, size_t len)
|
||||
logger_on_send_call_latency(t1 - t0);
|
||||
if (n > 0) {
|
||||
logger_on_send((size_t)n);
|
||||
logger_on_send_transmission_bytes((size_t)n);
|
||||
logger_maybe_print_performance_log("on_send");
|
||||
}
|
||||
logger_log("DEBUG", "network", "omni_send proto=%d bytes=%zd call_ms=%llu",
|
||||
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
||||
if (n > 0) {
|
||||
logger_print_performance_log("on_send");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -114,12 +113,11 @@ ssize_t omni_recv(OmniContext *ctx, void *buf, size_t len)
|
||||
logger_on_recv_call_latency(t1 - t0);
|
||||
if (n > 0) {
|
||||
logger_on_recv((size_t)n);
|
||||
logger_on_recv_transmission_bytes((size_t)n);
|
||||
logger_maybe_print_performance_log("on_recv");
|
||||
}
|
||||
logger_log("DEBUG", "network", "omni_recv proto=%d bytes=%zd call_ms=%llu",
|
||||
(int)ctx->proto, n, (unsigned long long)(t1 - t0));
|
||||
if (n > 0) {
|
||||
logger_print_performance_log("on_recv");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,79 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct KcpContext {
|
||||
int fd;
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len;
|
||||
ikcpcb *kcp;
|
||||
uint32_t last_xmit; /* 用于推算重传/发送次数变化 */
|
||||
};
|
||||
struct KcpContext {
|
||||
int fd;
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len;
|
||||
ikcpcb *kcp;
|
||||
uint32_t *seg_xmit_seen; /* 按 sn 记录上次采样到的 xmit 值 */
|
||||
size_t seg_xmit_cap;
|
||||
};
|
||||
|
||||
static int kcp_ensure_seg_track_capacity(struct KcpContext *ctx, uint32_t sn)
|
||||
{
|
||||
size_t need;
|
||||
size_t new_cap;
|
||||
uint32_t *new_seen;
|
||||
|
||||
if (!ctx) return OMNI_ERR_PARAM;
|
||||
|
||||
need = (size_t)sn + 1u;
|
||||
if (need <= ctx->seg_xmit_cap) {
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
new_cap = (ctx->seg_xmit_cap == 0) ? 64u : ctx->seg_xmit_cap;
|
||||
while (new_cap < need) {
|
||||
new_cap *= 2u;
|
||||
}
|
||||
|
||||
new_seen = (uint32_t *)realloc(ctx->seg_xmit_seen, new_cap * sizeof(uint32_t));
|
||||
if (!new_seen) {
|
||||
return OMNI_ERR_GENERIC;
|
||||
}
|
||||
memset(new_seen + ctx->seg_xmit_cap, 0,
|
||||
(new_cap - ctx->seg_xmit_cap) * sizeof(uint32_t));
|
||||
ctx->seg_xmit_seen = new_seen;
|
||||
ctx->seg_xmit_cap = new_cap;
|
||||
return OMNI_OK;
|
||||
}
|
||||
|
||||
static void kcp_sample_transport_stats(struct KcpContext *ctx)
|
||||
{
|
||||
struct IQUEUEHEAD *p;
|
||||
|
||||
if (!ctx || !ctx->kcp) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (p = ctx->kcp->snd_buf.next; p != &ctx->kcp->snd_buf; p = p->next) {
|
||||
struct IKCPSEG *segment = iqueue_entry(p, struct IKCPSEG, node);
|
||||
uint32_t prev_xmit;
|
||||
|
||||
if (!segment || segment->len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (kcp_ensure_seg_track_capacity(ctx, segment->sn) != OMNI_OK) {
|
||||
logger_log("WARN", "kcp", "seg_track_alloc_failed sn=%u",
|
||||
(unsigned)segment->sn);
|
||||
return;
|
||||
}
|
||||
|
||||
prev_xmit = ctx->seg_xmit_seen[segment->sn];
|
||||
if (prev_xmit == 0 && segment->xmit > 0) {
|
||||
logger_on_kcp_tx(1u, (uint64_t)segment->len);
|
||||
prev_xmit = 1u;
|
||||
}
|
||||
if (segment->xmit > prev_xmit) {
|
||||
uint32_t delta = segment->xmit - prev_xmit;
|
||||
logger_on_kcp_retrans((uint64_t)delta,
|
||||
(uint64_t)delta * (uint64_t)segment->len);
|
||||
}
|
||||
|
||||
ctx->seg_xmit_seen[segment->sn] = segment->xmit;
|
||||
}
|
||||
}
|
||||
|
||||
static int kcp_output(const char *buf, int len, ikcpcb *kcp, void *user)
|
||||
{
|
||||
@@ -120,14 +186,38 @@ static void kcp_update_loop(struct KcpContext *ctx)
|
||||
ikcp_input(ctx->kcp, buf, (long)n);
|
||||
}
|
||||
|
||||
/* KCP 内部状态监控 */
|
||||
uint32_t xmit = ctx->kcp->xmit;
|
||||
if (xmit >= ctx->last_xmit) {
|
||||
logger_on_kcp_retrans((uint64_t)(xmit - ctx->last_xmit));
|
||||
}
|
||||
ctx->last_xmit = xmit;
|
||||
|
||||
uint64_t t1 = omni_now_ms();
|
||||
/* KCP 内部状态监控:按分片 xmit 变化统计首次发送和真实重传。 */
|
||||
kcp_sample_transport_stats(ctx);
|
||||
/* rx_srtt 是 KCP 平滑后的 RTT(单位 ms),大于 0 才说明已经有有效采样值。 */
|
||||
if (ctx->kcp->rx_srtt > 0) {
|
||||
logger_on_rtt((uint64_t)ctx->kcp->rx_srtt);
|
||||
}
|
||||
|
||||
/* cwnd 是当前拥塞窗口大小,用来观察 KCP 的拥塞控制是否在收缩或增长。 */
|
||||
logger_on_cwnd((double)ctx->kcp->cwnd);
|
||||
|
||||
/* 统计发送/接收窗口的占用百分比,方便观察当前缓冲区压力。 */
|
||||
{
|
||||
double send_pct = 0.0;
|
||||
double recv_pct = 0.0;
|
||||
|
||||
/* ikcp_waitsnd() 表示还在发送路径中的分片数量,用发送窗口大小换算成占用率。 */
|
||||
if (ctx->kcp->snd_wnd > 0) {
|
||||
send_pct = ((double)ikcp_waitsnd(ctx->kcp) * 100.0) / (double)ctx->kcp->snd_wnd;
|
||||
logger_on_send_queue_bytes((size_t)ikcp_waitsnd(ctx->kcp) * (size_t)ctx->kcp->mss);
|
||||
}
|
||||
|
||||
/* nrcv_que 是已经收到、等待应用层取走的数据包数量,用接收窗口换算成占用率。 */
|
||||
if (ctx->kcp->rcv_wnd > 0) {
|
||||
recv_pct = ((double)ctx->kcp->nrcv_que * 100.0) / (double)ctx->kcp->rcv_wnd;
|
||||
logger_on_recv_queue_bytes((size_t)ctx->kcp->nrcv_que * (size_t)ctx->kcp->mss);
|
||||
}
|
||||
|
||||
/* 将收发两侧的缓冲区占用情况统一上报给监控模块。 */
|
||||
logger_on_buffer_status(send_pct, recv_pct);
|
||||
}
|
||||
|
||||
uint64_t t1 = omni_now_ms();
|
||||
logger_log("DEBUG", "kcp",
|
||||
"update ms=%llu cwnd=%u ssthresh=%u rmt_wnd=%u snd_wnd=%u rcv_wnd=%u "
|
||||
"rx_srtt=%u rx_rto=%u nsnd_buf=%u nsnd_que=%u nrcv_buf=%u nrcv_que=%u xmit=%u state=%u",
|
||||
@@ -191,14 +281,15 @@ static void kcp_close(OmniContext *c)
|
||||
{
|
||||
struct KcpContext *ctx = (struct KcpContext *)c;
|
||||
if (!ctx) return;
|
||||
if (ctx->kcp) {
|
||||
ikcp_release(ctx->kcp);
|
||||
}
|
||||
if (ctx->fd >= 0) {
|
||||
close(ctx->fd);
|
||||
}
|
||||
free(ctx);
|
||||
}
|
||||
if (ctx->kcp) {
|
||||
ikcp_release(ctx->kcp);
|
||||
}
|
||||
if (ctx->fd >= 0) {
|
||||
close(ctx->fd);
|
||||
}
|
||||
free(ctx->seg_xmit_seen);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
const struct ProtoVTable KCP_PROTO_VTABLE = {
|
||||
.init = kcp_init,
|
||||
|
||||
@@ -14,44 +14,192 @@
|
||||
#include "logger.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Linux 下 TCP_INFO 定义通常已在 <netinet/tcp.h> 提供,避免引入 <linux/tcp.h> 重定义 */
|
||||
#include <errno.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Linux 下 glibc 的 <netinet/tcp.h> 只暴露了 tcp_info 的旧前缀字段,
|
||||
* 这里复制到 tcpi_bytes_retrans 为止的内核布局,用来安全读取扩展快照。
|
||||
*/
|
||||
#ifdef __linux__
|
||||
struct OmniLinuxTcpInfo {
|
||||
uint8_t tcpi_state;
|
||||
uint8_t tcpi_ca_state;
|
||||
uint8_t tcpi_retransmits;
|
||||
uint8_t tcpi_probes;
|
||||
uint8_t tcpi_backoff;
|
||||
uint8_t tcpi_options;
|
||||
uint8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
|
||||
uint8_t tcpi_delivery_rate_app_limited : 1, tcpi_fastopen_client_fail : 2;
|
||||
|
||||
uint32_t tcpi_rto;
|
||||
uint32_t tcpi_ato;
|
||||
uint32_t tcpi_snd_mss;
|
||||
uint32_t tcpi_rcv_mss;
|
||||
|
||||
uint32_t tcpi_unacked;
|
||||
uint32_t tcpi_sacked;
|
||||
uint32_t tcpi_lost;
|
||||
uint32_t tcpi_retrans;
|
||||
uint32_t tcpi_fackets;
|
||||
|
||||
uint32_t tcpi_last_data_sent;
|
||||
uint32_t tcpi_last_ack_sent;
|
||||
uint32_t tcpi_last_data_recv;
|
||||
uint32_t tcpi_last_ack_recv;
|
||||
|
||||
uint32_t tcpi_pmtu;
|
||||
uint32_t tcpi_rcv_ssthresh;
|
||||
uint32_t tcpi_rtt;
|
||||
uint32_t tcpi_rttvar;
|
||||
uint32_t tcpi_snd_ssthresh;
|
||||
uint32_t tcpi_snd_cwnd;
|
||||
uint32_t tcpi_advmss;
|
||||
uint32_t tcpi_reordering;
|
||||
|
||||
uint32_t tcpi_rcv_rtt;
|
||||
uint32_t tcpi_rcv_space;
|
||||
|
||||
uint32_t tcpi_total_retrans;
|
||||
|
||||
uint64_t tcpi_pacing_rate;
|
||||
uint64_t tcpi_max_pacing_rate;
|
||||
uint64_t tcpi_bytes_acked;
|
||||
uint64_t tcpi_bytes_received;
|
||||
uint32_t tcpi_segs_out;
|
||||
uint32_t tcpi_segs_in;
|
||||
|
||||
uint32_t tcpi_notsent_bytes;
|
||||
uint32_t tcpi_min_rtt;
|
||||
uint32_t tcpi_data_segs_in;
|
||||
uint32_t tcpi_data_segs_out;
|
||||
|
||||
uint64_t tcpi_delivery_rate;
|
||||
|
||||
uint64_t tcpi_busy_time;
|
||||
uint64_t tcpi_rwnd_limited;
|
||||
uint64_t tcpi_sndbuf_limited;
|
||||
|
||||
uint32_t tcpi_delivered;
|
||||
uint32_t tcpi_delivered_ce;
|
||||
|
||||
uint64_t tcpi_bytes_sent;
|
||||
uint64_t tcpi_bytes_retrans;
|
||||
};
|
||||
|
||||
static int tcp_info_has_field(socklen_t len, size_t field_end)
|
||||
{
|
||||
return (size_t)len >= field_end;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct TcpContext {
|
||||
/* 已建立连接的 socket fd(服务端 accept 后或客户端 connect 后)。 */
|
||||
int fd;
|
||||
};
|
||||
|
||||
#ifdef __linux__
|
||||
static void tcp_log_info(int fd, const char *tag)
|
||||
{
|
||||
struct tcp_info ti;
|
||||
socklen_t len = sizeof(ti);
|
||||
if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 注意:tcpi_rtt 单位通常为微秒(Linux),这里转 ms 仅用于日志观察 */
|
||||
unsigned long long rtt_ms = (unsigned long long)(ti.tcpi_rtt / 1000u);
|
||||
unsigned long long rttvar_ms = (unsigned long long)(ti.tcpi_rttvar / 1000u);
|
||||
|
||||
logger_log("INFO", "tcpinfo",
|
||||
"tag=%s state=%u retransmits=%u probes=%u backoff=%u "
|
||||
"rto=%u ato=%u rtt_ms=%llu rttvar_ms=%llu "
|
||||
"snd_cwnd=%u snd_ssthresh=%u snd_mss=%u rcv_mss=%u "
|
||||
"lost=%u retrans=%u fackets=%u "
|
||||
"last_data_sent_ms=%u last_data_recv_ms=%u",
|
||||
tag ? tag : "sample",
|
||||
(unsigned)ti.tcpi_state,
|
||||
#ifdef __linux__
|
||||
static void tcp_sample_socket_buffers(int fd)
|
||||
{
|
||||
/* socket 层配置的发送/接收缓冲区总大小。 */
|
||||
int sndbuf = 0;
|
||||
int rcvbuf = 0;
|
||||
socklen_t optlen = sizeof(int);
|
||||
|
||||
/* 当前内核发送队列/接收队列里还压着的字节数。 */
|
||||
int outq = 0;
|
||||
int inq = 0;
|
||||
|
||||
/* 最终换算成百分比后上报,表示缓冲区占用压力。 */
|
||||
double send_pct = 0.0;
|
||||
double recv_pct = 0.0;
|
||||
|
||||
/*
|
||||
* SO_SNDBUF 取发送缓冲区容量;
|
||||
* TIOCOUTQ 取当前还没真正发出去的字节数。
|
||||
* 两者相除后得到发送侧缓冲占用率。
|
||||
*/
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) == 0 && sndbuf > 0) {
|
||||
#ifdef TIOCOUTQ
|
||||
if (ioctl(fd, TIOCOUTQ, &outq) == 0 && outq >= 0) {
|
||||
send_pct = ((double)outq * 100.0) / (double)sndbuf;
|
||||
logger_on_send_queue_bytes((size_t)outq);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* SO_RCVBUF 取接收缓冲区容量;
|
||||
* FIONREAD 取当前已经到达、但应用层还没 read 的字节数。
|
||||
* 两者相除后得到接收侧缓冲占用率。
|
||||
*/
|
||||
optlen = sizeof(int);
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) == 0 && rcvbuf > 0) {
|
||||
if (ioctl(fd, FIONREAD, &inq) == 0 && inq >= 0) {
|
||||
recv_pct = ((double)inq * 100.0) / (double)rcvbuf;
|
||||
logger_on_recv_queue_bytes((size_t)inq);
|
||||
}
|
||||
}
|
||||
|
||||
/* 将 TCP 收发缓冲区的占用情况统一交给 logger 记录。 */
|
||||
logger_on_buffer_status(send_pct, recv_pct);
|
||||
}
|
||||
|
||||
static void tcp_log_info(int fd, const char *tag)
|
||||
{
|
||||
struct OmniLinuxTcpInfo ti;
|
||||
uint64_t total_retrans = 0;
|
||||
uint64_t data_segs_out = 0;
|
||||
uint64_t bytes_sent = 0;
|
||||
uint64_t bytes_retrans = 0;
|
||||
socklen_t len = sizeof(ti);
|
||||
|
||||
memset(&ti, 0, sizeof(ti));
|
||||
if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 注意:tcpi_rtt 单位通常为微秒(Linux),这里转 ms 仅用于日志观察 */
|
||||
unsigned long long rtt_ms = (unsigned long long)(ti.tcpi_rtt / 1000u);
|
||||
unsigned long long rttvar_ms = (unsigned long long)(ti.tcpi_rttvar / 1000u);
|
||||
|
||||
if (tcp_info_has_field(len, offsetof(struct OmniLinuxTcpInfo, tcpi_total_retrans) +
|
||||
sizeof(ti.tcpi_total_retrans))) {
|
||||
total_retrans = (uint64_t)ti.tcpi_total_retrans;
|
||||
} else {
|
||||
total_retrans = (uint64_t)ti.tcpi_retrans;
|
||||
}
|
||||
if (tcp_info_has_field(len, offsetof(struct OmniLinuxTcpInfo, tcpi_data_segs_out) +
|
||||
sizeof(ti.tcpi_data_segs_out))) {
|
||||
data_segs_out = (uint64_t)ti.tcpi_data_segs_out;
|
||||
}
|
||||
if (tcp_info_has_field(len, offsetof(struct OmniLinuxTcpInfo, tcpi_bytes_sent) +
|
||||
sizeof(ti.tcpi_bytes_sent))) {
|
||||
bytes_sent = (uint64_t)ti.tcpi_bytes_sent;
|
||||
}
|
||||
if (tcp_info_has_field(len, offsetof(struct OmniLinuxTcpInfo, tcpi_bytes_retrans) +
|
||||
sizeof(ti.tcpi_bytes_retrans))) {
|
||||
bytes_retrans = (uint64_t)ti.tcpi_bytes_retrans;
|
||||
}
|
||||
|
||||
logger_log("INFO", "tcpinfo",
|
||||
"tag=%s state=%u retransmits=%u probes=%u backoff=%u "
|
||||
"rto=%u ato=%u rtt_ms=%llu rttvar_ms=%llu "
|
||||
"snd_cwnd=%u snd_ssthresh=%u snd_mss=%u rcv_mss=%u "
|
||||
"lost=%u retrans=%u total_retrans=%llu data_segs_out=%llu "
|
||||
"bytes_sent=%llu bytes_retrans=%llu fackets=%u "
|
||||
"last_data_sent_ms=%u last_data_recv_ms=%u",
|
||||
tag ? tag : "sample",
|
||||
(unsigned)ti.tcpi_state,
|
||||
(unsigned)ti.tcpi_retransmits,
|
||||
(unsigned)ti.tcpi_probes,
|
||||
(unsigned)ti.tcpi_backoff,
|
||||
@@ -61,15 +209,24 @@ static void tcp_log_info(int fd, const char *tag)
|
||||
rttvar_ms,
|
||||
(unsigned)ti.tcpi_snd_cwnd,
|
||||
(unsigned)ti.tcpi_snd_ssthresh,
|
||||
(unsigned)ti.tcpi_snd_mss,
|
||||
(unsigned)ti.tcpi_rcv_mss,
|
||||
(unsigned)ti.tcpi_lost,
|
||||
(unsigned)ti.tcpi_retrans,
|
||||
(unsigned)ti.tcpi_fackets,
|
||||
(unsigned)ti.tcpi_last_data_sent,
|
||||
(unsigned)ti.tcpi_last_data_recv);
|
||||
}
|
||||
#endif
|
||||
(unsigned)ti.tcpi_snd_mss,
|
||||
(unsigned)ti.tcpi_rcv_mss,
|
||||
(unsigned)ti.tcpi_lost,
|
||||
(unsigned)ti.tcpi_retrans,
|
||||
(unsigned long long)total_retrans,
|
||||
(unsigned long long)data_segs_out,
|
||||
(unsigned long long)bytes_sent,
|
||||
(unsigned long long)bytes_retrans,
|
||||
(unsigned)ti.tcpi_fackets,
|
||||
(unsigned)ti.tcpi_last_data_sent,
|
||||
(unsigned)ti.tcpi_last_data_recv);
|
||||
|
||||
logger_on_rtt(rtt_ms);
|
||||
logger_on_tcp_transport(total_retrans, data_segs_out, bytes_sent, bytes_retrans);
|
||||
logger_on_cwnd((double)ti.tcpi_snd_cwnd);
|
||||
tcp_sample_socket_buffers(fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int tcp_set_nodelay(int fd)
|
||||
{
|
||||
@@ -277,13 +434,13 @@ static ssize_t tcp_send(OmniContext *c, const void *buf, size_t len)
|
||||
uint64_t t1 = omni_now_ms();
|
||||
/* 记录协议层发送耗时,便于后续性能分析。 */
|
||||
logger_on_proto_send_latency(t1 - t0);
|
||||
logger_log("DEBUG", "tcp", "send payload_bytes=%zu header_bytes=%zu proto_ms=%llu",
|
||||
len, (size_t)MSG_HEADER_SIZE, (unsigned long long)(t1 - t0));
|
||||
#ifdef __linux__
|
||||
tcp_log_info(ctx->fd, "after_send");
|
||||
#endif
|
||||
return (ssize_t)len;
|
||||
}
|
||||
logger_log("DEBUG", "tcp", "send payload_bytes=%zu header_bytes=%zu proto_ms=%llu",
|
||||
len, (size_t)MSG_HEADER_SIZE, (unsigned long long)(t1 - t0));
|
||||
#ifdef __linux__
|
||||
tcp_log_info(ctx->fd, "after_send");
|
||||
#endif
|
||||
return (ssize_t)len;
|
||||
}
|
||||
|
||||
static ssize_t tcp_recv(OmniContext *c, void *buf, size_t len)
|
||||
{
|
||||
@@ -338,11 +495,11 @@ static ssize_t tcp_recv(OmniContext *c, void *buf, size_t len)
|
||||
(unsigned)host_hdr.type,
|
||||
(unsigned long long)host_hdr.timestamp,
|
||||
(unsigned long long)(t1 - t0));
|
||||
#ifdef __linux__
|
||||
tcp_log_info(ctx->fd, "after_recv");
|
||||
#endif
|
||||
return (ssize_t)payload_len;
|
||||
}
|
||||
#ifdef __linux__
|
||||
tcp_log_info(ctx->fd, "after_recv");
|
||||
#endif
|
||||
return (ssize_t)payload_len;
|
||||
}
|
||||
|
||||
static void tcp_close(OmniContext *c)
|
||||
{
|
||||
|
||||
@@ -9,18 +9,65 @@
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct UdpContext {
|
||||
int fd;
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len;
|
||||
};
|
||||
struct UdpContext {
|
||||
int fd;
|
||||
struct sockaddr_in peer_addr;
|
||||
socklen_t peer_len;
|
||||
};
|
||||
|
||||
static void udp_sample_socket_buffers(int fd)
|
||||
{
|
||||
/* socket 层配置的发送/接收缓冲区总大小。 */
|
||||
int sndbuf = 0;
|
||||
int rcvbuf = 0;
|
||||
socklen_t optlen = sizeof(int);
|
||||
|
||||
/* 当前内核发送队列/接收队列里还压着的字节数。 */
|
||||
int outq = 0;
|
||||
int inq = 0;
|
||||
|
||||
/* 最终换算成百分比后上报,表示缓冲区占用压力。 */
|
||||
double send_pct = 0.0;
|
||||
double recv_pct = 0.0;
|
||||
|
||||
/*
|
||||
* SO_SNDBUF 取发送缓冲区容量;
|
||||
* TIOCOUTQ 取当前还没从内核发送队列发走的字节数。
|
||||
* 两者相除后得到发送侧缓冲占用率。
|
||||
*/
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) == 0 && sndbuf > 0) {
|
||||
#ifdef TIOCOUTQ
|
||||
if (ioctl(fd, TIOCOUTQ, &outq) == 0 && outq >= 0) {
|
||||
send_pct = ((double)outq * 100.0) / (double)sndbuf;
|
||||
logger_on_send_queue_bytes((size_t)outq);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* SO_RCVBUF 取接收缓冲区容量;
|
||||
* FIONREAD 取当前已经到达、但应用层还没 recv 的字节数。
|
||||
* 两者相除后得到接收侧缓冲占用率。
|
||||
*/
|
||||
optlen = sizeof(int);
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) == 0 && rcvbuf > 0) {
|
||||
if (ioctl(fd, FIONREAD, &inq) == 0 && inq >= 0) {
|
||||
recv_pct = ((double)inq * 100.0) / (double)rcvbuf;
|
||||
logger_on_recv_queue_bytes((size_t)inq);
|
||||
}
|
||||
}
|
||||
|
||||
/* 将 UDP 收发缓冲区的占用情况统一交给 logger 记录。 */
|
||||
logger_on_buffer_status(send_pct, recv_pct);
|
||||
}
|
||||
|
||||
static OmniContext *udp_init(OmniRole role,
|
||||
const char *bind_ip,
|
||||
@@ -78,12 +125,13 @@ static ssize_t udp_send(OmniContext *c, const void *buf, size_t len)
|
||||
|
||||
ssize_t n = sendto(ctx->fd, buf, len, 0,
|
||||
(struct sockaddr *)&ctx->peer_addr, ctx->peer_len);
|
||||
if (n < 0) {
|
||||
logger_log("ERROR", "udp", "sendto_failed errno=%d", errno);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
if (n < 0) {
|
||||
logger_log("ERROR", "udp", "sendto_failed errno=%d", errno);
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
udp_sample_socket_buffers(ctx->fd);
|
||||
return n;
|
||||
}
|
||||
|
||||
static ssize_t udp_recv(OmniContext *c, void *buf, size_t len)
|
||||
{
|
||||
@@ -100,12 +148,13 @@ static ssize_t udp_recv(OmniContext *c, void *buf, size_t len)
|
||||
return OMNI_ERR_IO;
|
||||
}
|
||||
|
||||
/* 默认更新 peer 为最近一次通信对端,便于“伪长连接” */
|
||||
ctx->peer_addr = from;
|
||||
ctx->peer_len = fromlen;
|
||||
|
||||
return n;
|
||||
}
|
||||
/* 默认更新 peer 为最近一次通信对端,便于“伪长连接” */
|
||||
ctx->peer_addr = from;
|
||||
ctx->peer_len = fromlen;
|
||||
udp_sample_socket_buffers(ctx->fd);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
static void udp_close(OmniContext *c)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user