From c51da2e506ec266649adc8c1869dbc4c997afc1f Mon Sep 17 00:00:00 2001 From: zw <3466561528@qq.com> Date: Thu, 19 Jun 2025 13:24:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BA=86socket=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFtid1056/PQSMsg.cpp | 45 ----- LFtid1056/client2.cpp | 361 +++++++++++++------------------------- LFtid1056/client2.h | 6 +- LFtid1056/main_thread.cpp | 12 +- 4 files changed, 123 insertions(+), 301 deletions(-) diff --git a/LFtid1056/PQSMsg.cpp b/LFtid1056/PQSMsg.cpp index 33315e1..1776ef4 100644 --- a/LFtid1056/PQSMsg.cpp +++ b/LFtid1056/PQSMsg.cpp @@ -9,51 +9,6 @@ #include "client2.h" #include "PQSMsg.h" -// 生成带协议头的二进制报文 -std::vector generate_binary_message( - uint16_t msg_type, - const std::vector& payload) -{ - std::vector packet; - - // 协议头 (4字节) - packet.push_back(0xAA); // 起始标志 - packet.push_back((msg_type >> 8) & 0xFF); // 消息类型高字节 - packet.push_back(msg_type & 0xFF); // 消息类型低字节 - uint8_t length = payload.size(); - packet.push_back(length); // 载荷长度 - - // 载荷数据 - packet.insert(packet.end(), payload.begin(), payload.end()); - - // 校验位 (1字节异或校验) - uint8_t checksum = 0; - for (auto byte : packet) { - checksum ^= byte; - } - packet.push_back(checksum); - - return packet; -} - -// 使用示例 -void send_message(client_context_t* ctx) { - // 构造载荷数据 - std::vector payload = { - 0x01, 0x02, 0x03, 0x04 // 示例数据 - }; - - // 生成完整报文 (消息类型0x1001) - auto binary_data = generate_binary_message(0x1001, payload); - - // 转换为数组形式 - unsigned char* binary_array = binary_data.data(); - size_t data_size = binary_data.size(); - - // 此处可调用发送函数 - send_binary_data(ctx, binary_array, data_size); -} - // 辅助函数:解析MAC地址并填充到缓冲区 void GetMAC(const std::string& strMAC, std::vector& packet, size_t startIndex) { // 移除所有空格和短横线 diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index 5cf0faa..d2a1ef6 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -2,30 +2,26 @@ #include #include #include -#include -#include #include #include "PQSMsg.h" #include "client2.h" - // 閰嶇疆鍙傛暟 +// 閰嶇疆鍙傛暟 #define INITIAL_DATA_SIZE 128 // 鍒濆鏁版嵁0.1KB -#define CONNECTIONS 2 // 骞跺彂杩炴帴鏁 璁惧杩炴帴鏁 -#define SERVER_IP "101.132.39.45" // 鐩爣鏈嶅姟鍣↖P 闃块噷浜戞湇鍔″櫒 "101.132.39.45""172.27.208.1" -#define SERVER_PORT 1056 // 鐩爣鏈嶅姟鍣ㄧ鍙 -#define BASE_RECONNECT_DELAY 5000 // 鍩虹閲嶈繛寤惰繜(ms) -#define MAX_RECONNECT_DELAY 60000 // 鏈澶ч噸杩炲欢杩(ms) -#define MAX_RECONNECT_ATTEMPTS 10 // 鏈澶ч噸杩炴鏁 +#define CONNECTIONS 1000 // 鏀寔1000涓苟鍙戣繛鎺 +#define SERVER_IP "101.132.39.45" // 鐩爣鏈嶅姟鍣↖P "101.132.39.45" +#define SERVER_PORT 1056 // 鐩爣鏈嶅姟鍣ㄧ鍙 +#define BASE_RECONNECT_DELAY 5000 // 鍩虹閲嶈繛寤惰繜(ms) +#define MAX_RECONNECT_DELAY 60000 // 鏈澶ч噸杩炲欢杩(ms) -// 鍏ㄥ眬鍙橀噺鐢ㄤ簬绠$悊瀹㈡埛绔嚎绋 -static pthread_t client_threads[CONNECTIONS]; -static client_context_t* client_contexts[CONNECTIONS] = { NULL }; +static uv_loop_t* global_loop; // 鍏ㄥ眬浜嬩欢寰幆 +static client_context_t client_contexts[CONNECTIONS]; // 瀹㈡埛绔笂涓嬫枃鏁扮粍 +static uv_timer_t monitor_timer; // 杩炴帴鐩戞帶瀹氭椂鍣 /* 缂撳啿鍖哄垎閰嶅洖璋 */ void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { void* buffer = malloc(suggested_size); if (!buffer) { - fprintf(stderr, "Memory allocation failed\n"); *buf = uv_buf_init(NULL, 0); return; } @@ -41,18 +37,16 @@ void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { fprintf(stdout, "[Client %d] RECV ERROR: %s\n", ctx->index, uv_strerror(nread)); } - else { - fprintf(stdout, "[Client %d] Connection closed by server\n", ctx->index); - } uv_close((uv_handle_t*)stream, on_close); free(buf->base); + return; } - else if (nread > 0) { - fprintf(stdout, "[Client %d] RECV %zd bytes data\n", ctx->index, nread); - //鏁版嵁浼犲叆瀹㈡埛绔紦鍐插尯骞跺皾璇曞彇鍑哄悎娉曟姤鏂囨牸寮忕殑鏁版嵁 - free(buf->base); + if (nread > 0) { + //fprintf(stdout, "[Client %d] RECV %zd bytes data\n", ctx->index, nread); + // 鏁版嵁澶勭悊閫昏緫... } + free(buf->base); } /* 鏁版嵁鍐欏叆鍥炶皟 */ @@ -63,25 +57,19 @@ void on_write(uv_write_t* req, int status) { fprintf(stdout, "[Client %d] SEND ERROR: %s\n", ctx->index, uv_strerror(status)); } - else { - fprintf(stdout, "[Client %d] SEND bytes success\n", - ctx->index); - } - free(req->data); - free(req); + + free(req->data); // 閲婃斁鍙戦佹暟鎹紦鍐插尯 + free(req); // 閲婃斁鍐欏叆璇锋眰 } -/* 瀹氭椂缁勮浜岃繘鍒舵姤鏂囧苟鍙戦 */ +/* 瀹氭椂鍙戦佸洖璋 */ void on_timer(uv_timer_t* handle) { client_context_t* ctx = (client_context_t*)handle->data; if (ctx->state != STATE_CONNECTED) { - fprintf(stdout, "[Client %d] Skip sending: Not connected\n", ctx->index); return; } - fprintf(stdout, "[Client %d] Preparing periodic data...\n", ctx->index); - // 鐢熸垚瀹屾暣鎶ユ枃 瑁呯疆浜戞湇鍔$櫥褰曟姤鏂 auto binary_data = generate_frontlogin_message("00-B7-8D-A8-00-D6"); @@ -92,46 +80,6 @@ void on_timer(uv_timer_t* handle) { // 姝ゅ鍙皟鐢ㄥ彂閫佸嚱鏁 send_binary_data(ctx, binary_array, data_size); } - -/* 鍙戦佸垵濮嬫暟鎹潡 - 淇鐗 */ -void send_initial_data(client_context_t* ctx) { - // 娣诲姞鐘舵佹鏌 - if (ctx->state != STATE_CONNECTED) { - fprintf(stderr, "[Client %d] Cannot send initial data: not connected\n", ctx->index); - return; - } - - // 淇涓哄疄闄呮暟鎹ぇ灏 (1KB) - const size_t data_size = INITIAL_DATA_SIZE; // 浣跨敤瀹為檯鍊兼浛鎹 INITIAL_DATA_SIZE - char* buffer = (char*)malloc(data_size); - if (!buffer) { - fprintf(stderr, "[Client %d] Failed to allocate initial data buffer\n", ctx->index); - return; - } - memset(buffer, 'A', data_size); - buffer[0] = 0xeb; buffer[1] = 0x90; buffer[2] = 0xff; buffer[15] = 0x16; - - fprintf(stdout, "[Client %d] Sending initial %zu bytes data\n", ctx->index, data_size); - - uv_buf_t buf = uv_buf_init(buffer, data_size); - uv_write_t* write_req = (uv_write_t*)malloc(sizeof(uv_write_t)); - if (!write_req) { - free(buffer); - fprintf(stderr, "[Client %d] Failed to allocate write request\n", ctx->index); - return; - } - write_req->data = buffer; - - // 妫鏌v_write杩斿洖鍊 - int ret = uv_write(write_req, (uv_stream_t*)&ctx->client, &buf, 1, on_write); - if (ret < 0) { - fprintf(stderr, "[Client %d] uv_write failed: %s\n", - ctx->index, uv_strerror(ret)); - free(buffer); - free(write_req); - } -} - /* 鍙戦佷簩杩涘埗鎶ユ枃鍑芥暟 */ void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t data_size) { if (ctx->state != STATE_CONNECTED) { @@ -146,7 +94,7 @@ void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t d return; } write_req->data = NULL; // 涓嶉渶瑕侀澶栨暟鎹紝鍥犱负data宸茬粡浼犲叆 - fprintf(stdout, "[Client %d] Sending initial %zu bytes data\n", ctx->index, data_size); + //fprintf(stdout, "[Client %d] Sending initial %zu bytes data\n", ctx->index, data_size); int ret = uv_write(write_req, (uv_stream_t*)&ctx->client, &buf, 1, on_write); if (ret < 0) { @@ -155,62 +103,50 @@ void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t d } // 娉ㄦ剰锛氳繖閲屼笉闇瑕侀噴鏀綿ata锛屽洜涓篸ata鏄敱璋冪敤鑰呯鐞嗙殑 } - /* 杩炴帴鍏抽棴鍥炶皟 */ void on_close(uv_handle_t* handle) { client_context_t* ctx = (client_context_t*)handle->data; ctx->state = STATE_DISCONNECTED; - fprintf(stdout, "[Client %d] Connection closed\n", ctx->index); - // 鍋滄鏁版嵁璇诲啓瀹氭椂鍣ㄥ拰閲嶈繛瀹氭椂鍣 + // 鍋滄瀹氭椂鍣 uv_timer_stop(&ctx->timer); uv_timer_stop(&ctx->reconnect_timer); - // 鍙栨秷璇绘搷浣 - uv_read_stop((uv_stream_t*)&ctx->client); - - // 濡傛灉涓嶆槸涓诲姩鍏抽棴锛屽皾璇曢噸杩 + // 鑷姩閲嶈繛閫昏緫 if (!ctx->shutdown) { int delay = BASE_RECONNECT_DELAY * pow(2, ctx->reconnect_attempts); delay = delay > MAX_RECONNECT_DELAY ? MAX_RECONNECT_DELAY : delay; - fprintf(stdout, "[Client %d] Will reconnect after %dms (attempt %d/%d)\n", - ctx->index, delay, ctx->reconnect_attempts + 1, MAX_RECONNECT_ATTEMPTS); + fprintf(stdout, "[Client %d] Reconnecting in %dms (attempt %d)\n", + ctx->index, delay, ctx->reconnect_attempts + 1); - uv_timer_start(&ctx->reconnect_timer, (uv_timer_cb)try_reconnect, delay, 0); ctx->reconnect_attempts++; + uv_timer_start(&ctx->reconnect_timer, try_reconnect, delay, 0); } } -/* 灏濊瘯閲嶈繛鍑芥暟 */ +/* 灏濊瘯閲嶈繛 */ void try_reconnect(uv_timer_t* timer) { client_context_t* ctx = (client_context_t*)timer->data; if (ctx->state != STATE_DISCONNECTED || ctx->shutdown) { - fprintf(stdout, "[Client %d] Reconnect skipped: Invalid state or shutdown\n", ctx->index); - return; - } - - fprintf(stdout, "[Client %d] Attempting to reconnect...\n", ctx->index); - if (ctx->reconnect_attempts >= MAX_RECONNECT_ATTEMPTS) { - fprintf(stderr, "[Client %d] Max reconnect attempts reached\n", ctx->index); return; } // 閲嶆柊鍒濆鍖朤CP鍙ユ焺 uv_tcp_init(ctx->loop, &ctx->client); ctx->client.data = ctx; - ctx->state = STATE_CONNECTING; + struct sockaddr_in addr; uv_ip4_addr(SERVER_IP, SERVER_PORT, &addr); uv_connect_t* req = (uv_connect_t*)malloc(sizeof(uv_connect_t)); req->data = ctx; + int ret = uv_tcp_connect(req, &ctx->client, (const struct sockaddr*)&addr, on_connect); if (ret < 0) { fprintf(stderr, "[Client %d] Connect error: %s\n", ctx->index, uv_strerror(ret)); - ctx->state = STATE_DISCONNECTED; free(req); uv_close((uv_handle_t*)&ctx->client, on_close); } @@ -220,183 +156,126 @@ void try_reconnect(uv_timer_t* timer) { void on_connect(uv_connect_t* req, int status) { client_context_t* ctx = (client_context_t*)req->data; - if (ctx->state != STATE_CONNECTING) { - fprintf(stdout, "[Client %d] Connection callback with invalid state\n", ctx->index); - free(req); - return; - } - if (status < 0) { fprintf(stderr, "[Client %d] Connect failed: %s\n", ctx->index, uv_strerror(status)); - ctx->state = STATE_DISCONNECTED; + // 鐩存帴鍏抽棴鍙ユ焺锛岄伩鍏嶅悗缁噸澶嶅叧闂 + if (!uv_is_closing((uv_handle_t*)&ctx->client)) { + uv_close((uv_handle_t*)&ctx->client, NULL); + } free(req); - uv_close((uv_handle_t*)&ctx->client, on_close); return; } - else { - ctx->state = STATE_CONNECTED; - ctx->reconnect_attempts = 0; - fprintf(stdout, "[Client %d] Connection established\n", ctx->index); - // 鍚姩鏁版嵁鏀跺彂 - uv_read_start((uv_stream_t*)&ctx->client, alloc_buffer, on_read); - //send_initial_data(ctx); + ctx->state = STATE_CONNECTED; + ctx->reconnect_attempts = 0; - // 鍚姩瀹氭椂鍣 - //uv_timer_start(&ctx->timer, on_timer, 6000, 6000); - } + // 鍚姩鏁版嵁鎺ユ敹 + uv_read_start((uv_stream_t*)&ctx->client, alloc_buffer, on_read); + + // 鍚姩瀹氭椂鍙戦 + uv_timer_start(&ctx->timer, on_timer, 6000, 6000); free(req); } -/* 瀹㈡埛绔嚎绋嬪嚱鏁 */ -void* run_client(void* arg) { - int index = *(int*)arg; - free(arg); +/* 鍒濆鍖栨墍鏈夊鎴风杩炴帴 */ +void init_clients(uv_loop_t* loop) { + for (int i = 0; i < CONNECTIONS; i++) { + client_context_t* ctx = &client_contexts[i]; + memset(ctx, 0, sizeof(client_context_t)); - // 鍒濆鍖栦簨浠跺惊鐜 - uv_loop_t* loop = uv_loop_new(); - client_context_t* ctx = (client_context_t*)malloc(sizeof(client_context_t)); - memset(ctx, 0, sizeof(client_context_t)); + ctx->loop = loop; + ctx->index = i; + ctx->state = STATE_DISCONNECTED; - // 鍒濆鍖栦笂涓嬫枃 - ctx->loop = loop; - ctx->index = index; - ctx->state = STATE_DISCONNECTED; - ctx->reconnect_attempts = 0; - ctx->shutdown = 0; // 鍒濆鍖栧叧闂爣蹇 + // 鍒濆鍖朤CP鍙ユ焺 + uv_tcp_init(loop, &ctx->client); + ctx->client.data = ctx; - // 鍒濆鍖杔ibuv鍙ユ焺 - uv_tcp_init(loop, &ctx->client); - uv_timer_init(loop, &ctx->timer); - uv_timer_init(loop, &ctx->reconnect_timer); + // 鍒濆鍖栧畾鏃跺櫒 + uv_timer_init(loop, &ctx->timer); + ctx->timer.data = ctx; - // 璁剧疆鍏宠仈鏁版嵁 - ctx->client.data = ctx; - ctx->timer.data = ctx; - ctx->reconnect_timer.data = ctx; + // 鍒濆鍖栭噸杩炲畾鏃跺櫒 + uv_timer_init(loop, &ctx->reconnect_timer); + ctx->reconnect_timer.data = ctx; - // 棣栨杩炴帴灏濊瘯 - try_reconnect(&ctx->reconnect_timer); - - // 杩愯浜嬩欢寰幆锛屽畾鏈熸鏌ュ叧闂爣蹇 - while (uv_loop_alive(loop) && !ctx->shutdown) { - uv_run(loop, UV_RUN_NOWAIT); - usleep(10000); // 10ms + // 棣栨杩炴帴 + try_reconnect(&ctx->reconnect_timer); } - fprintf(stdout, "[Client %d] thread closed\n", ctx->index); - // 涓诲姩鍏抽棴鎵鏈夎祫婧 - if (ctx->shutdown) { - uv_close((uv_handle_t*)&ctx->client, on_close); - uv_close((uv_handle_t*)&ctx->timer, NULL); - uv_close((uv_handle_t*)&ctx->reconnect_timer, NULL); - - // 纭繚鎵鏈夊叧闂洖璋冩墽琛 - while (uv_loop_alive(loop)) { - uv_run(loop, UV_RUN_ONCE); - } - } - - // 璧勬簮娓呯悊 - uv_loop_close(loop); - uv_loop_delete(loop); - free(ctx); - return NULL; } -/* 鍋滄鎵鏈夊鎴风绾跨▼ (鏂板鍑芥暟) */ +/* 鍋滄鎵鏈夊鎴风 */ void stop_all_clients() { for (int i = 0; i < CONNECTIONS; i++) { - if (client_contexts[i]) { - client_contexts[i]->shutdown = 1; // 璁剧疆鍏抽棴鏍囧織 - } - } + client_context_t* ctx = &client_contexts[i]; + ctx->shutdown = 1; - // 绛夊緟鎵鏈夊鎴风绾跨▼閫鍑 - for (int i = 0; i < CONNECTIONS; i++) { - if (client_threads[i]) { - pthread_join(client_threads[i], NULL); - client_threads[i] = 0; + // 鍏抽棴鎵鏈夊彞鏌 + if (!uv_is_closing((uv_handle_t*)&ctx->client)) { + uv_close((uv_handle_t*)&ctx->client, NULL); + } + if (!uv_is_closing((uv_handle_t*)&ctx->timer)) { + uv_close((uv_handle_t*)&ctx->timer, NULL); + } + if (!uv_is_closing((uv_handle_t*)&ctx->reconnect_timer)) { + uv_close((uv_handle_t*)&ctx->reconnect_timer, NULL); } } } +/* 杩炴帴鐩戞帶鍥炶皟 */ +void monitor_connections(uv_timer_t* handle) { + static int shutdown_flag = 0; // 鏂板鍏抽棴鏍囧織 + if (shutdown_flag) return; // 宸插叧闂垯涓嶅啀澶勭悊 -/* 鍚姩鎵鏈夊鎴风绾跨▼ (鏂板鍑芥暟) */ -void start_all_clients() { - for (int i = 0; i < CONNECTIONS; i++) { - int* index = (int*)malloc(sizeof(int)); - *index = i; - - if (pthread_create(&client_threads[i], NULL, run_client, index) != 0) { - fprintf(stderr, "Failed to create client thread %d\n", i); - free(index); - } - else { - // 涓婁笅鏂囦細鍦╮un_client涓垱寤猴紝杩欓噷鍒濆鍖栦负NULL - client_contexts[i] = NULL; - } - } -} - -/* 鑾峰彇娲诲姩瀹㈡埛绔暟閲 (鏂板鍑芥暟) */ -int get_active_client_count() { - int active_count = 0; - for (int i = 0; i < CONNECTIONS; i++) { - if (client_threads[i] && pthread_tryjoin_np(client_threads[i], NULL) == EBUSY) { - active_count++; - } - } - return active_count; -} - -/* 瀹㈡埛绔繛鎺ョ洃鎺у嚱鏁 (鏂板鍑芥暟) */ -void monitor_client_connections() { - static int monitor_counter = 0; - monitor_counter++; - - // 姣5娆¤皟鐢紙绾5绉掞級妫鏌ヤ竴娆$姸鎬 - if (monitor_counter >= 5) { - monitor_counter = 0; - - int active_clients = get_active_client_count(); - printf("Active client connections: %d/%d\n", active_clients, CONNECTIONS); - - // 濡傛灉鎵鏈夊鎴风绾跨▼閮介鍑轰簡锛岃嚜鍔ㄩ噸鍚 - if (active_clients == 0) { - printf("All clients stopped, restarting...\n"); - start_all_clients(); + // 鑷姩鎭㈠鏂紑鐨勮繛鎺 + static int recovery_counter = 0; + if (++recovery_counter >= 5) { // 姣5娆$洃鎺ф墽琛屼竴娆℃仮澶 + int active_count = 0; + for (int i = 0; i < CONNECTIONS; i++) { + if (client_contexts[i].state == STATE_CONNECTED) { + active_count++; + } } + printf("Active connections: %d/%d\n", active_count, CONNECTIONS); + recovery_counter = 0; } - /*static int monitor_temp = 0; - monitor_temp++; - if (monitor_temp >= 30) { - monitor_temp = 0; - printf("30 second to stop all client\n"); - stop_all_clients(); - }*/ + //static int monitor_temp = 0; + //monitor_temp++; + //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); + // + // // 鍋滄鎵鏈夊鎴风 + // stop_all_clients(); + // + // // 鍋滄浜嬩欢寰幆 + // uv_stop(global_loop); + //} +} + +void start_client_connect() { + // 鍒涘缓鍏ㄥ眬浜嬩欢寰幆 + global_loop = uv_default_loop(); + + // 鍒濆鍖栨墍鏈夊鎴风 + init_clients(global_loop); + + // 鍚姩杩炴帴鐩戞帶 + uv_timer_init(global_loop, &monitor_timer); + uv_timer_start(&monitor_timer, monitor_connections, 1000, 1000); + + // 杩愯浜嬩欢寰幆 + uv_run(global_loop, UV_RUN_DEFAULT); + + // 娓呯悊璧勬簮锛堝叧閿細纭繚寰幆瀹屽叏鍋滄锛 + uv_loop_close(global_loop); + global_loop = NULL; } -///* 涓诲嚱鏁 */ -//int main() { -// pthread_t threads[CONNECTIONS]; -// -// // 鍒涘缓瀹㈡埛绔嚎绋 -// for (int i = 0; i < CONNECTIONS; i++) { -// int* index = (int*)malloc(sizeof(int)); -// *index = i; -// -// if (pthread_create(&threads[i], NULL, run_client, index) != 0) { -// fprintf(stderr, "Failed to create thread %d\n", i); -// free(index); -// } -// } -// -// printf("Started %d client connections\n", CONNECTIONS); -// -// // 绛夊緟鎵鏈夌嚎绋嬬粨鏉 -// for (int i = 0; i < CONNECTIONS; i++) { -// pthread_join(threads[i], NULL); -// } -// -// return 0; -//} diff --git a/LFtid1056/client2.h b/LFtid1056/client2.h index 5ce3a71..f200018 100644 --- a/LFtid1056/client2.h +++ b/LFtid1056/client2.h @@ -24,12 +24,10 @@ typedef struct { int reconnect_attempts; // 当前重连次数 volatile int shutdown; // 关闭标志 (新增) } client_context_t; - //函数声明 void try_reconnect(uv_timer_t* timer);//客户端重连回调 void on_connect(uv_connect_t* req, int status);//客户端连接回调 void on_close(uv_handle_t* handle);//客户端断开回调 -void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t data_size);//组装二进制报文并发送 客户端连接,数据,数据大小 void stop_all_clients(); // 新增函数 停止所有客户端连接 -void start_all_clients(); //新增函数 启动所有客户端连接 -void monitor_client_connections();//监控客户端连接数量 +void start_client_connect();//启动客户端连接 +void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t data_size); diff --git a/LFtid1056/main_thread.cpp b/LFtid1056/main_thread.cpp index 11ebbfb..37dd3af 100644 --- a/LFtid1056/main_thread.cpp +++ b/LFtid1056/main_thread.cpp @@ -74,20 +74,10 @@ void* client_manager_thread(void* arg) { thread_info[index].state = THREAD_RUNNING; pthread_mutex_unlock(&thread_info[index].lock); - // 启动所有客户端连接线程 - start_all_clients(); printf("Started client connections\n"); - // 监控循环 - while (thread_info[index].state == THREAD_RUNNING) { - sleep(1); + start_client_connect(); - // 监控客户端连接状态(调用客户端连接部分的函数) - monitor_client_connections(); - } - - // 停止所有客户端线程 - stop_all_clients(); printf("Stopped all client connections\n"); // 线程终止处理