| 1 |
事件循环和IO多路复用机制介绍 |
| 2 |
|
| 3 |
事件循环是`libevent、libev、libuv、libhv`这类网络库里最核心的概念,即在事件循环里处理IO读写事件、定时器事件、自定义事件等各种事件;<br> |
| 4 |
IO多路复用即在一个IO线程监听多个fd,如最早期的`select`、后来的`poll`,`linux的epoll`、`windows的iocp`、`bsd的kqueue`、`solaris的port`等,都属于IO多路复用机制。<br> |
| 5 |
非阻塞NIO搭配IO多路复用机制就是高并发的钥匙。<br> |
| 6 |
`libhv`下的`event`模块正是封装了多种平台的IO多路复用机制,提供了统一的事件接口,是`libhv`的核心模块。<br> |
| 7 |
|
| 8 |
`hloop.h`: 事件循环模块对外头文件。<br> |
| 9 |
|
| 10 |
```c |
| 11 |
|
| 12 |
// 事件结构体 |
| 13 |
struct hevent_s { |
| 14 |
hloop_t* loop; // 事件所属循环 |
| 15 |
hevent_type_e event_type; // 事件类型 |
| 16 |
uint64_t event_id; // 事件ID |
| 17 |
hevent_cb cb; // 事件回调 |
| 18 |
void* userdata; // 用户数据 |
| 19 |
void* privdata; // 私有数据 |
| 20 |
struct hevent_s* pending_next; // 指向下一个事件,用于实现事件队列 |
| 21 |
int priority; // 事件优先级 |
| 22 |
}; |
| 23 |
|
| 24 |
// 设置事件ID |
| 25 |
#define hevent_set_id(ev, id) ((hevent_t*)(ev))->event_id = id |
| 26 |
// 设置事件回调 |
| 27 |
#define hevent_set_cb(ev, cb) ((hevent_t*)(ev))->cb = cb |
| 28 |
// 设置事件优先级 |
| 29 |
#define hevent_set_priority(ev, prio) ((hevent_t*)(ev))->priority = prio |
| 30 |
// 设置事件用户数据 |
| 31 |
#define hevent_set_userdata(ev, udata) ((hevent_t*)(ev))->userdata = (void*)udata |
| 32 |
|
| 33 |
// 获取事件所属循环 |
| 34 |
#define hevent_loop(ev) (((hevent_t*)(ev))->loop) |
| 35 |
// 获取事件类型 |
| 36 |
#define hevent_type(ev) (((hevent_t*)(ev))->event_type) |
| 37 |
// 获取事件ID |
| 38 |
#define hevent_id(ev) (((hevent_t*)(ev))->event_id) |
| 39 |
// 获取事件回调 |
| 40 |
#define hevent_cb(ev) (((hevent_t*)(ev))->cb) |
| 41 |
// 获取事件优先级 |
| 42 |
#define hevent_priority(ev) (((hevent_t*)(ev))->priority) |
| 43 |
// 获取事件用户数据 |
| 44 |
#define hevent_userdata(ev) (((hevent_t*)(ev))->userdata) |
| 45 |
|
| 46 |
// hio_t、htimer_t、hsignal_t、hidle_t皆是继承自hevent_t,继承上面的数据成员和函数方法 |
| 47 |
|
| 48 |
// 新建事件循环 |
| 49 |
hloop_t* hloop_new(int flags DEFAULT(HLOOP_FLAG_AUTO_FREE)); |
| 50 |
|
| 51 |
// 释放事件循环 |
| 52 |
void hloop_free(hloop_t** pp); |
| 53 |
|
| 54 |
// 运行事件循环 |
| 55 |
int hloop_run(hloop_t* loop); |
| 56 |
|
| 57 |
// 停止事件循环 |
| 58 |
int hloop_stop(hloop_t* loop); |
| 59 |
|
| 60 |
// 暂停事件循环 |
| 61 |
int hloop_pause(hloop_t* loop); |
| 62 |
|
| 63 |
// 继续事件循环 |
| 64 |
int hloop_resume(hloop_t* loop); |
| 65 |
|
| 66 |
// 唤醒事件循环 |
| 67 |
int hloop_wakeup(hloop_t* loop); |
| 68 |
|
| 69 |
// 返回事件循环状态 |
| 70 |
hloop_status_e hloop_status(hloop_t* loop); |
| 71 |
|
| 72 |
// 更新事件循环里的时间 |
| 73 |
void hloop_update_time(hloop_t* loop); |
| 74 |
|
| 75 |
// 返回事件循环里记录的时间 |
| 76 |
uint64_t hloop_now(hloop_t* loop); // s |
| 77 |
uint64_t hloop_now_ms(hloop_t* loop); // ms |
| 78 |
uint64_t hloop_now_us(hloop_t* loop); // us |
| 79 |
|
| 80 |
// 返回事件循环所在进程ID |
| 81 |
long hloop_pid(hloop_t* loop); |
| 82 |
|
| 83 |
// 返回事件循环所在线程ID |
| 84 |
long hloop_tid(hloop_t* loop); |
| 85 |
|
| 86 |
// 返回事件循环的循环次数 |
| 87 |
uint64_t hloop_count(hloop_t* loop); |
| 88 |
|
| 89 |
// 返回事件循环里激活的IO事件数量 |
| 90 |
uint32_t hloop_nios(hloop_t* loop); |
| 91 |
|
| 92 |
// 返回事件循环里激活的定时器事件数量 |
| 93 |
uint32_t hloop_ntimers(hloop_t* loop); |
| 94 |
|
| 95 |
// 返回事件循环里激活的空闲事件数量 |
| 96 |
uint32_t hloop_nidles(hloop_t* loop); |
| 97 |
|
| 98 |
// 返回事件循环里激活的事件数量 |
| 99 |
uint32_t hloop_nactives(hloop_t* loop); |
| 100 |
|
| 101 |
// 设置事件循环的用户数据 |
| 102 |
void hloop_set_userdata(hloop_t* loop, void* userdata); |
| 103 |
|
| 104 |
// 获取事件循环的用户数据 |
| 105 |
void* hloop_userdata(hloop_t* loop); |
| 106 |
|
| 107 |
// 投递事件 |
| 108 |
void hloop_post_event(hloop_t* loop, hevent_t* ev); |
| 109 |
|
| 110 |
// 添加信号处理 |
| 111 |
hsignal_t* hsignal_add(hloop_t* loop, hsignal_cb cb, int signo); |
| 112 |
|
| 113 |
// 删除信号处理 |
| 114 |
void hsignal_del(hsignal_t* sig); |
| 115 |
|
| 116 |
// 添加空闲事件 |
| 117 |
hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE)); |
| 118 |
|
| 119 |
// 删除空闲事件 |
| 120 |
void hidle_del(hidle_t* idle); |
| 121 |
|
| 122 |
// 添加超时定时器 |
| 123 |
htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout_ms, uint32_t repeat DEFAULT(INFINITE)); |
| 124 |
|
| 125 |
// 添加时间定时器 |
| 126 |
htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb, |
| 127 |
int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1), |
| 128 |
int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE)); |
| 129 |
|
| 130 |
// 删除定时器 |
| 131 |
void htimer_del(htimer_t* timer); |
| 132 |
|
| 133 |
// 重置定时器 |
| 134 |
void htimer_reset(htimer_t* timer, uint32_t timeout_ms DEFAULT(0)); |
| 135 |
|
| 136 |
// 返回IO多路复用引擎 (select、poll、epoll、etc.) |
| 137 |
const char* hio_engine(); |
| 138 |
|
| 139 |
// 获取IO对象 |
| 140 |
hio_t* hio_get(hloop_t* loop, int fd); |
| 141 |
|
| 142 |
// 添加IO读写事件 |
| 143 |
int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(HV_READ)); |
| 144 |
|
| 145 |
// 删除IO读写事件 |
| 146 |
int hio_del(hio_t* io, int events DEFAULT(HV_RDWR)); |
| 147 |
|
| 148 |
// 将IO对象从当前所属事件循环中剥离 |
| 149 |
void hio_detach(/*hloop_t* loop,*/ hio_t* io); |
| 150 |
|
| 151 |
// 将IO对象关联到新的事件循环 |
| 152 |
void hio_attach(hloop_t* loop, hio_t* io); |
| 153 |
|
| 154 |
// hio_detach 和 hio_attach 的示例代码见 examples/multi-thread/one-acceptor-multi-workers.c |
| 155 |
/* |
| 156 |
void new_conn_event(hevent_t* ev) { |
| 157 |
hloop_t* loop = ev->loop; |
| 158 |
hio_t* io = (hio_t*)hevent_userdata(ev); |
| 159 |
// 关联到新的worker事件循环 |
| 160 |
hio_attach(loop, io); |
| 161 |
} |
| 162 |
|
| 163 |
void on_accpet(hio_t* io) { |
| 164 |
// 从acceptor所在事件循环中剥离 |
| 165 |
hio_detach(io); |
| 166 |
|
| 167 |
// 将新的连接按照负载均衡策略分发到worker线程 |
| 168 |
hloop_t* worker_loop = get_one_loop(); |
| 169 |
hevent_t ev; |
| 170 |
memset(&ev, 0, sizeof(ev)); |
| 171 |
ev.loop = worker_loop; |
| 172 |
ev.cb = new_conn_event; |
| 173 |
ev.userdata = io; |
| 174 |
hloop_post_event(worker_loop, &ev); |
| 175 |
} |
| 176 |
*/ |
| 177 |
|
| 178 |
// 判断fd是否存在于事件循环 |
| 179 |
bool hio_exists(hloop_t* loop, int fd); |
| 180 |
|
| 181 |
// 返回一个唯一标示ID |
| 182 |
uint32_t hio_id (hio_t* io); |
| 183 |
|
| 184 |
// 返回文件描述符 |
| 185 |
int hio_fd (hio_t* io); |
| 186 |
|
| 187 |
// 返回错误码 |
| 188 |
int hio_error (hio_t* io); |
| 189 |
|
| 190 |
// 返回添加的事件 |
| 191 |
int hio_events (hio_t* io); |
| 192 |
|
| 193 |
// 获取返回的事件 |
| 194 |
int hio_revents (hio_t* io); |
| 195 |
|
| 196 |
// 返回IO类型 |
| 197 |
hio_type_e hio_type (hio_t* io); |
| 198 |
|
| 199 |
// 返回本地地址 |
| 200 |
struct sockaddr* hio_localaddr(hio_t* io); |
| 201 |
|
| 202 |
// 返回对端地址 |
| 203 |
struct sockaddr* hio_peeraddr (hio_t* io); |
| 204 |
|
| 205 |
// 设置上下文 |
| 206 |
void hio_set_context(hio_t* io, void* ctx); |
| 207 |
|
| 208 |
// 获取上下文 |
| 209 |
void* hio_context(hio_t* io); |
| 210 |
|
| 211 |
// 是否已打开 |
| 212 |
bool hio_is_opened(hio_t* io); |
| 213 |
|
| 214 |
// 是否已连接 |
| 215 |
bool hio_is_connected(hio_t* io); |
| 216 |
|
| 217 |
// 是否已关闭 |
| 218 |
bool hio_is_closed(hio_t* io); |
| 219 |
|
| 220 |
// 设置读缓存 |
| 221 |
void hio_set_readbuf(hio_t* io, void* buf, size_t len); |
| 222 |
|
| 223 |
// 获取读缓存 |
| 224 |
hio_readbuf_t* hio_get_readbuf(hio_t* io); |
| 225 |
|
| 226 |
// 设置最大读缓存 |
| 227 |
void hio_set_max_read_bufsize (hio_t* io, uint32_t size); |
| 228 |
|
| 229 |
// 设置最大写缓存 |
| 230 |
void hio_set_max_write_bufsize(hio_t* io, uint32_t size); |
| 231 |
|
| 232 |
// 获取当前写缓存大小 |
| 233 |
size_t hio_write_bufsize(hio_t* io); |
| 234 |
|
| 235 |
// 判断是否写完成 |
| 236 |
#define hio_write_is_complete(io) (hio_write_bufsize(io) == 0) |
| 237 |
|
| 238 |
// 获取最后读的时间 |
| 239 |
uint64_t hio_last_read_time(hio_t* io); // ms |
| 240 |
|
| 241 |
// 获取最后写的时间 |
| 242 |
uint64_t hio_last_write_time(hio_t* io); // ms |
| 243 |
|
| 244 |
// 设置accept回调 |
| 245 |
void hio_setcb_accept (hio_t* io, haccept_cb accept_cb); |
| 246 |
// 设置连接回调 |
| 247 |
void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb); |
| 248 |
// 设置读回调 |
| 249 |
void hio_setcb_read (hio_t* io, hread_cb read_cb); |
| 250 |
// 设置写回调 |
| 251 |
void hio_setcb_write (hio_t* io, hwrite_cb write_cb); |
| 252 |
// 设置关闭回调 |
| 253 |
void hio_setcb_close (hio_t* io, hclose_cb close_cb); |
| 254 |
|
| 255 |
// 获取accept回调 |
| 256 |
haccept_cb hio_getcb_accept(hio_t* io); |
| 257 |
// 获取连接回调 |
| 258 |
hconnect_cb hio_getcb_connect(hio_t* io); |
| 259 |
// 获取读回调 |
| 260 |
hread_cb hio_getcb_read(hio_t* io); |
| 261 |
// 获取写回调 |
| 262 |
hwrite_cb hio_getcb_write(hio_t* io); |
| 263 |
// 获取关闭回调 |
| 264 |
hclose_cb hio_getcb_close(hio_t* io); |
| 265 |
|
| 266 |
// 开启SSL/TLS加密通信 |
| 267 |
int hio_enable_ssl(hio_t* io); |
| 268 |
// 是否SSL/TLS加密通信 |
| 269 |
bool hio_is_ssl(hio_t* io); |
| 270 |
// 设置SSL |
| 271 |
int hio_set_ssl (hio_t* io, hssl_t ssl); |
| 272 |
// 设置SSL_CTX |
| 273 |
int hio_set_ssl_ctx(hio_t* io, hssl_ctx_t ssl_ctx); |
| 274 |
// 新建SSL_CTX |
| 275 |
int hio_new_ssl_ctx(hio_t* io, hssl_ctx_opt_t* opt); |
| 276 |
// 获取SSL |
| 277 |
hssl_t hio_get_ssl(hio_t* io); |
| 278 |
// 获取SSL_CTX |
| 279 |
hssl_ctx_t hio_get_ssl_ctx(hio_t* io); |
| 280 |
// 设置主机名 |
| 281 |
int hio_set_hostname(hio_t* io, const char* hostname); |
| 282 |
// 获取主机名 |
| 283 |
const char* hio_get_hostname(hio_t* io); |
| 284 |
|
| 285 |
// 设置连接超时 |
| 286 |
void hio_set_connect_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CONNECT_TIMEOUT)); |
| 287 |
// 设置关闭超时 (说明:非阻塞写队列非空时,需要等待写完成再关闭) |
| 288 |
void hio_set_close_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CLOSE_TIMEOUT)); |
| 289 |
// 设置读超时 (一段时间没有数据到来便自动关闭连接) |
| 290 |
void hio_set_read_timeout(hio_t* io, int timeout_ms); |
| 291 |
// 设置写超时 (一段时间没有数据发送便自动关闭连接) |
| 292 |
void hio_set_write_timeout(hio_t* io, int timeout_ms); |
| 293 |
// 设置keepalive超时 (一段时间没有数据收发便自动关闭连接) |
| 294 |
void hio_set_keepalive_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_KEEPALIVE_TIMEOUT)); |
| 295 |
|
| 296 |
// 设置心跳 (定时发送心跳包) |
| 297 |
typedef void (*hio_send_heartbeat_fn)(hio_t* io); |
| 298 |
void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn); |
| 299 |
|
| 300 |
// 接收连接 |
| 301 |
// hio_add(io, HV_READ) => accept => haccept_cb |
| 302 |
int hio_accept (hio_t* io); |
| 303 |
|
| 304 |
// 连接 |
| 305 |
// connect => hio_add(io, HV_WRITE) => hconnect_cb |
| 306 |
int hio_connect(hio_t* io); |
| 307 |
|
| 308 |
// 读 |
| 309 |
// hio_add(io, HV_READ) => read => hread_cb |
| 310 |
int hio_read (hio_t* io); |
| 311 |
|
| 312 |
// 开始读 |
| 313 |
#define hio_read_start(io) hio_read(io) |
| 314 |
|
| 315 |
// 停止读 |
| 316 |
#define hio_read_stop(io) hio_del(io, HV_READ) |
| 317 |
|
| 318 |
// 读一次 |
| 319 |
// hio_read_start => hread_cb => hio_read_stop |
| 320 |
int hio_read_once (hio_t* io); |
| 321 |
|
| 322 |
// 读取直到指定长度 |
| 323 |
// hio_read_once => hread_cb(len) |
| 324 |
int hio_read_until_length(hio_t* io, unsigned int len); |
| 325 |
|
| 326 |
// 读取直到遇到分隔符 |
| 327 |
// hio_read_once => hread_cb(...delim) |
| 328 |
int hio_read_until_delim (hio_t* io, unsigned char delim); |
| 329 |
|
| 330 |
// 读取一行 |
| 331 |
#define hio_readline(io) hio_read_until_delim(io, '\n') |
| 332 |
|
| 333 |
// 读取字符串 |
| 334 |
#define hio_readstring(io) hio_read_until_delim(io, '\0') |
| 335 |
|
| 336 |
// 读取N个字节 |
| 337 |
#define hio_readbytes(io, len) hio_read_until_length(io, len) |
| 338 |
#define hio_read_until(io, len) hio_read_until_length(io, len) |
| 339 |
|
| 340 |
// 写 |
| 341 |
// hio_try_write => hio_add(io, HV_WRITE) => write => hwrite_cb |
| 342 |
int hio_write (hio_t* io, const void* buf, size_t len); |
| 343 |
|
| 344 |
// 关闭 |
| 345 |
// hio_del(io, HV_RDWR) => close => hclose_cb |
| 346 |
int hio_close (hio_t* io); |
| 347 |
|
| 348 |
// 异步关闭 (投递一个close事件) |
| 349 |
// NOTE: hloop_post_event(hio_close_event) |
| 350 |
int hio_close_async(hio_t* io); |
| 351 |
|
| 352 |
//------------------高等级的接口------------------------------------------- |
| 353 |
// 读 |
| 354 |
// hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read |
| 355 |
hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb); |
| 356 |
// 写 |
| 357 |
// hio_get -> hio_setcb_write -> hio_write |
| 358 |
hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL)); |
| 359 |
// 关闭 |
| 360 |
// hio_get -> hio_close |
| 361 |
void hclose (hloop_t* loop, int fd); |
| 362 |
|
| 363 |
// tcp |
| 364 |
// 接收连接 |
| 365 |
// hio_get -> hio_setcb_accept -> hio_accept |
| 366 |
hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb); |
| 367 |
// 连接 |
| 368 |
// hio_get -> hio_setcb_connect -> hio_connect |
| 369 |
hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb); |
| 370 |
// 接收 |
| 371 |
// hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read |
| 372 |
hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb); |
| 373 |
// 发送 |
| 374 |
// hio_get -> hio_setcb_write -> hio_write |
| 375 |
hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL)); |
| 376 |
|
| 377 |
// udp |
| 378 |
// 设置IO类型 |
| 379 |
void hio_set_type(hio_t* io, hio_type_e type); |
| 380 |
// 设置本地地址 |
| 381 |
void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen); |
| 382 |
// 设置对端地址 |
| 383 |
void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen); |
| 384 |
// 接收 |
| 385 |
// hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read |
| 386 |
hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb); |
| 387 |
// 发送 |
| 388 |
// hio_get -> hio_setcb_write -> hio_write |
| 389 |
hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL)); |
| 390 |
|
| 391 |
//-----------------顶层的接口--------------------------------------------- |
| 392 |
// 创建socket套接字,返回IO对象 |
| 393 |
// @hio_create_socket: socket -> bind -> listen |
| 394 |
// sockaddr_set_ipport -> socket -> hio_get(loop, sockfd) -> |
| 395 |
// side == HIO_SERVER_SIDE ? bind -> |
| 396 |
// type & HIO_TYPE_SOCK_STREAM ? listen -> |
| 397 |
hio_t* hio_create_socket(hloop_t* loop, const char* host, int port, |
| 398 |
hio_type_e type DEFAULT(HIO_TYPE_TCP), |
| 399 |
hio_side_e side DEFAULT(HIO_SERVER_SIDE)); |
| 400 |
|
| 401 |
// @tcp_server: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept |
| 402 |
// 创建TCP服务,示例代码见 examples/tcp_echo_server.c |
| 403 |
hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb); |
| 404 |
|
| 405 |
// @tcp_client: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_setcb_close -> hio_connect |
| 406 |
// 创建TCP客户端,示例代码见 examples/nc.c |
| 407 |
hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb, hclose_cb close_cb); |
| 408 |
|
| 409 |
// @ssl_server: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept |
| 410 |
// 创建SSL服务端,示例代码见 examples/tcp_echo_server.c => #define TEST_SSL 1 |
| 411 |
hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb); |
| 412 |
|
| 413 |
// @ssl_client: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_setcb_close -> hio_connect |
| 414 |
// 创建SSL客户端,示例代码见 examples/nc.c => #define TEST_SSL 1 |
| 415 |
hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb, hclose_cb close_cb); |
| 416 |
|
| 417 |
// @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_SERVER_SIDE) |
| 418 |
// 创建UDP服务端,示例代码见 examples/udp_echo_server.c |
| 419 |
hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port); |
| 420 |
|
| 421 |
// @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE) |
| 422 |
// 创建UDP客户端,示例代码见 examples/nc.c |
| 423 |
hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port); |
| 424 |
|
| 425 |
//-----------------pipe--------------------------------------------- |
| 426 |
// 创建pipe,示例代码见 examples/pipe_test.c |
| 427 |
int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]); |
| 428 |
|
| 429 |
//-----------------转发--------------------------------------------- |
| 430 |
// hio_read(io) |
| 431 |
// hio_read(io->upstream_io) |
| 432 |
void hio_read_upstream(hio_t* io); |
| 433 |
// on_write(io) -> hio_write_is_complete(io) -> hio_read(io->upstream_io) |
| 434 |
void hio_read_upstream_on_write_complete(hio_t* io, const void* buf, int writebytes); |
| 435 |
// hio_write(io->upstream_io, buf, bytes) |
| 436 |
void hio_write_upstream(hio_t* io, void* buf, int bytes); |
| 437 |
// hio_close(io->upstream_io) |
| 438 |
void hio_close_upstream(hio_t* io); |
| 439 |
|
| 440 |
// io1->upstream_io = io2; |
| 441 |
// io2->upstream_io = io1; |
| 442 |
// 建立转发,示例代码见 examples/socks5_proxy_server.c |
| 443 |
void hio_setup_upstream(hio_t* io1, hio_t* io2); |
| 444 |
|
| 445 |
// @return io->upstream_io |
| 446 |
hio_t* hio_get_upstream(hio_t* io); |
| 447 |
|
| 448 |
// @tcp_upstream: hio_create_socket -> hio_setup_upstream -> hio_connect -> on_connect -> hio_read_upstream |
| 449 |
// @return upstream_io |
| 450 |
// 建立TCP转发,示例代码见 examples/tcp_proxy_server.c |
| 451 |
hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl DEFAULT(0)); |
| 452 |
// 建立SSL转发 |
| 453 |
#define hio_setup_ssl_upstream(io, host, port) hio_setup_tcp_upstream(io, host, port, 1) |
| 454 |
|
| 455 |
// @udp_upstream: hio_create_socket -> hio_setup_upstream -> hio_read_upstream |
| 456 |
// @return upstream_io |
| 457 |
// 建立UDP转发,示例代码见 examples/udp_proxy_server.c |
| 458 |
hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port); |
| 459 |
|
| 460 |
//-----------------拆包--------------------------------------------- |
| 461 |
// 拆包模式 |
| 462 |
typedef enum { |
| 463 |
UNPACK_MODE_NONE = 0, |
| 464 |
UNPACK_BY_FIXED_LENGTH = 1, // 固定长度拆包,不建议 |
| 465 |
UNPACK_BY_DELIMITER = 2, // 根据分隔符拆包,适用于文本协议 |
| 466 |
UNPACK_BY_LENGTH_FIELD = 3, // 根据头部长度字段拆包,适用于二进制协议 |
| 467 |
} unpack_mode_e; |
| 468 |
|
| 469 |
// 拆包设置 |
| 470 |
typedef struct unpack_setting_s { |
| 471 |
unpack_mode_e mode; // 拆包模式 |
| 472 |
unsigned int package_max_length; // 最大的包长 |
| 473 |
union { |
| 474 |
// UNPACK_BY_FIXED_LENGTH: 固定长度拆包设置 |
| 475 |
struct { |
| 476 |
unsigned int fixed_length; // 固定长度 |
| 477 |
}; |
| 478 |
// UNPACK_BY_DELIMITER: 分隔符拆包设置 |
| 479 |
struct { |
| 480 |
unsigned char delimiter[PACKAGE_MAX_DELIMITER_BYTES]; // 分隔符 |
| 481 |
unsigned short delimiter_bytes; // 分隔符所占字节数 |
| 482 |
}; |
| 483 |
/* |
| 484 |
* UNPACK_BY_LENGTH_FIELD: 头部长度字段拆包设置 |
| 485 |
* |
| 486 |
* 包长 = 头部长度 + 数据长度 + 调整长度 |
| 487 |
* package_len = head_len + body_len + length_adjustment |
| 488 |
* |
| 489 |
* if (length_field_coding == ENCODE_BY_VARINT) head_len = body_offset + varint_bytes - length_field_bytes; |
| 490 |
* else head_len = body_offset; |
| 491 |
* |
| 492 |
* 注意:头部长度字段的值仅代表数据长度,不包括头部本身长度, |
| 493 |
* 如果你的头部长度字段代表总包长,那么应该将length_adjustment设置为负的头部长度 |
| 494 |
* length_field stores body length, exclude head length, |
| 495 |
* if length_field = head_len + body_len, then length_adjustment should be set to -head_len. |
| 496 |
* |
| 497 |
*/ |
| 498 |
struct { |
| 499 |
unsigned short body_offset; // 到数据的偏移,通常等于头部长度 |
| 500 |
unsigned short length_field_offset; // 长度字段偏移 |
| 501 |
unsigned short length_field_bytes; // 长度字段所占字节数 |
| 502 |
short length_adjustment; // 调整长度 |
| 503 |
unpack_coding_e length_field_coding; // 长度字段编码方式 |
| 504 |
}; |
| 505 |
}; |
| 506 |
} unpack_setting_t; |
| 507 |
|
| 508 |
/* |
| 509 |
* 拆包示例代码见 examples/jsonrpc examples/protorpc |
| 510 |
* |
| 511 |
* 注意:多个IO对象的unpack_setting_t可能是一样的,所有hio_t里仅保存了unpack_setting_t的指针, |
| 512 |
* unpack_setting_t的生命周期应该被调用者所保证,不应该使用局部变量。 |
| 513 |
*/ |
| 514 |
|
| 515 |
// 设置拆包 |
| 516 |
void hio_set_unpack(hio_t* io, unpack_setting_t* setting); |
| 517 |
// 取消拆包设置 |
| 518 |
void hio_unset_unpack(hio_t* io); |
| 519 |
|
| 520 |
// 拆包设置示例: |
| 521 |
/* |
| 522 |
|
| 523 |
// FTP协议通过\r\n分割符拆包 |
| 524 |
unpack_setting_t ftp_unpack_setting; |
| 525 |
memset(&ftp_unpack_setting, 0, sizeof(unpack_setting_t)); |
| 526 |
ftp_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH; |
| 527 |
ftp_unpack_setting.mode = UNPACK_BY_DELIMITER; |
| 528 |
ftp_unpack_setting.delimiter[0] = '\r'; |
| 529 |
ftp_unpack_setting.delimiter[1] = '\n'; |
| 530 |
ftp_unpack_setting.delimiter_bytes = 2; |
| 531 |
|
| 532 |
// MQTT协议通过头部长度字段拆包,头部长度字段使用了varint编码 |
| 533 |
unpack_setting_t mqtt_unpack_setting = { |
| 534 |
.mode = UNPACK_BY_LENGTH_FIELD, |
| 535 |
.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH, |
| 536 |
.body_offset = 2, |
| 537 |
.length_field_offset = 1, |
| 538 |
.length_field_bytes = 1, |
| 539 |
.length_field_coding = ENCODE_BY_VARINT, |
| 540 |
}; |
| 541 |
|
| 542 |
*/ |
| 543 |
|
| 544 |
//-----------------重连---------------------------------------- |
| 545 |
// 重连设置 |
| 546 |
typedef struct reconn_setting_s { |
| 547 |
uint32_t min_delay; // ms 重连最小延时 |
| 548 |
uint32_t max_delay; // ms 重连最大延时 |
| 549 |
uint32_t cur_delay; // ms 当前延时 |
| 550 |
/* |
| 551 |
* @delay_policy: 延时策略 |
| 552 |
* 0: fixed 固定延时 |
| 553 |
* min_delay=3s => 3,3,3... |
| 554 |
* 1: linear 线性增长延时 |
| 555 |
* min_delay=3s max_delay=10s => 3,6,9,10,10... |
| 556 |
* other: exponential 指数增长延时 |
| 557 |
* min_delay=3s max_delay=60s delay_policy=2 => 3,6,12,24,48,60,60... |
| 558 |
*/ |
| 559 |
uint32_t delay_policy; // 延时策略 |
| 560 |
uint32_t max_retry_cnt; // 最大重试次数 |
| 561 |
uint32_t cur_retry_cnt; // 当前重试次数 |
| 562 |
} reconn_setting_t; |
| 563 |
|
| 564 |
// 重连设置初始化 |
| 565 |
void reconn_setting_init(reconn_setting_t* reconn); |
| 566 |
|
| 567 |
// 重连设置重置 |
| 568 |
void reconn_setting_reset(reconn_setting_t* reconn); |
| 569 |
|
| 570 |
// 增加重试次数并判断是否未超过最大重试次数 |
| 571 |
bool reconn_setting_can_retry(reconn_setting_t* reconn); |
| 572 |
|
| 573 |
// 计算当前重连延时 |
| 574 |
uint32_t reconn_setting_calc_delay(reconn_setting_t* reconn); |
| 575 |
|
| 576 |
//-----------------负载均衡------------------------------------- |
| 577 |
// 负载均衡策略枚举 |
| 578 |
typedef enum { |
| 579 |
LB_RoundRobin, // 轮询 |
| 580 |
LB_Random, // 随机 |
| 581 |
LB_LeastConnections,// 最少连接数 |
| 582 |
LB_IpHash, // IP hash |
| 583 |
LB_UrlHash, // URL hash |
| 584 |
} load_balance_e; |
| 585 |
|
| 586 |
//-----------------可靠UDP--------------------------------------------- |
| 587 |
// 关闭可靠UDP |
| 588 |
int hio_close_rudp(hio_t* io, struct sockaddr* peeraddr DEFAULT(NULL)); |
| 589 |
|
| 590 |
// KCP设置 |
| 591 |
typedef struct kcp_setting_s { |
| 592 |
// ikcp_create(conv, ...) |
| 593 |
unsigned int conv; |
| 594 |
// ikcp_nodelay(kcp, nodelay, interval, fastresend, nocwnd) |
| 595 |
int nodelay; |
| 596 |
int interval; |
| 597 |
int fastresend; |
| 598 |
int nocwnd; |
| 599 |
// ikcp_wndsize(kcp, sndwnd, rcvwnd) |
| 600 |
int sndwnd; |
| 601 |
int rcvwnd; |
| 602 |
// ikcp_setmtu(kcp, mtu) |
| 603 |
int mtu; |
| 604 |
// ikcp_update |
| 605 |
int update_interval; |
| 606 |
} kcp_setting_t; |
| 607 |
|
| 608 |
// KCP 正常模式 |
| 609 |
HV_INLINE void kcp_setting_init_with_normal_mode(kcp_setting_t* setting); |
| 610 |
|
| 611 |
// KCP fast模式 |
| 612 |
void kcp_setting_init_with_fast_mode(kcp_setting_t* setting); |
| 613 |
|
| 614 |
// KCP fast2模式 |
| 615 |
void kcp_setting_init_with_fast2_mode(kcp_setting_t* setting); |
| 616 |
|
| 617 |
// KCP fast3模式 |
| 618 |
void kcp_setting_init_with_fast3_mode(kcp_setting_t* setting); |
| 619 |
|
| 620 |
// 设置KCP,示例代码见 examples/udp_echo_server.c => #define TEST_KCP 1 |
| 621 |
int hio_set_kcp(hio_t* io, kcp_setting_t* setting DEFAULT(NULL)); |
| 622 |
|
| 623 |
``` |
| 624 |
|
| 625 |
示例代码: |
| 626 |
|
| 627 |
- 事件循环: [examples/hloop_test.c](../../examples/hloop_test.c) |
| 628 |
- 定时器: [examples/htimer_test.c](../../examples/htimer_test.c) |
| 629 |
- TCP回显服务: [examples/tcp_echo_server.c](../../examples/tcp_echo_server.c) |
| 630 |
- TCP聊天服务: [examples/tcp_chat_server.c](../../examples/tcp_chat_server.c) |
| 631 |
- TCP代理服务: [examples/tcp_proxy_server.c](../../examples/tcp_proxy_server.c) |
| 632 |
- TCP客户端: [examples/tcp_client_test.c](../../examples/tcp_client_test.c) |
| 633 |
- UDP回显服务: [examples/udp_echo_server.c](../../examples/udp_echo_server.c) |
| 634 |
- UDP代理服务: [examples/udp_proxy_server.c](../../examples/udp_proxy_server.c) |
| 635 |
- 网络客户端: [examples/nc](../../examples/nc.c) |
| 636 |
- SOCKS5代理服务: [examples/socks5_proxy_server.c](../../examples/socks5_proxy_server.c) |
| 637 |
- HTTP服务: [examples/tinyhttpd.c](../../examples/tinyhttpd.c) |
| 638 |
- HTTP代理服务: [examples/tinyproxyd.c](../../examples/tinyproxyd.c) |
| 639 |
- jsonRPC示例: [examples/jsonrpc](../../examples/jsonrpc) |
| 640 |
- protobufRPC示例: [examples/protorpc](../../examples/protorpc) |
| 641 |
|
| 642 |
多进程/多线程模式示例代码: |
| 643 |
|
| 644 |
- 多accept进程模式: [examples/multi-thread/multi-acceptor-processes.c](../../examples/multi-thread/multi-acceptor-processes.c) |
| 645 |
- 多accept线程模式: [examples/multi-thread/multi-acceptor-threads.c](../../examples/multi-thread/multi-acceptor-threads.c) |
| 646 |
- 一个accept线程+多worker线程: [examples/multi-thread/one-acceptor-multi-workers.c](../../examples/multi-thread/one-acceptor-multi-workers.c) |