Make SocketAccept non-blocking
This way it is no longer required to first explicitly check whether connections are pending (through checkSockets, which select(2)s) and only then accept(2) it. Now you can attempt to accept the connection, and if one is available it'll get returned, NULL otherwise. git-svn-id: https://warzone2100.svn.sourceforge.net/svnroot/warzone2100/trunk@7300 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
36f536fcdb
commit
63b6d37a94
|
@ -612,7 +612,25 @@ static void socketClose(Socket* sock)
|
|||
|
||||
static Socket* socketAccept(Socket* sock)
|
||||
{
|
||||
Socket* const conn = malloc(sizeof(*conn));
|
||||
SOCKET newConn;
|
||||
Socket* conn;
|
||||
|
||||
ASSERT(sock != NULL, "NULL Socket provided");
|
||||
|
||||
newConn = accept(sock->fd, NULL, NULL);
|
||||
if (newConn == INVALID_SOCKET)
|
||||
{
|
||||
// Ignore the case where no connection is pending
|
||||
if (getSockErr() != EAGAIN
|
||||
&& getSockErr() != EWOULDBLOCK)
|
||||
{
|
||||
debug(LOG_ERROR, "accept failed: %s", strSockError(getSockErr()));
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conn = malloc(sizeof(*conn));
|
||||
if (conn == NULL)
|
||||
{
|
||||
debug(LOG_ERROR, "Out of memory!");
|
||||
|
@ -620,20 +638,11 @@ static Socket* socketAccept(Socket* sock)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ASSERT(sock != NULL, "NULL Socket provided");
|
||||
|
||||
conn->ready = false;
|
||||
conn->fd = accept(sock->fd, NULL, NULL);
|
||||
conn->fd = newConn;
|
||||
|
||||
sock->ready = false;
|
||||
|
||||
if (conn->fd == INVALID_SOCKET)
|
||||
{
|
||||
debug(LOG_ERROR, "accept failed: %s", strSockError(getSockErr()));
|
||||
socketClose(conn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
@ -780,7 +789,8 @@ static Socket* socketListen(unsigned int port)
|
|||
}
|
||||
|
||||
if (bind(conn->fd, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR
|
||||
|| listen(conn->fd, 5) == SOCKET_ERROR)
|
||||
|| listen(conn->fd, 5) == SOCKET_ERROR
|
||||
|| !setSocketBlocking(conn->fd, false))
|
||||
{
|
||||
debug(LOG_ERROR, "Failed to set up IPv4 socket for listening on port %u: %s", port, strSockError(getSockErr()));
|
||||
socketClose(conn);
|
||||
|
|
Loading…
Reference in New Issue