hevent.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #include "hevent.h"
  2. #include "hsocket.h"
  3. #include "hatomic.h"
  4. #include "hlog.h"
  5. #include "unpack.h"
  6. uint64_t hloop_next_event_id() {
  7. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  8. return ++s_id;
  9. }
  10. uint32_t hio_next_id() {
  11. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  12. return ++s_id;
  13. }
  14. static void fill_io_type(hio_t* io) {
  15. int type = 0;
  16. socklen_t optlen = sizeof(int);
  17. int ret = getsockopt(io->fd, SOL_SOCKET, SO_TYPE, (char*)&type, &optlen);
  18. printd("getsockopt SO_TYPE fd=%d ret=%d type=%d errno=%d\n", io->fd, ret, type, socket_errno());
  19. if (ret == 0) {
  20. switch (type) {
  21. case SOCK_STREAM: io->io_type = HIO_TYPE_TCP; break;
  22. case SOCK_DGRAM: io->io_type = HIO_TYPE_UDP; break;
  23. case SOCK_RAW: io->io_type = HIO_TYPE_IP; break;
  24. default: io->io_type = HIO_TYPE_SOCKET; break;
  25. }
  26. }
  27. else if (socket_errno() == ENOTSOCK) {
  28. switch (io->fd) {
  29. case 0: io->io_type = HIO_TYPE_STDIN; break;
  30. case 1: io->io_type = HIO_TYPE_STDOUT; break;
  31. case 2: io->io_type = HIO_TYPE_STDERR; break;
  32. default: io->io_type = HIO_TYPE_FILE; break;
  33. }
  34. }
  35. else {
  36. io->io_type = HIO_TYPE_TCP;
  37. }
  38. }
  39. static void hio_socket_init(hio_t* io) {
  40. if ((io->io_type & HIO_TYPE_SOCK_DGRAM) || (io->io_type & HIO_TYPE_SOCK_RAW)) {
  41. // NOTE: sendto multiple peeraddr cannot use io->write_queue
  42. blocking(io->fd);
  43. } else {
  44. nonblocking(io->fd);
  45. }
  46. // fill io->localaddr io->peeraddr
  47. if (io->localaddr == NULL) {
  48. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  49. }
  50. if (io->peeraddr == NULL) {
  51. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  52. }
  53. socklen_t addrlen = sizeof(sockaddr_u);
  54. int ret = getsockname(io->fd, io->localaddr, &addrlen);
  55. printd("getsockname fd=%d ret=%d errno=%d\n", io->fd, ret, socket_errno());
  56. // NOTE: udp peeraddr set by recvfrom/sendto
  57. if (io->io_type & HIO_TYPE_SOCK_STREAM) {
  58. addrlen = sizeof(sockaddr_u);
  59. ret = getpeername(io->fd, io->peeraddr, &addrlen);
  60. printd("getpeername fd=%d ret=%d errno=%d\n", io->fd, ret, socket_errno());
  61. }
  62. }
  63. void hio_init(hio_t* io) {
  64. // alloc localaddr,peeraddr when hio_socket_init
  65. /*
  66. if (io->localaddr == NULL) {
  67. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  68. }
  69. if (io->peeraddr == NULL) {
  70. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  71. }
  72. */
  73. // write_queue init when hwrite try_write failed
  74. // write_queue_init(&io->write_queue, 4);
  75. hrecursive_mutex_init(&io->write_mutex);
  76. }
  77. void hio_ready(hio_t* io) {
  78. if (io->ready) return;
  79. // flags
  80. io->ready = 1;
  81. io->closed = 0;
  82. io->accept = io->connect = io->connectex = 0;
  83. io->recv = io->send = 0;
  84. io->recvfrom = io->sendto = 0;
  85. io->close = 0;
  86. // public:
  87. io->id = hio_next_id();
  88. io->io_type = HIO_TYPE_UNKNOWN;
  89. io->error = 0;
  90. io->events = io->revents = 0;
  91. io->last_read_hrtime = io->last_write_hrtime = io->loop->cur_hrtime;
  92. // readbuf
  93. io->alloced_readbuf = 0;
  94. io->readbuf.base = io->loop->readbuf.base;
  95. io->readbuf.len = io->loop->readbuf.len;
  96. io->readbuf.head = io->readbuf.tail = 0;
  97. io->read_flags = 0;
  98. io->read_until_length = 0;
  99. io->small_readbytes_cnt = 0;
  100. // write_queue
  101. io->write_bufsize = 0;
  102. // callbacks
  103. io->read_cb = NULL;
  104. io->write_cb = NULL;
  105. io->close_cb = NULL;
  106. io->accept_cb = NULL;
  107. io->connect_cb = NULL;
  108. // timers
  109. io->connect_timeout = 0;
  110. io->connect_timer = NULL;
  111. io->close_timeout = 0;
  112. io->close_timer = NULL;
  113. io->read_timeout = 0;
  114. io->read_timer = NULL;
  115. io->write_timeout = 0;
  116. io->write_timer = NULL;
  117. io->keepalive_timeout = 0;
  118. io->keepalive_timer = NULL;
  119. io->heartbeat_interval = 0;
  120. io->heartbeat_fn = NULL;
  121. io->heartbeat_timer = NULL;
  122. // upstream
  123. io->upstream_io = NULL;
  124. // unpack
  125. io->unpack_setting = NULL;
  126. // ssl
  127. io->ssl = NULL;
  128. io->ssl_ctx = NULL;
  129. io->alloced_ssl_ctx = 0;
  130. // context
  131. io->ctx = NULL;
  132. // private:
  133. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  134. io->event_index[0] = io->event_index[1] = -1;
  135. #endif
  136. #ifdef EVENT_IOCP
  137. io->hovlp = NULL;
  138. #endif
  139. // io_type
  140. fill_io_type(io);
  141. if (io->io_type & HIO_TYPE_SOCKET) {
  142. hio_socket_init(io);
  143. }
  144. #if WITH_RUDP
  145. if ((io->io_type & HIO_TYPE_SOCK_DGRAM) || (io->io_type & HIO_TYPE_SOCK_RAW)) {
  146. rudp_init(&io->rudp);
  147. }
  148. #endif
  149. }
  150. void hio_done(hio_t* io) {
  151. if (!io->ready) return;
  152. io->ready = 0;
  153. hio_del(io, HV_RDWR);
  154. // readbuf
  155. hio_free_readbuf(io);
  156. // write_queue
  157. offset_buf_t* pbuf = NULL;
  158. hrecursive_mutex_lock(&io->write_mutex);
  159. while (!write_queue_empty(&io->write_queue)) {
  160. pbuf = write_queue_front(&io->write_queue);
  161. HV_FREE(pbuf->base);
  162. write_queue_pop_front(&io->write_queue);
  163. }
  164. write_queue_cleanup(&io->write_queue);
  165. hrecursive_mutex_unlock(&io->write_mutex);
  166. #if WITH_RUDP
  167. if ((io->io_type & HIO_TYPE_SOCK_DGRAM) || (io->io_type & HIO_TYPE_SOCK_RAW)) {
  168. rudp_cleanup(&io->rudp);
  169. }
  170. #endif
  171. }
  172. void hio_free(hio_t* io) {
  173. if (io == NULL) return;
  174. hio_close(io);
  175. hrecursive_mutex_destroy(&io->write_mutex);
  176. HV_FREE(io->localaddr);
  177. HV_FREE(io->peeraddr);
  178. HV_FREE(io);
  179. }
  180. bool hio_is_opened(hio_t* io) {
  181. if (io == NULL) return false;
  182. return io->ready == 1 && io->closed == 0;
  183. }
  184. bool hio_is_closed(hio_t* io) {
  185. if (io == NULL) return true;
  186. return io->ready == 0 && io->closed == 1;
  187. }
  188. uint32_t hio_id (hio_t* io) {
  189. return io->id;
  190. }
  191. int hio_fd(hio_t* io) {
  192. return io->fd;
  193. }
  194. hio_type_e hio_type(hio_t* io) {
  195. return io->io_type;
  196. }
  197. int hio_error(hio_t* io) {
  198. return io->error;
  199. }
  200. int hio_events(hio_t* io) {
  201. return io->events;
  202. }
  203. int hio_revents(hio_t* io) {
  204. return io->revents;
  205. }
  206. struct sockaddr* hio_localaddr(hio_t* io) {
  207. return io->localaddr;
  208. }
  209. struct sockaddr* hio_peeraddr(hio_t* io) {
  210. return io->peeraddr;
  211. }
  212. void hio_set_context(hio_t* io, void* ctx) {
  213. io->ctx = ctx;
  214. }
  215. void* hio_context(hio_t* io) {
  216. return io->ctx;
  217. }
  218. hio_readbuf_t* hio_get_readbuf(hio_t* io) {
  219. return &io->readbuf;
  220. }
  221. size_t hio_write_bufsize(hio_t* io) {
  222. return io->write_bufsize;
  223. }
  224. haccept_cb hio_getcb_accept(hio_t* io) {
  225. return io->accept_cb;
  226. }
  227. hconnect_cb hio_getcb_connect(hio_t* io) {
  228. return io->connect_cb;
  229. }
  230. hread_cb hio_getcb_read(hio_t* io) {
  231. return io->read_cb;
  232. }
  233. hwrite_cb hio_getcb_write(hio_t* io) {
  234. return io->write_cb;
  235. }
  236. hclose_cb hio_getcb_close(hio_t* io) {
  237. return io->close_cb;
  238. }
  239. void hio_setcb_accept(hio_t* io, haccept_cb accept_cb) {
  240. io->accept_cb = accept_cb;
  241. }
  242. void hio_setcb_connect(hio_t* io, hconnect_cb connect_cb) {
  243. io->connect_cb = connect_cb;
  244. }
  245. void hio_setcb_read(hio_t* io, hread_cb read_cb) {
  246. io->read_cb = read_cb;
  247. }
  248. void hio_setcb_write(hio_t* io, hwrite_cb write_cb) {
  249. io->write_cb = write_cb;
  250. }
  251. void hio_setcb_close(hio_t* io, hclose_cb close_cb) {
  252. io->close_cb = close_cb;
  253. }
  254. void hio_accept_cb(hio_t* io) {
  255. /*
  256. char localaddrstr[SOCKADDR_STRLEN] = {0};
  257. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  258. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  259. SOCKADDR_STR(io->localaddr, localaddrstr),
  260. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  261. */
  262. if (io->accept_cb) {
  263. // printd("accept_cb------\n");
  264. io->accept_cb(io);
  265. // printd("accept_cb======\n");
  266. }
  267. }
  268. void hio_connect_cb(hio_t* io) {
  269. /*
  270. char localaddrstr[SOCKADDR_STRLEN] = {0};
  271. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  272. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  273. SOCKADDR_STR(io->localaddr, localaddrstr),
  274. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  275. */
  276. if (io->connect_cb) {
  277. // printd("connect_cb------\n");
  278. io->connect_cb(io);
  279. // printd("connect_cb======\n");
  280. }
  281. }
  282. void hio_handle_read(hio_t* io, void* buf, int readbytes) {
  283. #if WITH_KCP
  284. if (io->io_type == HIO_TYPE_KCP) {
  285. hio_read_kcp(io, buf, readbytes);
  286. return;
  287. }
  288. #endif
  289. if (io->unpack_setting) {
  290. // hio_set_unpack
  291. hio_unpack(io, buf, readbytes);
  292. } else {
  293. const unsigned char* sp = (const unsigned char*)io->readbuf.base + io->readbuf.head;
  294. const unsigned char* ep = (const unsigned char*)buf + readbytes;
  295. if (io->read_flags & HIO_READ_UNTIL_LENGTH) {
  296. // hio_read_until_length
  297. if (ep - sp >= io->read_until_length) {
  298. io->readbuf.head += io->read_until_length;
  299. if (io->readbuf.head == io->readbuf.tail) {
  300. io->readbuf.head = io->readbuf.tail = 0;
  301. }
  302. io->read_flags &= ~HIO_READ_UNTIL_LENGTH;
  303. hio_read_cb(io, (void*)sp, io->read_until_length);
  304. }
  305. } else if (io->read_flags & HIO_READ_UNTIL_DELIM) {
  306. // hio_read_until_delim
  307. const unsigned char* p = (const unsigned char*)buf;
  308. for (int i = 0; i < readbytes; ++i, ++p) {
  309. if (*p == io->read_until_delim) {
  310. int len = p - sp + 1;
  311. io->readbuf.head += len;
  312. if (io->readbuf.head == io->readbuf.tail) {
  313. io->readbuf.head = io->readbuf.tail = 0;
  314. }
  315. io->read_flags &= ~HIO_READ_UNTIL_DELIM;
  316. hio_read_cb(io, (void*)sp, len);
  317. return;
  318. }
  319. }
  320. } else {
  321. // hio_read
  322. io->readbuf.head = io->readbuf.tail = 0;
  323. hio_read_cb(io, (void*)sp, ep - sp);
  324. }
  325. }
  326. if (io->readbuf.head == io->readbuf.tail) {
  327. io->readbuf.head = io->readbuf.tail = 0;
  328. }
  329. // readbuf autosize
  330. if (io->readbuf.tail == io->readbuf.len) {
  331. if (io->readbuf.head == 0) {
  332. // scale up * 2
  333. hio_alloc_readbuf(io, io->readbuf.len * 2);
  334. } else {
  335. // [head, tail] => [base, tail - head]
  336. memmove(io->readbuf.base, io->readbuf.base + io->readbuf.head, io->readbuf.tail - io->readbuf.head);
  337. }
  338. } else {
  339. size_t small_size = io->readbuf.len / 2;
  340. if (io->readbuf.tail < small_size &&
  341. io->small_readbytes_cnt >= 3) {
  342. // scale down / 2
  343. hio_alloc_readbuf(io, small_size);
  344. }
  345. }
  346. }
  347. void hio_read_cb(hio_t* io, void* buf, int len) {
  348. if (io->read_flags & HIO_READ_ONCE) {
  349. io->read_flags &= ~HIO_READ_ONCE;
  350. hio_read_stop(io);
  351. }
  352. if (io->read_cb) {
  353. // printd("read_cb------\n");
  354. io->read_cb(io, buf, len);
  355. // printd("read_cb======\n");
  356. }
  357. // for readbuf autosize
  358. if (hio_is_alloced_readbuf(io) && io->readbuf.len > READ_BUFSIZE_HIGH_WATER) {
  359. size_t small_size = io->readbuf.len / 2;
  360. if (len < small_size) {
  361. ++io->small_readbytes_cnt;
  362. } else {
  363. io->small_readbytes_cnt = 0;
  364. }
  365. }
  366. }
  367. void hio_write_cb(hio_t* io, const void* buf, int len) {
  368. if (io->write_cb) {
  369. // printd("write_cb------\n");
  370. io->write_cb(io, buf, len);
  371. // printd("write_cb======\n");
  372. }
  373. }
  374. void hio_close_cb(hio_t* io) {
  375. if (io->close_cb) {
  376. // printd("close_cb------\n");
  377. io->close_cb(io);
  378. // printd("close_cb======\n");
  379. }
  380. }
  381. void hio_set_type(hio_t* io, hio_type_e type) {
  382. io->io_type = type;
  383. }
  384. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  385. if (io->localaddr == NULL) {
  386. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  387. }
  388. memcpy(io->localaddr, addr, addrlen);
  389. }
  390. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  391. if (io->peeraddr == NULL) {
  392. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  393. }
  394. memcpy(io->peeraddr, addr, addrlen);
  395. }
  396. int hio_enable_ssl(hio_t* io) {
  397. io->io_type = HIO_TYPE_SSL;
  398. return 0;
  399. }
  400. bool hio_is_ssl(hio_t* io) {
  401. return io->io_type == HIO_TYPE_SSL;
  402. }
  403. hssl_t hio_get_ssl(hio_t* io) {
  404. return io->ssl;
  405. }
  406. hssl_ctx_t hio_get_ssl_ctx(hio_t* io) {
  407. return io->ssl_ctx;
  408. }
  409. int hio_set_ssl(hio_t* io, hssl_t ssl) {
  410. io->io_type = HIO_TYPE_SSL;
  411. io->ssl = ssl;
  412. return 0;
  413. }
  414. int hio_set_ssl_ctx(hio_t* io, hssl_ctx_t ssl_ctx) {
  415. io->io_type = HIO_TYPE_SSL;
  416. io->ssl_ctx = ssl_ctx;
  417. return 0;
  418. }
  419. int hio_new_ssl_ctx(hio_t* io, hssl_ctx_opt_t* opt) {
  420. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  421. if (ssl_ctx == NULL) return HSSL_ERROR;
  422. io->alloced_ssl_ctx = 1;
  423. return hio_set_ssl_ctx(io, ssl_ctx);
  424. }
  425. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  426. assert(io && buf && len != 0);
  427. hio_free_readbuf(io);
  428. io->readbuf.base = (char*)buf;
  429. io->readbuf.len = len;
  430. io->readbuf.head = io->readbuf.tail = 0;
  431. io->alloced_readbuf = 0;
  432. }
  433. void hio_del_connect_timer(hio_t* io) {
  434. if (io->connect_timer) {
  435. htimer_del(io->connect_timer);
  436. io->connect_timer = NULL;
  437. io->connect_timeout = 0;
  438. }
  439. }
  440. void hio_del_close_timer(hio_t* io) {
  441. if (io->close_timer) {
  442. htimer_del(io->close_timer);
  443. io->close_timer = NULL;
  444. io->close_timeout = 0;
  445. }
  446. }
  447. void hio_del_read_timer(hio_t* io) {
  448. if (io->read_timer) {
  449. htimer_del(io->read_timer);
  450. io->read_timer = NULL;
  451. io->read_timeout = 0;
  452. }
  453. }
  454. void hio_del_write_timer(hio_t* io) {
  455. if (io->write_timer) {
  456. htimer_del(io->write_timer);
  457. io->write_timer = NULL;
  458. io->write_timeout = 0;
  459. }
  460. }
  461. void hio_del_keepalive_timer(hio_t* io) {
  462. if (io->keepalive_timer) {
  463. htimer_del(io->keepalive_timer);
  464. io->keepalive_timer = NULL;
  465. io->keepalive_timeout = 0;
  466. }
  467. }
  468. void hio_del_heartbeat_timer(hio_t* io) {
  469. if (io->heartbeat_timer) {
  470. htimer_del(io->heartbeat_timer);
  471. io->heartbeat_timer = NULL;
  472. io->heartbeat_interval = 0;
  473. io->heartbeat_fn = NULL;
  474. }
  475. }
  476. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  477. io->connect_timeout = timeout_ms;
  478. }
  479. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  480. io->close_timeout = timeout_ms;
  481. }
  482. static void __read_timeout_cb(htimer_t* timer) {
  483. hio_t* io = (hio_t*)timer->privdata;
  484. uint64_t inactive_ms = (io->loop->cur_hrtime - io->last_read_hrtime) / 1000;
  485. if (inactive_ms + 100 < io->read_timeout) {
  486. ((struct htimeout_s*)io->read_timer)->timeout = io->read_timeout - inactive_ms;
  487. htimer_reset(io->read_timer);
  488. } else {
  489. char localaddrstr[SOCKADDR_STRLEN] = {0};
  490. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  491. hlogw("read timeout [%s] <=> [%s]",
  492. SOCKADDR_STR(io->localaddr, localaddrstr),
  493. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  494. io->error = ETIMEDOUT;
  495. hio_close(io);
  496. }
  497. }
  498. void hio_set_read_timeout(hio_t* io, int timeout_ms) {
  499. if (timeout_ms <= 0) {
  500. // del
  501. hio_del_read_timer(io);
  502. return;
  503. }
  504. if (io->read_timer) {
  505. // reset
  506. ((struct htimeout_s*)io->read_timer)->timeout = timeout_ms;
  507. htimer_reset(io->read_timer);
  508. } else {
  509. // add
  510. io->read_timer = htimer_add(io->loop, __read_timeout_cb, timeout_ms, 1);
  511. io->read_timer->privdata = io;
  512. }
  513. io->read_timeout = timeout_ms;
  514. }
  515. static void __write_timeout_cb(htimer_t* timer) {
  516. hio_t* io = (hio_t*)timer->privdata;
  517. uint64_t inactive_ms = (io->loop->cur_hrtime - io->last_write_hrtime) / 1000;
  518. if (inactive_ms + 100 < io->write_timeout) {
  519. ((struct htimeout_s*)io->write_timer)->timeout = io->write_timeout - inactive_ms;
  520. htimer_reset(io->write_timer);
  521. } else {
  522. char localaddrstr[SOCKADDR_STRLEN] = {0};
  523. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  524. hlogw("write timeout [%s] <=> [%s]",
  525. SOCKADDR_STR(io->localaddr, localaddrstr),
  526. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  527. io->error = ETIMEDOUT;
  528. hio_close(io);
  529. }
  530. }
  531. void hio_set_write_timeout(hio_t* io, int timeout_ms) {
  532. if (timeout_ms <= 0) {
  533. // del
  534. hio_del_write_timer(io);
  535. return;
  536. }
  537. if (io->write_timer) {
  538. // reset
  539. ((struct htimeout_s*)io->write_timer)->timeout = timeout_ms;
  540. htimer_reset(io->write_timer);
  541. } else {
  542. // add
  543. io->write_timer = htimer_add(io->loop, __write_timeout_cb, timeout_ms, 1);
  544. io->write_timer->privdata = io;
  545. }
  546. io->write_timeout = timeout_ms;
  547. }
  548. static void __keepalive_timeout_cb(htimer_t* timer) {
  549. hio_t* io = (hio_t*)timer->privdata;
  550. uint64_t last_rw_hrtime = MAX(io->last_read_hrtime, io->last_write_hrtime);
  551. uint64_t inactive_ms = (io->loop->cur_hrtime - last_rw_hrtime) / 1000;
  552. if (inactive_ms + 100 < io->keepalive_timeout) {
  553. ((struct htimeout_s*)io->keepalive_timer)->timeout = io->keepalive_timeout - inactive_ms;
  554. htimer_reset(io->keepalive_timer);
  555. } else {
  556. char localaddrstr[SOCKADDR_STRLEN] = {0};
  557. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  558. hlogw("keepalive timeout [%s] <=> [%s]",
  559. SOCKADDR_STR(io->localaddr, localaddrstr),
  560. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  561. io->error = ETIMEDOUT;
  562. hio_close(io);
  563. }
  564. }
  565. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  566. if (timeout_ms <= 0) {
  567. // del
  568. hio_del_keepalive_timer(io);
  569. return;
  570. }
  571. if (io->keepalive_timer) {
  572. // reset
  573. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  574. htimer_reset(io->keepalive_timer);
  575. } else {
  576. // add
  577. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  578. io->keepalive_timer->privdata = io;
  579. }
  580. io->keepalive_timeout = timeout_ms;
  581. }
  582. static void __heartbeat_timer_cb(htimer_t* timer) {
  583. hio_t* io = (hio_t*)timer->privdata;
  584. if (io && io->heartbeat_fn) {
  585. io->heartbeat_fn(io);
  586. }
  587. }
  588. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  589. if (interval_ms <= 0) {
  590. // del
  591. hio_del_heartbeat_timer(io);
  592. return;
  593. }
  594. if (io->heartbeat_timer) {
  595. // reset
  596. ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
  597. htimer_reset(io->heartbeat_timer);
  598. } else {
  599. // add
  600. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  601. io->heartbeat_timer->privdata = io;
  602. }
  603. io->heartbeat_interval = interval_ms;
  604. io->heartbeat_fn = fn;
  605. }
  606. void hio_alloc_readbuf(hio_t* io, int len) {
  607. if (len > MAX_READ_BUFSIZE) {
  608. hloge("read bufsize > %u, close it!", (unsigned int)MAX_READ_BUFSIZE);
  609. hio_close_async(io);
  610. return;
  611. }
  612. if (hio_is_alloced_readbuf(io)) {
  613. io->readbuf.base = (char*)safe_realloc(io->readbuf.base, len, io->readbuf.len);
  614. } else {
  615. HV_ALLOC(io->readbuf.base, len);
  616. }
  617. io->readbuf.len = len;
  618. io->alloced_readbuf = 1;
  619. io->small_readbytes_cnt = 0;
  620. }
  621. void hio_free_readbuf(hio_t* io) {
  622. if (hio_is_alloced_readbuf(io)) {
  623. HV_FREE(io->readbuf.base);
  624. io->alloced_readbuf = 0;
  625. // reset to loop->readbuf
  626. io->readbuf.base = io->loop->readbuf.base;
  627. io->readbuf.len = io->loop->readbuf.len;
  628. }
  629. }
  630. int hio_read_once (hio_t* io) {
  631. io->read_flags |= HIO_READ_ONCE;
  632. return hio_read_start(io);
  633. }
  634. int hio_read_until_length(hio_t* io, unsigned int len) {
  635. if (len == 0) return 0;
  636. if (io->readbuf.tail - io->readbuf.head >= len) {
  637. void* buf = io->readbuf.base + io->readbuf.head;
  638. io->readbuf.head += len;
  639. if (io->readbuf.head == io->readbuf.tail) {
  640. io->readbuf.head = io->readbuf.tail = 0;
  641. }
  642. hio_read_cb(io, buf, len);
  643. return len;
  644. }
  645. io->read_flags = HIO_READ_UNTIL_LENGTH;
  646. io->read_until_length = len;
  647. // NOTE: prepare readbuf
  648. if (hio_is_loop_readbuf(io) ||
  649. io->readbuf.len < len) {
  650. hio_alloc_readbuf(io, len);
  651. }
  652. return hio_read_once(io);
  653. }
  654. int hio_read_until_delim(hio_t* io, unsigned char delim) {
  655. if (io->readbuf.tail - io->readbuf.head > 0) {
  656. const unsigned char* sp = (const unsigned char*)io->readbuf.base + io->readbuf.head;
  657. const unsigned char* ep = (const unsigned char*)io->readbuf.base + io->readbuf.tail;
  658. const unsigned char* p = sp;
  659. while (p <= ep) {
  660. if (*p == delim) {
  661. int len = p - sp + 1;
  662. io->readbuf.head += len;
  663. if (io->readbuf.head == io->readbuf.tail) {
  664. io->readbuf.head = io->readbuf.tail = 0;
  665. }
  666. hio_read_cb(io, (void*)sp, len);
  667. return len;
  668. }
  669. ++p;
  670. }
  671. }
  672. io->read_flags = HIO_READ_UNTIL_DELIM;
  673. io->read_until_length = delim;
  674. // NOTE: prepare readbuf
  675. if (hio_is_loop_readbuf(io) ||
  676. io->readbuf.len < HLOOP_READ_BUFSIZE) {
  677. hio_alloc_readbuf(io, HLOOP_READ_BUFSIZE);
  678. }
  679. return hio_read_once(io);
  680. }
  681. int hio_read_remain(hio_t* io) {
  682. int remain = io->readbuf.tail - io->readbuf.head;
  683. if (remain > 0) {
  684. void* buf = io->readbuf.base + io->readbuf.head;
  685. io->readbuf.head = io->readbuf.tail = 0;
  686. hio_read_cb(io, buf, remain);
  687. }
  688. return remain;
  689. }
  690. //-----------------unpack---------------------------------------------
  691. void hio_set_unpack(hio_t* io, unpack_setting_t* setting) {
  692. hio_unset_unpack(io);
  693. if (setting == NULL) return;
  694. io->unpack_setting = setting;
  695. if (io->unpack_setting->package_max_length == 0) {
  696. io->unpack_setting->package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  697. }
  698. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  699. assert(io->unpack_setting->fixed_length != 0 &&
  700. io->unpack_setting->fixed_length <= io->unpack_setting->package_max_length);
  701. }
  702. else if (io->unpack_setting->mode == UNPACK_BY_DELIMITER) {
  703. if (io->unpack_setting->delimiter_bytes == 0) {
  704. io->unpack_setting->delimiter_bytes = strlen((char*)io->unpack_setting->delimiter);
  705. }
  706. }
  707. else if (io->unpack_setting->mode == UNPACK_BY_LENGTH_FIELD) {
  708. assert(io->unpack_setting->body_offset >=
  709. io->unpack_setting->length_field_offset +
  710. io->unpack_setting->length_field_bytes);
  711. }
  712. // NOTE: unpack must have own readbuf
  713. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  714. io->readbuf.len = io->unpack_setting->fixed_length;
  715. } else {
  716. io->readbuf.len = HLOOP_READ_BUFSIZE;
  717. }
  718. hio_alloc_readbuf(io, io->readbuf.len);
  719. }
  720. void hio_unset_unpack(hio_t* io) {
  721. if (io->unpack_setting) {
  722. io->unpack_setting = NULL;
  723. // NOTE: unpack has own readbuf
  724. hio_free_readbuf(io);
  725. }
  726. }
  727. //-----------------upstream---------------------------------------------
  728. void hio_read_upstream(hio_t* io) {
  729. hio_t* upstream_io = io->upstream_io;
  730. if (upstream_io) {
  731. hio_read(io);
  732. hio_read(upstream_io);
  733. }
  734. }
  735. void hio_write_upstream(hio_t* io, void* buf, int bytes) {
  736. hio_t* upstream_io = io->upstream_io;
  737. if (upstream_io) {
  738. hio_write(upstream_io, buf, bytes);
  739. }
  740. }
  741. void hio_close_upstream(hio_t* io) {
  742. hio_t* upstream_io = io->upstream_io;
  743. if (upstream_io) {
  744. hio_close(upstream_io);
  745. }
  746. }
  747. void hio_setup_upstream(hio_t* io1, hio_t* io2) {
  748. io1->upstream_io = io2;
  749. io2->upstream_io = io1;
  750. }
  751. hio_t* hio_get_upstream(hio_t* io) {
  752. return io->upstream_io;
  753. }
  754. hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
  755. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  756. if (upstream_io == NULL) return NULL;
  757. if (ssl) hio_enable_ssl(upstream_io);
  758. hio_setup_upstream(io, upstream_io);
  759. hio_setcb_read(io, hio_write_upstream);
  760. hio_setcb_read(upstream_io, hio_write_upstream);
  761. hio_setcb_close(io, hio_close_upstream);
  762. hio_setcb_close(upstream_io, hio_close_upstream);
  763. hio_setcb_connect(upstream_io, hio_read_upstream);
  764. hio_connect(upstream_io);
  765. return upstream_io;
  766. }
  767. hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
  768. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
  769. if (upstream_io == NULL) return NULL;
  770. hio_setup_upstream(io, upstream_io);
  771. hio_setcb_read(io, hio_write_upstream);
  772. hio_setcb_read(upstream_io, hio_write_upstream);
  773. hio_read_upstream(io);
  774. return upstream_io;
  775. }