Browse Source

[DFS] add select implementation.

Bernard Xiong 10 years ago
parent
commit
3aa144d0be

+ 3 - 1
components/dfs/filesystems/lwip/dfs_lwip.c

@@ -35,7 +35,9 @@ int dfs_lwip_getsocket(int fd)
     _dfs_fd = fd_get(fd);
     _dfs_fd = fd_get(fd);
     if (_dfs_fd == RT_NULL) return -1;
     if (_dfs_fd == RT_NULL) return -1;
 
 
-    return 0;
+    if (_dfs_fd->type != FT_SOCKET) return -1;
+    
+    return (int)_dfs_fd->data;
 }
 }
 
 
 int dfs_lwip_ioctl(struct dfs_fd* file, int cmd, void* args)
 int dfs_lwip_ioctl(struct dfs_fd* file, int cmd, void* args)

+ 96 - 0
components/dfs/filesystems/lwip/lwip_select.c

@@ -0,0 +1,96 @@
+#include <rtthread.h>
+#include <sys/select.h>
+
+#ifdef RT_USING_LWIP
+
+int
+select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
+            struct timeval *timeout)
+{
+    int index, result;
+    int sock, maxfd;
+
+    fd_set sock_readset;
+    fd_set sock_writeset;
+    fd_set sock_exceptset;
+
+    FD_ZERO(&sock_readset);
+    FD_ZERO(&sock_writeset);
+    FD_ZERO(&sock_exceptset);
+
+    maxfd = 0;
+    for (index = 0; index < maxfdp1; index ++)
+    {
+        if (readset && FD_ISSET(index, readset))
+        {
+            sock = dfs_lwip_getsocket(index);
+            if (sock > maxfd) maxfd = sock;
+            FD_SET(sock, &sock_readset);
+        }
+        
+        if (writeset && FD_ISSET(index, writeset))
+        {
+            sock = dfs_lwip_getsocket(index);
+            if (sock > maxfd) maxfd = sock;
+            FD_SET(sock, &sock_writeset);
+        }
+        
+        if (exceptset && FD_ISSET(index, exceptset))
+        {
+            sock = dfs_lwip_getsocket(index);
+            if (sock > maxfd) maxfd = sock;
+            FD_SET(sock, &sock_exceptset);
+        }
+    }
+
+    if (maxfd == 0) return -EBADF;
+    maxfd += 1;
+    
+    result = lwip_selscan(maxfd, &sock_readset, &sock_writeset, &sock_exceptset, timeout);
+    
+    if (readset)   FD_ZERO(readset);
+    if (writeset)  FD_ZERO(writeset);
+    if (exceptset) FD_ZERO(exceptset);
+
+    if (result != -1)
+    {
+        for (index = 0; index < maxfd; index ++)
+        {
+            /* check each socket */
+            if ((FD_ISSET(index, &sock_readset))  ||
+                (FD_ISSET(index, &sock_writeset)) ||
+                (FD_ISSET(index, &sock_exceptset)))
+            {
+                int fd_index;
+                
+                /* Because we can not get the corresponding fd, we have to search it one by one */
+                for (fd_index = 0; fd_index < maxfdp1; fd_index ++)
+                {
+                    sock = dfs_lwip_getsocket(fd_index);
+                    if (sock == index) /* found it */
+                    {
+                        if (readset && FD_ISSET(index, &sock_readset))
+                        {
+                            FD_SET(sock, readset);
+                        }
+                        if (writeset && FD_ISSET(index, &sock_writeset))
+                        {
+                            FD_SET(sock, writeset);
+                        }
+                        if (exceptset && FD_ISSET(index, &sock_exceptset))
+                        {
+                            FD_SET(sock, exceptset);
+                        }
+                        
+                        /* end of search */
+                        break;
+                    }
+                }
+            }
+        }
+    }
+    
+    return result;
+}
+
+#endif