73 lines
2.3 KiB
Markdown
73 lines
2.3 KiB
Markdown
# 动态通道与转发配置方案
|
||
|
||
> **日期**: 2026-06-30 | **目标**: 用 std::vector 替代硬编码枚举,XML 驱动动态增删通道和转发规则
|
||
|
||
---
|
||
|
||
## 1. 现状问题
|
||
|
||
- `enum_comm` 硬编码通道数量和顺序 (ENUM_COMM_TCP_S_0/UART_0/TCP_S_1)
|
||
- `g_channel_para[ENUM_COMM_MAX]` 固定数组,增删都改代码
|
||
- `com_channel_get_send_info()` 用 if/else 写死转发关系
|
||
- `com_channel_interface_get(ENUM_COMM_*)` 依赖枚举值
|
||
|
||
## 2. 新设计
|
||
|
||
### 2.1 通道管理
|
||
|
||
```cpp
|
||
// key = "mode:idx", 如 "tcp_server:0", "uart:0"
|
||
struct stru_channel_para {
|
||
std::string key; // 唯一标识
|
||
std::string desc;
|
||
CommType type;
|
||
void *p_para; // TCP → g_tcp_para[i], UART → g_uart_para[i]
|
||
int id; // comm_create 返回的通道id
|
||
int socket_fd;
|
||
pthread_t thread_id;
|
||
};
|
||
std::vector<stru_channel_para> g_channels; // 动态增长
|
||
|
||
// TCP/串口参数也改为 vector
|
||
std::vector<stru_tcp_para> g_tcp_params;
|
||
std::vector<stru_uart_para> g_uart_params;
|
||
```
|
||
|
||
### 2.2 转发规则
|
||
|
||
```cpp
|
||
struct stru_forward_rule {
|
||
std::string from_key; // "tcp_server:0"
|
||
std::string to_key; // "uart:0"
|
||
};
|
||
std::vector<stru_forward_rule> g_forward_rules;
|
||
```
|
||
|
||
### 2.3 XML 新增 Forward 段
|
||
|
||
```xml
|
||
<Root>
|
||
<Channel mode="tcp_server" idx="0" desc="..." local_ip="..." local_port="..."/>
|
||
<Channel mode="uart" idx="0" desc="..." device="..." .../>
|
||
<Forward from_mode="tcp_server" from_idx="0" to_mode="uart" to_idx="0"/>
|
||
</Root>
|
||
```
|
||
|
||
### 2.4 API 变更
|
||
|
||
| 旧 | 新 | 说明 |
|
||
|----|-----|------|
|
||
| `com_channel_interface_get(ENUM_COMM_*)` | `com_channel_interface_get_by_key("uart:0")` | 用 key 查找 |
|
||
| `com_channel_get_send_info(src_id, ...)` | `com_channel_forward(src_key, ...)` | 查转发规则 |
|
||
| `g_channel_para` 固定数组 | `g_channels` vector | 动态 |
|
||
|
||
### 2.5 修改文件列表
|
||
|
||
| 文件 | 改动 |
|
||
|------|------|
|
||
| `com_channel_def.h` | 删除 enum,定义 stru_channel_para / stru_forward_rule |
|
||
| `decode_channel_mgr.cpp` | vector 替代数组,XML 解析循环,转发规则解析 |
|
||
| `decode_data_router.cpp` | `ENUM_COMM_UART_0` → `com_channel_interface_get_by_key("uart:0")` |
|
||
| `self_ptl.cpp` | dc signal key 改为 `sys.ch.uart_0_if` / `sys.ch.tcp_server_0_if` |
|
||
| `channel_config*.xml` | 新增 `Forward` 段 |
|