Exemple #1
0
void net_listen_thread_entry(void *p)
{
    int listenfd;
    struct sockaddr_in saddr;
    fd_set readset;

    // check mode first.
    if( !((getWorkingMode() == TCP_SERVER) || (getWorkingMode() == TCP_AUTO)) )
        return;
    // Acquire our socket for listening for connections
    listenfd = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( listenfd == -1 )
    {
        rt_kprintf("Can not create socket!\n");
        return;
    }

    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    saddr.sin_port = htons(getListenPort());     //server port

    if (lwip_bind(listenfd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1)
    {
        lwip_close(listenfd);
        rt_kprintf("Socket bind failed!\n");
        return;
    }

    /* Put socket into listening mode */
    if (lwip_listen(listenfd, MAX_LISTEN_SOCK) == -1)
    {
        lwip_close(listenfd);
        rt_kprintf("Listen failed.\n");
        return;
    }

    /* Wait for data or a new connection */
    while(1)
    {
        /* Determine what sockets need to be in readset */
        FD_ZERO(&readset);
        FD_SET(listenfd, &readset);

        // wait forever.
        if( lwip_select(listenfd+1, &readset, 0, 0, 0) == 0 )
            continue;

        if (FD_ISSET(listenfd, &readset))
        {
            int i;
            socklen_t clilen = sizeof(struct sockaddr_in);

            for( i = 0 ; i < SOCKET_LIST_SIZE ; i++ )
            {
                // we don't block here.
                rt_err_t ret = rt_mutex_take(&(socket_list[i].mu_sock),RT_WAITING_NO);
                if( ret == -RT_ETIMEOUT )
                    continue;

                // we already took the mutex here.
                if( socket_list[i].used )
                {
                    rt_mutex_release(&(socket_list[i].mu_sock));
                    continue;
                }

                // it's an empty slot.
                socket_list[i].socket = lwip_accept(listenfd,
                        (struct sockaddr *)&(socket_list[i].cliaddr),&clilen);
                // if accept failed.
                if( socket_list[i].socket < 0 )
                {
                    rt_kprintf("Accept failed!\n");
                }
                // accept successfully.
                else
                {
                    int optval = 1;
                    // set keepalive.
                    lwip_setsockopt(socket_list[i].socket,SOL_SOCKET,SO_KEEPALIVE,&optval,sizeof(optval));
                    // set used flag.
                    socket_list[i].used = 1;
                    rt_kprintf("Accept connection.\n");
                }
                rt_mutex_release(&(socket_list[i].mu_sock));
                break;
            }

            // check if not enough slot.
            if( i == SOCKET_LIST_SIZE )
            {
                // just accept and disconnect.
                int sock;
                struct sockaddr cliaddr;
                socklen_t clilen;
                sock = lwip_accept(listenfd, &cliaddr, &clilen);
                if (sock >= 0)
                    lwip_close(sock);
                rt_kprintf("Not enough slot, accept and close connection.\n");
            }
        }
        else
        {
            rt_kprintf("ERROR,should not reach here!\n");
        }
    }
}
/*----------------------------------------------------------------------------------------
 * Purpose: show the active port or the login listener
 * Input: commandArgument - A linked list that provides all the parameters the users typed 
 * 		in. This list is in the same order as they were typed.
 * 	clientThreadArguments - A struct providing the basic address information for the 
 * 		current connection.
 * 	commandNode - A pointer to the current node in the command tree structure.
 * Output:  0 for success or 1 for failure
 * Kevin Burnett @ February 5, 2009
 * -------------------------------------------------------------------------------------*/
int cmdShowLoginListenerPort(commandArgument * ca, clientThreadArguments * client, commandNode * root) {
	int port = 0;
	port = getListenPort();
	sendMessage(client->socket, "login-listener socket is %d\n", port);
	return 0;
}