33 lines
1.1 KiB
C
33 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <uv.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#include <math.h>
|
|
|
|
/* 连接状态枚举 */
|
|
typedef enum {
|
|
STATE_DISCONNECTED, // 未连接状态
|
|
STATE_CONNECTING, // 连接中状态
|
|
STATE_CONNECTED // 已连接状态
|
|
} connection_state_t;
|
|
|
|
/* 客户端上下文结构体 */
|
|
typedef struct {
|
|
uv_loop_t* loop; // libuv事件循环
|
|
uv_tcp_t client; // TCP客户端句柄
|
|
int index; // 客户端索引
|
|
uv_timer_t timer; // 数据发送定时器
|
|
uv_timer_t reconnect_timer;// 重连定时器
|
|
connection_state_t state; // 当前连接状态
|
|
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 stop_all_clients(); // 新增函数 停止所有客户端连接
|
|
void start_client_connect();//启动客户端连接
|
|
void send_binary_data(client_context_t* ctx, const unsigned char* data, size_t data_size); |