hevent.c 26 KB

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