blob: 0008dc416982fcbd78f9ba6d80959f49dc4b7091 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01002 * Copyright 2009-2017 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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// -=- Single-Threaded VNC Server implementation
21
22
23// Note about how sockets get closed:
24//
25// Closing sockets to clients is non-trivial because the code which calls
26// VNCServerST must explicitly know about all the sockets (so that it can block
27// on them appropriately). However, VNCServerST may want to close clients for
28// a number of reasons, and from a variety of entry points. The simplest is
29// when processSocketEvent() is called for a client, and the remote end has
30// closed its socket. A more complex reason is when processSocketEvent() is
31// called for a client which has just sent a ClientInit with the shared flag
32// set to false - in this case we want to close all other clients. Yet another
33// reason for disconnecting clients is when the desktop size has changed as a
34// result of a call to setPixelBuffer().
35//
36// The responsibility for creating and deleting sockets is entirely with the
37// calling code. When VNCServerST wants to close a connection to a client it
38// calls the VNCSConnectionST's close() method which calls shutdown() on the
39// socket. Eventually the calling code will notice that the socket has been
40// shut down and call removeSocket() so that we can delete the
41// VNCSConnectionST. Note that the socket must not be deleted by the calling
42// code until after removeSocket() has been called.
43//
44// One minor complication is that we don't allocate a VNCSConnectionST object
45// for a blacklisted host (since we want to minimise the resources used for
46// dealing with such a connection). In order to properly implement the
47// getSockets function, we must maintain a separate closingSockets list,
48// otherwise blacklisted connections might be "forgotten".
49
50
Pierre Ossman559a2e82012-01-23 15:54:11 +000051#include <assert.h>
Pierre Ossmanf99c5712009-03-13 14:41:27 +000052#include <stdlib.h>
53
Pierre Ossman707fa122015-12-11 20:21:20 +010054#include <rfb/ComparingUpdateTracker.h>
55#include <rfb/KeyRemapper.h>
56#include <rfb/ListConnInfo.h>
57#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058#include <rfb/ServerCore.h>
59#include <rfb/VNCServerST.h>
60#include <rfb/VNCSConnectionST.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061#include <rfb/util.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010062#include <rfb/ledStates.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063
64#include <rdr/types.h>
65
66using namespace rfb;
67
68static LogWriter slog("VNCServerST");
69LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070
71//
72// -=- VNCServerST Implementation
73//
74
75// -=- Constructors/Destructor
76
Adam Tkaca6578bf2010-04-23 14:07:41 +000077VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000078 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010079 blockCounter(0), pb(0), ledState(ledUnknown),
Adam Tkacd36b6262009-09-04 10:57:20 +000080 name(strDup(name_)), pointerClient(0), comparer(0),
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010081 cursor(new Cursor(0, 0, Point(), NULL)),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000084 lastConnectionTime(0), disableclients(false),
Pierre Ossman6e49e952016-10-07 15:59:38 +020085 frameTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086{
87 lastUserInputTime = lastDisconnectTime = time(0);
88 slog.debug("creating single-threaded server %s", name.buf);
89}
90
91VNCServerST::~VNCServerST()
92{
93 slog.debug("shutting down server %s", name.buf);
94
95 // Close any active clients, with appropriate logging & cleanup
96 closeClients("Server shutdown");
97
Pierre Ossman6e49e952016-10-07 15:59:38 +020098 // Stop trying to render things
99 stopFrameClock();
100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 // Delete all the clients, and their sockets, and any closing sockets
102 // NB: Deleting a client implicitly removes it from the clients list
103 while (!clients.empty()) {
104 delete clients.front();
105 }
106
107 // Stop the desktop object if active, *only* after deleting all clients!
108 if (desktopStarted) {
109 desktopStarted = false;
110 desktop->stop();
111 }
112
Pierre Ossman05338bc2016-11-08 14:57:11 +0100113 if (comparer)
114 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100116
117 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118}
119
120
121// SocketServer methods
122
123void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
124{
125 // - Check the connection isn't black-marked
126 // *** do this in getSecurity instead?
127 CharArray address(sock->getPeerAddress());
128 if (blHosts->isBlackmarked(address.buf)) {
129 connectionsLog.error("blacklisted: %s", address.buf);
130 try {
131 SConnection::writeConnFailedFromScratch("Too many security failures",
132 &sock->outStream());
133 } catch (rdr::Exception&) {
134 }
135 sock->shutdown();
136 closingSockets.push_back(sock);
137 return;
138 }
139
140 if (clients.empty()) {
141 lastConnectionTime = time(0);
142 }
143
144 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
145 client->init();
146}
147
148void VNCServerST::removeSocket(network::Socket* sock) {
149 // - If the socket has resources allocated to it, delete them
150 std::list<VNCSConnectionST*>::iterator ci;
151 for (ci = clients.begin(); ci != clients.end(); ci++) {
152 if ((*ci)->getSock() == sock) {
153 // - Delete the per-Socket resources
154 delete *ci;
155
156 // - Check that the desktop object is still required
157 if (authClientCount() == 0 && desktopStarted) {
158 slog.debug("no authenticated clients - stopping desktop");
159 desktopStarted = false;
160 desktop->stop();
Michal Srb28d570d2017-09-29 14:45:33 +0200161 stopFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 }
Pierre Ossman05338bc2016-11-08 14:57:11 +0100163
164 if (comparer)
165 comparer->logStats();
166
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000167 return;
168 }
169 }
170
171 // - If the Socket has no resources, it may have been a closingSocket
172 closingSockets.remove(sock);
173}
174
Pierre Ossmand408ca52016-04-29 14:26:05 +0200175void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176{
177 // - Find the appropriate VNCSConnectionST and process the event
178 std::list<VNCSConnectionST*>::iterator ci;
179 for (ci = clients.begin(); ci != clients.end(); ci++) {
180 if ((*ci)->getSock() == sock) {
181 (*ci)->processMessages();
182 return;
183 }
184 }
185 throw rdr::Exception("invalid Socket in VNCServerST");
186}
187
Pierre Ossmand408ca52016-04-29 14:26:05 +0200188void VNCServerST::processSocketWriteEvent(network::Socket* sock)
189{
190 // - Find the appropriate VNCSConnectionST and process the event
191 std::list<VNCSConnectionST*>::iterator ci;
192 for (ci = clients.begin(); ci != clients.end(); ci++) {
193 if ((*ci)->getSock() == sock) {
194 (*ci)->flushSocket();
195 return;
196 }
197 }
198 throw rdr::Exception("invalid Socket in VNCServerST");
199}
200
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201int VNCServerST::checkTimeouts()
202{
203 int timeout = 0;
204 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000205
206 soonestTimeout(&timeout, Timer::checkTimeouts());
207
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
209 ci_next = ci; ci_next++;
210 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
211 }
212
213 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000214 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 // Check MaxDisconnectionTime
217 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
218 if (now < lastDisconnectTime) {
219 // Someone must have set the time backwards.
220 slog.info("Time has gone backwards - resetting lastDisconnectTime");
221 lastDisconnectTime = now;
222 }
223 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
224 if (timeLeft < -60) {
225 // Someone must have set the time forwards.
226 slog.info("Time has gone forwards - resetting lastDisconnectTime");
227 lastDisconnectTime = now;
228 timeLeft = rfb::Server::maxDisconnectionTime;
229 }
230 if (timeLeft <= 0) {
231 slog.info("MaxDisconnectionTime reached, exiting");
232 exit(0);
233 }
234 soonestTimeout(&timeout, timeLeft * 1000);
235 }
236
237 // Check MaxConnectionTime
238 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
239 if (now < lastConnectionTime) {
240 // Someone must have set the time backwards.
241 slog.info("Time has gone backwards - resetting lastConnectionTime");
242 lastConnectionTime = now;
243 }
244 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
245 if (timeLeft < -60) {
246 // Someone must have set the time forwards.
247 slog.info("Time has gone forwards - resetting lastConnectionTime");
248 lastConnectionTime = now;
249 timeLeft = rfb::Server::maxConnectionTime;
250 }
251 if (timeLeft <= 0) {
252 slog.info("MaxConnectionTime reached, exiting");
253 exit(0);
254 }
255 soonestTimeout(&timeout, timeLeft * 1000);
256 }
257
258
259 // Check MaxIdleTime
260 if (rfb::Server::maxIdleTime) {
261 if (now < lastUserInputTime) {
262 // Someone must have set the time backwards.
263 slog.info("Time has gone backwards - resetting lastUserInputTime");
264 lastUserInputTime = now;
265 }
266 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
267 if (timeLeft < -60) {
268 // Someone must have set the time forwards.
269 slog.info("Time has gone forwards - resetting lastUserInputTime");
270 lastUserInputTime = now;
271 timeLeft = rfb::Server::maxIdleTime;
272 }
273 if (timeLeft <= 0) {
274 slog.info("MaxIdleTime reached, exiting");
275 exit(0);
276 }
277 soonestTimeout(&timeout, timeLeft * 1000);
278 }
279
280 return timeout;
281}
282
283
284// VNCServer methods
285
Pierre Ossman559a2e82012-01-23 15:54:11 +0000286void VNCServerST::blockUpdates()
287{
288 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200289
290 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000291}
292
293void VNCServerST::unblockUpdates()
294{
295 assert(blockCounter > 0);
296
297 blockCounter--;
298
Pierre Ossman6e49e952016-10-07 15:59:38 +0200299 // Restart the frame clock if we have updates
300 if (blockCounter == 0) {
301 if (!comparer->is_empty())
302 startFrameClock();
303 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000304}
305
Pierre Ossman04e62db2009-03-23 16:57:07 +0000306void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000307{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100308 if (comparer)
309 comparer->logStats();
310
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000311 pb = pb_;
312 delete comparer;
313 comparer = 0;
314
Pierre Ossman04e62db2009-03-23 16:57:07 +0000315 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000316
Pierre Ossman04e62db2009-03-23 16:57:07 +0000317 if (!pb) {
Michal Srb28d570d2017-09-29 14:45:33 +0200318 screenLayout = ScreenSet();
319
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000320 if (desktopStarted)
321 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Michal Srb28d570d2017-09-29 14:45:33 +0200322
Pierre Ossman04e62db2009-03-23 16:57:07 +0000323 return;
324 }
325
326 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100327 renderedCursorInvalid = true;
Pierre Ossmanb1e80f72017-09-22 16:48:14 +0200328 startFrameClock();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000329
330 // Make sure that we have at least one screen
331 if (screenLayout.num_screens() == 0)
332 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
333
334 std::list<VNCSConnectionST*>::iterator ci, ci_next;
335 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
336 ci_next = ci; ci_next++;
337 (*ci)->pixelBufferChange();
338 // Since the new pixel buffer means an ExtendedDesktopSize needs to
339 // be sent anyway, we don't need to call screenLayoutChange.
340 }
341}
342
343void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
344{
Michal Srb28d570d2017-09-29 14:45:33 +0200345 ScreenSet layout = screenLayout;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000346
347 // Check that the screen layout is still valid
Michal Srb28d570d2017-09-29 14:45:33 +0200348 if (pb_ && !layout.validate(pb_->width(), pb_->height())) {
Pierre Ossman04e62db2009-03-23 16:57:07 +0000349 Rect fbRect;
350 ScreenSet::iterator iter, iter_next;
351
352 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
353
354 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
355 iter_next = iter; ++iter_next;
356 if (iter->dimensions.enclosed_by(fbRect))
357 continue;
358 iter->dimensions = iter->dimensions.intersect(fbRect);
359 if (iter->dimensions.is_empty()) {
360 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
361 (int)iter->id, (unsigned)iter->id);
362 layout.remove_screen(iter->id);
363 }
364 }
365 }
366
367 setPixelBuffer(pb_, layout);
368}
369
370void VNCServerST::setScreenLayout(const ScreenSet& layout)
371{
372 if (!pb)
373 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
374 if (!layout.validate(pb->width(), pb->height()))
375 throw Exception("setScreenLayout: invalid screen layout");
376
Pierre Ossmandf453202009-04-02 14:26:45 +0000377 screenLayout = layout;
378
Pierre Ossman04e62db2009-03-23 16:57:07 +0000379 std::list<VNCSConnectionST*>::iterator ci, ci_next;
380 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
381 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000382 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000383 }
384}
385
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000386void VNCServerST::bell()
387{
388 std::list<VNCSConnectionST*>::iterator ci, ci_next;
389 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
390 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000391 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000392 }
393}
394
395void VNCServerST::serverCutText(const char* str, int len)
396{
397 std::list<VNCSConnectionST*>::iterator ci, ci_next;
398 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
399 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000400 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000401 }
402}
403
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000404void VNCServerST::setName(const char* name_)
405{
Adam Tkacd36b6262009-09-04 10:57:20 +0000406 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000407 std::list<VNCSConnectionST*>::iterator ci, ci_next;
408 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
409 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000410 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000411 }
412}
413
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000414void VNCServerST::add_changed(const Region& region)
415{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000416 if (comparer == NULL)
417 return;
418
419 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200420 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421}
422
423void VNCServerST::add_copied(const Region& dest, const Point& delta)
424{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000425 if (comparer == NULL)
426 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000428 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200429 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430}
431
432void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100433 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000434{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100435 delete cursor;
436 cursor = new Cursor(width, height, newHotspot, data);
437 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000438
439 renderedCursorInvalid = true;
440
441 std::list<VNCSConnectionST*>::iterator ci, ci_next;
442 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
443 ci_next = ci; ci_next++;
444 (*ci)->renderedCursorChange();
445 (*ci)->setCursorOrClose();
446 }
447}
448
449void VNCServerST::setCursorPos(const Point& pos)
450{
451 if (!cursorPos.equals(pos)) {
452 cursorPos = pos;
453 renderedCursorInvalid = true;
454 std::list<VNCSConnectionST*>::iterator ci;
455 for (ci = clients.begin(); ci != clients.end(); ci++)
456 (*ci)->renderedCursorChange();
457 }
458}
459
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100460void VNCServerST::setLEDState(unsigned int state)
461{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100462 std::list<VNCSConnectionST*>::iterator ci, ci_next;
463
464 if (state == ledState)
465 return;
466
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100467 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100468
469 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
470 ci_next = ci; ci_next++;
471 (*ci)->setLEDStateOrClose(state);
472 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100473}
474
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000475// Other public methods
476
477void VNCServerST::approveConnection(network::Socket* sock, bool accept,
478 const char* reason)
479{
480 std::list<VNCSConnectionST*>::iterator ci;
481 for (ci = clients.begin(); ci != clients.end(); ci++) {
482 if ((*ci)->getSock() == sock) {
483 (*ci)->approveConnectionOrClose(accept, reason);
484 return;
485 }
486 }
487}
488
489void VNCServerST::closeClients(const char* reason, network::Socket* except)
490{
491 std::list<VNCSConnectionST*>::iterator i, next_i;
492 for (i=clients.begin(); i!=clients.end(); i=next_i) {
493 next_i = i; next_i++;
494 if ((*i)->getSock() != except)
495 (*i)->close(reason);
496 }
497}
498
499void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
500{
501 sockets->clear();
502 std::list<VNCSConnectionST*>::iterator ci;
503 for (ci = clients.begin(); ci != clients.end(); ci++) {
504 sockets->push_back((*ci)->getSock());
505 }
506 std::list<network::Socket*>::iterator si;
507 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
508 sockets->push_back(*si);
509 }
510}
511
512SConnection* VNCServerST::getSConnection(network::Socket* sock) {
513 std::list<VNCSConnectionST*>::iterator ci;
514 for (ci = clients.begin(); ci != clients.end(); ci++) {
515 if ((*ci)->getSock() == sock)
516 return *ci;
517 }
518 return 0;
519}
520
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000521bool VNCServerST::handleTimeout(Timer* t)
522{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200523 if (t == &frameTimer) {
524 // We keep running until we go a full interval without any updates
525 if (comparer->is_empty())
526 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000527
Pierre Ossman6e49e952016-10-07 15:59:38 +0200528 writeUpdate();
Pierre Ossman7be73d72017-11-06 13:16:35 +0100529
530 // If this is the first iteration then we need to adjust the timeout
531 if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) {
532 frameTimer.start(1000/rfb::Server::frameRate);
533 return false;
534 }
535
Pierre Ossman6e49e952016-10-07 15:59:38 +0200536 return true;
537 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000538
539 return false;
540}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000541
542// -=- Internal methods
543
544void VNCServerST::startDesktop()
545{
546 if (!desktopStarted) {
547 slog.debug("starting desktop");
548 desktop->start(this);
549 desktopStarted = true;
550 if (!pb)
551 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
552 }
553}
554
555int VNCServerST::authClientCount() {
556 int count = 0;
557 std::list<VNCSConnectionST*>::iterator ci;
558 for (ci = clients.begin(); ci != clients.end(); ci++) {
559 if ((*ci)->authenticated())
560 count++;
561 }
562 return count;
563}
564
565inline bool VNCServerST::needRenderedCursor()
566{
567 std::list<VNCSConnectionST*>::iterator ci;
568 for (ci = clients.begin(); ci != clients.end(); ci++)
569 if ((*ci)->needRenderedCursor()) return true;
570 return false;
571}
572
Pierre Ossman6e49e952016-10-07 15:59:38 +0200573void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000574{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200575 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000576 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000577 if (blockCounter > 0)
578 return;
579
Pierre Ossman7be73d72017-11-06 13:16:35 +0100580 // The first iteration will be just half a frame as we get a very
581 // unstable update rate if we happen to be perfectly in sync with
582 // the application's update rate
583 frameTimer.start(1000/rfb::Server::frameRate/2);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000584}
585
Pierre Ossman6e49e952016-10-07 15:59:38 +0200586void VNCServerST::stopFrameClock()
587{
588 frameTimer.stop();
589}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000590
Pierre Ossman6e49e952016-10-07 15:59:38 +0200591// writeUpdate() is called on a regular interval in order to see what
592// updates are pending and propagates them to the update tracker for
593// each client. It uses the ComparingUpdateTracker's compare() method
594// to filter out areas of the screen which haven't actually changed. It
595// also checks the state of the (server-side) rendered cursor, if
596// necessary rendering it again with the correct background.
597
598void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000599{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000600 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200601 Region toCheck;
602
603 std::list<VNCSConnectionST*>::iterator ci, ci_next;
604
605 assert(blockCounter == 0);
606
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000607 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200608 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000609
Pierre Ossman6e49e952016-10-07 15:59:38 +0200610 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100611 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
612 .translate(cursorPos.subtract(cursor->hotspot()))
613 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000614
Pierre Ossman24684e52016-12-05 16:58:19 +0100615 if (!toCheck.intersect(clippedCursorRect).is_empty())
616 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000617 }
618
619 pb->grabRegion(toCheck);
620
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000621 if (getComparerState())
622 comparer->enable();
623 else
624 comparer->disable();
625
626 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000627 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000628
Pierre Ossman6e49e952016-10-07 15:59:38 +0200629 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000631 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
632 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000633 (*ci)->add_copied(ui.copied, ui.copy_delta);
634 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200635 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000636 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200637}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000638
Pierre Ossman6e49e952016-10-07 15:59:38 +0200639// checkUpdate() is called by clients to see if it is safe to read from
640// the framebuffer at this time.
641
642bool VNCServerST::checkUpdate()
643{
644 // Block clients as the frame buffer cannot be safely accessed
645 if (blockCounter > 0)
646 return false;
647
648 // Block client from updating if there are pending updates
649 if (!comparer->is_empty())
650 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000651
652 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000653}
654
Pierre Ossman24684e52016-12-05 16:58:19 +0100655const RenderedCursor* VNCServerST::getRenderedCursor()
656{
657 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100658 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100659 renderedCursorInvalid = false;
660 }
661
662 return &renderedCursor;
663}
664
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000665void VNCServerST::getConnInfo(ListConnInfo * listConn)
666{
667 listConn->Clear();
668 listConn->setDisable(getDisable());
669 if (clients.empty())
670 return;
671 std::list<VNCSConnectionST*>::iterator i;
672 for (i = clients.begin(); i != clients.end(); i++)
673 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
674 (*i)->getStartTime(), (*i)->getStatus());
675}
676
677void VNCServerST::setConnStatus(ListConnInfo* listConn)
678{
679 setDisable(listConn->getDisable());
680 if (listConn->Empty() || clients.empty()) return;
681 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
682 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
683 std::list<VNCSConnectionST*>::iterator i;
684 for (i = clients.begin(); i != clients.end(); i++) {
685 if ((*i) == conn) {
686 int status = listConn->iGetStatus();
687 if (status == 3) {
688 (*i)->close(0);
689 } else {
690 (*i)->setStatus(status);
691 }
692 break;
693 }
694 }
695 }
696}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000697
Pierre Ossman04e62db2009-03-23 16:57:07 +0000698void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
699{
700 std::list<VNCSConnectionST*>::iterator ci, ci_next;
701 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
702 ci_next = ci; ci_next++;
703 if ((*ci) == requester)
704 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000705 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000706 }
707}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000708
709bool VNCServerST::getComparerState()
710{
711 if (rfb::Server::compareFB == 0)
712 return false;
713 if (rfb::Server::compareFB != 2)
714 return true;
715
716 std::list<VNCSConnectionST*>::iterator ci, ci_next;
717 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
718 ci_next = ci; ci_next++;
719 if ((*ci)->getComparerState())
720 return true;
721 }
722 return false;
723}