blob: 7e36876ce2af03f6e2f2565b89264994cacc1794 [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),
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +020083 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
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200537void VNCServerST::queryConnection(network::Socket* sock,
538 const char* userName)
539{
540 desktop->queryConnection(sock, userName);
541}
542
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000543// -=- Internal methods
544
545void VNCServerST::startDesktop()
546{
547 if (!desktopStarted) {
548 slog.debug("starting desktop");
549 desktop->start(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000550 if (!pb)
551 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
Pierre Ossman6cd61172018-05-07 14:24:56 +0200552 desktopStarted = true;
553 // The tracker might have accumulated changes whilst we were
554 // stopped, so flush those out
555 if (!comparer->is_empty())
556 writeUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000557 }
558}
559
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100560void VNCServerST::stopDesktop()
561{
562 if (desktopStarted) {
563 slog.debug("stopping desktop");
564 desktopStarted = false;
565 desktop->stop();
566 stopFrameClock();
567 }
568}
569
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000570int VNCServerST::authClientCount() {
571 int count = 0;
572 std::list<VNCSConnectionST*>::iterator ci;
573 for (ci = clients.begin(); ci != clients.end(); ci++) {
574 if ((*ci)->authenticated())
575 count++;
576 }
577 return count;
578}
579
580inline bool VNCServerST::needRenderedCursor()
581{
582 std::list<VNCSConnectionST*>::iterator ci;
583 for (ci = clients.begin(); ci != clients.end(); ci++)
584 if ((*ci)->needRenderedCursor()) return true;
585 return false;
586}
587
Pierre Ossman6e49e952016-10-07 15:59:38 +0200588void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000589{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200590 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000591 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000592 if (blockCounter > 0)
593 return;
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100594 if (!desktopStarted)
595 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000596
Pierre Ossman7be73d72017-11-06 13:16:35 +0100597 // The first iteration will be just half a frame as we get a very
598 // unstable update rate if we happen to be perfectly in sync with
599 // the application's update rate
600 frameTimer.start(1000/rfb::Server::frameRate/2);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000601}
602
Pierre Ossman6e49e952016-10-07 15:59:38 +0200603void VNCServerST::stopFrameClock()
604{
605 frameTimer.stop();
606}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000607
Pierre Ossmana2b80d62018-03-23 09:30:09 +0100608int VNCServerST::msToNextUpdate()
609{
610 // FIXME: If the application is updating slower than frameRate then
611 // we could allow the clients more time here
612
613 if (!frameTimer.isStarted())
614 return 1000/rfb::Server::frameRate/2;
615 else
616 return frameTimer.getRemainingMs();
617}
618
Pierre Ossman6e49e952016-10-07 15:59:38 +0200619// writeUpdate() is called on a regular interval in order to see what
620// updates are pending and propagates them to the update tracker for
621// each client. It uses the ComparingUpdateTracker's compare() method
622// to filter out areas of the screen which haven't actually changed. It
623// also checks the state of the (server-side) rendered cursor, if
624// necessary rendering it again with the correct background.
625
626void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000627{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000628 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200629 Region toCheck;
630
631 std::list<VNCSConnectionST*>::iterator ci, ci_next;
632
633 assert(blockCounter == 0);
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100634 assert(desktopStarted);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200635
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000636 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200637 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000638
Pierre Ossman6e49e952016-10-07 15:59:38 +0200639 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100640 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
641 .translate(cursorPos.subtract(cursor->hotspot()))
642 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000643
Pierre Ossman24684e52016-12-05 16:58:19 +0100644 if (!toCheck.intersect(clippedCursorRect).is_empty())
645 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000646 }
647
648 pb->grabRegion(toCheck);
649
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000650 if (getComparerState())
651 comparer->enable();
652 else
653 comparer->disable();
654
655 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000656 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657
Pierre Ossman6e49e952016-10-07 15:59:38 +0200658 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000660 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
661 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000662 (*ci)->add_copied(ui.copied, ui.copy_delta);
663 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200664 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000665 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200666}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000667
Pierre Ossman6e49e952016-10-07 15:59:38 +0200668// checkUpdate() is called by clients to see if it is safe to read from
669// the framebuffer at this time.
670
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100671Region VNCServerST::getPendingRegion()
Pierre Ossman6e49e952016-10-07 15:59:38 +0200672{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100673 UpdateInfo ui;
674
Pierre Ossman6e49e952016-10-07 15:59:38 +0200675 // Block clients as the frame buffer cannot be safely accessed
676 if (blockCounter > 0)
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100677 return pb->getRect();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200678
679 // Block client from updating if there are pending updates
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100680 if (comparer->is_empty())
681 return Region();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000682
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100683 comparer->getUpdateInfo(&ui, pb->getRect());
684
685 return ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000686}
687
Pierre Ossman24684e52016-12-05 16:58:19 +0100688const RenderedCursor* VNCServerST::getRenderedCursor()
689{
690 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100691 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100692 renderedCursorInvalid = false;
693 }
694
695 return &renderedCursor;
696}
697
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000698void VNCServerST::getConnInfo(ListConnInfo * listConn)
699{
700 listConn->Clear();
701 listConn->setDisable(getDisable());
702 if (clients.empty())
703 return;
704 std::list<VNCSConnectionST*>::iterator i;
705 for (i = clients.begin(); i != clients.end(); i++)
706 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
707 (*i)->getStartTime(), (*i)->getStatus());
708}
709
710void VNCServerST::setConnStatus(ListConnInfo* listConn)
711{
712 setDisable(listConn->getDisable());
713 if (listConn->Empty() || clients.empty()) return;
714 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
715 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
716 std::list<VNCSConnectionST*>::iterator i;
717 for (i = clients.begin(); i != clients.end(); i++) {
718 if ((*i) == conn) {
719 int status = listConn->iGetStatus();
720 if (status == 3) {
721 (*i)->close(0);
722 } else {
723 (*i)->setStatus(status);
724 }
725 break;
726 }
727 }
728 }
729}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000730
Pierre Ossman04e62db2009-03-23 16:57:07 +0000731void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
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) == requester)
737 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000738 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000739 }
740}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000741
742bool VNCServerST::getComparerState()
743{
744 if (rfb::Server::compareFB == 0)
745 return false;
746 if (rfb::Server::compareFB != 2)
747 return true;
748
749 std::list<VNCSConnectionST*>::iterator ci, ci_next;
750 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
751 ci_next = ci; ci_next++;
752 if ((*ci)->getComparerState())
753 return true;
754 }
755 return false;
756}