hevent.c 24 KB

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