1
0

hevent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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_queue_bytes = 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. // context
  129. io->ctx = NULL;
  130. // private:
  131. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  132. io->event_index[0] = io->event_index[1] = -1;
  133. #endif
  134. #ifdef EVENT_IOCP
  135. io->hovlp = NULL;
  136. #endif
  137. // io_type
  138. fill_io_type(io);
  139. if (io->io_type & HIO_TYPE_SOCKET) {
  140. hio_socket_init(io);
  141. }
  142. #if WITH_RUDP
  143. if ((io->io_type & HIO_TYPE_SOCK_DGRAM) || (io->io_type & HIO_TYPE_SOCK_RAW)) {
  144. rudp_init(&io->rudp);
  145. }
  146. #endif
  147. }
  148. void hio_done(hio_t* io) {
  149. if (!io->ready) return;
  150. io->ready = 0;
  151. hio_del(io, HV_RDWR);
  152. // readbuf
  153. hio_free_readbuf(io);
  154. // write_queue
  155. offset_buf_t* pbuf = NULL;
  156. hrecursive_mutex_lock(&io->write_mutex);
  157. while (!write_queue_empty(&io->write_queue)) {
  158. pbuf = write_queue_front(&io->write_queue);
  159. HV_FREE(pbuf->base);
  160. write_queue_pop_front(&io->write_queue);
  161. }
  162. write_queue_cleanup(&io->write_queue);
  163. hrecursive_mutex_unlock(&io->write_mutex);
  164. #if WITH_RUDP
  165. if ((io->io_type & HIO_TYPE_SOCK_DGRAM) || (io->io_type & HIO_TYPE_SOCK_RAW)) {
  166. rudp_cleanup(&io->rudp);
  167. }
  168. #endif
  169. }
  170. void hio_free(hio_t* io) {
  171. if (io == NULL) return;
  172. hio_close(io);
  173. hrecursive_mutex_destroy(&io->write_mutex);
  174. HV_FREE(io->localaddr);
  175. HV_FREE(io->peeraddr);
  176. HV_FREE(io);
  177. }
  178. bool hio_is_opened(hio_t* io) {
  179. if (io == NULL) return false;
  180. return io->ready == 1 && io->closed == 0;
  181. }
  182. bool hio_is_closed(hio_t* io) {
  183. if (io == NULL) return true;
  184. return io->ready == 0 && io->closed == 1;
  185. }
  186. uint32_t hio_id (hio_t* io) {
  187. return io->id;
  188. }
  189. int hio_fd(hio_t* io) {
  190. return io->fd;
  191. }
  192. hio_type_e hio_type(hio_t* io) {
  193. return io->io_type;
  194. }
  195. int hio_error(hio_t* io) {
  196. return io->error;
  197. }
  198. int hio_events(hio_t* io) {
  199. return io->events;
  200. }
  201. int hio_revents(hio_t* io) {
  202. return io->revents;
  203. }
  204. struct sockaddr* hio_localaddr(hio_t* io) {
  205. return io->localaddr;
  206. }
  207. struct sockaddr* hio_peeraddr(hio_t* io) {
  208. return io->peeraddr;
  209. }
  210. void hio_set_context(hio_t* io, void* ctx) {
  211. io->ctx = ctx;
  212. }
  213. void* hio_context(hio_t* io) {
  214. return io->ctx;
  215. }
  216. size_t hio_read_bufsize(hio_t* io) {
  217. return io->readbuf.len;
  218. }
  219. size_t hio_write_bufsize(hio_t* io) {
  220. return io->write_queue_bytes;
  221. }
  222. haccept_cb hio_getcb_accept(hio_t* io) {
  223. return io->accept_cb;
  224. }
  225. hconnect_cb hio_getcb_connect(hio_t* io) {
  226. return io->connect_cb;
  227. }
  228. hread_cb hio_getcb_read(hio_t* io) {
  229. return io->read_cb;
  230. }
  231. hwrite_cb hio_getcb_write(hio_t* io) {
  232. return io->write_cb;
  233. }
  234. hclose_cb hio_getcb_close(hio_t* io) {
  235. return io->close_cb;
  236. }
  237. void hio_setcb_accept(hio_t* io, haccept_cb accept_cb) {
  238. io->accept_cb = accept_cb;
  239. }
  240. void hio_setcb_connect(hio_t* io, hconnect_cb connect_cb) {
  241. io->connect_cb = connect_cb;
  242. }
  243. void hio_setcb_read(hio_t* io, hread_cb read_cb) {
  244. io->read_cb = read_cb;
  245. }
  246. void hio_setcb_write(hio_t* io, hwrite_cb write_cb) {
  247. io->write_cb = write_cb;
  248. }
  249. void hio_setcb_close(hio_t* io, hclose_cb close_cb) {
  250. io->close_cb = close_cb;
  251. }
  252. void hio_accept_cb(hio_t* io) {
  253. /*
  254. char localaddrstr[SOCKADDR_STRLEN] = {0};
  255. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  256. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  257. SOCKADDR_STR(io->localaddr, localaddrstr),
  258. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  259. */
  260. if (io->accept_cb) {
  261. // printd("accept_cb------\n");
  262. io->accept_cb(io);
  263. // printd("accept_cb======\n");
  264. }
  265. }
  266. void hio_connect_cb(hio_t* io) {
  267. /*
  268. char localaddrstr[SOCKADDR_STRLEN] = {0};
  269. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  270. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  271. SOCKADDR_STR(io->localaddr, localaddrstr),
  272. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  273. */
  274. if (io->connect_cb) {
  275. // printd("connect_cb------\n");
  276. io->connect_cb(io);
  277. // printd("connect_cb======\n");
  278. }
  279. }
  280. void hio_handle_read(hio_t* io, void* buf, int readbytes) {
  281. #if WITH_KCP
  282. if (io->io_type == HIO_TYPE_KCP) {
  283. hio_read_kcp(io, buf, readbytes);
  284. return;
  285. }
  286. #endif
  287. if (io->unpack_setting) {
  288. // hio_set_unpack
  289. hio_unpack(io, buf, readbytes);
  290. } else {
  291. const unsigned char* sp = (const unsigned char*)io->readbuf.base + io->readbuf.head;
  292. const unsigned char* ep = (const unsigned char*)buf + readbytes;
  293. if (io->read_flags & HIO_READ_UNTIL_LENGTH) {
  294. // hio_read_until_length
  295. if (ep - sp >= io->read_until_length) {
  296. io->readbuf.head += io->read_until_length;
  297. if (io->readbuf.head == io->readbuf.tail) {
  298. io->readbuf.head = io->readbuf.tail = 0;
  299. }
  300. io->readbuf.head = io->readbuf.tail = 0;
  301. io->read_flags &= ~HIO_READ_UNTIL_LENGTH;
  302. hio_read_cb(io, (void*)sp, io->read_until_length);
  303. }
  304. } else if (io->read_flags & HIO_READ_UNTIL_DELIM) {
  305. // hio_read_until_delim
  306. const unsigned char* p = (const unsigned char*)buf;
  307. for (int i = 0; i < readbytes; ++i, ++p) {
  308. if (*p == io->read_until_delim) {
  309. int len = p - sp + 1;
  310. io->readbuf.head += len;
  311. if (io->readbuf.head == io->readbuf.tail) {
  312. io->readbuf.head = io->readbuf.tail = 0;
  313. }
  314. io->read_flags &= ~HIO_READ_UNTIL_DELIM;
  315. hio_read_cb(io, (void*)sp, len);
  316. return;
  317. }
  318. }
  319. } else {
  320. // hio_read
  321. io->readbuf.head = io->readbuf.tail = 0;
  322. hio_read_cb(io, (void*)sp, ep - sp);
  323. }
  324. }
  325. if (io->readbuf.head == io->readbuf.tail) {
  326. io->readbuf.head = io->readbuf.tail = 0;
  327. }
  328. // readbuf autosize
  329. if (io->readbuf.tail == io->readbuf.len) {
  330. if (io->readbuf.head == 0) {
  331. // scale up * 2
  332. hio_alloc_readbuf(io, io->readbuf.len * 2);
  333. } else {
  334. // [head, tail] => [base, tail - head]
  335. memmove(io->readbuf.base, io->readbuf.base + io->readbuf.head, io->readbuf.tail - io->readbuf.head);
  336. }
  337. } else {
  338. size_t small_size = io->readbuf.len / 2;
  339. if (io->readbuf.tail < small_size &&
  340. io->small_readbytes_cnt >= 3) {
  341. // scale down / 2
  342. hio_alloc_readbuf(io, small_size);
  343. }
  344. }
  345. }
  346. void hio_read_cb(hio_t* io, void* buf, int len) {
  347. if (io->read_flags & HIO_READ_ONCE) {
  348. io->read_flags &= ~HIO_READ_ONCE;
  349. hio_read_stop(io);
  350. }
  351. if (io->read_cb) {
  352. // printd("read_cb------\n");
  353. io->read_cb(io, buf, len);
  354. // printd("read_cb======\n");
  355. }
  356. // for readbuf autosize
  357. if (hio_is_alloced_readbuf(io) && io->readbuf.len > READ_BUFSIZE_HIGH_WATER) {
  358. size_t small_size = io->readbuf.len / 2;
  359. if (len < small_size) {
  360. ++io->small_readbytes_cnt;
  361. } else {
  362. io->small_readbytes_cnt = 0;
  363. }
  364. }
  365. }
  366. void hio_write_cb(hio_t* io, const void* buf, int len) {
  367. if (io->write_cb) {
  368. // printd("write_cb------\n");
  369. io->write_cb(io, buf, len);
  370. // printd("write_cb======\n");
  371. }
  372. }
  373. void hio_close_cb(hio_t* io) {
  374. if (io->close_cb) {
  375. // printd("close_cb------\n");
  376. io->close_cb(io);
  377. // printd("close_cb======\n");
  378. }
  379. }
  380. void hio_set_type(hio_t* io, hio_type_e type) {
  381. io->io_type = type;
  382. }
  383. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  384. if (io->localaddr == NULL) {
  385. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  386. }
  387. memcpy(io->localaddr, addr, addrlen);
  388. }
  389. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  390. if (io->peeraddr == NULL) {
  391. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  392. }
  393. memcpy(io->peeraddr, addr, addrlen);
  394. }
  395. int hio_enable_ssl(hio_t* io) {
  396. io->io_type = HIO_TYPE_SSL;
  397. return 0;
  398. }
  399. bool hio_is_ssl(hio_t* io) {
  400. return io->io_type == HIO_TYPE_SSL;
  401. }
  402. hssl_t hio_get_ssl(hio_t* io) {
  403. return io->ssl;
  404. }
  405. int hio_set_ssl(hio_t* io, hssl_t ssl) {
  406. io->io_type = HIO_TYPE_SSL;
  407. io->ssl = ssl;
  408. return 0;
  409. }
  410. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  411. assert(io && buf && len != 0);
  412. hio_free_readbuf(io);
  413. io->readbuf.base = (char*)buf;
  414. io->readbuf.len = len;
  415. io->readbuf.head = io->readbuf.tail = 0;
  416. io->alloced_readbuf = 0;
  417. }
  418. void hio_del_connect_timer(hio_t* io) {
  419. if (io->connect_timer) {
  420. htimer_del(io->connect_timer);
  421. io->connect_timer = NULL;
  422. io->connect_timeout = 0;
  423. }
  424. }
  425. void hio_del_close_timer(hio_t* io) {
  426. if (io->close_timer) {
  427. htimer_del(io->close_timer);
  428. io->close_timer = NULL;
  429. io->close_timeout = 0;
  430. }
  431. }
  432. void hio_del_read_timer(hio_t* io) {
  433. if (io->read_timer) {
  434. htimer_del(io->read_timer);
  435. io->read_timer = NULL;
  436. io->read_timeout = 0;
  437. }
  438. }
  439. void hio_del_write_timer(hio_t* io) {
  440. if (io->write_timer) {
  441. htimer_del(io->write_timer);
  442. io->write_timer = NULL;
  443. io->write_timeout = 0;
  444. }
  445. }
  446. void hio_del_keepalive_timer(hio_t* io) {
  447. if (io->keepalive_timer) {
  448. htimer_del(io->keepalive_timer);
  449. io->keepalive_timer = NULL;
  450. io->keepalive_timeout = 0;
  451. }
  452. }
  453. void hio_del_heartbeat_timer(hio_t* io) {
  454. if (io->heartbeat_timer) {
  455. htimer_del(io->heartbeat_timer);
  456. io->heartbeat_timer = NULL;
  457. io->heartbeat_interval = 0;
  458. io->heartbeat_fn = NULL;
  459. }
  460. }
  461. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  462. io->connect_timeout = timeout_ms;
  463. }
  464. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  465. io->close_timeout = timeout_ms;
  466. }
  467. static void __read_timeout_cb(htimer_t* timer) {
  468. hio_t* io = (hio_t*)timer->privdata;
  469. uint64_t inactive_ms = (io->loop->cur_hrtime - io->last_read_hrtime) / 1000;
  470. if (inactive_ms + 100 < io->read_timeout) {
  471. ((struct htimeout_s*)io->read_timer)->timeout = io->read_timeout - inactive_ms;
  472. htimer_reset(io->read_timer);
  473. } else {
  474. char localaddrstr[SOCKADDR_STRLEN] = {0};
  475. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  476. hlogw("read timeout [%s] <=> [%s]",
  477. SOCKADDR_STR(io->localaddr, localaddrstr),
  478. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  479. io->error = ETIMEDOUT;
  480. hio_close(io);
  481. }
  482. }
  483. void hio_set_read_timeout(hio_t* io, int timeout_ms) {
  484. if (timeout_ms <= 0) {
  485. // del
  486. hio_del_read_timer(io);
  487. return;
  488. }
  489. if (io->read_timer) {
  490. // reset
  491. ((struct htimeout_s*)io->read_timer)->timeout = timeout_ms;
  492. htimer_reset(io->read_timer);
  493. } else {
  494. // add
  495. io->read_timer = htimer_add(io->loop, __read_timeout_cb, timeout_ms, 1);
  496. io->read_timer->privdata = io;
  497. }
  498. io->read_timeout = timeout_ms;
  499. }
  500. static void __write_timeout_cb(htimer_t* timer) {
  501. hio_t* io = (hio_t*)timer->privdata;
  502. uint64_t inactive_ms = (io->loop->cur_hrtime - io->last_write_hrtime) / 1000;
  503. if (inactive_ms + 100 < io->write_timeout) {
  504. ((struct htimeout_s*)io->write_timer)->timeout = io->write_timeout - inactive_ms;
  505. htimer_reset(io->write_timer);
  506. } else {
  507. char localaddrstr[SOCKADDR_STRLEN] = {0};
  508. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  509. hlogw("write timeout [%s] <=> [%s]",
  510. SOCKADDR_STR(io->localaddr, localaddrstr),
  511. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  512. io->error = ETIMEDOUT;
  513. hio_close(io);
  514. }
  515. }
  516. void hio_set_write_timeout(hio_t* io, int timeout_ms) {
  517. if (timeout_ms <= 0) {
  518. // del
  519. hio_del_write_timer(io);
  520. return;
  521. }
  522. if (io->write_timer) {
  523. // reset
  524. ((struct htimeout_s*)io->write_timer)->timeout = timeout_ms;
  525. htimer_reset(io->write_timer);
  526. } else {
  527. // add
  528. io->write_timer = htimer_add(io->loop, __write_timeout_cb, timeout_ms, 1);
  529. io->write_timer->privdata = io;
  530. }
  531. io->write_timeout = timeout_ms;
  532. }
  533. static void __keepalive_timeout_cb(htimer_t* timer) {
  534. hio_t* io = (hio_t*)timer->privdata;
  535. uint64_t last_rw_hrtime = MAX(io->last_read_hrtime, io->last_write_hrtime);
  536. uint64_t inactive_ms = (io->loop->cur_hrtime - last_rw_hrtime) / 1000;
  537. if (inactive_ms + 100 < io->keepalive_timeout) {
  538. ((struct htimeout_s*)io->keepalive_timer)->timeout = io->keepalive_timeout - inactive_ms;
  539. htimer_reset(io->keepalive_timer);
  540. } else {
  541. char localaddrstr[SOCKADDR_STRLEN] = {0};
  542. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  543. hlogw("keepalive timeout [%s] <=> [%s]",
  544. SOCKADDR_STR(io->localaddr, localaddrstr),
  545. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  546. io->error = ETIMEDOUT;
  547. hio_close(io);
  548. }
  549. }
  550. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  551. if (timeout_ms <= 0) {
  552. // del
  553. hio_del_keepalive_timer(io);
  554. return;
  555. }
  556. if (io->keepalive_timer) {
  557. // reset
  558. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  559. htimer_reset(io->keepalive_timer);
  560. } else {
  561. // add
  562. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  563. io->keepalive_timer->privdata = io;
  564. }
  565. io->keepalive_timeout = timeout_ms;
  566. }
  567. static void __heartbeat_timer_cb(htimer_t* timer) {
  568. hio_t* io = (hio_t*)timer->privdata;
  569. if (io && io->heartbeat_fn) {
  570. io->heartbeat_fn(io);
  571. }
  572. }
  573. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  574. if (interval_ms <= 0) {
  575. // del
  576. hio_del_heartbeat_timer(io);
  577. return;
  578. }
  579. if (io->heartbeat_timer) {
  580. // reset
  581. ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
  582. htimer_reset(io->heartbeat_timer);
  583. } else {
  584. // add
  585. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  586. io->heartbeat_timer->privdata = io;
  587. }
  588. io->heartbeat_interval = interval_ms;
  589. io->heartbeat_fn = fn;
  590. }
  591. void hio_alloc_readbuf(hio_t* io, int len) {
  592. if (hio_is_alloced_readbuf(io)) {
  593. io->readbuf.base = (char*)safe_realloc(io->readbuf.base, len, io->readbuf.len);
  594. } else {
  595. HV_ALLOC(io->readbuf.base, len);
  596. }
  597. io->readbuf.len = len;
  598. io->alloced_readbuf = 1;
  599. io->small_readbytes_cnt = 0;
  600. }
  601. void hio_free_readbuf(hio_t* io) {
  602. if (hio_is_alloced_readbuf(io)) {
  603. HV_FREE(io->readbuf.base);
  604. io->alloced_readbuf = 0;
  605. // reset to loop->readbuf
  606. io->readbuf.base = io->loop->readbuf.base;
  607. io->readbuf.len = io->loop->readbuf.len;
  608. }
  609. }
  610. int hio_read_once (hio_t* io) {
  611. io->read_flags |= HIO_READ_ONCE;
  612. return hio_read_start(io);
  613. }
  614. int hio_read_until_length(hio_t* io, unsigned int len) {
  615. if (len == 0) return 0;
  616. if (io->readbuf.tail - io->readbuf.head >= len) {
  617. void* buf = io->readbuf.base + io->readbuf.head;
  618. io->readbuf.head += len;
  619. if (io->readbuf.head == io->readbuf.tail) {
  620. io->readbuf.head = io->readbuf.tail = 0;
  621. }
  622. hio_read_cb(io, buf, len);
  623. return len;
  624. }
  625. io->read_flags = HIO_READ_UNTIL_LENGTH;
  626. io->read_until_length = len;
  627. // NOTE: prepare readbuf
  628. if (hio_is_loop_readbuf(io) ||
  629. io->readbuf.len < len) {
  630. hio_alloc_readbuf(io, len);
  631. }
  632. return hio_read_once(io);
  633. }
  634. int hio_read_until_delim(hio_t* io, unsigned char delim) {
  635. if (io->readbuf.tail - io->readbuf.head > 0) {
  636. const unsigned char* sp = (const unsigned char*)io->readbuf.base + io->readbuf.head;
  637. const unsigned char* ep = (const unsigned char*)io->readbuf.base + io->readbuf.tail;
  638. const unsigned char* p = sp;
  639. while (p <= ep) {
  640. if (*p == delim) {
  641. int len = p - sp + 1;
  642. io->readbuf.head += len;
  643. if (io->readbuf.head == io->readbuf.tail) {
  644. io->readbuf.head = io->readbuf.tail = 0;
  645. }
  646. hio_read_cb(io, (void*)sp, len);
  647. return len;
  648. }
  649. ++p;
  650. }
  651. }
  652. io->read_flags = HIO_READ_UNTIL_DELIM;
  653. io->read_until_length = delim;
  654. // NOTE: prepare readbuf
  655. if (hio_is_loop_readbuf(io) ||
  656. io->readbuf.len < HLOOP_READ_BUFSIZE) {
  657. hio_alloc_readbuf(io, HLOOP_READ_BUFSIZE);
  658. }
  659. return hio_read_once(io);
  660. }
  661. //-----------------unpack---------------------------------------------
  662. void hio_set_unpack(hio_t* io, unpack_setting_t* setting) {
  663. hio_unset_unpack(io);
  664. if (setting == NULL) return;
  665. io->unpack_setting = setting;
  666. if (io->unpack_setting->package_max_length == 0) {
  667. io->unpack_setting->package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  668. }
  669. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  670. assert(io->unpack_setting->fixed_length != 0 &&
  671. io->unpack_setting->fixed_length <= io->unpack_setting->package_max_length);
  672. }
  673. else if (io->unpack_setting->mode == UNPACK_BY_DELIMITER) {
  674. if (io->unpack_setting->delimiter_bytes == 0) {
  675. io->unpack_setting->delimiter_bytes = strlen((char*)io->unpack_setting->delimiter);
  676. }
  677. }
  678. else if (io->unpack_setting->mode == UNPACK_BY_LENGTH_FIELD) {
  679. assert(io->unpack_setting->body_offset >=
  680. io->unpack_setting->length_field_offset +
  681. io->unpack_setting->length_field_bytes);
  682. }
  683. // NOTE: unpack must have own readbuf
  684. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  685. io->readbuf.len = io->unpack_setting->fixed_length;
  686. } else {
  687. io->readbuf.len = HLOOP_READ_BUFSIZE;
  688. }
  689. hio_alloc_readbuf(io, io->readbuf.len);
  690. }
  691. void hio_unset_unpack(hio_t* io) {
  692. if (io->unpack_setting) {
  693. io->unpack_setting = NULL;
  694. // NOTE: unpack has own readbuf
  695. hio_free_readbuf(io);
  696. }
  697. }
  698. //-----------------upstream---------------------------------------------
  699. void hio_read_upstream(hio_t* io) {
  700. hio_t* upstream_io = io->upstream_io;
  701. if (upstream_io) {
  702. hio_read(io);
  703. hio_read(upstream_io);
  704. }
  705. }
  706. void hio_write_upstream(hio_t* io, void* buf, int bytes) {
  707. hio_t* upstream_io = io->upstream_io;
  708. if (upstream_io) {
  709. hio_write(upstream_io, buf, bytes);
  710. }
  711. }
  712. void hio_close_upstream(hio_t* io) {
  713. hio_t* upstream_io = io->upstream_io;
  714. if (upstream_io) {
  715. hio_close(upstream_io);
  716. }
  717. }
  718. void hio_setup_upstream(hio_t* io1, hio_t* io2) {
  719. io1->upstream_io = io2;
  720. io2->upstream_io = io1;
  721. hio_setcb_read(io1, hio_write_upstream);
  722. hio_setcb_read(io2, hio_write_upstream);
  723. }
  724. hio_t* hio_get_upstream(hio_t* io) {
  725. return io->upstream_io;
  726. }
  727. hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
  728. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  729. if (upstream_io == NULL) return NULL;
  730. if (ssl) hio_enable_ssl(upstream_io);
  731. hio_setup_upstream(io, upstream_io);
  732. hio_setcb_close(io, hio_close_upstream);
  733. hio_setcb_close(upstream_io, hio_close_upstream);
  734. hconnect(io->loop, upstream_io->fd, hio_read_upstream);
  735. return upstream_io;
  736. }
  737. hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
  738. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
  739. if (upstream_io == NULL) return NULL;
  740. hio_setup_upstream(io, upstream_io);
  741. hio_read_upstream(io);
  742. return upstream_io;
  743. }