input_windows.c 755 B

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