blob: 15df71b9c465ddf5269808148f577ac748798605 [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
319 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100320 renderedCursorInvalid = true;
Pierre Ossmanb1e80f72017-09-22 16:48:14 +0200321 startFrameClock();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000322
323 // Make sure that we have at least one screen
324 if (screenLayout.num_screens() == 0)
325 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
326
327 std::list<VNCSConnectionST*>::iterator ci, ci_next;
328 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
329 ci_next = ci; ci_next++;
330 (*ci)->pixelBufferChange();
331 // Since the new pixel buffer means an ExtendedDesktopSize needs to
332 // be sent anyway, we don't need to call screenLayoutChange.
333 }
334}
335
336void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
337{
Michal Srb28d570d2017-09-29 14:45:33 +0200338 ScreenSet layout = screenLayout;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000339
340 // Check that the screen layout is still valid
Michal Srb28d570d2017-09-29 14:45:33 +0200341 if (pb_ && !layout.validate(pb_->width(), pb_->height())) {
Pierre Ossman04e62db2009-03-23 16:57:07 +0000342 Rect fbRect;
343 ScreenSet::iterator iter, iter_next;
344
345 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
346
347 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
348 iter_next = iter; ++iter_next;
349 if (iter->dimensions.enclosed_by(fbRect))
350 continue;
351 iter->dimensions = iter->dimensions.intersect(fbRect);
352 if (iter->dimensions.is_empty()) {
353 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
354 (int)iter->id, (unsigned)iter->id);
355 layout.remove_screen(iter->id);
356 }
357 }
358 }
359
360 setPixelBuffer(pb_, layout);
361}
362
363void VNCServerST::setScreenLayout(const ScreenSet& layout)
364{
365 if (!pb)
366 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
367 if (!layout.validate(pb->width(), pb->height()))
368 throw Exception("setScreenLayout: invalid screen layout");
369
Pierre Ossmandf453202009-04-02 14:26:45 +0000370 screenLayout = layout;
371
Pierre Ossman04e62db2009-03-23 16:57:07 +0000372 std::list<VNCSConnectionST*>::iterator ci, ci_next;
373 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
374 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000375 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000376 }
377}
378
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000379void VNCServerST::bell()
380{
381 std::list<VNCSConnectionST*>::iterator ci, ci_next;
382 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
383 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000384 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000385 }
386}
387
388void VNCServerST::serverCutText(const char* str, int len)
389{
390 std::list<VNCSConnectionST*>::iterator ci, ci_next;
391 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
392 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000393 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000394 }
395}
396
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000397void VNCServerST::setName(const char* name_)
398{
Adam Tkacd36b6262009-09-04 10:57:20 +0000399 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000400 std::list<VNCSConnectionST*>::iterator ci, ci_next;
401 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
402 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000403 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000404 }
405}
406
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000407void VNCServerST::add_changed(const Region& region)
408{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000409 if (comparer == NULL)
410 return;
411
412 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200413 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000414}
415
416void VNCServerST::add_copied(const Region& dest, const Point& delta)
417{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000418 if (comparer == NULL)
419 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000420
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000421 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200422 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423}
424
425void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100426 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100428 delete cursor;
429 cursor = new Cursor(width, height, newHotspot, data);
430 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000431
432 renderedCursorInvalid = true;
433
434 std::list<VNCSConnectionST*>::iterator ci, ci_next;
435 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
436 ci_next = ci; ci_next++;
437 (*ci)->renderedCursorChange();
438 (*ci)->setCursorOrClose();
439 }
440}
441
442void VNCServerST::setCursorPos(const Point& pos)
443{
444 if (!cursorPos.equals(pos)) {
445 cursorPos = pos;
446 renderedCursorInvalid = true;
447 std::list<VNCSConnectionST*>::iterator ci;
448 for (ci = clients.begin(); ci != clients.end(); ci++)
449 (*ci)->renderedCursorChange();
450 }
451}
452
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100453void VNCServerST::setLEDState(unsigned int state)
454{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100455 std::list<VNCSConnectionST*>::iterator ci, ci_next;
456
457 if (state == ledState)
458 return;
459
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100460 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100461
462 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
463 ci_next = ci; ci_next++;
464 (*ci)->setLEDStateOrClose(state);
465 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100466}
467
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000468// Other public methods
469
470void VNCServerST::approveConnection(network::Socket* sock, bool accept,
471 const char* reason)
472{
473 std::list<VNCSConnectionST*>::iterator ci;
474 for (ci = clients.begin(); ci != clients.end(); ci++) {
475 if ((*ci)->getSock() == sock) {
476 (*ci)->approveConnectionOrClose(accept, reason);
477 return;
478 }
479 }
480}
481
482void VNCServerST::closeClients(const char* reason, network::Socket* except)
483{
484 std::list<VNCSConnectionST*>::iterator i, next_i;
485 for (i=clients.begin(); i!=clients.end(); i=next_i) {
486 next_i = i; next_i++;
487 if ((*i)->getSock() != except)
488 (*i)->close(reason);
489 }
490}
491
492void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
493{
494 sockets->clear();
495 std::list<VNCSConnectionST*>::iterator ci;
496 for (ci = clients.begin(); ci != clients.end(); ci++) {
497 sockets->push_back((*ci)->getSock());
498 }
499 std::list<network::Socket*>::iterator si;
500 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
501 sockets->push_back(*si);
502 }
503}
504
505SConnection* VNCServerST::getSConnection(network::Socket* sock) {
506 std::list<VNCSConnectionST*>::iterator ci;
507 for (ci = clients.begin(); ci != clients.end(); ci++) {
508 if ((*ci)->getSock() == sock)
509 return *ci;
510 }
511 return 0;
512}
513
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000514bool VNCServerST::handleTimeout(Timer* t)
515{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200516 if (t == &frameTimer) {
517 // We keep running until we go a full interval without any updates
518 if (comparer->is_empty())
519 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000520
Pierre Ossman6e49e952016-10-07 15:59:38 +0200521 writeUpdate();
Pierre Ossman7be73d72017-11-06 13:16:35 +0100522
523 // If this is the first iteration then we need to adjust the timeout
524 if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) {
525 frameTimer.start(1000/rfb::Server::frameRate);
526 return false;
527 }
528
Pierre Ossman6e49e952016-10-07 15:59:38 +0200529 return true;
530 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000531
532 return false;
533}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000534
535// -=- Internal methods
536
537void VNCServerST::startDesktop()
538{
539 if (!desktopStarted) {
540 slog.debug("starting desktop");
541 desktop->start(this);
542 desktopStarted = true;
543 if (!pb)
544 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
545 }
546}
547
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100548void VNCServerST::stopDesktop()
549{
550 if (desktopStarted) {
551 slog.debug("stopping desktop");
552 desktopStarted = false;
553 desktop->stop();
554 stopFrameClock();
555 }
556}
557
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000558int VNCServerST::authClientCount() {
559 int count = 0;
560 std::list<VNCSConnectionST*>::iterator ci;
561 for (ci = clients.begin(); ci != clients.end(); ci++) {
562 if ((*ci)->authenticated())
563 count++;
564 }
565 return count;
566}
567
568inline bool VNCServerST::needRenderedCursor()
569{
570 std::list<VNCSConnectionST*>::iterator ci;
571 for (ci = clients.begin(); ci != clients.end(); ci++)
572 if ((*ci)->needRenderedCursor()) return true;
573 return false;
574}
575
Pierre Ossman6e49e952016-10-07 15:59:38 +0200576void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000577{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200578 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000579 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000580 if (blockCounter > 0)
581 return;
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100582 if (!desktopStarted)
583 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000584
Pierre Ossman7be73d72017-11-06 13:16:35 +0100585 // The first iteration will be just half a frame as we get a very
586 // unstable update rate if we happen to be perfectly in sync with
587 // the application's update rate
588 frameTimer.start(1000/rfb::Server::frameRate/2);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000589}
590
Pierre Ossman6e49e952016-10-07 15:59:38 +0200591void VNCServerST::stopFrameClock()
592{
593 frameTimer.stop();
594}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000595
Pierre Ossmana2b80d62018-03-23 09:30:09 +0100596int VNCServerST::msToNextUpdate()
597{
598 // FIXME: If the application is updating slower than frameRate then
599 // we could allow the clients more time here
600
601 if (!frameTimer.isStarted())
602 return 1000/rfb::Server::frameRate/2;
603 else
604 return frameTimer.getRemainingMs();
605}
606
Pierre Ossman6e49e952016-10-07 15:59:38 +0200607// writeUpdate() is called on a regular interval in order to see what
608// updates are pending and propagates them to the update tracker for
609// each client. It uses the ComparingUpdateTracker's compare() method
610// to filter out areas of the screen which haven't actually changed. It
611// also checks the state of the (server-side) rendered cursor, if
612// necessary rendering it again with the correct background.
613
614void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000615{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000616 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200617 Region toCheck;
618
619 std::list<VNCSConnectionST*>::iterator ci, ci_next;
620
621 assert(blockCounter == 0);
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100622 assert(desktopStarted);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200623
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000624 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200625 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000626
Pierre Ossman6e49e952016-10-07 15:59:38 +0200627 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100628 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
629 .translate(cursorPos.subtract(cursor->hotspot()))
630 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000631
Pierre Ossman24684e52016-12-05 16:58:19 +0100632 if (!toCheck.intersect(clippedCursorRect).is_empty())
633 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000634 }
635
636 pb->grabRegion(toCheck);
637
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000638 if (getComparerState())
639 comparer->enable();
640 else
641 comparer->disable();
642
643 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000644 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000645
Pierre Ossman6e49e952016-10-07 15:59:38 +0200646 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000647
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000648 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
649 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000650 (*ci)->add_copied(ui.copied, ui.copy_delta);
651 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200652 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000653 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200654}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000655
Pierre Ossman6e49e952016-10-07 15:59:38 +0200656// checkUpdate() is called by clients to see if it is safe to read from
657// the framebuffer at this time.
658
659bool VNCServerST::checkUpdate()
660{
661 // Block clients as the frame buffer cannot be safely accessed
662 if (blockCounter > 0)
663 return false;
664
665 // Block client from updating if there are pending updates
666 if (!comparer->is_empty())
667 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000668
669 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000670}
671
Pierre Ossman24684e52016-12-05 16:58:19 +0100672const RenderedCursor* VNCServerST::getRenderedCursor()
673{
674 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100675 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100676 renderedCursorInvalid = false;
677 }
678
679 return &renderedCursor;
680}
681
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000682void VNCServerST::getConnInfo(ListConnInfo * listConn)
683{
684 listConn->Clear();
685 listConn->setDisable(getDisable());
686 if (clients.empty())
687 return;
688 std::list<VNCSConnectionST*>::iterator i;
689 for (i = clients.begin(); i != clients.end(); i++)
690 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
691 (*i)->getStartTime(), (*i)->getStatus());
692}
693
694void VNCServerST::setConnStatus(ListConnInfo* listConn)
695{
696 setDisable(listConn->getDisable());
697 if (listConn->Empty() || clients.empty()) return;
698 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
699 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
700 std::list<VNCSConnectionST*>::iterator i;
701 for (i = clients.begin(); i != clients.end(); i++) {
702 if ((*i) == conn) {
703 int status = listConn->iGetStatus();
704 if (status == 3) {
705 (*i)->close(0);
706 } else {
707 (*i)->setStatus(status);
708 }
709 break;
710 }
711 }
712 }
713}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000714
Pierre Ossman04e62db2009-03-23 16:57:07 +0000715void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
716{
717 std::list<VNCSConnectionST*>::iterator ci, ci_next;
718 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
719 ci_next = ci; ci_next++;
720 if ((*ci) == requester)
721 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000722 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000723 }
724}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000725
726bool VNCServerST::getComparerState()
727{
728 if (rfb::Server::compareFB == 0)
729 return false;
730 if (rfb::Server::compareFB != 2)
731 return true;
732
733 std::list<VNCSConnectionST*>::iterator ci, ci_next;
734 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
735 ci_next = ci; ci_next++;
736 if ((*ci)->getComparerState())
737 return true;
738 }
739 return false;
740}