在select()调用中被阻止时建立新连接

时间:2013-10-06 17:48:01

标签: multithreading sockets select networking tcp

我有2个帖子:

线程A: 这是 select()循环。执行读取操作的套接字处理,例如接受新连接,接收数据。

while (1) {

    FD_ZERO(&fdReadSet);

    numActiveSockets = 0;

    for (std::unordered_map<SOCKET, TcpSocket*>::iterator it = m_sock_table.begin(); it != m_sock_table.end(); it++)
    {
        numActiveSockets++;
        FD_SET(it->first, &fdReadSet);
    }

    int ret;
    bool hasListen = false;

    if (( ret = select(numActiveSockets, &fdReadSet, NULL, NULL, NULL)) == SOCKET_ERROR) {
        printf("Select Failed, Error code = %d\n", WSAGetLastError());
        return -1;
    }

    for (std::unordered_map<SOCKET, TcpSocket*>::iterator it = m_sock_table.begin(); it != m_sock_table.end(); it++)
    {
        if (FD_ISSET(it->first, &fdReadSet))
        {
            if (it->first == TcpSocket::m_listen_sock)
            {
                if (!hasListen)
                {
                    sockaddr_in sock_addr;
                    int sockLength = sizeof(sock_addr);

                    SOCKET sock = accept(it->first, (sockaddr *) &sock_addr, &sockLength);
                    TcpSocket * socket = new TcpSocket();
                    socket->m_sock = sock;
                    m_sock_table[sock] = socket;

                    it = m_sock_table.begin();
                    hasListen = true;
                }
            }
            else
            {
                char * buffer = it->second->GetWriteBuffer();
                int numRead = recv(it->first, buffer, SOCKET_BUFFER_SIZE, 0);

                if (numRead == SOCKET_ERROR)
                {
                    int err = WSAGetLastError();
                    if (err == WSAECONNRESET)
                    {
                        printf("Connection [%i]: RESET Received. Closing Socket\n", it->first);
                        closesocket(it->first);
                        it = socketVector.erase(it->first);  // iterator invalidated after erase
                    }
                    else
                    {
                        printf("Recv Failed. Error code = %d\n", err);
                        return -1;
                    }
                }
                else if (numRead == 0)//connection close
                { 
                    printf("Connection [%i]: Graceful exit. Closing Socket\n", it->first);
                    closesocket(it->first);
                    it = socketVector.erase(it->first);  // iterator invalidated after erase

                }
                else {
                    /* Process received data */

                }

            }
        }
    }
}

主题B: 允许应用程序执行 connect()以建立新连接。如果 connect()成功,则会将返回的套接字添加到 m_sock_table

我有一个名为 m_sock_table 的套接字表,它包含所有套接字。我使用 m_sock_table 来初始化要在 select()中使用的 fdReadSet

-----------问题-----------------

如果线程A被 select()阻止,同时线程B通过 connect()建立新连接,则应用程序将无法执行接收来自新连接的数据,因为 fdReadset 尚未使用新连接的套接字更新

什么是解决这个问题的好方法?或者从一开始设计就错了?

1 个答案:

答案 0 :(得分:1)

您可以使用除中断系统调用之外不执行任何操作的信号:

#include <signal.h>
void do_nothing() { }

struct sigaction sa;
sa.sa_handler = do_nothing;
sigemptyset(sa.sa_mask);
#ifdef SA_INTERRUPT
sa.sa_flags = SA_INTERRUPT;
#else
sa.sa_flags = 0;
#endif
sigaction(SIGUSR1, &sa, 0);

然后,在线程B中,在开始新连接之后,在确保线程A将处理它之后发送信号:

/* need only be done once, but needed in every thread other than A */
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, SIGUSR1)
pthread_sigmask(SIG_BLOCK, &sigs, 0);

/* each time we create a new connection */
kill(getpid, SIGUSR1);

如上所述,select将返回EINTR错误 - 所以检查并循环(将新连接添加到集合中)。