hevent.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #include "hevent.h"
  2. #include "hsocket.h"
  3. #include "hatomic.h"
  4. #include "hlog.h"
  5. uint64_t hloop_next_event_id() {
  6. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  7. return ++s_id;
  8. }
  9. uint32_t hio_next_id() {
  10. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  11. return ++s_id;
  12. }
  13. static void fill_io_type(hio_t* io) {
  14. int type = 0;
  15. socklen_t optlen = sizeof(int);
  16. int ret = getsockopt(io->fd, SOL_SOCKET, SO_TYPE, (char*)&type, &optlen);
  17. printd("getsockopt SO_TYPE fd=%d ret=%d type=%d errno=%d\n", io->fd, ret, type, socket_errno());
  18. if (ret == 0) {
  19. switch (type) {
  20. case SOCK_STREAM: io->io_type = HIO_TYPE_TCP; break;
  21. case SOCK_DGRAM: io->io_type = HIO_TYPE_UDP; break;
  22. case SOCK_RAW: io->io_type = HIO_TYPE_IP; break;
  23. default: io->io_type = HIO_TYPE_SOCKET; break;
  24. }
  25. }
  26. else if (socket_errno() == ENOTSOCK) {
  27. switch (io->fd) {
  28. case 0: io->io_type = HIO_TYPE_STDIN; break;
  29. case 1: io->io_type = HIO_TYPE_STDOUT; break;
  30. case 2: io->io_type = HIO_TYPE_STDERR; break;
  31. default: io->io_type = HIO_TYPE_FILE; break;
  32. }
  33. }
  34. else {
  35. io->io_type = HIO_TYPE_TCP;
  36. }
  37. }
  38. static void hio_socket_init(hio_t* io) {
  39. // nonblocking
  40. nonblocking(io->fd);
  41. // fill io->localaddr io->peeraddr
  42. if (io->localaddr == NULL) {
  43. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  44. }
  45. if (io->peeraddr == NULL) {
  46. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  47. }
  48. socklen_t addrlen = sizeof(sockaddr_u);
  49. int ret = getsockname(io->fd, io->localaddr, &addrlen);
  50. printd("getsockname fd=%d ret=%d errno=%d\n", io->fd, ret, socket_errno());
  51. // NOTE:
  52. // tcp_server peeraddr set by accept
  53. // udp_server peeraddr set by recvfrom
  54. // tcp_client/udp_client peeraddr set by hio_setpeeraddr
  55. if (io->io_type & HIO_TYPE_SOCK_STREAM) {
  56. // tcp acceptfd
  57. addrlen = sizeof(sockaddr_u);
  58. ret = getpeername(io->fd, io->peeraddr, &addrlen);
  59. printd("getpeername fd=%d ret=%d errno=%d\n", io->fd, ret, socket_errno());
  60. }
  61. }
  62. void hio_init(hio_t* io) {
  63. // alloc localaddr,peeraddr when hio_socket_init
  64. /*
  65. if (io->localaddr == NULL) {
  66. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  67. }
  68. if (io->peeraddr == NULL) {
  69. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  70. }
  71. */
  72. // write_queue init when hwrite try_write failed
  73. // write_queue_init(&io->write_queue, 4);
  74. hrecursive_mutex_init(&io->write_mutex);
  75. }
  76. void hio_ready(hio_t* io) {
  77. if (io->ready) return;
  78. // flags
  79. io->ready = 1;
  80. io->closed = 0;
  81. io->accept = io->connect = io->connectex = 0;
  82. io->recv = io->send = 0;
  83. io->recvfrom = io->sendto = 0;
  84. io->close = 0;
  85. // public:
  86. io->id = hio_next_id();
  87. io->io_type = HIO_TYPE_UNKNOWN;
  88. io->error = 0;
  89. io->events = io->revents = 0;
  90. // readbuf
  91. io->alloced_readbuf = 0;
  92. io->readbuf.base = io->loop->readbuf.base;
  93. io->readbuf.len = io->loop->readbuf.len;
  94. io->readbuf.offset = 0;
  95. io->read_once = 0;
  96. io->read_until = 0;
  97. io->small_readbytes_cnt = 0;
  98. // write_queue
  99. io->write_queue_bytes = 0;
  100. // callbacks
  101. io->read_cb = NULL;
  102. io->write_cb = NULL;
  103. io->close_cb = NULL;
  104. io->accept_cb = NULL;
  105. io->connect_cb = NULL;
  106. // timers
  107. io->connect_timeout = 0;
  108. io->connect_timer = NULL;
  109. io->close_timeout = 0;
  110. io->close_timer = NULL;
  111. io->keepalive_timeout = 0;
  112. io->keepalive_timer = NULL;
  113. io->heartbeat_interval = 0;
  114. io->heartbeat_fn = NULL;
  115. io->heartbeat_timer = NULL;
  116. // upstream
  117. io->upstream_io = NULL;
  118. // unpack
  119. io->unpack_setting = NULL;
  120. // ssl
  121. io->ssl = NULL;
  122. // context
  123. io->ctx = NULL;
  124. // private:
  125. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  126. io->event_index[0] = io->event_index[1] = -1;
  127. #endif
  128. #ifdef EVENT_IOCP
  129. io->hovlp = NULL;
  130. #endif
  131. // io_type
  132. fill_io_type(io);
  133. if (io->io_type & HIO_TYPE_SOCKET) {
  134. hio_socket_init(io);
  135. }
  136. }
  137. void hio_done(hio_t* io) {
  138. if (!io->ready) return;
  139. io->ready = 0;
  140. hio_del(io, HV_RDWR);
  141. // readbuf
  142. hio_free_readbuf(io);
  143. // write_queue
  144. offset_buf_t* pbuf = NULL;
  145. hrecursive_mutex_lock(&io->write_mutex);
  146. while (!write_queue_empty(&io->write_queue)) {
  147. pbuf = write_queue_front(&io->write_queue);
  148. HV_FREE(pbuf->base);
  149. write_queue_pop_front(&io->write_queue);
  150. }
  151. write_queue_cleanup(&io->write_queue);
  152. hrecursive_mutex_unlock(&io->write_mutex);
  153. }
  154. void hio_free(hio_t* io) {
  155. if (io == NULL) return;
  156. hio_close(io);
  157. hrecursive_mutex_destroy(&io->write_mutex);
  158. HV_FREE(io->localaddr);
  159. HV_FREE(io->peeraddr);
  160. HV_FREE(io);
  161. }
  162. bool hio_is_opened(hio_t* io) {
  163. if (io == NULL) return false;
  164. return io->ready == 1 && io->closed == 0;
  165. }
  166. bool hio_is_closed(hio_t* io) {
  167. if (io == NULL) return true;
  168. return io->ready == 0 && io->closed == 1;
  169. }
  170. uint32_t hio_id (hio_t* io) {
  171. return io->id;
  172. }
  173. int hio_fd(hio_t* io) {
  174. return io->fd;
  175. }
  176. hio_type_e hio_type(hio_t* io) {
  177. return io->io_type;
  178. }
  179. int hio_error(hio_t* io) {
  180. return io->error;
  181. }
  182. int hio_events(hio_t* io) {
  183. return io->events;
  184. }
  185. int hio_revents(hio_t* io) {
  186. return io->revents;
  187. }
  188. struct sockaddr* hio_localaddr(hio_t* io) {
  189. return io->localaddr;
  190. }
  191. struct sockaddr* hio_peeraddr(hio_t* io) {
  192. return io->peeraddr;
  193. }
  194. void hio_set_context(hio_t* io, void* ctx) {
  195. io->ctx = ctx;
  196. }
  197. void* hio_context(hio_t* io) {
  198. return io->ctx;
  199. }
  200. size_t hio_read_bufsize(hio_t* io) {
  201. return io->readbuf.len;
  202. }
  203. size_t hio_write_bufsize(hio_t* io) {
  204. return io->write_queue_bytes;
  205. }
  206. haccept_cb hio_getcb_accept(hio_t* io) {
  207. return io->accept_cb;
  208. }
  209. hconnect_cb hio_getcb_connect(hio_t* io) {
  210. return io->connect_cb;
  211. }
  212. hread_cb hio_getcb_read(hio_t* io) {
  213. return io->read_cb;
  214. }
  215. hwrite_cb hio_getcb_write(hio_t* io) {
  216. return io->write_cb;
  217. }
  218. hclose_cb hio_getcb_close(hio_t* io) {
  219. return io->close_cb;
  220. }
  221. void hio_setcb_accept(hio_t* io, haccept_cb accept_cb) {
  222. io->accept_cb = accept_cb;
  223. }
  224. void hio_setcb_connect(hio_t* io, hconnect_cb connect_cb) {
  225. io->connect_cb = connect_cb;
  226. }
  227. void hio_setcb_read(hio_t* io, hread_cb read_cb) {
  228. io->read_cb = read_cb;
  229. }
  230. void hio_setcb_write(hio_t* io, hwrite_cb write_cb) {
  231. io->write_cb = write_cb;
  232. }
  233. void hio_setcb_close(hio_t* io, hclose_cb close_cb) {
  234. io->close_cb = close_cb;
  235. }
  236. void hio_accept_cb(hio_t* io) {
  237. /*
  238. char localaddrstr[SOCKADDR_STRLEN] = {0};
  239. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  240. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  241. SOCKADDR_STR(io->localaddr, localaddrstr),
  242. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  243. */
  244. if (io->accept_cb) {
  245. // printd("accept_cb------\n");
  246. io->accept_cb(io);
  247. // printd("accept_cb======\n");
  248. }
  249. }
  250. void hio_connect_cb(hio_t* io) {
  251. /*
  252. char localaddrstr[SOCKADDR_STRLEN] = {0};
  253. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  254. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  255. SOCKADDR_STR(io->localaddr, localaddrstr),
  256. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  257. */
  258. if (io->connect_cb) {
  259. // printd("connect_cb------\n");
  260. io->connect_cb(io);
  261. // printd("connect_cb======\n");
  262. }
  263. }
  264. void hio_read_cb(hio_t* io, void* buf, int len) {
  265. if (io->read_cb) {
  266. // printd("read_cb------\n");
  267. io->read_cb(io, buf, len);
  268. // printd("read_cb======\n");
  269. }
  270. // for readbuf autosize
  271. if (hio_is_alloced_readbuf(io) && io->readbuf.len > READ_BUFSIZE_HIGH_WATER) {
  272. size_t small_size = io->readbuf.len / 2;
  273. if (len < small_size) {
  274. ++io->small_readbytes_cnt;
  275. } else {
  276. io->small_readbytes_cnt = 0;
  277. }
  278. }
  279. }
  280. void hio_write_cb(hio_t* io, const void* buf, int len) {
  281. if (io->write_cb) {
  282. // printd("write_cb------\n");
  283. io->write_cb(io, buf, len);
  284. // printd("write_cb======\n");
  285. }
  286. }
  287. void hio_close_cb(hio_t* io) {
  288. if (io->close_cb) {
  289. // printd("close_cb------\n");
  290. io->close_cb(io);
  291. // printd("close_cb======\n");
  292. }
  293. }
  294. void hio_set_type(hio_t* io, hio_type_e type) {
  295. io->io_type = type;
  296. }
  297. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  298. if (io->localaddr == NULL) {
  299. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  300. }
  301. memcpy(io->localaddr, addr, addrlen);
  302. }
  303. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  304. if (io->peeraddr == NULL) {
  305. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  306. }
  307. memcpy(io->peeraddr, addr, addrlen);
  308. }
  309. int hio_enable_ssl(hio_t* io) {
  310. io->io_type = HIO_TYPE_SSL;
  311. return 0;
  312. }
  313. bool hio_is_ssl(hio_t* io) {
  314. return io->io_type == HIO_TYPE_SSL;
  315. }
  316. hssl_t hio_get_ssl(hio_t* io) {
  317. return io->ssl;
  318. }
  319. int hio_set_ssl(hio_t* io, hssl_t ssl) {
  320. io->io_type = HIO_TYPE_SSL;
  321. io->ssl = ssl;
  322. return 0;
  323. }
  324. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  325. assert(io && buf && len != 0);
  326. hio_free_readbuf(io);
  327. io->readbuf.base = (char*)buf;
  328. io->readbuf.len = len;
  329. io->readbuf.offset = 0;
  330. io->alloced_readbuf = 0;
  331. }
  332. void hio_del_connect_timer(hio_t* io) {
  333. if (io->connect_timer) {
  334. htimer_del(io->connect_timer);
  335. io->connect_timer = NULL;
  336. io->connect_timeout = 0;
  337. }
  338. }
  339. void hio_del_close_timer(hio_t* io) {
  340. if (io->close_timer) {
  341. htimer_del(io->close_timer);
  342. io->close_timer = NULL;
  343. io->close_timeout = 0;
  344. }
  345. }
  346. void hio_del_keepalive_timer(hio_t* io) {
  347. if (io->keepalive_timer) {
  348. htimer_del(io->keepalive_timer);
  349. io->keepalive_timer = NULL;
  350. io->keepalive_timeout = 0;
  351. }
  352. }
  353. void hio_del_heartbeat_timer(hio_t* io) {
  354. if (io->heartbeat_timer) {
  355. htimer_del(io->heartbeat_timer);
  356. io->heartbeat_timer = NULL;
  357. io->heartbeat_interval = 0;
  358. io->heartbeat_fn = NULL;
  359. }
  360. }
  361. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  362. io->connect_timeout = timeout_ms;
  363. }
  364. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  365. io->close_timeout = timeout_ms;
  366. }
  367. static void __keepalive_timeout_cb(htimer_t* timer) {
  368. hio_t* io = (hio_t*)timer->privdata;
  369. if (io) {
  370. char localaddrstr[SOCKADDR_STRLEN] = {0};
  371. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  372. hlogw("keepalive timeout [%s] <=> [%s]",
  373. SOCKADDR_STR(io->localaddr, localaddrstr),
  374. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  375. io->error = ETIMEDOUT;
  376. hio_close(io);
  377. }
  378. }
  379. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  380. if (timeout_ms == 0) {
  381. // del
  382. hio_del_keepalive_timer(io);
  383. return;
  384. }
  385. if (io->keepalive_timer) {
  386. // reset
  387. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  388. htimer_reset(io->keepalive_timer);
  389. } else {
  390. // add
  391. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  392. io->keepalive_timer->privdata = io;
  393. }
  394. io->keepalive_timeout = timeout_ms;
  395. }
  396. static void __heartbeat_timer_cb(htimer_t* timer) {
  397. hio_t* io = (hio_t*)timer->privdata;
  398. if (io && io->heartbeat_fn) {
  399. io->heartbeat_fn(io);
  400. }
  401. }
  402. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  403. if (interval_ms == 0) {
  404. // del
  405. hio_del_heartbeat_timer(io);
  406. return;
  407. }
  408. if (io->heartbeat_timer) {
  409. // reset
  410. ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
  411. htimer_reset(io->heartbeat_timer);
  412. } else {
  413. // add
  414. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  415. io->heartbeat_timer->privdata = io;
  416. }
  417. io->heartbeat_interval = interval_ms;
  418. io->heartbeat_fn = fn;
  419. }
  420. void hio_alloc_readbuf(hio_t* io, int len) {
  421. if (hio_is_alloced_readbuf(io)) {
  422. io->readbuf.base = (char*)safe_realloc(io->readbuf.base, len, io->readbuf.len);
  423. } else {
  424. HV_ALLOC(io->readbuf.base, len);
  425. }
  426. io->readbuf.len = len;
  427. io->alloced_readbuf = 1;
  428. }
  429. void hio_free_readbuf(hio_t* io) {
  430. if (hio_is_alloced_readbuf(io)) {
  431. HV_FREE(io->readbuf.base);
  432. io->alloced_readbuf = 0;
  433. // reset to loop->readbuf
  434. io->readbuf.base = io->loop->readbuf.base;
  435. io->readbuf.len = io->loop->readbuf.len;
  436. }
  437. }
  438. int hio_read_once (hio_t* io) {
  439. io->read_once = 1;
  440. return hio_read_start(io);
  441. }
  442. int hio_read_until(hio_t* io, int len) {
  443. io->read_until = len;
  444. // NOTE: prepare readbuf
  445. if (hio_is_loop_readbuf(io) ||
  446. io->readbuf.len < len) {
  447. hio_alloc_readbuf(io, len);
  448. }
  449. return hio_read_once(io);
  450. }
  451. //-----------------unpack---------------------------------------------
  452. void hio_set_unpack(hio_t* io, unpack_setting_t* setting) {
  453. hio_unset_unpack(io);
  454. if (setting == NULL) return;
  455. io->unpack_setting = setting;
  456. if (io->unpack_setting->package_max_length == 0) {
  457. io->unpack_setting->package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  458. }
  459. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  460. assert(io->unpack_setting->fixed_length != 0 &&
  461. io->unpack_setting->fixed_length <= io->unpack_setting->package_max_length);
  462. }
  463. else if (io->unpack_setting->mode == UNPACK_BY_DELIMITER) {
  464. if (io->unpack_setting->delimiter_bytes == 0) {
  465. io->unpack_setting->delimiter_bytes = strlen((char*)io->unpack_setting->delimiter);
  466. }
  467. }
  468. else if (io->unpack_setting->mode == UNPACK_BY_LENGTH_FIELD) {
  469. assert(io->unpack_setting->body_offset >=
  470. io->unpack_setting->length_field_offset +
  471. io->unpack_setting->length_field_bytes);
  472. }
  473. // NOTE: unpack must have own readbuf
  474. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  475. io->readbuf.len = io->unpack_setting->fixed_length;
  476. } else {
  477. io->readbuf.len = HLOOP_READ_BUFSIZE;
  478. }
  479. hio_alloc_readbuf(io, io->readbuf.len);
  480. }
  481. void hio_unset_unpack(hio_t* io) {
  482. if (io->unpack_setting) {
  483. io->unpack_setting = NULL;
  484. // NOTE: unpack has own readbuf
  485. hio_free_readbuf(io);
  486. }
  487. }
  488. //-----------------upstream---------------------------------------------
  489. void hio_read_upstream(hio_t* io) {
  490. hio_t* upstream_io = io->upstream_io;
  491. if (upstream_io) {
  492. hio_read(io);
  493. hio_read(upstream_io);
  494. }
  495. }
  496. void hio_write_upstream(hio_t* io, void* buf, int bytes) {
  497. hio_t* upstream_io = io->upstream_io;
  498. if (upstream_io) {
  499. hio_write(upstream_io, buf, bytes);
  500. }
  501. }
  502. void hio_close_upstream(hio_t* io) {
  503. hio_t* upstream_io = io->upstream_io;
  504. if (upstream_io) {
  505. hio_close(upstream_io);
  506. }
  507. }
  508. void hio_setup_upstream(hio_t* io1, hio_t* io2) {
  509. io1->upstream_io = io2;
  510. io2->upstream_io = io1;
  511. hio_setcb_read(io1, hio_write_upstream);
  512. hio_setcb_read(io2, hio_write_upstream);
  513. }
  514. hio_t* hio_get_upstream(hio_t* io) {
  515. return io->upstream_io;
  516. }
  517. hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
  518. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  519. if (upstream_io == NULL) return NULL;
  520. if (ssl) hio_enable_ssl(upstream_io);
  521. hio_setup_upstream(io, upstream_io);
  522. hio_setcb_close(io, hio_close_upstream);
  523. hio_setcb_close(upstream_io, hio_close_upstream);
  524. hconnect(io->loop, upstream_io->fd, hio_read_upstream);
  525. return upstream_io;
  526. }
  527. hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
  528. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
  529. if (upstream_io == NULL) return NULL;
  530. hio_setup_upstream(io, upstream_io);
  531. hio_read_upstream(io);
  532. return upstream_io;
  533. }