blob: 8cc04f740812951bf55b9616502271db8bfb2013 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +01002 * Copyright 2009-2018 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!
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100108 stopDesktop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109
Pierre Ossman05338bc2016-11-08 14:57:11 +0100110 if (comparer)
111 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100113
114 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115}
116
117
118// SocketServer methods
119
120void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
121{
122 // - Check the connection isn't black-marked
123 // *** do this in getSecurity instead?
124 CharArray address(sock->getPeerAddress());
125 if (blHosts->isBlackmarked(address.buf)) {
126 connectionsLog.error("blacklisted: %s", address.buf);
127 try {
128 SConnection::writeConnFailedFromScratch("Too many security failures",
129 &sock->outStream());
130 } catch (rdr::Exception&) {
131 }
132 sock->shutdown();
133 closingSockets.push_back(sock);
134 return;
135 }
136
137 if (clients.empty()) {
138 lastConnectionTime = time(0);
139 }
140
141 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
142 client->init();
143}
144
145void VNCServerST::removeSocket(network::Socket* sock) {
146 // - If the socket has resources allocated to it, delete them
147 std::list<VNCSConnectionST*>::iterator ci;
148 for (ci = clients.begin(); ci != clients.end(); ci++) {
149 if ((*ci)->getSock() == sock) {
150 // - Delete the per-Socket resources
151 delete *ci;
152
153 // - Check that the desktop object is still required
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100154 if (authClientCount() == 0)
155 stopDesktop();
Pierre Ossman05338bc2016-11-08 14:57:11 +0100156
157 if (comparer)
158 comparer->logStats();
159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 return;
161 }
162 }
163
164 // - If the Socket has no resources, it may have been a closingSocket
165 closingSockets.remove(sock);
166}
167
Pierre Ossmand408ca52016-04-29 14:26:05 +0200168void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169{
170 // - Find the appropriate VNCSConnectionST and process the event
171 std::list<VNCSConnectionST*>::iterator ci;
172 for (ci = clients.begin(); ci != clients.end(); ci++) {
173 if ((*ci)->getSock() == sock) {
174 (*ci)->processMessages();
175 return;
176 }
177 }
178 throw rdr::Exception("invalid Socket in VNCServerST");
179}
180
Pierre Ossmand408ca52016-04-29 14:26:05 +0200181void VNCServerST::processSocketWriteEvent(network::Socket* sock)
182{
183 // - Find the appropriate VNCSConnectionST and process the event
184 std::list<VNCSConnectionST*>::iterator ci;
185 for (ci = clients.begin(); ci != clients.end(); ci++) {
186 if ((*ci)->getSock() == sock) {
187 (*ci)->flushSocket();
188 return;
189 }
190 }
191 throw rdr::Exception("invalid Socket in VNCServerST");
192}
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194int VNCServerST::checkTimeouts()
195{
196 int timeout = 0;
197 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000198
199 soonestTimeout(&timeout, Timer::checkTimeouts());
200
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
202 ci_next = ci; ci_next++;
203 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
204 }
205
206 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000207 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 // Check MaxDisconnectionTime
210 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
211 if (now < lastDisconnectTime) {
212 // Someone must have set the time backwards.
213 slog.info("Time has gone backwards - resetting lastDisconnectTime");
214 lastDisconnectTime = now;
215 }
216 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
217 if (timeLeft < -60) {
218 // Someone must have set the time forwards.
219 slog.info("Time has gone forwards - resetting lastDisconnectTime");
220 lastDisconnectTime = now;
221 timeLeft = rfb::Server::maxDisconnectionTime;
222 }
223 if (timeLeft <= 0) {
224 slog.info("MaxDisconnectionTime reached, exiting");
225 exit(0);
226 }
227 soonestTimeout(&timeout, timeLeft * 1000);
228 }
229
230 // Check MaxConnectionTime
231 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
232 if (now < lastConnectionTime) {
233 // Someone must have set the time backwards.
234 slog.info("Time has gone backwards - resetting lastConnectionTime");
235 lastConnectionTime = now;
236 }
237 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
238 if (timeLeft < -60) {
239 // Someone must have set the time forwards.
240 slog.info("Time has gone forwards - resetting lastConnectionTime");
241 lastConnectionTime = now;
242 timeLeft = rfb::Server::maxConnectionTime;
243 }
244 if (timeLeft <= 0) {
245 slog.info("MaxConnectionTime reached, exiting");
246 exit(0);
247 }
248 soonestTimeout(&timeout, timeLeft * 1000);
249 }
250
251
252 // Check MaxIdleTime
253 if (rfb::Server::maxIdleTime) {
254 if (now < lastUserInputTime) {
255 // Someone must have set the time backwards.
256 slog.info("Time has gone backwards - resetting lastUserInputTime");
257 lastUserInputTime = now;
258 }
259 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
260 if (timeLeft < -60) {
261 // Someone must have set the time forwards.
262 slog.info("Time has gone forwards - resetting lastUserInputTime");
263 lastUserInputTime = now;
264 timeLeft = rfb::Server::maxIdleTime;
265 }
266 if (timeLeft <= 0) {
267 slog.info("MaxIdleTime reached, exiting");
268 exit(0);
269 }
270 soonestTimeout(&timeout, timeLeft * 1000);
271 }
272
273 return timeout;
274}
275
276
277// VNCServer methods
278
Pierre Ossman559a2e82012-01-23 15:54:11 +0000279void VNCServerST::blockUpdates()
280{
281 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200282
283 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000284}
285
286void VNCServerST::unblockUpdates()
287{
288 assert(blockCounter > 0);
289
290 blockCounter--;
291
Pierre Ossman6e49e952016-10-07 15:59:38 +0200292 // Restart the frame clock if we have updates
293 if (blockCounter == 0) {
294 if (!comparer->is_empty())
295 startFrameClock();
296 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000297}
298
Pierre Ossman04e62db2009-03-23 16:57:07 +0000299void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000300{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100301 if (comparer)
302 comparer->logStats();
303
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304 pb = pb_;
305 delete comparer;
306 comparer = 0;
307
Pierre Ossman04e62db2009-03-23 16:57:07 +0000308 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309
Pierre Ossman04e62db2009-03-23 16:57:07 +0000310 if (!pb) {
Michal Srb28d570d2017-09-29 14:45:33 +0200311 screenLayout = ScreenSet();
312
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000313 if (desktopStarted)
314 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Michal Srb28d570d2017-09-29 14:45:33 +0200315
Pierre Ossman04e62db2009-03-23 16:57:07 +0000316 return;
317 }
318
Pierre Ossman6cd61172018-05-07 14:24:56 +0200319 // Assume the framebuffer contents wasn't saved and reset everything
320 // that tracks its contents
Pierre Ossman04e62db2009-03-23 16:57:07 +0000321 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100322 renderedCursorInvalid = true;
Pierre Ossman6cd61172018-05-07 14:24:56 +0200323 add_changed(pb->getRect());
Pierre Ossman04e62db2009-03-23 16:57:07 +0000324
325 // Make sure that we have at least one screen
326 if (screenLayout.num_screens() == 0)
327 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
328
329 std::list<VNCSConnectionST*>::iterator ci, ci_next;
330 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
331 ci_next = ci; ci_next++;
332 (*ci)->pixelBufferChange();
333 // Since the new pixel buffer means an ExtendedDesktopSize needs to
334 // be sent anyway, we don't need to call screenLayoutChange.
335 }
336}
337
338void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
339{
Michal Srb28d570d2017-09-29 14:45:33 +0200340 ScreenSet layout = screenLayout;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000341
342 // Check that the screen layout is still valid
Michal Srb28d570d2017-09-29 14:45:33 +0200343 if (pb_ && !layout.validate(pb_->width(), pb_->height())) {
Pierre Ossman04e62db2009-03-23 16:57:07 +0000344 Rect fbRect;
345 ScreenSet::iterator iter, iter_next;
346
347 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
348
349 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
350 iter_next = iter; ++iter_next;
351 if (iter->dimensions.enclosed_by(fbRect))
352 continue;
353 iter->dimensions = iter->dimensions.intersect(fbRect);
354 if (iter->dimensions.is_empty()) {
355 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
356 (int)iter->id, (unsigned)iter->id);
357 layout.remove_screen(iter->id);
358 }
359 }
360 }
361
362 setPixelBuffer(pb_, layout);
363}
364
365void VNCServerST::setScreenLayout(const ScreenSet& layout)
366{
367 if (!pb)
368 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
369 if (!layout.validate(pb->width(), pb->height()))
370 throw Exception("setScreenLayout: invalid screen layout");
371
Pierre Ossmandf453202009-04-02 14:26:45 +0000372 screenLayout = layout;
373
Pierre Ossman04e62db2009-03-23 16:57:07 +0000374 std::list<VNCSConnectionST*>::iterator ci, ci_next;
375 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
376 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000377 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000378 }
379}
380
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000381void VNCServerST::bell()
382{
383 std::list<VNCSConnectionST*>::iterator ci, ci_next;
384 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
385 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000386 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000387 }
388}
389
390void VNCServerST::serverCutText(const char* str, int len)
391{
392 std::list<VNCSConnectionST*>::iterator ci, ci_next;
393 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
394 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000395 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000396 }
397}
398
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000399void VNCServerST::setName(const char* name_)
400{
Adam Tkacd36b6262009-09-04 10:57:20 +0000401 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000402 std::list<VNCSConnectionST*>::iterator ci, ci_next;
403 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
404 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000405 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000406 }
407}
408
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000409void VNCServerST::add_changed(const Region& region)
410{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000411 if (comparer == NULL)
412 return;
413
414 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200415 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000416}
417
418void VNCServerST::add_copied(const Region& dest, const Point& delta)
419{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000420 if (comparer == NULL)
421 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000422
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000423 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200424 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000425}
426
427void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100428 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000429{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100430 delete cursor;
431 cursor = new Cursor(width, height, newHotspot, data);
432 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000433
434 renderedCursorInvalid = true;
435
436 std::list<VNCSConnectionST*>::iterator ci, ci_next;
437 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
438 ci_next = ci; ci_next++;
439 (*ci)->renderedCursorChange();
440 (*ci)->setCursorOrClose();
441 }
442}
443
444void VNCServerST::setCursorPos(const Point& pos)
445{
446 if (!cursorPos.equals(pos)) {
447 cursorPos = pos;
448 renderedCursorInvalid = true;
449 std::list<VNCSConnectionST*>::iterator ci;
450 for (ci = clients.begin(); ci != clients.end(); ci++)
451 (*ci)->renderedCursorChange();
452 }
453}
454
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100455void VNCServerST::setLEDState(unsigned int state)
456{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100457 std::list<VNCSConnectionST*>::iterator ci, ci_next;
458
459 if (state == ledState)
460 return;
461
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100462 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100463
464 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
465 ci_next = ci; ci_next++;
466 (*ci)->setLEDStateOrClose(state);
467 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100468}
469
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000470// Other public methods
471
472void VNCServerST::approveConnection(network::Socket* sock, bool accept,
473 const char* reason)
474{
475 std::list<VNCSConnectionST*>::iterator ci;
476 for (ci = clients.begin(); ci != clients.end(); ci++) {
477 if ((*ci)->getSock() == sock) {
478 (*ci)->approveConnectionOrClose(accept, reason);
479 return;
480 }
481 }
482}
483
484void VNCServerST::closeClients(const char* reason, network::Socket* except)
485{
486 std::list<VNCSConnectionST*>::iterator i, next_i;
487 for (i=clients.begin(); i!=clients.end(); i=next_i) {
488 next_i = i; next_i++;
489 if ((*i)->getSock() != except)
490 (*i)->close(reason);
491 }
492}
493
494void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
495{
496 sockets->clear();
497 std::list<VNCSConnectionST*>::iterator ci;
498 for (ci = clients.begin(); ci != clients.end(); ci++) {
499 sockets->push_back((*ci)->getSock());
500 }
501 std::list<network::Socket*>::iterator si;
502 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
503 sockets->push_back(*si);
504 }
505}
506
507SConnection* VNCServerST::getSConnection(network::Socket* sock) {
508 std::list<VNCSConnectionST*>::iterator ci;
509 for (ci = clients.begin(); ci != clients.end(); ci++) {
510 if ((*ci)->getSock() == sock)
511 return *ci;
512 }
513 return 0;
514}
515
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000516bool VNCServerST::handleTimeout(Timer* t)
517{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200518 if (t == &frameTimer) {
519 // We keep running until we go a full interval without any updates
520 if (comparer->is_empty())
521 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000522
Pierre Ossman6e49e952016-10-07 15:59:38 +0200523 writeUpdate();
Pierre Ossman7be73d72017-11-06 13:16:35 +0100524
525 // If this is the first iteration then we need to adjust the timeout
526 if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) {
527 frameTimer.start(1000/rfb::Server::frameRate);
528 return false;
529 }
530
Pierre Ossman6e49e952016-10-07 15:59:38 +0200531 return true;
532 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000533
534 return false;
535}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000536
537// -=- Internal methods
538
539void VNCServerST::startDesktop()
540{
541 if (!desktopStarted) {
542 slog.debug("starting desktop");
543 desktop->start(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000544 if (!pb)
545 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
Pierre Ossman6cd61172018-05-07 14:24:56 +0200546 desktopStarted = true;
547 // The tracker might have accumulated changes whilst we were
548 // stopped, so flush those out
549 if (!comparer->is_empty())
550 writeUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000551 }
552}
553
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100554void VNCServerST::stopDesktop()
555{
556 if (desktopStarted) {
557 slog.debug("stopping desktop");
558 desktopStarted = false;
559 desktop->stop();
560 stopFrameClock();
561 }
562}
563
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000564int VNCServerST::authClientCount() {
565 int count = 0;
566 std::list<VNCSConnectionST*>::iterator ci;
567 for (ci = clients.begin(); ci != clients.end(); ci++) {
568 if ((*ci)->authenticated())
569 count++;
570 }
571 return count;
572}
573
574inline bool VNCServerST::needRenderedCursor()
575{
576 std::list<VNCSConnectionST*>::iterator ci;
577 for (ci = clients.begin(); ci != clients.end(); ci++)
578 if ((*ci)->needRenderedCursor()) return true;
579 return false;
580}
581
Pierre Ossman6e49e952016-10-07 15:59:38 +0200582void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000583{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200584 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000585 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000586 if (blockCounter > 0)
587 return;
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100588 if (!desktopStarted)
589 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000590
Pierre Ossman7be73d72017-11-06 13:16:35 +0100591 // The first iteration will be just half a frame as we get a very
592 // unstable update rate if we happen to be perfectly in sync with
593 // the application's update rate
594 frameTimer.start(1000/rfb::Server::frameRate/2);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000595}
596
Pierre Ossman6e49e952016-10-07 15:59:38 +0200597void VNCServerST::stopFrameClock()
598{
599 frameTimer.stop();
600}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000601
Pierre Ossmana2b80d62018-03-23 09:30:09 +0100602int VNCServerST::msToNextUpdate()
603{
604 // FIXME: If the application is updating slower than frameRate then
605 // we could allow the clients more time here
606
607 if (!frameTimer.isStarted())
608 return 1000/rfb::Server::frameRate/2;
609 else
610 return frameTimer.getRemainingMs();
611}
612
Pierre Ossman6e49e952016-10-07 15:59:38 +0200613// writeUpdate() is called on a regular interval in order to see what
614// updates are pending and propagates them to the update tracker for
615// each client. It uses the ComparingUpdateTracker's compare() method
616// to filter out areas of the screen which haven't actually changed. It
617// also checks the state of the (server-side) rendered cursor, if
618// necessary rendering it again with the correct background.
619
620void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000621{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000622 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200623 Region toCheck;
624
625 std::list<VNCSConnectionST*>::iterator ci, ci_next;
626
627 assert(blockCounter == 0);
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100628 assert(desktopStarted);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200629
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000630 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200631 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000632
Pierre Ossman6e49e952016-10-07 15:59:38 +0200633 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100634 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
635 .translate(cursorPos.subtract(cursor->hotspot()))
636 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637
Pierre Ossman24684e52016-12-05 16:58:19 +0100638 if (!toCheck.intersect(clippedCursorRect).is_empty())
639 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000640 }
641
642 pb->grabRegion(toCheck);
643
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000644 if (getComparerState())
645 comparer->enable();
646 else
647 comparer->disable();
648
649 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000650 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000651
Pierre Ossman6e49e952016-10-07 15:59:38 +0200652 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000653
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000654 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
655 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000656 (*ci)->add_copied(ui.copied, ui.copy_delta);
657 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200658 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200660}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000661
Pierre Ossman6e49e952016-10-07 15:59:38 +0200662// checkUpdate() is called by clients to see if it is safe to read from
663// the framebuffer at this time.
664
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100665Region VNCServerST::getPendingRegion()
Pierre Ossman6e49e952016-10-07 15:59:38 +0200666{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100667 UpdateInfo ui;
668
Pierre Ossman6e49e952016-10-07 15:59:38 +0200669 // Block clients as the frame buffer cannot be safely accessed
670 if (blockCounter > 0)
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100671 return pb->getRect();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200672
673 // Block client from updating if there are pending updates
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100674 if (comparer->is_empty())
675 return Region();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000676
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100677 comparer->getUpdateInfo(&ui, pb->getRect());
678
679 return ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000680}
681
Pierre Ossman24684e52016-12-05 16:58:19 +0100682const RenderedCursor* VNCServerST::getRenderedCursor()
683{
684 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100685 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100686 renderedCursorInvalid = false;
687 }
688
689 return &renderedCursor;
690}
691
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692void VNCServerST::getConnInfo(ListConnInfo * listConn)
693{
694 listConn->Clear();
695 listConn->setDisable(getDisable());
696 if (clients.empty())
697 return;
698 std::list<VNCSConnectionST*>::iterator i;
699 for (i = clients.begin(); i != clients.end(); i++)
700 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
701 (*i)->getStartTime(), (*i)->getStatus());
702}
703
704void VNCServerST::setConnStatus(ListConnInfo* listConn)
705{
706 setDisable(listConn->getDisable());
707 if (listConn->Empty() || clients.empty()) return;
708 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
709 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
710 std::list<VNCSConnectionST*>::iterator i;
711 for (i = clients.begin(); i != clients.end(); i++) {
712 if ((*i) == conn) {
713 int status = listConn->iGetStatus();
714 if (status == 3) {
715 (*i)->close(0);
716 } else {
717 (*i)->setStatus(status);
718 }
719 break;
720 }
721 }
722 }
723}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000724
Pierre Ossman04e62db2009-03-23 16:57:07 +0000725void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
726{
727 std::list<VNCSConnectionST*>::iterator ci, ci_next;
728 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
729 ci_next = ci; ci_next++;
730 if ((*ci) == requester)
731 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000732 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000733 }
734}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000735
736bool VNCServerST::getComparerState()
737{
738 if (rfb::Server::compareFB == 0)
739 return false;
740 if (rfb::Server::compareFB != 2)
741 return true;
742
743 std::list<VNCSConnectionST*>::iterator ci, ci_next;
744 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
745 ci_next = ci; ci_next++;
746 if ((*ci)->getComparerState())
747 return true;
748 }
749 return false;
750}