Commit 49cdfb4c authored by Christian Beier's avatar Christian Beier Committed by Johannes Schindelin

libvncclient: better return value for non-forking listen.

The return value now better reflects what has happened:
1 on success (incoming connection on listen socket, we
accepted it successfully), -1 on error, 0 on timeout.

Also change the select calls to not check _all_ possible
file descriptors.
Signed-off-by: 's avatarChristian Beier <dontmind@freeshell.org>
Signed-off-by: 's avatarJohannes Schindelin <johannes.schindelin@gmx.de>
parent 9ed41066
...@@ -76,7 +76,7 @@ listenForIncomingConnections(rfbClient* client) ...@@ -76,7 +76,7 @@ listenForIncomingConnections(rfbClient* client)
FD_SET(listenSocket, &fds); FD_SET(listenSocket, &fds);
select(FD_SETSIZE, &fds, NULL, NULL, NULL); select(listenSocket+1, &fds, NULL, NULL, NULL);
if (FD_ISSET(listenSocket, &fds)) { if (FD_ISSET(listenSocket, &fds)) {
client->sock = AcceptTcpConnection(listenSocket); client->sock = AcceptTcpConnection(listenSocket);
...@@ -114,13 +114,16 @@ listenForIncomingConnections(rfbClient* client) ...@@ -114,13 +114,16 @@ listenForIncomingConnections(rfbClient* client)
* listenForIncomingConnectionsNoFork() - listen for incoming connections * listenForIncomingConnectionsNoFork() - listen for incoming connections
* from servers, but DON'T fork, instead just wait timeout microseconds. * from servers, but DON'T fork, instead just wait timeout microseconds.
* If timeout is negative, block indefinitly. * If timeout is negative, block indefinitly.
* Returns 1 on success (there was an incoming connection on the listen socket
* and we accepted it successfully), -1 on error, 0 on timeout.
*/ */
rfbBool int
listenForIncomingConnectionsNoFork(rfbClient* client, int timeout) listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
{ {
fd_set fds; fd_set fds;
struct timeval to; struct timeval to;
int r;
to.tv_sec= timeout / 1000000; to.tv_sec= timeout / 1000000;
to.tv_usec= timeout % 1000000; to.tv_usec= timeout % 1000000;
...@@ -132,7 +135,7 @@ listenForIncomingConnectionsNoFork(rfbClient* client, int timeout) ...@@ -132,7 +135,7 @@ listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
client->listenSock = ListenAtTcpPort(client->listenPort); client->listenSock = ListenAtTcpPort(client->listenPort);
if (client->listenSock < 0) if (client->listenSock < 0)
return FALSE; return -1;
rfbClientLog("%s -listennofork: Listening on port %d\n", rfbClientLog("%s -listennofork: Listening on port %d\n",
client->programName,client->listenPort); client->programName,client->listenPort);
...@@ -145,23 +148,24 @@ listenForIncomingConnectionsNoFork(rfbClient* client, int timeout) ...@@ -145,23 +148,24 @@ listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
FD_SET(client->listenSock, &fds); FD_SET(client->listenSock, &fds);
if (timeout < 0) if (timeout < 0)
select(FD_SETSIZE, &fds, NULL, NULL, NULL); r = select(client->listenSock+1, &fds, NULL, NULL, NULL);
else else
select(FD_SETSIZE, &fds, NULL, NULL, &to); r = select(client->listenSock+1, &fds, NULL, NULL, &to);
if (FD_ISSET(client->listenSock, &fds)) if (r > 0)
{ {
client->sock = AcceptTcpConnection(client->listenSock); client->sock = AcceptTcpConnection(client->listenSock);
if (client->sock < 0) if (client->sock < 0)
return FALSE; return -1;
if (!SetNonBlocking(client->sock)) if (!SetNonBlocking(client->sock))
return FALSE; return -1;
close(client->listenSock); close(client->listenSock);
return TRUE; return r;
} }
return FALSE; /* r is now either 0 (timeout) or -1 (error) */
return r;
} }
...@@ -314,7 +314,7 @@ extern rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width ...@@ -314,7 +314,7 @@ extern rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width
/* listen.c */ /* listen.c */
extern void listenForIncomingConnections(rfbClient* viewer); extern void listenForIncomingConnections(rfbClient* viewer);
extern rfbBool listenForIncomingConnectionsNoFork(rfbClient* viewer, int usec_timeout); extern int listenForIncomingConnectionsNoFork(rfbClient* viewer, int usec_timeout);
/* rfbproto.c */ /* rfbproto.c */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment