net_select.c 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * File      : lwip_select.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2015, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-05-05     Bernard      First version
 */

B
Bernard Xiong 已提交
25 26 27 28 29
#include <rtthread.h>
#include <sys/select.h>

#ifdef RT_USING_LWIP

30
#include "dfs_net.h"
B
Bernard Xiong 已提交
31

B
Bernard Xiong 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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 ++)
    {
50
        /* convert fd to sock */
51
        sock = dfs_net_getsocket(index);
52 53 54 55 56
        if (sock == -1) continue;

        if (sock > maxfd) maxfd = sock;

        /* if FD is set, set the socket set */
B
Bernard Xiong 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        if (readset && FD_ISSET(index, readset))
        {
            FD_SET(sock, &sock_readset);
        }
        if (writeset && FD_ISSET(index, writeset))
        {
            FD_SET(sock, &sock_writeset);
        }
        if (exceptset && FD_ISSET(index, exceptset))
        {
            FD_SET(sock, &sock_exceptset);
        }
    }

71
    /* no socket found, return bad file descriptor */
B
Bernard Xiong 已提交
72 73 74
    if (maxfd == 0) return -EBADF;
    maxfd += 1;
    
B
Bernard Xiong 已提交
75
    result = lwip_select(maxfd, &sock_readset, &sock_writeset, &sock_exceptset, timeout);
B
Bernard Xiong 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    
    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;
}
121
RTM_EXPORT(select);
B
Bernard Xiong 已提交
122 123

#endif