net_select.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * File : lwip_select.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-05-05 Bernard First version
  23. */
  24. #include <rtthread.h>
  25. #include <sys/select.h>
  26. #ifdef RT_USING_LWIP
  27. #include "dfs_net.h"
  28. int
  29. select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  30. struct timeval *timeout)
  31. {
  32. int index, result;
  33. int sock, maxfd;
  34. fd_set sock_readset;
  35. fd_set sock_writeset;
  36. fd_set sock_exceptset;
  37. FD_ZERO(&sock_readset);
  38. FD_ZERO(&sock_writeset);
  39. FD_ZERO(&sock_exceptset);
  40. maxfd = 0;
  41. for (index = 0; index < maxfdp1; index ++)
  42. {
  43. /* convert fd to sock */
  44. sock = dfs_net_getsocket(index);
  45. if (sock == -1) continue;
  46. if (sock > maxfd) maxfd = sock;
  47. /* if FD is set, set the socket set */
  48. if (readset && FD_ISSET(index, readset))
  49. {
  50. FD_SET(sock, &sock_readset);
  51. }
  52. if (writeset && FD_ISSET(index, writeset))
  53. {
  54. FD_SET(sock, &sock_writeset);
  55. }
  56. if (exceptset && FD_ISSET(index, exceptset))
  57. {
  58. FD_SET(sock, &sock_exceptset);
  59. }
  60. }
  61. /* no socket found, return bad file descriptor */
  62. if (maxfd == 0) return -EBADF;
  63. maxfd += 1;
  64. result = lwip_select(maxfd, &sock_readset, &sock_writeset, &sock_exceptset, timeout);
  65. if (readset) FD_ZERO(readset);
  66. if (writeset) FD_ZERO(writeset);
  67. if (exceptset) FD_ZERO(exceptset);
  68. if (result != -1)
  69. {
  70. for (index = 0; index < maxfd; index ++)
  71. {
  72. /* check each socket */
  73. if ((FD_ISSET(index, &sock_readset)) ||
  74. (FD_ISSET(index, &sock_writeset)) ||
  75. (FD_ISSET(index, &sock_exceptset)))
  76. {
  77. int fd_index;
  78. /* Because we can not get the corresponding fd, we have to search it one by one */
  79. for (fd_index = 0; fd_index < maxfdp1; fd_index ++)
  80. {
  81. sock = dfs_lwip_getsocket(fd_index);
  82. if (sock == index) /* found it */
  83. {
  84. if (readset && FD_ISSET(index, &sock_readset))
  85. {
  86. FD_SET(sock, readset);
  87. }
  88. if (writeset && FD_ISSET(index, &sock_writeset))
  89. {
  90. FD_SET(sock, writeset);
  91. }
  92. if (exceptset && FD_ISSET(index, &sock_exceptset))
  93. {
  94. FD_SET(sock, exceptset);
  95. }
  96. /* end of search */
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return result;
  104. }
  105. RTM_EXPORT(select);
  106. #endif