hevent.c 26 KB

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