1
0

nio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. static void __connect_timeout_cb(htimer_t* timer) {
  8. hio_t* io = (hio_t*)timer->privdata;
  9. if (io) {
  10. char localaddrstr[SOCKADDR_STRLEN] = {0};
  11. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  12. hlogw("connect timeout [%s] <=> [%s]",
  13. SOCKADDR_STR(io->localaddr, localaddrstr),
  14. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  15. io->error = ETIMEDOUT;
  16. hio_close(io);
  17. }
  18. }
  19. static void __close_timeout_cb(htimer_t* timer) {
  20. hio_t* io = (hio_t*)timer->privdata;
  21. if (io) {
  22. char localaddrstr[SOCKADDR_STRLEN] = {0};
  23. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  24. hlogw("close timeout [%s] <=> [%s]",
  25. SOCKADDR_STR(io->localaddr, localaddrstr),
  26. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  27. io->error = ETIMEDOUT;
  28. hio_close(io);
  29. }
  30. }
  31. static void __keepalive_timeout_cb(htimer_t* timer) {
  32. hio_t* io = (hio_t*)timer->privdata;
  33. if (io) {
  34. char localaddrstr[SOCKADDR_STRLEN] = {0};
  35. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  36. hlogw("keepalive timeout [%s] <=> [%s]",
  37. SOCKADDR_STR(io->localaddr, localaddrstr),
  38. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  39. io->error = ETIMEDOUT;
  40. hio_close(io);
  41. }
  42. }
  43. static void __heartbeat_timer_cb(htimer_t* timer) {
  44. hio_t* io = (hio_t*)timer->privdata;
  45. if (io && io->heartbeat_fn) {
  46. io->heartbeat_fn(io);
  47. }
  48. }
  49. static void __accept_cb(hio_t* io) {
  50. /*
  51. char localaddrstr[SOCKADDR_STRLEN] = {0};
  52. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  53. printd("accept connfd=%d [%s] <= [%s]\n", io->fd,
  54. SOCKADDR_STR(io->localaddr, localaddrstr),
  55. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  56. */
  57. if (io->accept_cb) {
  58. // printd("accept_cb------\n");
  59. io->accept_cb(io);
  60. // printd("accept_cb======\n");
  61. }
  62. if (io->keepalive_timeout > 0) {
  63. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
  64. io->keepalive_timer->privdata = io;
  65. }
  66. if (io->heartbeat_interval > 0) {
  67. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
  68. io->heartbeat_timer->privdata = io;
  69. }
  70. }
  71. static void __connect_cb(hio_t* io) {
  72. /*
  73. char localaddrstr[SOCKADDR_STRLEN] = {0};
  74. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  75. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  76. SOCKADDR_STR(io->localaddr, localaddrstr),
  77. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  78. */
  79. if (io->connect_timer) {
  80. htimer_del(io->connect_timer);
  81. io->connect_timer = NULL;
  82. io->connect_timeout = 0;
  83. }
  84. if (io->connect_cb) {
  85. // printd("connect_cb------\n");
  86. io->connect_cb(io);
  87. // printd("connect_cb======\n");
  88. }
  89. if (io->keepalive_timeout > 0) {
  90. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
  91. io->keepalive_timer->privdata = io;
  92. }
  93. if (io->heartbeat_interval > 0) {
  94. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
  95. io->heartbeat_timer->privdata = io;
  96. }
  97. }
  98. static void __read_cb(hio_t* io, void* buf, int readbytes) {
  99. // printd("> %.*s\n", readbytes, buf);
  100. if (io->keepalive_timer) {
  101. htimer_reset(io->keepalive_timer);
  102. }
  103. if (io->read_cb) {
  104. // printd("read_cb------\n");
  105. io->read_cb(io, buf, readbytes);
  106. // printd("read_cb======\n");
  107. }
  108. }
  109. static void __write_cb(hio_t* io, const void* buf, int writebytes) {
  110. // printd("< %.*s\n", writebytes, buf);
  111. if (io->keepalive_timer) {
  112. htimer_reset(io->keepalive_timer);
  113. }
  114. if (io->write_cb) {
  115. // printd("write_cb------\n");
  116. io->write_cb(io, buf, writebytes);
  117. // printd("write_cb======\n");
  118. }
  119. }
  120. static void __close_cb(hio_t* io) {
  121. // printd("close fd=%d\n", io->fd);
  122. if (io->connect_timer) {
  123. htimer_del(io->connect_timer);
  124. io->connect_timer = NULL;
  125. io->connect_timeout = 0;
  126. }
  127. if (io->close_timer) {
  128. htimer_del(io->close_timer);
  129. io->close_timer = NULL;
  130. io->close_timeout = 0;
  131. }
  132. if (io->keepalive_timer) {
  133. htimer_del(io->keepalive_timer);
  134. io->keepalive_timer = NULL;
  135. io->keepalive_timeout = 0;
  136. }
  137. if (io->heartbeat_timer) {
  138. htimer_del(io->heartbeat_timer);
  139. io->heartbeat_timer = NULL;
  140. io->heartbeat_interval = 0;
  141. io->heartbeat_fn = NULL;
  142. }
  143. if (io->close_cb) {
  144. // printd("close_cb------\n");
  145. io->close_cb(io);
  146. // printd("close_cb======\n");
  147. }
  148. }
  149. static void ssl_server_handshark(hio_t* io) {
  150. printd("ssl server handshark...\n");
  151. int ret = hssl_accept(io->ssl);
  152. if (ret == 0) {
  153. // handshark finish
  154. iowatcher_del_event(io->loop, io->fd, HV_READ);
  155. io->events &= ~HV_READ;
  156. io->cb = NULL;
  157. printd("ssl handshark finished.\n");
  158. __accept_cb(io);
  159. }
  160. else if (ret == HSSL_WANT_READ) {
  161. if ((io->events & HV_READ) == 0) {
  162. hio_add(io, ssl_server_handshark, HV_READ);
  163. }
  164. }
  165. else {
  166. hloge("ssl handshake failed: %d", ret);
  167. hio_close(io);
  168. }
  169. }
  170. static void ssl_client_handshark(hio_t* io) {
  171. printd("ssl client handshark...\n");
  172. int ret = hssl_connect(io->ssl);
  173. if (ret == 0) {
  174. // handshark finish
  175. iowatcher_del_event(io->loop, io->fd, HV_READ);
  176. io->events &= ~HV_READ;
  177. io->cb = NULL;
  178. printd("ssl handshark finished.\n");
  179. __connect_cb(io);
  180. }
  181. else if (ret == HSSL_WANT_READ) {
  182. if ((io->events & HV_READ) == 0) {
  183. hio_add(io, ssl_client_handshark, HV_READ);
  184. }
  185. }
  186. else {
  187. hloge("ssl handshake failed: %d", ret);
  188. hio_close(io);
  189. }
  190. }
  191. static void nio_accept(hio_t* io) {
  192. //printd("nio_accept listenfd=%d\n", io->fd);
  193. socklen_t addrlen;
  194. accept:
  195. addrlen = sizeof(sockaddr_u);
  196. int connfd = accept(io->fd, io->peeraddr, &addrlen);
  197. hio_t* connio = NULL;
  198. if (connfd < 0) {
  199. if (socket_errno() == EAGAIN) {
  200. //goto accept_done;
  201. return;
  202. }
  203. else {
  204. io->error = socket_errno();
  205. perror("accept");
  206. goto accept_error;
  207. }
  208. }
  209. addrlen = sizeof(sockaddr_u);
  210. getsockname(connfd, io->localaddr, &addrlen);
  211. connio = hio_get(io->loop, connfd);
  212. // NOTE: inherit from listenio
  213. connio->accept_cb = io->accept_cb;
  214. connio->userdata = io->userdata;
  215. if (io->io_type == HIO_TYPE_SSL) {
  216. hssl_ctx_t ssl_ctx = hssl_ctx_instance();
  217. if (ssl_ctx == NULL) {
  218. goto accept_error;
  219. }
  220. hssl_t ssl = hssl_new(ssl_ctx, connfd);
  221. if (ssl == NULL) {
  222. goto accept_error;
  223. }
  224. hio_enable_ssl(connio);
  225. connio->ssl = ssl;
  226. ssl_server_handshark(connio);
  227. }
  228. else {
  229. // NOTE: SSL call accept_cb after handshark finished
  230. __accept_cb(connio);
  231. }
  232. goto accept;
  233. accept_error:
  234. hio_close(io);
  235. }
  236. static void nio_connect(hio_t* io) {
  237. //printd("nio_connect connfd=%d\n", io->fd);
  238. socklen_t addrlen = sizeof(sockaddr_u);
  239. int ret = getpeername(io->fd, io->peeraddr, &addrlen);
  240. if (ret < 0) {
  241. io->error = socket_errno();
  242. printd("connect failed: %s: %d\n", strerror(socket_errno()), socket_errno());
  243. goto connect_failed;
  244. }
  245. else {
  246. addrlen = sizeof(sockaddr_u);
  247. getsockname(io->fd, io->localaddr, &addrlen);
  248. if (io->io_type == HIO_TYPE_SSL) {
  249. hssl_ctx_t ssl_ctx = hssl_ctx_instance();
  250. if (ssl_ctx == NULL) {
  251. goto connect_failed;
  252. }
  253. hssl_t ssl = hssl_new(ssl_ctx, io->fd);
  254. if (ssl == NULL) {
  255. goto connect_failed;
  256. }
  257. io->ssl = ssl;
  258. ssl_client_handshark(io);
  259. }
  260. else {
  261. // NOTE: SSL call connect_cb after handshark finished
  262. __connect_cb(io);
  263. }
  264. return;
  265. }
  266. connect_failed:
  267. hio_close(io);
  268. }
  269. static int __nio_read(hio_t* io, void* buf, int len) {
  270. int nread = 0;
  271. switch (io->io_type) {
  272. case HIO_TYPE_SSL:
  273. nread = hssl_read(io->ssl, buf, len);
  274. break;
  275. case HIO_TYPE_TCP:
  276. #ifdef OS_UNIX
  277. nread = read(io->fd, buf, len);
  278. #else
  279. nread = recv(io->fd, buf, len, 0);
  280. #endif
  281. break;
  282. case HIO_TYPE_UDP:
  283. case HIO_TYPE_IP:
  284. {
  285. socklen_t addrlen = sizeof(sockaddr_u);
  286. nread = recvfrom(io->fd, buf, len, 0, io->peeraddr, &addrlen);
  287. }
  288. break;
  289. default:
  290. nread = read(io->fd, buf, len);
  291. break;
  292. }
  293. return nread;
  294. }
  295. static int __nio_write(hio_t* io, const void* buf, int len) {
  296. int nwrite = 0;
  297. switch (io->io_type) {
  298. case HIO_TYPE_SSL:
  299. nwrite = hssl_write(io->ssl, buf, len);
  300. break;
  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. io->accept = 1;
  424. hio_add(io, hio_handle_events, HV_READ);
  425. return 0;
  426. }
  427. int hio_connect(hio_t* io) {
  428. int ret = connect(io->fd, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
  429. #ifdef OS_WIN
  430. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  431. #else
  432. if (ret < 0 && socket_errno() != EINPROGRESS) {
  433. #endif
  434. perror("connect");
  435. hio_close(io);
  436. return ret;
  437. }
  438. if (ret == 0) {
  439. // connect ok
  440. __connect_cb(io);
  441. return 0;
  442. }
  443. int timeout = io->connect_timeout ? io->connect_timeout : HIO_DEFAULT_CONNECT_TIMEOUT;
  444. io->connect_timer = htimer_add(io->loop, __connect_timeout_cb, timeout, 1);
  445. io->connect_timer->privdata = io;
  446. io->connect = 1;
  447. return hio_add(io, hio_handle_events, HV_WRITE);
  448. }
  449. int hio_read (hio_t* io) {
  450. return hio_add(io, hio_handle_events, HV_READ);
  451. }
  452. int hio_write (hio_t* io, const void* buf, size_t len) {
  453. int nwrite = 0;
  454. if (write_queue_empty(&io->write_queue)) {
  455. try_write:
  456. nwrite = __nio_write(io, buf, len);
  457. //printd("write retval=%d\n", nwrite);
  458. if (nwrite < 0) {
  459. if (socket_errno() == EAGAIN) {
  460. nwrite = 0;
  461. hlogw("try_write failed, enqueue!");
  462. goto enqueue;
  463. }
  464. else {
  465. // perror("write");
  466. io->error = socket_errno();
  467. goto write_error;
  468. }
  469. }
  470. if (nwrite == 0) {
  471. goto disconnect;
  472. }
  473. __write_cb(io, buf, nwrite);
  474. if (nwrite == len) {
  475. //goto write_done;
  476. return nwrite;
  477. }
  478. enqueue:
  479. hio_add(io, hio_handle_events, HV_WRITE);
  480. }
  481. if (nwrite < len) {
  482. offset_buf_t rest;
  483. rest.len = len;
  484. rest.offset = nwrite;
  485. // NOTE: free in nio_write
  486. HV_ALLOC(rest.base, rest.len);
  487. memcpy(rest.base, (char*)buf, rest.len);
  488. if (io->write_queue.maxsize == 0) {
  489. write_queue_init(&io->write_queue, 4);
  490. }
  491. write_queue_push_back(&io->write_queue, &rest);
  492. }
  493. return nwrite;
  494. write_error:
  495. disconnect:
  496. hio_close(io);
  497. return nwrite;
  498. }
  499. int hio_close (hio_t* io) {
  500. if (io->closed) return 0;
  501. if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
  502. io->close = 1;
  503. hlogw("write_queue not empty, close later.");
  504. int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
  505. io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
  506. io->close_timer->privdata = io;
  507. return 0;
  508. }
  509. io->closed = 1;
  510. hio_del(io, HV_RDWR);
  511. __close_cb(io);
  512. if (io->ssl) {
  513. hssl_free(io->ssl);
  514. io->ssl = NULL;
  515. }
  516. if (io->io_type & HIO_TYPE_SOCKET) {
  517. closesocket(io->fd);
  518. }
  519. return 0;
  520. }
  521. #endif