nio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #include "iowatcher.h"
  2. #ifndef EVENT_IOCP
  3. #include "hevent.h"
  4. #include "hsocket.h"
  5. #include "hssl.h"
  6. #include "hlog.h"
  7. #include "hthread.h"
  8. static void __connect_timeout_cb(htimer_t* timer) {
  9. hio_t* io = (hio_t*)timer->privdata;
  10. if (io) {
  11. char localaddrstr[SOCKADDR_STRLEN] = {0};
  12. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  13. hlogw("connect timeout [%s] <=> [%s]",
  14. SOCKADDR_STR(io->localaddr, localaddrstr),
  15. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  16. io->error = ETIMEDOUT;
  17. hio_close(io);
  18. }
  19. }
  20. static void __close_timeout_cb(htimer_t* timer) {
  21. hio_t* io = (hio_t*)timer->privdata;
  22. if (io) {
  23. char localaddrstr[SOCKADDR_STRLEN] = {0};
  24. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  25. hlogw("close timeout [%s] <=> [%s]",
  26. SOCKADDR_STR(io->localaddr, localaddrstr),
  27. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  28. io->error = ETIMEDOUT;
  29. hio_close(io);
  30. }
  31. }
  32. static void __keepalive_timeout_cb(htimer_t* timer) {
  33. hio_t* io = (hio_t*)timer->privdata;
  34. if (io) {
  35. char localaddrstr[SOCKADDR_STRLEN] = {0};
  36. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  37. hlogw("keepalive timeout [%s] <=> [%s]",
  38. SOCKADDR_STR(io->localaddr, localaddrstr),
  39. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  40. io->error = ETIMEDOUT;
  41. hio_close(io);
  42. }
  43. }
  44. static void __heartbeat_timer_cb(htimer_t* timer) {
  45. hio_t* io = (hio_t*)timer->privdata;
  46. if (io && io->heartbeat_fn) {
  47. io->heartbeat_fn(io);
  48. }
  49. }
  50. static void __accept_cb(hio_t* io) {
  51. /*
  52. char localaddrstr[SOCKADDR_STRLEN] = {0};
  53. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  54. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  55. SOCKADDR_STR(io->localaddr, localaddrstr),
  56. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  57. */
  58. if (io->accept_cb) {
  59. // printd("accept_cb------\n");
  60. io->accept_cb(io);
  61. // printd("accept_cb======\n");
  62. }
  63. if (io->keepalive_timeout > 0) {
  64. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
  65. io->keepalive_timer->privdata = io;
  66. }
  67. if (io->heartbeat_interval > 0) {
  68. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
  69. io->heartbeat_timer->privdata = io;
  70. }
  71. }
  72. static void __connect_cb(hio_t* io) {
  73. /*
  74. char localaddrstr[SOCKADDR_STRLEN] = {0};
  75. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  76. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  77. SOCKADDR_STR(io->localaddr, localaddrstr),
  78. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  79. */
  80. if (io->connect_timer) {
  81. htimer_del(io->connect_timer);
  82. io->connect_timer = NULL;
  83. io->connect_timeout = 0;
  84. }
  85. if (io->connect_cb) {
  86. // printd("connect_cb------\n");
  87. io->connect_cb(io);
  88. // printd("connect_cb======\n");
  89. }
  90. if (io->keepalive_timeout > 0) {
  91. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
  92. io->keepalive_timer->privdata = io;
  93. }
  94. if (io->heartbeat_interval > 0) {
  95. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
  96. io->heartbeat_timer->privdata = io;
  97. }
  98. }
  99. static void __read_cb(hio_t* io, void* buf, int readbytes) {
  100. // printd("> %.*s\n", readbytes, buf);
  101. if (io->keepalive_timer) {
  102. htimer_reset(io->keepalive_timer);
  103. }
  104. if (io->read_cb) {
  105. // printd("read_cb------\n");
  106. io->read_cb(io, buf, readbytes);
  107. // printd("read_cb======\n");
  108. }
  109. }
  110. static void __write_cb(hio_t* io, const void* buf, int writebytes) {
  111. // printd("< %.*s\n", writebytes, buf);
  112. if (io->keepalive_timer) {
  113. htimer_reset(io->keepalive_timer);
  114. }
  115. if (io->write_cb) {
  116. // printd("write_cb------\n");
  117. io->write_cb(io, buf, writebytes);
  118. // printd("write_cb======\n");
  119. }
  120. }
  121. static void __close_cb(hio_t* io) {
  122. // printd("close fd=%d\n", io->fd);
  123. if (io->connect_timer) {
  124. htimer_del(io->connect_timer);
  125. io->connect_timer = NULL;
  126. io->connect_timeout = 0;
  127. }
  128. if (io->close_timer) {
  129. htimer_del(io->close_timer);
  130. io->close_timer = NULL;
  131. io->close_timeout = 0;
  132. }
  133. if (io->keepalive_timer) {
  134. htimer_del(io->keepalive_timer);
  135. io->keepalive_timer = NULL;
  136. io->keepalive_timeout = 0;
  137. }
  138. if (io->heartbeat_timer) {
  139. htimer_del(io->heartbeat_timer);
  140. io->heartbeat_timer = NULL;
  141. io->heartbeat_interval = 0;
  142. io->heartbeat_fn = NULL;
  143. }
  144. if (io->close_cb) {
  145. // printd("close_cb------\n");
  146. io->close_cb(io);
  147. // printd("close_cb======\n");
  148. }
  149. }
  150. static void ssl_server_handshark(hio_t* io) {
  151. printd("ssl server handshark...\n");
  152. int ret = hssl_accept(io->ssl);
  153. if (ret == 0) {
  154. // handshark finish
  155. iowatcher_del_event(io->loop, io->fd, HV_READ);
  156. io->events &= ~HV_READ;
  157. io->cb = NULL;
  158. printd("ssl handshark finished.\n");
  159. __accept_cb(io);
  160. }
  161. else if (ret == HSSL_WANT_READ) {
  162. if ((io->events & HV_READ) == 0) {
  163. hio_add(io, ssl_server_handshark, HV_READ);
  164. }
  165. }
  166. else {
  167. hloge("ssl handshake failed: %d", ret);
  168. hio_close(io);
  169. }
  170. }
  171. static void ssl_client_handshark(hio_t* io) {
  172. printd("ssl client handshark...\n");
  173. int ret = hssl_connect(io->ssl);
  174. if (ret == 0) {
  175. // handshark finish
  176. iowatcher_del_event(io->loop, io->fd, HV_READ);
  177. io->events &= ~HV_READ;
  178. io->cb = NULL;
  179. printd("ssl handshark finished.\n");
  180. __connect_cb(io);
  181. }
  182. else if (ret == HSSL_WANT_READ) {
  183. if ((io->events & HV_READ) == 0) {
  184. hio_add(io, ssl_client_handshark, HV_READ);
  185. }
  186. }
  187. else {
  188. hloge("ssl handshake failed: %d", ret);
  189. hio_close(io);
  190. }
  191. }
  192. static void nio_accept(hio_t* io) {
  193. //printd("nio_accept listenfd=%d\n", io->fd);
  194. socklen_t addrlen;
  195. accept:
  196. addrlen = sizeof(sockaddr_u);
  197. int connfd = accept(io->fd, io->peeraddr, &addrlen);
  198. hio_t* connio = NULL;
  199. if (connfd < 0) {
  200. if (socket_errno() == EAGAIN) {
  201. //goto accept_done;
  202. return;
  203. }
  204. else {
  205. io->error = socket_errno();
  206. perror("accept");
  207. goto accept_error;
  208. }
  209. }
  210. addrlen = sizeof(sockaddr_u);
  211. getsockname(connfd, io->localaddr, &addrlen);
  212. connio = hio_get(io->loop, connfd);
  213. // NOTE: inherit from listenio
  214. connio->accept_cb = io->accept_cb;
  215. connio->userdata = io->userdata;
  216. if (io->io_type == HIO_TYPE_SSL) {
  217. hssl_ctx_t ssl_ctx = hssl_ctx_instance();
  218. if (ssl_ctx == NULL) {
  219. goto accept_error;
  220. }
  221. hssl_t ssl = hssl_new(ssl_ctx, connfd);
  222. if (ssl == NULL) {
  223. goto accept_error;
  224. }
  225. hio_enable_ssl(connio);
  226. connio->ssl = ssl;
  227. ssl_server_handshark(connio);
  228. }
  229. else {
  230. // NOTE: SSL call accept_cb after handshark finished
  231. __accept_cb(connio);
  232. }
  233. goto accept;
  234. accept_error:
  235. hio_close(io);
  236. }
  237. static void nio_connect(hio_t* io) {
  238. //printd("nio_connect connfd=%d\n", io->fd);
  239. socklen_t addrlen = sizeof(sockaddr_u);
  240. int ret = getpeername(io->fd, io->peeraddr, &addrlen);
  241. if (ret < 0) {
  242. io->error = socket_errno();
  243. printd("connect failed: %s: %d\n", strerror(socket_errno()), socket_errno());
  244. goto connect_failed;
  245. }
  246. else {
  247. addrlen = sizeof(sockaddr_u);
  248. getsockname(io->fd, io->localaddr, &addrlen);
  249. if (io->io_type == HIO_TYPE_SSL) {
  250. hssl_ctx_t ssl_ctx = hssl_ctx_instance();
  251. if (ssl_ctx == NULL) {
  252. goto connect_failed;
  253. }
  254. hssl_t ssl = hssl_new(ssl_ctx, io->fd);
  255. if (ssl == NULL) {
  256. goto connect_failed;
  257. }
  258. io->ssl = ssl;
  259. ssl_client_handshark(io);
  260. }
  261. else {
  262. // NOTE: SSL call connect_cb after handshark finished
  263. __connect_cb(io);
  264. }
  265. return;
  266. }
  267. connect_failed:
  268. hio_close(io);
  269. }
  270. static int __nio_read(hio_t* io, void* buf, int len) {
  271. int nread = 0;
  272. switch (io->io_type) {
  273. case HIO_TYPE_SSL:
  274. nread = hssl_read(io->ssl, buf, len);
  275. break;
  276. case HIO_TYPE_TCP:
  277. #ifdef OS_UNIX
  278. nread = read(io->fd, buf, len);
  279. #else
  280. nread = recv(io->fd, buf, len, 0);
  281. #endif
  282. break;
  283. case HIO_TYPE_UDP:
  284. case HIO_TYPE_IP:
  285. {
  286. socklen_t addrlen = sizeof(sockaddr_u);
  287. nread = recvfrom(io->fd, buf, len, 0, io->peeraddr, &addrlen);
  288. }
  289. break;
  290. default:
  291. nread = read(io->fd, buf, len);
  292. break;
  293. }
  294. return nread;
  295. }
  296. static int __nio_write(hio_t* io, const void* buf, int len) {
  297. int nwrite = 0;
  298. switch (io->io_type) {
  299. case HIO_TYPE_SSL:
  300. nwrite = hssl_write(io->ssl, buf, len);
  301. break;
  302. case HIO_TYPE_TCP:
  303. #ifdef OS_UNIX
  304. nwrite = write(io->fd, buf, len);
  305. #else
  306. nwrite = send(io->fd, buf, len, 0);
  307. #endif
  308. break;
  309. case HIO_TYPE_UDP:
  310. case HIO_TYPE_IP:
  311. nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  312. break;
  313. default:
  314. nwrite = write(io->fd, buf, len);
  315. break;
  316. }
  317. return nwrite;
  318. }
  319. static void nio_read(hio_t* io) {
  320. //printd("nio_read fd=%d\n", io->fd);
  321. void* buf;
  322. int len, nread;
  323. read:
  324. if (io->readbuf.base == NULL || io->readbuf.len == 0) {
  325. hio_set_readbuf(io, io->loop->readbuf.base, io->loop->readbuf.len);
  326. }
  327. buf = io->readbuf.base;
  328. len = io->readbuf.len;
  329. nread = __nio_read(io, buf, len);
  330. //printd("read retval=%d\n", nread);
  331. if (nread < 0) {
  332. if (socket_errno() == EAGAIN) {
  333. //goto read_done;
  334. return;
  335. }
  336. else {
  337. io->error = socket_errno();
  338. // perror("read");
  339. goto read_error;
  340. }
  341. }
  342. if (nread == 0) {
  343. goto disconnect;
  344. }
  345. __read_cb(io, buf, nread);
  346. if (nread == len) {
  347. goto read;
  348. }
  349. return;
  350. read_error:
  351. disconnect:
  352. hio_close(io);
  353. }
  354. static void nio_write(hio_t* io) {
  355. //printd("nio_write fd=%d\n", io->fd);
  356. int nwrite = 0;
  357. hrecursive_mutex_lock(&io->write_mutex);
  358. write:
  359. if (write_queue_empty(&io->write_queue)) {
  360. hrecursive_mutex_unlock(&io->write_mutex);
  361. if (io->close) {
  362. io->close = 0;
  363. hio_close(io);
  364. }
  365. return;
  366. }
  367. offset_buf_t* pbuf = write_queue_front(&io->write_queue);
  368. char* buf = pbuf->base + pbuf->offset;
  369. int len = pbuf->len - pbuf->offset;
  370. nwrite = __nio_write(io, buf, len);
  371. //printd("write retval=%d\n", nwrite);
  372. if (nwrite < 0) {
  373. if (socket_errno() == EAGAIN) {
  374. //goto write_done;
  375. hrecursive_mutex_unlock(&io->write_mutex);
  376. return;
  377. }
  378. else {
  379. io->error = socket_errno();
  380. // perror("write");
  381. goto write_error;
  382. }
  383. }
  384. if (nwrite == 0) {
  385. goto disconnect;
  386. }
  387. __write_cb(io, buf, nwrite);
  388. pbuf->offset += nwrite;
  389. if (nwrite == len) {
  390. HV_FREE(pbuf->base);
  391. write_queue_pop_front(&io->write_queue);
  392. // write next
  393. goto write;
  394. }
  395. hrecursive_mutex_unlock(&io->write_mutex);
  396. return;
  397. write_error:
  398. disconnect:
  399. hrecursive_mutex_unlock(&io->write_mutex);
  400. hio_close(io);
  401. }
  402. static void hio_handle_events(hio_t* io) {
  403. if ((io->events & HV_READ) && (io->revents & HV_READ)) {
  404. if (io->accept) {
  405. nio_accept(io);
  406. }
  407. else {
  408. nio_read(io);
  409. }
  410. }
  411. if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
  412. // NOTE: del HV_WRITE, if write_queue empty
  413. hrecursive_mutex_lock(&io->write_mutex);
  414. if (write_queue_empty(&io->write_queue)) {
  415. iowatcher_del_event(io->loop, io->fd, HV_WRITE);
  416. io->events &= ~HV_WRITE;
  417. }
  418. hrecursive_mutex_unlock(&io->write_mutex);
  419. if (io->connect) {
  420. // NOTE: connect just do once
  421. // ONESHOT
  422. io->connect = 0;
  423. nio_connect(io);
  424. }
  425. else {
  426. nio_write(io);
  427. }
  428. }
  429. io->revents = 0;
  430. }
  431. int hio_accept(hio_t* io) {
  432. io->accept = 1;
  433. hio_add(io, hio_handle_events, HV_READ);
  434. return 0;
  435. }
  436. int hio_connect(hio_t* io) {
  437. int ret = connect(io->fd, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  438. #ifdef OS_WIN
  439. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  440. #else
  441. if (ret < 0 && socket_errno() != EINPROGRESS) {
  442. #endif
  443. perror("connect");
  444. hio_close(io);
  445. return ret;
  446. }
  447. if (ret == 0) {
  448. // connect ok
  449. __connect_cb(io);
  450. return 0;
  451. }
  452. int timeout = io->connect_timeout ? io->connect_timeout : HIO_DEFAULT_CONNECT_TIMEOUT;
  453. io->connect_timer = htimer_add(io->loop, __connect_timeout_cb, timeout, 1);
  454. io->connect_timer->privdata = io;
  455. io->connect = 1;
  456. return hio_add(io, hio_handle_events, HV_WRITE);
  457. }
  458. int hio_read (hio_t* io) {
  459. if (io->closed) {
  460. hloge("hio_read called but fd[%d] already closed!", io->fd);
  461. return -1;
  462. }
  463. return hio_add(io, hio_handle_events, HV_READ);
  464. }
  465. int hio_write (hio_t* io, const void* buf, size_t len) {
  466. if (io->closed) {
  467. hloge("hio_write called but fd[%d] already closed!", io->fd);
  468. return -1;
  469. }
  470. int nwrite = 0;
  471. hrecursive_mutex_lock(&io->write_mutex);
  472. if (write_queue_empty(&io->write_queue)) {
  473. try_write:
  474. nwrite = __nio_write(io, buf, len);
  475. //printd("write retval=%d\n", nwrite);
  476. if (nwrite < 0) {
  477. if (socket_errno() == EAGAIN) {
  478. nwrite = 0;
  479. hlogw("try_write failed, enqueue!");
  480. goto enqueue;
  481. }
  482. else {
  483. // perror("write");
  484. io->error = socket_errno();
  485. goto write_error;
  486. }
  487. }
  488. if (nwrite == 0) {
  489. goto disconnect;
  490. }
  491. __write_cb(io, buf, nwrite);
  492. if (nwrite == len) {
  493. //goto write_done;
  494. hrecursive_mutex_unlock(&io->write_mutex);
  495. return nwrite;
  496. }
  497. enqueue:
  498. hio_add(io, hio_handle_events, HV_WRITE);
  499. }
  500. if (nwrite < len) {
  501. offset_buf_t rest;
  502. rest.len = len;
  503. rest.offset = nwrite;
  504. // NOTE: free in nio_write
  505. HV_ALLOC(rest.base, rest.len);
  506. memcpy(rest.base, buf, rest.len);
  507. if (io->write_queue.maxsize == 0) {
  508. write_queue_init(&io->write_queue, 4);
  509. }
  510. write_queue_push_back(&io->write_queue, &rest);
  511. }
  512. hrecursive_mutex_unlock(&io->write_mutex);
  513. return nwrite;
  514. write_error:
  515. disconnect:
  516. hrecursive_mutex_unlock(&io->write_mutex);
  517. hio_close(io);
  518. return nwrite;
  519. }
  520. static void hio_close_event_cb(hevent_t* ev) {
  521. hio_t* io = (hio_t*)ev->userdata;
  522. uint32_t id = (uintptr_t)ev->privdata;
  523. if (io->id == id) {
  524. hio_close((hio_t*)ev->userdata);
  525. }
  526. }
  527. int hio_close (hio_t* io) {
  528. if (io->closed) return 0;
  529. if (hv_gettid() != io->loop->tid) {
  530. hevent_t ev;
  531. memset(&ev, 0, sizeof(ev));
  532. ev.cb = hio_close_event_cb;
  533. ev.userdata = io;
  534. ev.privdata = (void*)(uintptr_t)io->id;
  535. hloop_post_event(io->loop, &ev);
  536. return 0;
  537. }
  538. hrecursive_mutex_lock(&io->write_mutex);
  539. if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
  540. hrecursive_mutex_unlock(&io->write_mutex);
  541. io->close = 1;
  542. hlogw("write_queue not empty, close later.");
  543. int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
  544. io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
  545. io->close_timer->privdata = io;
  546. return 0;
  547. }
  548. hrecursive_mutex_unlock(&io->write_mutex);
  549. io->closed = 1;
  550. hio_done(io);
  551. __close_cb(io);
  552. if (io->ssl) {
  553. hssl_free(io->ssl);
  554. io->ssl = NULL;
  555. }
  556. if (io->io_type & HIO_TYPE_SOCKET) {
  557. closesocket(io->fd);
  558. }
  559. return 0;
  560. }
  561. #endif