1
0

nio.c 17 KB

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