添加了消息处理线程

This commit is contained in:
zw
2025-06-20 09:25:17 +08:00
parent c51da2e506
commit b487937ad6
6 changed files with 191 additions and 19 deletions

View File

@@ -5,10 +5,10 @@
#include <math.h>
#include "PQSMsg.h"
#include "client2.h"
#include "dealMsg.h"
// 配置参数
#define INITIAL_DATA_SIZE 128 // 初始数据0.1KB
#define CONNECTIONS 1000 // 支持1000个并发连接
#define CONNECTIONS 10 // 支持1000个并发连接
#define SERVER_IP "101.132.39.45" // 目标服务器IP "101.132.39.45"
#define SERVER_PORT 1056 // 目标服务器端口
#define BASE_RECONNECT_DELAY 5000 // 基础重连延迟(ms)
@@ -17,7 +17,7 @@
static uv_loop_t* global_loop; // 全局事件循环
static client_context_t client_contexts[CONNECTIONS]; // 客户端上下文数组
static uv_timer_t monitor_timer; // 连接监控定时器
extern SafeMessageQueue message_queue;
/* 缓冲区分配回调 */
void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
void* buffer = malloc(suggested_size);
@@ -43,8 +43,17 @@ void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
}
if (nread > 0) {
//fprintf(stdout, "[Client %d] RECV %zd bytes data\n", ctx->index, nread);
// 数据处理逻辑...
// 将接收到的数据放入消息队列
deal_message_t msg;
msg.client_index = ctx->index;
msg.data = (char*)malloc(nread);
msg.length = nread;
memcpy(msg.data, buf->base, nread);
if (!message_queue.push(msg)) {
fprintf(stderr, "[Client %d] Message queue full, dropping message\n", ctx->index);
free(msg.data);
}
}
free(buf->base);
}
@@ -69,7 +78,7 @@ void on_timer(uv_timer_t* handle) {
if (ctx->state != STATE_CONNECTED) {
return;
}
//单个装置定时消息收发机制
// 生成完整报文 装置云服务登录报文
auto binary_data = generate_frontlogin_message("00-B7-8D-A8-00-D6");
@@ -107,7 +116,7 @@ void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t d
void on_close(uv_handle_t* handle) {
client_context_t* ctx = (client_context_t*)handle->data;
ctx->state = STATE_DISCONNECTED;
fprintf(stderr, "[Client %d] closed\n", ctx->index);
// 停止定时器
uv_timer_stop(&ctx->timer);
uv_timer_stop(&ctx->reconnect_timer);
@@ -132,7 +141,7 @@ void try_reconnect(uv_timer_t* timer) {
if (ctx->state != STATE_DISCONNECTED || ctx->shutdown) {
return;
}
fprintf(stderr, "[Client %d] try_reconnect\n", ctx->index);
// 重新初始化TCP句柄
uv_tcp_init(ctx->loop, &ctx->client);
ctx->client.data = ctx;
@@ -165,7 +174,7 @@ void on_connect(uv_connect_t* req, int status) {
free(req);
return;
}
fprintf(stderr, "[Client %d] on_connect\n", ctx->index);
ctx->state = STATE_CONNECTED;
ctx->reconnect_attempts = 0;
@@ -225,9 +234,6 @@ void stop_all_clients() {
}
/* 连接监控回调 */
void monitor_connections(uv_timer_t* handle) {
static int shutdown_flag = 0; // 新增关闭标志
if (shutdown_flag) return; // 已关闭则不再处理
// 自动恢复断开的连接
static int recovery_counter = 0;
if (++recovery_counter >= 5) { // 每5次监控执行一次恢复
@@ -246,9 +252,6 @@ void monitor_connections(uv_timer_t* handle) {
//if (monitor_temp >= 30) {
// monitor_temp = 0;
// printf("30 second to stop all client\n");
// // 设置关闭标志
// shutdown_flag = 1;
//
// // 停止并关闭监控定时器
// uv_timer_stop(handle);
// uv_close((uv_handle_t*)handle, NULL);
@@ -261,6 +264,14 @@ void monitor_connections(uv_timer_t* handle) {
//}
}
static void close_walk_cb(uv_handle_t* handle, void* arg) {
if (!uv_is_closing(handle)) {
fprintf(stderr, "Force closing leaked handle: %p (type=%d)\n",
handle, handle->type);
uv_close(handle, NULL);
}
}
void start_client_connect() {
// 创建全局事件循环
global_loop = uv_default_loop();
@@ -275,7 +286,18 @@ void start_client_connect() {
// 运行事件循环
uv_run(global_loop, UV_RUN_DEFAULT);
// 清理资源(关键:确保循环完全停止)
uv_loop_close(global_loop);
// 添加资源清理阶段
while (uv_loop_alive(global_loop)) {
uv_run(global_loop, UV_RUN_ONCE);
}
// 安全关闭事件循环
int err = uv_loop_close(global_loop);
if (err) {
fprintf(stderr, "uv_loop_close error: %s\n", uv_strerror(err));
// 强制清理残留句柄(调试用)
uv_walk(global_loop, close_walk_cb, NULL);
uv_run(global_loop, UV_RUN_NOWAIT);
}
global_loop = NULL;
}