update about dialog
[presencevnc] / libvnc / libvncclient / listen.c
1 /*
2  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3  *
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.
8  *
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.
13  *
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,
17  *  USA.
18  */
19
20 /*
21  * listen.c - listen for incoming connections
22  */
23
24 #ifdef __STRICT_ANSI__
25 #define _BSD_SOURCE
26 #endif
27 #include <unistd.h>
28 #include <sys/types.h>
29 #ifdef __MINGW32__
30 #include <winsock2.h>
31 #else
32 #include <sys/wait.h>
33 #include <sys/utsname.h>
34 #endif
35 #include <sys/time.h>
36 #include <rfb/rfbclient.h>
37
38 /*
39  * listenForIncomingConnections() - listen for incoming connections from
40  * servers, and fork a new process to deal with each connection.
41  */
42
43 void
44 listenForIncomingConnections(rfbClient* client)
45 {
46 #ifdef __MINGW32__
47   /* FIXME */
48   rfbClientErr("listenForIncomingConnections on MinGW32 NOT IMPLEMENTED\n");
49   return;
50 #else
51   int listenSocket;
52   fd_set fds;
53
54   client->listenSpecified = TRUE;
55
56   listenSocket = ListenAtTcpPort(client->listenPort);
57
58   if ((listenSocket < 0))
59     return;
60
61   rfbClientLog("Listening on port %d\n",
62           client->listenPort);
63   rfbClientLog("Command line errors are not reported until "
64           "a connection comes in.\n");
65
66   while (client->listenSpecified) {
67
68     /* reap any zombies */
69     int status, pid;
70     while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
71
72     /* TODO: callback for discard any events (like X11 events) */
73
74     FD_ZERO(&fds); 
75
76     FD_SET(listenSocket, &fds);
77
78         //100ms
79     struct timeval timeout;
80     timeout.tv_sec=0;
81     timeout.tv_usec=100000;
82
83     select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
84
85     if (FD_ISSET(listenSocket, &fds)) {
86       client->sock = AcceptTcpConnection(listenSocket);
87       if (client->sock < 0)
88         return;
89       if (!SetNonBlocking(client->sock))
90         return;
91
92         //modified to accept only a single connection
93         //if something goes wrong, we can always create another listening socket
94         rfbClientLog("Accepted connection.\n");
95         break;
96
97         }
98   }
99
100   close(listenSocket);
101 #endif
102 }
103
104