lwip_select.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <rtthread.h>
  2. #include <sys/select.h>
  3. #ifdef RT_USING_LWIP
  4. int
  5. select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  6. struct timeval *timeout)
  7. {
  8. int index, result;
  9. int sock, maxfd;
  10. fd_set sock_readset;
  11. fd_set sock_writeset;
  12. fd_set sock_exceptset;
  13. FD_ZERO(&sock_readset);
  14. FD_ZERO(&sock_writeset);
  15. FD_ZERO(&sock_exceptset);
  16. maxfd = 0;
  17. for (index = 0; index < maxfdp1; index ++)
  18. {
  19. if (readset && FD_ISSET(index, readset))
  20. {
  21. sock = dfs_lwip_getsocket(index);
  22. if (sock > maxfd) maxfd = sock;
  23. FD_SET(sock, &sock_readset);
  24. }
  25. if (writeset && FD_ISSET(index, writeset))
  26. {
  27. sock = dfs_lwip_getsocket(index);
  28. if (sock > maxfd) maxfd = sock;
  29. FD_SET(sock, &sock_writeset);
  30. }
  31. if (exceptset && FD_ISSET(index, exceptset))
  32. {
  33. sock = dfs_lwip_getsocket(index);
  34. if (sock > maxfd) maxfd = sock;
  35. FD_SET(sock, &sock_exceptset);
  36. }
  37. }
  38. if (maxfd == 0) return -EBADF;
  39. maxfd += 1;
  40. result = lwip_selscan(maxfd, &sock_readset, &sock_writeset, &sock_exceptset, timeout);
  41. if (readset) FD_ZERO(readset);
  42. if (writeset) FD_ZERO(writeset);
  43. if (exceptset) FD_ZERO(exceptset);
  44. if (result != -1)
  45. {
  46. for (index = 0; index < maxfd; index ++)
  47. {
  48. /* check each socket */
  49. if ((FD_ISSET(index, &sock_readset)) ||
  50. (FD_ISSET(index, &sock_writeset)) ||
  51. (FD_ISSET(index, &sock_exceptset)))
  52. {
  53. int fd_index;
  54. /* Because we can not get the corresponding fd, we have to search it one by one */
  55. for (fd_index = 0; fd_index < maxfdp1; fd_index ++)
  56. {
  57. sock = dfs_lwip_getsocket(fd_index);
  58. if (sock == index) /* found it */
  59. {
  60. if (readset && FD_ISSET(index, &sock_readset))
  61. {
  62. FD_SET(sock, readset);
  63. }
  64. if (writeset && FD_ISSET(index, &sock_writeset))
  65. {
  66. FD_SET(sock, writeset);
  67. }
  68. if (exceptset && FD_ISSET(index, &sock_exceptset))
  69. {
  70. FD_SET(sock, exceptset);
  71. }
  72. /* end of search */
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return result;
  80. }
  81. #endif