hevent.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. // private:
  123. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  124. io->event_index[0] = io->event_index[1] = -1;
  125. #endif
  126. #ifdef EVENT_IOCP
  127. io->hovlp = NULL;
  128. #endif
  129. // io_type
  130. fill_io_type(io);
  131. if (io->io_type & HIO_TYPE_SOCKET) {
  132. hio_socket_init(io);
  133. }
  134. }
  135. void hio_done(hio_t* io) {
  136. if (!io->ready) return;
  137. io->ready = 0;
  138. hio_del(io, HV_RDWR);
  139. // readbuf
  140. hio_free_readbuf(io);
  141. // write_queue
  142. offset_buf_t* pbuf = NULL;
  143. hrecursive_mutex_lock(&io->write_mutex);
  144. while (!write_queue_empty(&io->write_queue)) {
  145. pbuf = write_queue_front(&io->write_queue);
  146. HV_FREE(pbuf->base);
  147. write_queue_pop_front(&io->write_queue);
  148. }
  149. write_queue_cleanup(&io->write_queue);
  150. hrecursive_mutex_unlock(&io->write_mutex);
  151. }
  152. void hio_free(hio_t* io) {
  153. if (io == NULL) return;
  154. // NOTE: call hio_done to cleanup write_queue
  155. hio_done(io);
  156. // NOTE: call hio_close to call hclose_cb
  157. hio_close(io);
  158. hrecursive_mutex_destroy(&io->write_mutex);
  159. HV_FREE(io->localaddr);
  160. HV_FREE(io->peeraddr);
  161. HV_FREE(io);
  162. }
  163. bool hio_is_opened(hio_t* io) {
  164. if (io == NULL) return false;
  165. return io->ready == 1 && io->closed == 0;
  166. }
  167. bool hio_is_closed(hio_t* io) {
  168. if (io == NULL) return true;
  169. return io->ready == 0 && io->closed == 1;
  170. }
  171. uint32_t hio_id (hio_t* io) {
  172. return io->id;
  173. }
  174. int hio_fd(hio_t* io) {
  175. return io->fd;
  176. }
  177. hio_type_e hio_type(hio_t* io) {
  178. return io->io_type;
  179. }
  180. int hio_error(hio_t* io) {
  181. return io->error;
  182. }
  183. int hio_events(hio_t* io) {
  184. return io->events;
  185. }
  186. int hio_revents(hio_t* io) {
  187. return io->revents;
  188. }
  189. struct sockaddr* hio_localaddr(hio_t* io) {
  190. return io->localaddr;
  191. }
  192. struct sockaddr* hio_peeraddr(hio_t* io) {
  193. return io->peeraddr;
  194. }
  195. void hio_set_context(hio_t* io, void* ctx) {
  196. io->ctx = ctx;
  197. }
  198. void* hio_context(hio_t* io) {
  199. return io->ctx;
  200. }
  201. size_t hio_read_bufsize(hio_t* io) {
  202. return io->readbuf.len;
  203. }
  204. size_t hio_write_bufsize(hio_t* io) {
  205. return io->write_queue_bytes;
  206. }
  207. haccept_cb hio_getcb_accept(hio_t* io) {
  208. return io->accept_cb;
  209. }
  210. hconnect_cb hio_getcb_connect(hio_t* io) {
  211. return io->connect_cb;
  212. }
  213. hread_cb hio_getcb_read(hio_t* io) {
  214. return io->read_cb;
  215. }
  216. hwrite_cb hio_getcb_write(hio_t* io) {
  217. return io->write_cb;
  218. }
  219. hclose_cb hio_getcb_close(hio_t* io) {
  220. return io->close_cb;
  221. }
  222. void hio_setcb_accept(hio_t* io, haccept_cb accept_cb) {
  223. io->accept_cb = accept_cb;
  224. }
  225. void hio_setcb_connect(hio_t* io, hconnect_cb connect_cb) {
  226. io->connect_cb = connect_cb;
  227. }
  228. void hio_setcb_read(hio_t* io, hread_cb read_cb) {
  229. io->read_cb = read_cb;
  230. }
  231. void hio_setcb_write(hio_t* io, hwrite_cb write_cb) {
  232. io->write_cb = write_cb;
  233. }
  234. void hio_setcb_close(hio_t* io, hclose_cb close_cb) {
  235. io->close_cb = close_cb;
  236. }
  237. void hio_accept_cb(hio_t* io) {
  238. /*
  239. char localaddrstr[SOCKADDR_STRLEN] = {0};
  240. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  241. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  242. SOCKADDR_STR(io->localaddr, localaddrstr),
  243. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  244. */
  245. if (io->accept_cb) {
  246. // printd("accept_cb------\n");
  247. io->accept_cb(io);
  248. // printd("accept_cb======\n");
  249. }
  250. }
  251. void hio_connect_cb(hio_t* io) {
  252. /*
  253. char localaddrstr[SOCKADDR_STRLEN] = {0};
  254. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  255. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  256. SOCKADDR_STR(io->localaddr, localaddrstr),
  257. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  258. */
  259. if (io->connect_cb) {
  260. // printd("connect_cb------\n");
  261. io->connect_cb(io);
  262. // printd("connect_cb======\n");
  263. }
  264. }
  265. void hio_read_cb(hio_t* io, void* buf, int len) {
  266. if (io->read_cb) {
  267. // printd("read_cb------\n");
  268. io->read_cb(io, buf, len);
  269. // printd("read_cb======\n");
  270. }
  271. // for readbuf autosize
  272. if (hio_is_alloced_readbuf(io) && io->readbuf.len > READ_BUFSIZE_HIGH_WATER) {
  273. size_t small_size = io->readbuf.len / 2;
  274. if (len < small_size) {
  275. ++io->small_readbytes_cnt;
  276. } else {
  277. io->small_readbytes_cnt = 0;
  278. }
  279. }
  280. }
  281. void hio_write_cb(hio_t* io, const void* buf, int len) {
  282. if (io->write_cb) {
  283. // printd("write_cb------\n");
  284. io->write_cb(io, buf, len);
  285. // printd("write_cb======\n");
  286. }
  287. }
  288. void hio_close_cb(hio_t* io) {
  289. if (io->close_cb) {
  290. // printd("close_cb------\n");
  291. io->close_cb(io);
  292. // printd("close_cb======\n");
  293. }
  294. }
  295. void hio_set_type(hio_t* io, hio_type_e type) {
  296. io->io_type = type;
  297. }
  298. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  299. if (io->localaddr == NULL) {
  300. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  301. }
  302. memcpy(io->localaddr, addr, addrlen);
  303. }
  304. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  305. if (io->peeraddr == NULL) {
  306. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  307. }
  308. memcpy(io->peeraddr, addr, addrlen);
  309. }
  310. int hio_enable_ssl(hio_t* io) {
  311. io->io_type = HIO_TYPE_SSL;
  312. return 0;
  313. }
  314. bool hio_is_ssl(hio_t* io) {
  315. return io->io_type == HIO_TYPE_SSL;
  316. }
  317. hssl_t hio_get_ssl(hio_t* io) {
  318. return io->ssl;
  319. }
  320. int hio_set_ssl(hio_t* io, hssl_t ssl) {
  321. io->io_type = HIO_TYPE_SSL;
  322. io->ssl = ssl;
  323. return 0;
  324. }
  325. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  326. assert(io && buf && len != 0);
  327. hio_free_readbuf(io);
  328. io->readbuf.base = (char*)buf;
  329. io->readbuf.len = len;
  330. io->readbuf.offset = 0;
  331. io->alloced_readbuf = 0;
  332. }
  333. void hio_del_connect_timer(hio_t* io) {
  334. if (io->connect_timer) {
  335. htimer_del(io->connect_timer);
  336. io->connect_timer = NULL;
  337. io->connect_timeout = 0;
  338. }
  339. }
  340. void hio_del_close_timer(hio_t* io) {
  341. if (io->close_timer) {
  342. htimer_del(io->close_timer);
  343. io->close_timer = NULL;
  344. io->close_timeout = 0;
  345. }
  346. }
  347. void hio_del_keepalive_timer(hio_t* io) {
  348. if (io->keepalive_timer) {
  349. htimer_del(io->keepalive_timer);
  350. io->keepalive_timer = NULL;
  351. io->keepalive_timeout = 0;
  352. }
  353. }
  354. void hio_del_heartbeat_timer(hio_t* io) {
  355. if (io->heartbeat_timer) {
  356. htimer_del(io->heartbeat_timer);
  357. io->heartbeat_timer = NULL;
  358. io->heartbeat_interval = 0;
  359. io->heartbeat_fn = NULL;
  360. }
  361. }
  362. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  363. io->connect_timeout = timeout_ms;
  364. }
  365. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  366. io->close_timeout = timeout_ms;
  367. }
  368. static void __keepalive_timeout_cb(htimer_t* timer) {
  369. hio_t* io = (hio_t*)timer->privdata;
  370. if (io) {
  371. char localaddrstr[SOCKADDR_STRLEN] = {0};
  372. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  373. hlogw("keepalive timeout [%s] <=> [%s]",
  374. SOCKADDR_STR(io->localaddr, localaddrstr),
  375. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  376. io->error = ETIMEDOUT;
  377. hio_close(io);
  378. }
  379. }
  380. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  381. if (timeout_ms == 0) {
  382. // del
  383. hio_del_keepalive_timer(io);
  384. return;
  385. }
  386. if (io->keepalive_timer) {
  387. // reset
  388. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  389. htimer_reset(io->keepalive_timer);
  390. } else {
  391. // add
  392. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  393. io->keepalive_timer->privdata = io;
  394. }
  395. io->keepalive_timeout = timeout_ms;
  396. }
  397. static void __heartbeat_timer_cb(htimer_t* timer) {
  398. hio_t* io = (hio_t*)timer->privdata;
  399. if (io && io->heartbeat_fn) {
  400. io->heartbeat_fn(io);
  401. }
  402. }
  403. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  404. if (interval_ms == 0) {
  405. // del
  406. hio_del_heartbeat_timer(io);
  407. return;
  408. }
  409. if (io->heartbeat_timer) {
  410. // reset
  411. ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
  412. htimer_reset(io->heartbeat_timer);
  413. } else {
  414. // add
  415. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  416. io->heartbeat_timer->privdata = io;
  417. }
  418. io->heartbeat_interval = interval_ms;
  419. io->heartbeat_fn = fn;
  420. }
  421. void hio_alloc_readbuf(hio_t* io, int len) {
  422. if (hio_is_alloced_readbuf(io)) {
  423. io->readbuf.base = (char*)safe_realloc(io->readbuf.base, len, io->readbuf.len);
  424. } else {
  425. HV_ALLOC(io->readbuf.base, len);
  426. }
  427. io->readbuf.len = len;
  428. io->alloced_readbuf = 1;
  429. }
  430. void hio_free_readbuf(hio_t* io) {
  431. if (hio_is_alloced_readbuf(io)) {
  432. HV_FREE(io->readbuf.base);
  433. io->alloced_readbuf = 0;
  434. // reset to loop->readbuf
  435. io->readbuf.base = io->loop->readbuf.base;
  436. io->readbuf.len = io->loop->readbuf.len;
  437. }
  438. }
  439. int hio_read_once (hio_t* io) {
  440. io->read_once = 1;
  441. return hio_read_start(io);
  442. }
  443. int hio_read_until(hio_t* io, int len) {
  444. io->read_until = len;
  445. // NOTE: prepare readbuf
  446. if (hio_is_loop_readbuf(io) ||
  447. io->readbuf.len < len) {
  448. hio_alloc_readbuf(io, len);
  449. }
  450. return hio_read_once(io);
  451. }
  452. //-----------------unpack---------------------------------------------
  453. void hio_set_unpack(hio_t* io, unpack_setting_t* setting) {
  454. hio_unset_unpack(io);
  455. if (setting == NULL) return;
  456. io->unpack_setting = setting;
  457. if (io->unpack_setting->package_max_length == 0) {
  458. io->unpack_setting->package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  459. }
  460. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  461. assert(io->unpack_setting->fixed_length != 0 &&
  462. io->unpack_setting->fixed_length <= io->unpack_setting->package_max_length);
  463. }
  464. else if (io->unpack_setting->mode == UNPACK_BY_DELIMITER) {
  465. if (io->unpack_setting->delimiter_bytes == 0) {
  466. io->unpack_setting->delimiter_bytes = strlen((char*)io->unpack_setting->delimiter);
  467. }
  468. }
  469. else if (io->unpack_setting->mode == UNPACK_BY_LENGTH_FIELD) {
  470. assert(io->unpack_setting->body_offset >=
  471. io->unpack_setting->length_field_offset +
  472. io->unpack_setting->length_field_bytes);
  473. }
  474. // NOTE: unpack must have own readbuf
  475. if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
  476. io->readbuf.len = io->unpack_setting->fixed_length;
  477. } else {
  478. io->readbuf.len = HLOOP_READ_BUFSIZE;
  479. }
  480. hio_alloc_readbuf(io, io->readbuf.len);
  481. }
  482. void hio_unset_unpack(hio_t* io) {
  483. if (io->unpack_setting) {
  484. io->unpack_setting = NULL;
  485. // NOTE: unpack has own readbuf
  486. hio_free_readbuf(io);
  487. }
  488. }
  489. //-----------------upstream---------------------------------------------
  490. void hio_read_upstream(hio_t* io) {
  491. hio_t* upstream_io = io->upstream_io;
  492. if (upstream_io) {
  493. hio_read(io);
  494. hio_read(upstream_io);
  495. }
  496. }
  497. void hio_write_upstream(hio_t* io, void* buf, int bytes) {
  498. hio_t* upstream_io = io->upstream_io;
  499. if (upstream_io) {
  500. hio_write(upstream_io, buf, bytes);
  501. }
  502. }
  503. void hio_close_upstream(hio_t* io) {
  504. hio_t* upstream_io = io->upstream_io;
  505. if (upstream_io) {
  506. hio_close(upstream_io);
  507. }
  508. }
  509. void hio_setup_upstream(hio_t* io1, hio_t* io2) {
  510. io1->upstream_io = io2;
  511. io2->upstream_io = io1;
  512. hio_setcb_read(io1, hio_write_upstream);
  513. hio_setcb_read(io2, hio_write_upstream);
  514. }
  515. hio_t* hio_get_upstream(hio_t* io) {
  516. return io->upstream_io;
  517. }
  518. hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
  519. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  520. if (upstream_io == NULL) return NULL;
  521. if (ssl) hio_enable_ssl(upstream_io);
  522. hio_setup_upstream(io, upstream_io);
  523. hio_setcb_close(io, hio_close_upstream);
  524. hio_setcb_close(upstream_io, hio_close_upstream);
  525. hconnect(io->loop, upstream_io->fd, hio_read_upstream);
  526. return upstream_io;
  527. }
  528. hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
  529. hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
  530. if (upstream_io == NULL) return NULL;
  531. hio_setup_upstream(io, upstream_io);
  532. hio_read_upstream(io);
  533. return upstream_io;
  534. }