hevent.c 16 KB

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