1
0

nio.c 15 KB

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