input_windows.c 761 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "input_windows.h"
  2. /*
  3. void init_wsa(void) {
  4. WSADATA wsa;
  5. WSAStartup(WINSOCK_VERSION, &wsa);
  6. }
  7. */
  8. int windows_read(int fd, char *buffer, int len) {
  9. struct timeval tv;
  10. fd_set rfd;
  11. retry:
  12. FD_ZERO(&rfd);
  13. FD_SET(fd, &rfd);
  14. tv.tv_sec = 0;
  15. tv.tv_usec = 200;
  16. int rs;
  17. int ret;
  18. rs = select(fd+1, &rfd, NULL, NULL, &tv);
  19. if (errno == EINTR) {
  20. goto retry;
  21. }
  22. if (rs == -1) {
  23. // error
  24. return -1;
  25. }
  26. if (rs == 0 ) {
  27. return 0;
  28. }
  29. if (FD_ISSET(fd, &rfd)) {
  30. ret = recv(fd, buffer, len, 0);
  31. if (ret == 0) {
  32. // select, recv = 0: connection closed.
  33. return -1;
  34. }
  35. return ret;
  36. }
  37. return -1;
  38. }