feat: 变更架构为广播,发送到所有绑定的端口

This commit is contained in:
2026-07-26 22:29:47 +08:00
parent e3c40c822b
commit e8ee92110b
10 changed files with 614 additions and 574 deletions
+21 -64
View File
@@ -1,6 +1,5 @@
/**
* @file datarouter.c
* @brief 数据路由库 —— 实现
*/
#include "datarouter.h"
@@ -18,7 +17,6 @@
typedef struct {
int send_type;
int recv_type;
datarouter_send_handler_t handlers[DATAROUTER_MAX_HANDLERS_PER_PORT];
int count;
uint8_t in_use;
@@ -27,7 +25,7 @@ typedef struct {
static port_entry_t ports[DATAROUTER_MAX_PORTS];
static uint8_t initialized;
/* ---- 发送能力元数据 ---- */
/* ---- 元数据 ---- */
static const char *send_names[] = {
#define DATAROUTER_SEND(id, desc) #id,
@@ -41,8 +39,6 @@ static const char *send_descs[] = {
#undef DATAROUTER_SEND
};
/* ---- 接收能力元数据 ---- */
static const char *recv_names[] = {
#define DATAROUTER_RECV(id, desc) #id,
DATAROUTER_RECV_CAPABILITIES
@@ -55,29 +51,31 @@ static const char *recv_descs[] = {
#undef DATAROUTER_RECV
};
/* ---- 弱符号接收回调默认实现 ---- */
/* ---- 弱符号接收回调 ---- */
#define DATAROUTER_RECV(id, desc) \
DATAROUTER_WEAK void datarouter_on_recv_##id( \
uint32_t port_id, const void *data, size_t len) \
{ \
(void)port_id; (void)data; (void)len; \
}
{ (void)port_id; (void)data; (void)len; }
DATAROUTER_RECV_CAPABILITIES
#undef DATAROUTER_RECV
/* ---- 发送触发函数实现 ---- */
/* ---- 发送触发函数 ---- */
#define DATAROUTER_SEND(id, desc) \
int datarouter_send_##id(uint32_t port_id, const void *data, size_t len) \
int datarouter_send_##id(const void *data, size_t len) \
{ \
if (!initialized) return -1; \
if (port_id >= DATAROUTER_MAX_PORTS || !ports[port_id].in_use) return -2; \
if (ports[port_id].send_type != DATAROUTER_SEND_##id) return -3; \
if (ports[port_id].count == 0) return -4; \
for (int _i = 0; _i < ports[port_id].count; _i++) \
ports[port_id].handlers[_i](port_id, data, len); \
return 0; \
int called = 0; \
for (uint32_t _p = 0; _p < DATAROUTER_MAX_PORTS; _p++) { \
if (!ports[_p].in_use) continue; \
if (ports[_p].send_type != DATAROUTER_SEND_##id) continue; \
for (int _h = 0; _h < ports[_p].count; _h++) { \
ports[_p].handlers[_h](_p, data, len); \
called = 1; \
} \
} \
return called ? 0 : -2; \
}
DATAROUTER_SEND_CAPABILITIES
#undef DATAROUTER_SEND
@@ -104,7 +102,6 @@ datarouter_port_t datarouter_port_create(void)
if (!ports[i].in_use) {
ports[i].in_use = 1;
ports[i].send_type = -1;
ports[i].recv_type = -1;
ports[i].count = 0;
return (datarouter_port_t)i;
}
@@ -133,20 +130,6 @@ int datarouter_port_get_send_type(datarouter_port_t port)
return ports[port].send_type;
}
void datarouter_port_set_recv_type(datarouter_port_t port, int cap_id)
{
if (!initialized) return;
if (port < 0 || port >= DATAROUTER_MAX_PORTS || !ports[port].in_use) return;
ports[port].recv_type = cap_id;
}
int datarouter_port_get_recv_type(datarouter_port_t port)
{
if (!initialized) return -1;
if (port < 0 || port >= DATAROUTER_MAX_PORTS || !ports[port].in_use) return -1;
return ports[port].recv_type;
}
int datarouter_port_add_handler(datarouter_port_t port, datarouter_send_handler_t handler)
{
if (!initialized) return -1;
@@ -187,36 +170,10 @@ int datarouter_port_handler_count(datarouter_port_t port)
/* ---- 元数据查询 ---- */
int datarouter_send_cap_count(void)
{
return DATAROUTER_SEND_CAP_COUNT;
}
int datarouter_send_cap_count(void) { return DATAROUTER_SEND_CAP_COUNT; }
const char *datarouter_send_cap_name(int c) { return (c<0||c>=DATAROUTER_SEND_CAP_COUNT)?NULL:send_names[c]; }
const char *datarouter_send_cap_desc(int c) { return (c<0||c>=DATAROUTER_SEND_CAP_COUNT)?NULL:send_descs[c]; }
const char *datarouter_send_cap_name(int cap_id)
{
if (cap_id < 0 || cap_id >= DATAROUTER_SEND_CAP_COUNT) return NULL;
return send_names[cap_id];
}
const char *datarouter_send_cap_desc(int cap_id)
{
if (cap_id < 0 || cap_id >= DATAROUTER_SEND_CAP_COUNT) return NULL;
return send_descs[cap_id];
}
int datarouter_recv_cap_count(void)
{
return DATAROUTER_RECV_CAP_COUNT;
}
const char *datarouter_recv_cap_name(int cap_id)
{
if (cap_id < 0 || cap_id >= DATAROUTER_RECV_CAP_COUNT) return NULL;
return recv_names[cap_id];
}
const char *datarouter_recv_cap_desc(int cap_id)
{
if (cap_id < 0 || cap_id >= DATAROUTER_RECV_CAP_COUNT) return NULL;
return recv_descs[cap_id];
}
int datarouter_recv_cap_count(void) { return DATAROUTER_RECV_CAP_COUNT; }
const char *datarouter_recv_cap_name(int c) { return (c<0||c>=DATAROUTER_RECV_CAP_COUNT)?NULL:recv_names[c]; }
const char *datarouter_recv_cap_desc(int c) { return (c<0||c>=DATAROUTER_RECV_CAP_COUNT)?NULL:recv_descs[c]; }