nio.c 16 KB

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