nio.c 15 KB

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