nio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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) goto read;
  300. return;
  301. read_error:
  302. disconnect:
  303. hio_close(io);
  304. }
  305. static void nio_write(hio_t* io) {
  306. // printd("nio_write fd=%d\n", io->fd);
  307. int nwrite = 0, err = 0;
  308. hrecursive_mutex_lock(&io->write_mutex);
  309. write:
  310. if (write_queue_empty(&io->write_queue)) {
  311. hrecursive_mutex_unlock(&io->write_mutex);
  312. if (io->close) {
  313. io->close = 0;
  314. hio_close(io);
  315. }
  316. return;
  317. }
  318. offset_buf_t* pbuf = write_queue_front(&io->write_queue);
  319. char* base = pbuf->base;
  320. char* buf = base + pbuf->offset;
  321. int len = pbuf->len - pbuf->offset;
  322. nwrite = __nio_write(io, buf, len);
  323. // printd("write retval=%d\n", nwrite);
  324. if (nwrite < 0) {
  325. err = socket_errno();
  326. if (err == EAGAIN) {
  327. hrecursive_mutex_unlock(&io->write_mutex);
  328. return;
  329. } else {
  330. // perror("write");
  331. io->error = err;
  332. goto write_error;
  333. }
  334. }
  335. if (nwrite == 0) {
  336. goto disconnect;
  337. }
  338. pbuf->offset += nwrite;
  339. io->write_bufsize -= nwrite;
  340. __write_cb(io, buf, nwrite);
  341. if (nwrite == len) {
  342. // NOTE: after write_cb, pbuf maybe invalid.
  343. // HV_FREE(pbuf->base);
  344. HV_FREE(base);
  345. write_queue_pop_front(&io->write_queue);
  346. if (!io->closed) {
  347. // write continue
  348. goto write;
  349. }
  350. }
  351. hrecursive_mutex_unlock(&io->write_mutex);
  352. return;
  353. write_error:
  354. disconnect:
  355. hrecursive_mutex_unlock(&io->write_mutex);
  356. hio_close(io);
  357. }
  358. static void hio_handle_events(hio_t* io) {
  359. if ((io->events & HV_READ) && (io->revents & HV_READ)) {
  360. if (io->accept) {
  361. nio_accept(io);
  362. }
  363. else {
  364. nio_read(io);
  365. }
  366. }
  367. if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
  368. // NOTE: del HV_WRITE, if write_queue empty
  369. hrecursive_mutex_lock(&io->write_mutex);
  370. if (write_queue_empty(&io->write_queue)) {
  371. iowatcher_del_event(io->loop, io->fd, HV_WRITE);
  372. io->events &= ~HV_WRITE;
  373. }
  374. hrecursive_mutex_unlock(&io->write_mutex);
  375. if (io->connect) {
  376. // NOTE: connect just do once
  377. // ONESHOT
  378. io->connect = 0;
  379. nio_connect(io);
  380. }
  381. else {
  382. nio_write(io);
  383. }
  384. }
  385. io->revents = 0;
  386. }
  387. int hio_accept(hio_t* io) {
  388. io->accept = 1;
  389. return hio_add(io, hio_handle_events, HV_READ);
  390. }
  391. int hio_connect(hio_t* io) {
  392. int ret = connect(io->fd, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  393. #ifdef OS_WIN
  394. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  395. #else
  396. if (ret < 0 && socket_errno() != EINPROGRESS) {
  397. #endif
  398. perror("connect");
  399. io->error = socket_errno();
  400. hio_close(io);
  401. return ret;
  402. }
  403. if (ret == 0) {
  404. // connect ok
  405. nio_connect(io);
  406. return 0;
  407. }
  408. int timeout = io->connect_timeout ? io->connect_timeout : HIO_DEFAULT_CONNECT_TIMEOUT;
  409. io->connect_timer = htimer_add(io->loop, __connect_timeout_cb, timeout, 1);
  410. io->connect_timer->privdata = io;
  411. io->connect = 1;
  412. return hio_add(io, hio_handle_events, HV_WRITE);
  413. }
  414. int hio_read (hio_t* io) {
  415. if (io->closed) {
  416. hloge("hio_read called but fd[%d] already closed!", io->fd);
  417. return -1;
  418. }
  419. hio_add(io, hio_handle_events, HV_READ);
  420. if (io->readbuf.tail > io->readbuf.head &&
  421. io->unpack_setting == NULL &&
  422. io->read_flags == 0) {
  423. hio_read_remain(io);
  424. }
  425. return 0;
  426. }
  427. int hio_write (hio_t* io, const void* buf, size_t len) {
  428. if (io->closed) {
  429. hloge("hio_write called but fd[%d] already closed!", io->fd);
  430. return -1;
  431. }
  432. int nwrite = 0, err = 0;
  433. hrecursive_mutex_lock(&io->write_mutex);
  434. #if WITH_KCP
  435. if (io->io_type == HIO_TYPE_KCP) {
  436. nwrite = hio_write_kcp(io, buf, len);
  437. // if (nwrite < 0) goto write_error;
  438. goto write_done;
  439. }
  440. #endif
  441. if (write_queue_empty(&io->write_queue)) {
  442. try_write:
  443. nwrite = __nio_write(io, buf, len);
  444. // printd("write retval=%d\n", nwrite);
  445. if (nwrite < 0) {
  446. err = socket_errno();
  447. if (err == EAGAIN) {
  448. nwrite = 0;
  449. hlogw("try_write failed, enqueue!");
  450. goto enqueue;
  451. } else {
  452. // perror("write");
  453. io->error = err;
  454. goto write_error;
  455. }
  456. }
  457. if (nwrite == 0) {
  458. goto disconnect;
  459. }
  460. if (nwrite == len) {
  461. goto write_done;
  462. }
  463. enqueue:
  464. hio_add(io, hio_handle_events, HV_WRITE);
  465. }
  466. if (nwrite < len) {
  467. if (io->write_bufsize + len - nwrite > MAX_WRITE_BUFSIZE) {
  468. if (io->write_bufsize > MAX_WRITE_BUFSIZE) {
  469. hloge("write bufsize > %u, close it!", (unsigned int)MAX_WRITE_BUFSIZE);
  470. io->error = ERR_OVER_LIMIT;
  471. goto write_error;
  472. }
  473. }
  474. offset_buf_t remain;
  475. remain.len = len - nwrite;
  476. remain.offset = 0;
  477. // NOTE: free in nio_write
  478. HV_ALLOC(remain.base, remain.len);
  479. memcpy(remain.base, ((char*)buf) + nwrite, remain.len);
  480. if (io->write_queue.maxsize == 0) {
  481. write_queue_init(&io->write_queue, 4);
  482. }
  483. write_queue_push_back(&io->write_queue, &remain);
  484. io->write_bufsize += remain.len;
  485. if (io->write_bufsize > WRITE_BUFSIZE_HIGH_WATER) {
  486. hlogw("write len=%d enqueue %u, bufsize=%u over high water %u",
  487. len, (unsigned int)(remain.len - remain.offset),
  488. (unsigned int)io->write_bufsize,
  489. (unsigned int)WRITE_BUFSIZE_HIGH_WATER);
  490. }
  491. }
  492. write_done:
  493. hrecursive_mutex_unlock(&io->write_mutex);
  494. if (nwrite > 0) {
  495. __write_cb(io, buf, nwrite);
  496. }
  497. return nwrite;
  498. write_error:
  499. disconnect:
  500. hrecursive_mutex_unlock(&io->write_mutex);
  501. /* NOTE:
  502. * We usually free resources in hclose_cb,
  503. * if hio_close_sync, we have to be very careful to avoid using freed resources.
  504. * But if hio_close_async, we do not have to worry about this.
  505. */
  506. hio_close_async(io);
  507. return nwrite < 0 ? nwrite : -1;
  508. }
  509. int hio_close (hio_t* io) {
  510. if (io->closed) return 0;
  511. if (hv_gettid() != io->loop->tid) {
  512. return hio_close_async(io);
  513. }
  514. hrecursive_mutex_lock(&io->write_mutex);
  515. if (io->closed) {
  516. hrecursive_mutex_unlock(&io->write_mutex);
  517. return 0;
  518. }
  519. if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
  520. io->close = 1;
  521. hrecursive_mutex_unlock(&io->write_mutex);
  522. hlogw("write_queue not empty, close later.");
  523. int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
  524. io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
  525. io->close_timer->privdata = io;
  526. return 0;
  527. }
  528. io->closed = 1;
  529. hrecursive_mutex_unlock(&io->write_mutex);
  530. hio_done(io);
  531. __close_cb(io);
  532. if (io->ssl) {
  533. hssl_free(io->ssl);
  534. io->ssl = NULL;
  535. }
  536. if (io->ssl_ctx && io->alloced_ssl_ctx) {
  537. hssl_ctx_free(io->ssl_ctx);
  538. io->ssl_ctx = NULL;
  539. }
  540. if (io->io_type & HIO_TYPE_SOCKET) {
  541. closesocket(io->fd);
  542. }
  543. return 0;
  544. }
  545. #endif