2
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
4
* This is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This software is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this software; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21
* listen.c - listen for incoming connections
24
#ifdef __STRICT_ANSI__
28
#include <sys/types.h>
30
#define close closesocket
34
#include <sys/utsname.h>
37
#include <rfb/rfbclient.h>
40
* listenForIncomingConnections() - listen for incoming connections from
41
* servers, and fork a new process to deal with each connection.
45
listenForIncomingConnections(rfbClient* client)
49
rfbClientErr("listenForIncomingConnections on MinGW32 NOT IMPLEMENTED\n");
55
client->listenSpecified = TRUE;
57
listenSocket = ListenAtTcpPort(client->listenPort);
59
if ((listenSocket < 0))
62
rfbClientLog("%s -listen: Listening on port %d\n",
63
client->programName,client->listenPort);
64
rfbClientLog("%s -listen: Command line errors are not reported until "
65
"a connection comes in.\n", client->programName);
69
/* reap any zombies */
71
while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
73
/* TODO: callback for discard any events (like X11 events) */
77
FD_SET(listenSocket, &fds);
79
select(listenSocket+1, &fds, NULL, NULL, NULL);
81
if (FD_ISSET(listenSocket, &fds)) {
82
client->sock = AcceptTcpConnection(listenSocket);
85
if (!SetNonBlocking(client->sock))
88
/* Now fork off a new process to deal with it... */
93
rfbClientErr("fork\n");
97
/* child - return to caller */
102
/* parent - go round and listen again */
114
* listenForIncomingConnectionsNoFork() - listen for incoming connections
115
* from servers, but DON'T fork, instead just wait timeout microseconds.
116
* If timeout is negative, block indefinitly.
117
* Returns 1 on success (there was an incoming connection on the listen socket
118
* and we accepted it successfully), -1 on error, 0 on timeout.
122
listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
128
to.tv_sec= timeout / 1000000;
129
to.tv_usec= timeout % 1000000;
131
client->listenSpecified = TRUE;
133
if (client->listenSock < 0)
135
client->listenSock = ListenAtTcpPort(client->listenPort);
137
if (client->listenSock < 0)
140
rfbClientLog("%s -listennofork: Listening on port %d\n",
141
client->programName,client->listenPort);
142
rfbClientLog("%s -listennofork: Command line errors are not reported until "
143
"a connection comes in.\n", client->programName);
148
FD_SET(client->listenSock, &fds);
151
r = select(client->listenSock+1, &fds, NULL, NULL, NULL);
153
r = select(client->listenSock+1, &fds, NULL, NULL, &to);
157
client->sock = AcceptTcpConnection(client->listenSock);
158
if (client->sock < 0)
160
if (!SetNonBlocking(client->sock))
163
close(client->listenSock);
167
/* r is now either 0 (timeout) or -1 (error) */