nio.c 15 KB

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