nio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. ssl_client_handshake(io);
  202. }
  203. else {
  204. // NOTE: SSL call connect_cb after handshake finished
  205. __connect_cb(io);
  206. }
  207. return;
  208. }
  209. connect_error:
  210. hlogw("connfd=%d connect error: %s:%d\n", io->fd, socket_strerror(io->error), io->error);
  211. hio_close(io);
  212. }
  213. static int __nio_read(hio_t* io, void* buf, int len) {
  214. int nread = 0;
  215. switch (io->io_type) {
  216. case HIO_TYPE_SSL:
  217. nread = hssl_read(io->ssl, buf, len);
  218. break;
  219. case HIO_TYPE_TCP:
  220. #ifdef OS_UNIX
  221. nread = read(io->fd, buf, len);
  222. #else
  223. nread = recv(io->fd, buf, len, 0);
  224. #endif
  225. break;
  226. case HIO_TYPE_UDP:
  227. case HIO_TYPE_KCP:
  228. case HIO_TYPE_IP:
  229. {
  230. socklen_t addrlen = sizeof(sockaddr_u);
  231. nread = recvfrom(io->fd, buf, len, 0, io->peeraddr, &addrlen);
  232. }
  233. break;
  234. default:
  235. nread = read(io->fd, buf, len);
  236. break;
  237. }
  238. // hlogd("read retval=%d", nread);
  239. return nread;
  240. }
  241. static int __nio_write(hio_t* io, const void* buf, int len) {
  242. int nwrite = 0;
  243. switch (io->io_type) {
  244. case HIO_TYPE_SSL:
  245. nwrite = hssl_write(io->ssl, buf, len);
  246. break;
  247. case HIO_TYPE_TCP:
  248. #ifdef OS_UNIX
  249. nwrite = write(io->fd, buf, len);
  250. #else
  251. nwrite = send(io->fd, buf, len, 0);
  252. #endif
  253. break;
  254. case HIO_TYPE_UDP:
  255. case HIO_TYPE_KCP:
  256. case HIO_TYPE_IP:
  257. nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  258. break;
  259. default:
  260. nwrite = write(io->fd, buf, len);
  261. break;
  262. }
  263. // hlogd("write retval=%d", nwrite);
  264. return nwrite;
  265. }
  266. static void nio_read(hio_t* io) {
  267. // printd("nio_read fd=%d\n", io->fd);
  268. void* buf;
  269. int len = 0, nread = 0, err = 0;
  270. read:
  271. buf = io->readbuf.base + io->readbuf.tail;
  272. if (io->read_flags & HIO_READ_UNTIL_LENGTH) {
  273. len = io->read_until_length - (io->readbuf.tail - io->readbuf.head);
  274. } else {
  275. len = io->readbuf.len - io->readbuf.tail;
  276. }
  277. assert(len > 0);
  278. nread = __nio_read(io, buf, len);
  279. // printd("read retval=%d\n", nread);
  280. if (nread < 0) {
  281. err = socket_errno();
  282. if (err == EAGAIN) {
  283. // goto read_done;
  284. return;
  285. } else if (err == EMSGSIZE) {
  286. // ignore
  287. return;
  288. } else {
  289. // perror("read");
  290. io->error = err;
  291. goto read_error;
  292. }
  293. }
  294. if (nread == 0) {
  295. goto disconnect;
  296. }
  297. io->readbuf.tail += nread;
  298. __read_cb(io, buf, nread);
  299. if (nread == len && !io->closed) {
  300. // NOTE: ssl may have own cache
  301. if (io->io_type == HIO_TYPE_SSL) {
  302. // read continue
  303. goto read;
  304. }
  305. }
  306. return;
  307. read_error:
  308. disconnect:
  309. hio_close(io);
  310. }
  311. static void nio_write(hio_t* io) {
  312. // printd("nio_write fd=%d\n", io->fd);
  313. int nwrite = 0, err = 0;
  314. hrecursive_mutex_lock(&io->write_mutex);
  315. write:
  316. if (write_queue_empty(&io->write_queue)) {
  317. hrecursive_mutex_unlock(&io->write_mutex);
  318. if (io->close) {
  319. io->close = 0;
  320. hio_close(io);
  321. }
  322. return;
  323. }
  324. offset_buf_t* pbuf = write_queue_front(&io->write_queue);
  325. char* base = pbuf->base;
  326. char* buf = base + pbuf->offset;
  327. int len = pbuf->len - pbuf->offset;
  328. nwrite = __nio_write(io, buf, len);
  329. // printd("write retval=%d\n", nwrite);
  330. if (nwrite < 0) {
  331. err = socket_errno();
  332. if (err == EAGAIN) {
  333. hrecursive_mutex_unlock(&io->write_mutex);
  334. return;
  335. } else {
  336. // perror("write");
  337. io->error = err;
  338. goto write_error;
  339. }
  340. }
  341. if (nwrite == 0) {
  342. goto disconnect;
  343. }
  344. pbuf->offset += nwrite;
  345. io->write_bufsize -= nwrite;
  346. __write_cb(io, buf, nwrite);
  347. if (nwrite == len) {
  348. // NOTE: after write_cb, pbuf maybe invalid.
  349. // HV_FREE(pbuf->base);
  350. HV_FREE(base);
  351. write_queue_pop_front(&io->write_queue);
  352. if (!io->closed) {
  353. // write continue
  354. goto write;
  355. }
  356. }
  357. hrecursive_mutex_unlock(&io->write_mutex);
  358. return;
  359. write_error:
  360. disconnect:
  361. hrecursive_mutex_unlock(&io->write_mutex);
  362. hio_close(io);
  363. }
  364. static void hio_handle_events(hio_t* io) {
  365. if ((io->events & HV_READ) && (io->revents & HV_READ)) {
  366. if (io->accept) {
  367. nio_accept(io);
  368. }
  369. else {
  370. nio_read(io);
  371. }
  372. }
  373. if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
  374. // NOTE: del HV_WRITE, if write_queue empty
  375. hrecursive_mutex_lock(&io->write_mutex);
  376. if (write_queue_empty(&io->write_queue)) {
  377. iowatcher_del_event(io->loop, io->fd, HV_WRITE);
  378. io->events &= ~HV_WRITE;
  379. }
  380. hrecursive_mutex_unlock(&io->write_mutex);
  381. if (io->connect) {
  382. // NOTE: connect just do once
  383. // ONESHOT
  384. io->connect = 0;
  385. nio_connect(io);
  386. }
  387. else {
  388. nio_write(io);
  389. }
  390. }
  391. io->revents = 0;
  392. }
  393. int hio_accept(hio_t* io) {
  394. io->accept = 1;
  395. return hio_add(io, hio_handle_events, HV_READ);
  396. }
  397. int hio_connect(hio_t* io) {
  398. int ret = connect(io->fd, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  399. #ifdef OS_WIN
  400. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  401. #else
  402. if (ret < 0 && socket_errno() != EINPROGRESS) {
  403. #endif
  404. perror("connect");
  405. io->error = socket_errno();
  406. hio_close(io);
  407. return ret;
  408. }
  409. if (ret == 0) {
  410. // connect ok
  411. nio_connect(io);
  412. return 0;
  413. }
  414. int timeout = io->connect_timeout ? io->connect_timeout : HIO_DEFAULT_CONNECT_TIMEOUT;
  415. io->connect_timer = htimer_add(io->loop, __connect_timeout_cb, timeout, 1);
  416. io->connect_timer->privdata = io;
  417. io->connect = 1;
  418. return hio_add(io, hio_handle_events, HV_WRITE);
  419. }
  420. int hio_read (hio_t* io) {
  421. if (io->closed) {
  422. hloge("hio_read called but fd[%d] already closed!", io->fd);
  423. return -1;
  424. }
  425. hio_add(io, hio_handle_events, HV_READ);
  426. if (io->readbuf.tail > io->readbuf.head &&
  427. io->unpack_setting == NULL &&
  428. io->read_flags == 0) {
  429. hio_read_remain(io);
  430. }
  431. return 0;
  432. }
  433. int hio_write (hio_t* io, const void* buf, size_t len) {
  434. if (io->closed) {
  435. hloge("hio_write called but fd[%d] already closed!", io->fd);
  436. return -1;
  437. }
  438. int nwrite = 0, err = 0;
  439. hrecursive_mutex_lock(&io->write_mutex);
  440. #if WITH_KCP
  441. if (io->io_type == HIO_TYPE_KCP) {
  442. nwrite = hio_write_kcp(io, buf, len);
  443. // if (nwrite < 0) goto write_error;
  444. goto write_done;
  445. }
  446. #endif
  447. if (write_queue_empty(&io->write_queue)) {
  448. try_write:
  449. nwrite = __nio_write(io, buf, len);
  450. // printd("write retval=%d\n", nwrite);
  451. if (nwrite < 0) {
  452. err = socket_errno();
  453. if (err == EAGAIN) {
  454. nwrite = 0;
  455. hlogw("try_write failed, enqueue!");
  456. goto enqueue;
  457. } else {
  458. // perror("write");
  459. io->error = err;
  460. goto write_error;
  461. }
  462. }
  463. if (nwrite == 0) {
  464. goto disconnect;
  465. }
  466. if (nwrite == len) {
  467. goto write_done;
  468. }
  469. enqueue:
  470. hio_add(io, hio_handle_events, HV_WRITE);
  471. }
  472. if (nwrite < len) {
  473. if (io->write_bufsize + len - nwrite > MAX_WRITE_BUFSIZE) {
  474. if (io->write_bufsize > MAX_WRITE_BUFSIZE) {
  475. hloge("write bufsize > %u, close it!", (unsigned int)MAX_WRITE_BUFSIZE);
  476. io->error = ERR_OVER_LIMIT;
  477. goto write_error;
  478. }
  479. }
  480. offset_buf_t remain;
  481. remain.len = len - nwrite;
  482. remain.offset = 0;
  483. // NOTE: free in nio_write
  484. HV_ALLOC(remain.base, remain.len);
  485. memcpy(remain.base, ((char*)buf) + nwrite, remain.len);
  486. if (io->write_queue.maxsize == 0) {
  487. write_queue_init(&io->write_queue, 4);
  488. }
  489. write_queue_push_back(&io->write_queue, &remain);
  490. io->write_bufsize += remain.len;
  491. if (io->write_bufsize > WRITE_BUFSIZE_HIGH_WATER) {
  492. hlogw("write len=%d enqueue %u, bufsize=%u over high water %u",
  493. len, (unsigned int)(remain.len - remain.offset),
  494. (unsigned int)io->write_bufsize,
  495. (unsigned int)WRITE_BUFSIZE_HIGH_WATER);
  496. }
  497. }
  498. write_done:
  499. hrecursive_mutex_unlock(&io->write_mutex);
  500. if (nwrite > 0) {
  501. __write_cb(io, buf, nwrite);
  502. }
  503. return nwrite;
  504. write_error:
  505. disconnect:
  506. hrecursive_mutex_unlock(&io->write_mutex);
  507. /* NOTE:
  508. * We usually free resources in hclose_cb,
  509. * if hio_close_sync, we have to be very careful to avoid using freed resources.
  510. * But if hio_close_async, we do not have to worry about this.
  511. */
  512. hio_close_async(io);
  513. return nwrite < 0 ? nwrite : -1;
  514. }
  515. int hio_close (hio_t* io) {
  516. if (io->closed) return 0;
  517. if (hv_gettid() != io->loop->tid) {
  518. return hio_close_async(io);
  519. }
  520. hrecursive_mutex_lock(&io->write_mutex);
  521. if (io->closed) {
  522. hrecursive_mutex_unlock(&io->write_mutex);
  523. return 0;
  524. }
  525. if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
  526. io->close = 1;
  527. hrecursive_mutex_unlock(&io->write_mutex);
  528. hlogw("write_queue not empty, close later.");
  529. int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
  530. io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
  531. io->close_timer->privdata = io;
  532. return 0;
  533. }
  534. io->closed = 1;
  535. hrecursive_mutex_unlock(&io->write_mutex);
  536. hio_done(io);
  537. __close_cb(io);
  538. if (io->ssl) {
  539. hssl_free(io->ssl);
  540. io->ssl = NULL;
  541. }
  542. if (io->ssl_ctx && io->alloced_ssl_ctx) {
  543. hssl_ctx_free(io->ssl_ctx);
  544. io->ssl_ctx = NULL;
  545. }
  546. if (io->io_type & HIO_TYPE_SOCKET) {
  547. closesocket(io->fd);
  548. }
  549. return 0;
  550. }
  551. #endif