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-861f7616d084
master
Giel van Schijndel 2009-05-03 15:49:56 +00:00 committed by Git SVN Gateway
parent 36f536fcdb
commit 63b6d37a94
1 changed files with 22 additions and 12 deletions

View File

@ -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);