Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Constantin Kaplinsky | 2c01983 | 2008-05-30 11:02:04 +0000 | [diff] [blame] | 2 | * Copyright (C) 2004-2008 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 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 | // FIXME: Check cases when screen width/height is not a multiply of 32. |
| 21 | // e.g. 800x600. |
| 22 | |
| 23 | #include <strings.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <rfb/Logger_stdio.h> |
| 29 | #include <rfb/LogWriter.h> |
| 30 | #include <rfb/VNCServerST.h> |
| 31 | #include <rfb/Configuration.h> |
| 32 | #include <rfb/SSecurityFactoryStandard.h> |
| 33 | #include <rfb/Timer.h> |
| 34 | #include <network/TcpSocket.h> |
| 35 | #include <tx/TXWindow.h> |
| 36 | |
Constantin Kaplinsky | a3b60c4 | 2006-06-02 04:07:49 +0000 | [diff] [blame] | 37 | #include <vncconfig/QueryConnectDialog.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 38 | |
| 39 | #include <signal.h> |
| 40 | #include <X11/X.h> |
| 41 | #include <X11/Xlib.h> |
| 42 | #include <X11/Xutil.h> |
| 43 | #ifdef HAVE_XTEST |
| 44 | #include <X11/extensions/XTest.h> |
| 45 | #endif |
| 46 | |
| 47 | #include <x0vncserver/Geometry.h> |
| 48 | #include <x0vncserver/Image.h> |
Constantin Kaplinsky | 614c7b5 | 2007-12-26 18:17:09 +0000 | [diff] [blame] | 49 | #include <x0vncserver/XPixelBuffer.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 50 | #include <x0vncserver/PollingScheduler.h> |
| 51 | |
| 52 | // XXX Lynx/OS 2.3: protos for select(), bzero() |
| 53 | #ifdef Lynx |
| 54 | #include <sys/proto.h> |
| 55 | #endif |
| 56 | |
Constantin Kaplinsky | b963270 | 2006-12-01 10:54:55 +0000 | [diff] [blame] | 57 | extern char buildtime[]; |
| 58 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 59 | using namespace rfb; |
| 60 | using namespace network; |
| 61 | |
| 62 | static LogWriter vlog("Main"); |
| 63 | |
| 64 | IntParameter pollingCycle("PollingCycle", "Milliseconds per one polling " |
| 65 | "cycle; actual interval may be dynamically " |
| 66 | "adjusted to satisfy MaxProcessorUsage setting", 30); |
| 67 | IntParameter maxProcessorUsage("MaxProcessorUsage", "Maximum percentage of " |
| 68 | "CPU time to be consumed", 35); |
| 69 | BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true); |
| 70 | BoolParameter useOverlay("OverlayMode", "Use overlay mode under " |
| 71 | "IRIX or Solaris", true); |
| 72 | StringParameter displayname("display", "The X display", ""); |
| 73 | IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",5900); |
| 74 | IntParameter queryConnectTimeout("QueryConnectTimeout", |
| 75 | "Number of seconds to show the Accept Connection dialog before " |
| 76 | "rejecting the connection", |
| 77 | 10); |
| 78 | StringParameter hostsFile("HostsFile", "File with IP access control rules", ""); |
| 79 | |
| 80 | // |
| 81 | // Allow the main loop terminate itself gracefully on receiving a signal. |
| 82 | // |
| 83 | |
| 84 | static bool caughtSignal = false; |
| 85 | |
| 86 | static void CleanupSignalHandler(int sig) |
| 87 | { |
| 88 | caughtSignal = true; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | class QueryConnHandler : public VNCServerST::QueryConnectionHandler, |
| 93 | public QueryResultCallback { |
| 94 | public: |
| 95 | QueryConnHandler(Display* dpy, VNCServerST* vs) |
| 96 | : display(dpy), server(vs), queryConnectDialog(0), queryConnectSock(0) {} |
| 97 | ~QueryConnHandler() { delete queryConnectDialog; } |
| 98 | |
| 99 | // -=- VNCServerST::QueryConnectionHandler interface |
| 100 | virtual VNCServerST::queryResult queryConnection(network::Socket* sock, |
| 101 | const char* userName, |
| 102 | char** reason) { |
| 103 | if (queryConnectSock) { |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 104 | *reason = strDup("Another connection is currently being queried."); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 105 | return VNCServerST::REJECT; |
| 106 | } |
| 107 | if (!userName) userName = "(anonymous)"; |
| 108 | queryConnectSock = sock; |
| 109 | CharArray address(sock->getPeerAddress()); |
| 110 | delete queryConnectDialog; |
| 111 | queryConnectDialog = new QueryConnectDialog(display, address.buf, |
| 112 | userName, queryConnectTimeout, |
| 113 | this); |
| 114 | queryConnectDialog->map(); |
| 115 | return VNCServerST::PENDING; |
| 116 | } |
| 117 | |
| 118 | // -=- QueryResultCallback interface |
| 119 | virtual void queryApproved() { |
| 120 | server->approveConnection(queryConnectSock, true, 0); |
| 121 | queryConnectSock = 0; |
| 122 | } |
| 123 | virtual void queryRejected() { |
| 124 | server->approveConnection(queryConnectSock, false, |
| 125 | "Connection rejected by local user"); |
| 126 | queryConnectSock = 0; |
| 127 | } |
| 128 | private: |
| 129 | Display* display; |
| 130 | VNCServerST* server; |
| 131 | QueryConnectDialog* queryConnectDialog; |
| 132 | network::Socket* queryConnectSock; |
| 133 | }; |
| 134 | |
| 135 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 136 | class XDesktop : public SDesktop, public ColourMap |
| 137 | { |
| 138 | public: |
| 139 | XDesktop(Display* dpy_, Geometry *geometry_) |
Constantin Kaplinsky | 303433a | 2008-06-04 05:57:06 +0000 | [diff] [blame] | 140 | : dpy(dpy_), geometry(geometry_), pb(0), server(0), |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 141 | oldButtonMask(0), haveXtest(false), maxButtons(0), running(false) |
| 142 | { |
| 143 | #ifdef HAVE_XTEST |
| 144 | int xtestEventBase; |
| 145 | int xtestErrorBase; |
| 146 | int major, minor; |
| 147 | |
| 148 | if (XTestQueryExtension(dpy, &xtestEventBase, |
| 149 | &xtestErrorBase, &major, &minor)) { |
| 150 | XTestGrabControl(dpy, True); |
| 151 | vlog.info("XTest extension present - version %d.%d",major,minor); |
| 152 | haveXtest = true; |
| 153 | } else { |
| 154 | #endif |
| 155 | vlog.info("XTest extension not present"); |
| 156 | vlog.info("Unable to inject events or display while server is grabbed"); |
| 157 | #ifdef HAVE_XTEST |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | } |
| 162 | virtual ~XDesktop() { |
| 163 | stop(); |
| 164 | } |
| 165 | |
Constantin Kaplinsky | dc873dc | 2008-06-04 09:46:57 +0000 | [diff] [blame] | 166 | inline void poll() { |
| 167 | if (pb) |
| 168 | pb->poll(server); |
| 169 | } |
| 170 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 171 | // -=- SDesktop interface |
| 172 | |
| 173 | virtual void start(VNCServer* vs) { |
| 174 | |
| 175 | // Determine actual number of buttons of the X pointer device. |
| 176 | unsigned char btnMap[8]; |
| 177 | int numButtons = XGetPointerMapping(dpy, btnMap, 8); |
| 178 | maxButtons = (numButtons > 8) ? 8 : numButtons; |
| 179 | vlog.info("Enabling %d button%s of X pointer device", |
| 180 | maxButtons, (maxButtons != 1) ? "s" : ""); |
| 181 | |
Constantin Kaplinsky | e0c80c5 | 2008-06-04 03:10:05 +0000 | [diff] [blame] | 182 | // Create an ImageFactory instance for producing Image objects. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 183 | ImageFactory factory((bool)useShm, (bool)useOverlay); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 184 | |
Constantin Kaplinsky | dc873dc | 2008-06-04 09:46:57 +0000 | [diff] [blame] | 185 | // Create pixel buffer and provide it to the server object. |
Constantin Kaplinsky | f773a8e | 2008-06-04 04:30:10 +0000 | [diff] [blame] | 186 | pb = new XPixelBuffer(dpy, factory, geometry->getRect(), this); |
Constantin Kaplinsky | e0c80c5 | 2008-06-04 03:10:05 +0000 | [diff] [blame] | 187 | vlog.info("Allocated %s", pb->getImage()->classDesc()); |
| 188 | |
Constantin Kaplinsky | c341ac6 | 2008-08-21 03:35:08 +0000 | [diff] [blame] | 189 | server = (VNCServerST *)vs; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 190 | server->setPixelBuffer(pb); |
| 191 | |
| 192 | running = true; |
| 193 | } |
| 194 | |
| 195 | virtual void stop() { |
| 196 | running = false; |
| 197 | |
| 198 | delete pb; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 199 | pb = 0; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | inline bool isRunning() { |
| 203 | return running; |
| 204 | } |
| 205 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 206 | virtual void pointerEvent(const Point& pos, int buttonMask) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 207 | #ifdef HAVE_XTEST |
| 208 | if (!haveXtest) return; |
| 209 | XTestFakeMotionEvent(dpy, DefaultScreen(dpy), |
| 210 | geometry->offsetLeft() + pos.x, |
| 211 | geometry->offsetTop() + pos.y, |
| 212 | CurrentTime); |
| 213 | if (buttonMask != oldButtonMask) { |
| 214 | for (int i = 0; i < maxButtons; i++) { |
| 215 | if ((buttonMask ^ oldButtonMask) & (1<<i)) { |
| 216 | if (buttonMask & (1<<i)) { |
| 217 | XTestFakeButtonEvent(dpy, i+1, True, CurrentTime); |
| 218 | } else { |
| 219 | XTestFakeButtonEvent(dpy, i+1, False, CurrentTime); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | oldButtonMask = buttonMask; |
| 225 | #endif |
| 226 | } |
| 227 | |
| 228 | virtual void keyEvent(rdr::U32 key, bool down) { |
| 229 | #ifdef HAVE_XTEST |
| 230 | if (!haveXtest) return; |
| 231 | int keycode = XKeysymToKeycode(dpy, key); |
| 232 | if (keycode) |
| 233 | XTestFakeKeyEvent(dpy, keycode, down, CurrentTime); |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | virtual void clientCutText(const char* str, int len) { |
| 238 | } |
| 239 | |
| 240 | virtual Point getFbSize() { |
| 241 | return Point(pb->width(), pb->height()); |
| 242 | } |
| 243 | |
| 244 | // -=- ColourMap callbacks |
| 245 | virtual void lookup(int index, int* r, int* g, int* b) { |
| 246 | XColor xc; |
| 247 | xc.pixel = index; |
| 248 | if (index < DisplayCells(dpy,DefaultScreen(dpy))) { |
| 249 | XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc); |
| 250 | } else { |
| 251 | xc.red = xc.green = xc.blue = 0; |
| 252 | } |
| 253 | *r = xc.red; |
| 254 | *g = xc.green; |
| 255 | *b = xc.blue; |
| 256 | } |
| 257 | |
| 258 | protected: |
| 259 | Display* dpy; |
| 260 | Geometry* geometry; |
Constantin Kaplinsky | 2c01983 | 2008-05-30 11:02:04 +0000 | [diff] [blame] | 261 | XPixelBuffer* pb; |
Constantin Kaplinsky | c341ac6 | 2008-08-21 03:35:08 +0000 | [diff] [blame] | 262 | VNCServerST* server; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 263 | int oldButtonMask; |
| 264 | bool haveXtest; |
| 265 | int maxButtons; |
| 266 | bool running; |
| 267 | }; |
| 268 | |
| 269 | |
| 270 | class FileTcpFilter : public TcpFilter |
| 271 | { |
| 272 | |
| 273 | public: |
| 274 | |
| 275 | FileTcpFilter(const char *fname) |
| 276 | : TcpFilter("-"), fileName(NULL), lastModTime(0) |
| 277 | { |
| 278 | if (fname != NULL) |
| 279 | fileName = strdup((char *)fname); |
| 280 | } |
| 281 | |
| 282 | virtual ~FileTcpFilter() |
| 283 | { |
| 284 | if (fileName != NULL) |
| 285 | free(fileName); |
| 286 | } |
| 287 | |
| 288 | virtual bool verifyConnection(Socket* s) |
| 289 | { |
| 290 | if (!reloadRules()) { |
| 291 | vlog.error("Could not read IP filtering rules: rejecting all clients"); |
| 292 | filter.clear(); |
| 293 | filter.push_back(parsePattern("-")); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return TcpFilter::verifyConnection(s); |
| 298 | } |
| 299 | |
| 300 | protected: |
| 301 | |
| 302 | bool reloadRules() |
| 303 | { |
| 304 | if (fileName == NULL) |
| 305 | return true; |
| 306 | |
| 307 | struct stat st; |
| 308 | if (stat(fileName, &st) != 0) |
| 309 | return false; |
| 310 | |
| 311 | if (st.st_mtime != lastModTime) { |
| 312 | // Actually reload only if the file was modified |
| 313 | FILE *fp = fopen(fileName, "r"); |
| 314 | if (fp == NULL) |
| 315 | return false; |
| 316 | |
| 317 | // Remove all the rules from the parent class |
| 318 | filter.clear(); |
| 319 | |
| 320 | // Parse the file contents adding rules to the parent class |
| 321 | char buf[32]; |
| 322 | while (readLine(buf, 32, fp)) { |
| 323 | if (buf[0] && strchr("+-?", buf[0])) { |
| 324 | filter.push_back(parsePattern(buf)); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | fclose(fp); |
| 329 | lastModTime = st.st_mtime; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | protected: |
| 335 | |
| 336 | char *fileName; |
| 337 | time_t lastModTime; |
| 338 | |
| 339 | private: |
| 340 | |
| 341 | // |
| 342 | // NOTE: we silently truncate long lines in this function. |
| 343 | // |
| 344 | |
| 345 | bool readLine(char *buf, int bufSize, FILE *fp) |
| 346 | { |
| 347 | if (fp == NULL || buf == NULL || bufSize == 0) |
| 348 | return false; |
| 349 | |
| 350 | if (fgets(buf, bufSize, fp) == NULL) |
| 351 | return false; |
| 352 | |
| 353 | char *ptr = strchr(buf, '\n'); |
| 354 | if (ptr != NULL) { |
| 355 | *ptr = '\0'; // remove newline at the end |
| 356 | } else { |
| 357 | if (!feof(fp)) { |
| 358 | int c; |
| 359 | do { // skip the rest of a long line |
| 360 | c = getc(fp); |
| 361 | } while (c != '\n' && c != EOF); |
| 362 | } |
| 363 | } |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | }; |
| 368 | |
| 369 | char* programName; |
| 370 | |
Constantin Kaplinsky | 2039d7b | 2008-06-04 10:43:10 +0000 | [diff] [blame] | 371 | static void printVersion(FILE *fp) |
| 372 | { |
Peter Ã…strand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 373 | fprintf(fp, "TigerVNC Server version %s, built %s\n", |
Constantin Kaplinsky | ea7b650 | 2008-09-28 05:08:48 +0000 | [diff] [blame] | 374 | PACKAGE_VERSION, buildtime); |
Constantin Kaplinsky | 2039d7b | 2008-06-04 10:43:10 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 377 | static void usage() |
| 378 | { |
Constantin Kaplinsky | 2039d7b | 2008-06-04 10:43:10 +0000 | [diff] [blame] | 379 | printVersion(stderr); |
| 380 | fprintf(stderr, "\nUsage: %s [<parameters>]\n", programName); |
| 381 | fprintf(stderr, " %s --version\n", programName); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 382 | fprintf(stderr,"\n" |
| 383 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 384 | "Parameters which take a value can be specified as " |
| 385 | "-<param> <value>\n" |
| 386 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 387 | "--<param>=<value>\n" |
| 388 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 389 | Configuration::listParams(79, 14); |
| 390 | exit(1); |
| 391 | } |
| 392 | |
| 393 | int main(int argc, char** argv) |
| 394 | { |
| 395 | initStdIOLoggers(); |
| 396 | LogWriter::setLogParams("*:stderr:30"); |
| 397 | |
| 398 | programName = argv[0]; |
| 399 | Display* dpy; |
| 400 | |
| 401 | for (int i = 1; i < argc; i++) { |
| 402 | if (Configuration::setParam(argv[i])) |
| 403 | continue; |
| 404 | |
| 405 | if (argv[i][0] == '-') { |
| 406 | if (i+1 < argc) { |
| 407 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 408 | i++; |
| 409 | continue; |
| 410 | } |
| 411 | } |
Constantin Kaplinsky | 2039d7b | 2008-06-04 10:43:10 +0000 | [diff] [blame] | 412 | if (strcmp(argv[i], "-v") == 0 || |
| 413 | strcmp(argv[i], "-version") == 0 || |
| 414 | strcmp(argv[i], "--version") == 0) { |
| 415 | printVersion(stdout); |
| 416 | return 0; |
| 417 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 418 | usage(); |
| 419 | } |
| 420 | |
| 421 | usage(); |
| 422 | } |
| 423 | |
| 424 | CharArray dpyStr(displayname.getData()); |
| 425 | if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) { |
Constantin Kaplinsky | 7bdccd7 | 2008-08-20 06:22:28 +0000 | [diff] [blame] | 426 | // FIXME: Why not vlog.error(...)? |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 427 | fprintf(stderr,"%s: unable to open display \"%s\"\r\n", |
Constantin Kaplinsky | 7bdccd7 | 2008-08-20 06:22:28 +0000 | [diff] [blame] | 428 | programName, XDisplayName(dpyStr.buf)); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 429 | exit(1); |
| 430 | } |
| 431 | |
| 432 | signal(SIGHUP, CleanupSignalHandler); |
| 433 | signal(SIGINT, CleanupSignalHandler); |
| 434 | signal(SIGTERM, CleanupSignalHandler); |
| 435 | |
| 436 | try { |
| 437 | TXWindow::init(dpy,"x0vncserver"); |
| 438 | Geometry geo(DisplayWidth(dpy, DefaultScreen(dpy)), |
| 439 | DisplayHeight(dpy, DefaultScreen(dpy))); |
Constantin Kaplinsky | 23c6022 | 2008-06-04 03:58:07 +0000 | [diff] [blame] | 440 | if (geo.getRect().is_empty()) { |
| 441 | vlog.error("Exiting with error"); |
| 442 | return 1; |
| 443 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 444 | XDesktop desktop(dpy, &geo); |
Constantin Kaplinsky | 8232831 | 2008-04-24 08:44:24 +0000 | [diff] [blame] | 445 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 446 | VNCServerST server("x0vncserver", &desktop); |
| 447 | QueryConnHandler qcHandler(dpy, &server); |
| 448 | server.setQueryConnectionHandler(&qcHandler); |
| 449 | |
| 450 | TcpListener listener((int)rfbport); |
| 451 | vlog.info("Listening on port %d", (int)rfbport); |
| 452 | |
Constantin Kaplinsky | 7bdccd7 | 2008-08-20 06:22:28 +0000 | [diff] [blame] | 453 | const char *hostsData = hostsFile.getData(); |
| 454 | FileTcpFilter fileTcpFilter(hostsData); |
| 455 | if (strlen(hostsData) != 0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 456 | listener.setFilter(&fileTcpFilter); |
Constantin Kaplinsky | 7bdccd7 | 2008-08-20 06:22:28 +0000 | [diff] [blame] | 457 | delete[] hostsData; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 458 | |
| 459 | PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage); |
| 460 | |
| 461 | while (!caughtSignal) { |
| 462 | struct timeval tv; |
| 463 | fd_set rfds; |
| 464 | std::list<Socket*> sockets; |
| 465 | std::list<Socket*>::iterator i; |
| 466 | |
| 467 | // Process any incoming X events |
| 468 | TXWindow::handleXEvents(dpy); |
| 469 | |
| 470 | FD_ZERO(&rfds); |
| 471 | FD_SET(listener.getFd(), &rfds); |
| 472 | server.getSockets(&sockets); |
| 473 | int clients_connected = 0; |
| 474 | for (i = sockets.begin(); i != sockets.end(); i++) { |
| 475 | if ((*i)->isShutdown()) { |
| 476 | server.removeSocket(*i); |
| 477 | delete (*i); |
| 478 | } else { |
| 479 | FD_SET((*i)->getFd(), &rfds); |
| 480 | clients_connected++; |
| 481 | } |
| 482 | } |
| 483 | |
Constantin Kaplinsky | 813dbb4 | 2006-12-05 08:03:18 +0000 | [diff] [blame] | 484 | if (!clients_connected) |
| 485 | sched.reset(); |
| 486 | |
| 487 | if (sched.isRunning()) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 488 | int wait_ms = sched.millisRemaining(); |
| 489 | if (wait_ms > 500) { |
| 490 | wait_ms = 500; |
| 491 | } |
| 492 | tv.tv_usec = wait_ms * 1000; |
| 493 | #ifdef DEBUG |
| 494 | // fprintf(stderr, "[%d]\t", wait_ms); |
| 495 | #endif |
| 496 | } else { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 497 | tv.tv_usec = 100000; |
| 498 | } |
| 499 | tv.tv_sec = 0; |
| 500 | |
| 501 | // Do the wait... |
| 502 | sched.sleepStarted(); |
| 503 | int n = select(FD_SETSIZE, &rfds, 0, 0, &tv); |
| 504 | sched.sleepFinished(); |
| 505 | |
| 506 | if (n < 0) { |
| 507 | if (errno == EINTR) { |
| 508 | vlog.debug("Interrupted select() system call"); |
| 509 | continue; |
| 510 | } else { |
| 511 | throw rdr::SystemException("select", errno); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Accept new VNC connections |
| 516 | if (FD_ISSET(listener.getFd(), &rfds)) { |
| 517 | Socket* sock = listener.accept(); |
| 518 | if (sock) { |
| 519 | server.addSocket(sock); |
| 520 | } else { |
| 521 | vlog.status("Client connection rejected"); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | Timer::checkTimeouts(); |
| 526 | server.checkTimeouts(); |
| 527 | |
| 528 | // Client list could have been changed. |
| 529 | server.getSockets(&sockets); |
| 530 | |
| 531 | // Nothing more to do if there are no client connections. |
| 532 | if (sockets.empty()) |
| 533 | continue; |
| 534 | |
| 535 | // Process events on existing VNC connections |
| 536 | for (i = sockets.begin(); i != sockets.end(); i++) { |
| 537 | if (FD_ISSET((*i)->getFd(), &rfds)) |
| 538 | server.processSocketEvent(*i); |
| 539 | } |
| 540 | |
| 541 | if (desktop.isRunning() && sched.goodTimeToPoll()) { |
| 542 | sched.newPass(); |
| 543 | desktop.poll(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | } catch (rdr::Exception &e) { |
| 548 | vlog.error(e.str()); |
| 549 | return 1; |
| 550 | } |
| 551 | |
Constantin Kaplinsky | 0c4306c | 2008-09-05 07:13:55 +0000 | [diff] [blame] | 552 | TXWindow::handleXEvents(dpy); |
| 553 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 554 | vlog.info("Terminated"); |
| 555 | return 0; |
| 556 | } |