blob: ec5e962f04bf142d8447fb76c8b65956a52ecccb [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054#include <rfb/ServerCore.h>
55#include <rfb/VNCServerST.h>
56#include <rfb/VNCSConnectionST.h>
57#include <rfb/ComparingUpdateTracker.h>
Adam Tkaca6578bf2010-04-23 14:07:41 +000058#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059#include <rfb/KeyRemapper.h>
60#include <rfb/util.h>
61
62#include <rdr/types.h>
63
64using namespace rfb;
65
66static LogWriter slog("VNCServerST");
67LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068
69//
70// -=- VNCServerST Implementation
71//
72
73// -=- Constructors/Destructor
74
Adam Tkaca6578bf2010-04-23 14:07:41 +000075VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000076 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
77 blockCounter(0), pb(0),
Adam Tkacd36b6262009-09-04 10:57:20 +000078 name(strDup(name_)), pointerClient(0), comparer(0),
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010079 cursor(new Cursor(0, 0, Point(), NULL)),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000082 lastConnectionTime(0), disableclients(false),
Pierre Ossman6e49e952016-10-07 15:59:38 +020083 frameTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084{
85 lastUserInputTime = lastDisconnectTime = time(0);
86 slog.debug("creating single-threaded server %s", name.buf);
87}
88
89VNCServerST::~VNCServerST()
90{
91 slog.debug("shutting down server %s", name.buf);
92
93 // Close any active clients, with appropriate logging & cleanup
94 closeClients("Server shutdown");
95
Pierre Ossman6e49e952016-10-07 15:59:38 +020096 // Stop trying to render things
97 stopFrameClock();
98
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 // Delete all the clients, and their sockets, and any closing sockets
100 // NB: Deleting a client implicitly removes it from the clients list
101 while (!clients.empty()) {
102 delete clients.front();
103 }
104
105 // Stop the desktop object if active, *only* after deleting all clients!
106 if (desktopStarted) {
107 desktopStarted = false;
108 desktop->stop();
109 }
110
Pierre Ossman05338bc2016-11-08 14:57:11 +0100111 if (comparer)
112 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100114
115 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116}
117
118
119// SocketServer methods
120
121void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
122{
123 // - Check the connection isn't black-marked
124 // *** do this in getSecurity instead?
125 CharArray address(sock->getPeerAddress());
126 if (blHosts->isBlackmarked(address.buf)) {
127 connectionsLog.error("blacklisted: %s", address.buf);
128 try {
129 SConnection::writeConnFailedFromScratch("Too many security failures",
130 &sock->outStream());
131 } catch (rdr::Exception&) {
132 }
133 sock->shutdown();
134 closingSockets.push_back(sock);
135 return;
136 }
137
138 if (clients.empty()) {
139 lastConnectionTime = time(0);
140 }
141
142 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
143 client->init();
144}
145
146void VNCServerST::removeSocket(network::Socket* sock) {
147 // - If the socket has resources allocated to it, delete them
148 std::list<VNCSConnectionST*>::iterator ci;
149 for (ci = clients.begin(); ci != clients.end(); ci++) {
150 if ((*ci)->getSock() == sock) {
151 // - Delete the per-Socket resources
152 delete *ci;
153
154 // - Check that the desktop object is still required
155 if (authClientCount() == 0 && desktopStarted) {
156 slog.debug("no authenticated clients - stopping desktop");
157 desktopStarted = false;
158 desktop->stop();
159 }
Pierre Ossman05338bc2016-11-08 14:57:11 +0100160
161 if (comparer)
162 comparer->logStats();
163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 return;
165 }
166 }
167
168 // - If the Socket has no resources, it may have been a closingSocket
169 closingSockets.remove(sock);
170}
171
Pierre Ossmand408ca52016-04-29 14:26:05 +0200172void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173{
174 // - Find the appropriate VNCSConnectionST and process the event
175 std::list<VNCSConnectionST*>::iterator ci;
176 for (ci = clients.begin(); ci != clients.end(); ci++) {
177 if ((*ci)->getSock() == sock) {
178 (*ci)->processMessages();
179 return;
180 }
181 }
182 throw rdr::Exception("invalid Socket in VNCServerST");
183}
184
Pierre Ossmand408ca52016-04-29 14:26:05 +0200185void VNCServerST::processSocketWriteEvent(network::Socket* sock)
186{
187 // - Find the appropriate VNCSConnectionST and process the event
188 std::list<VNCSConnectionST*>::iterator ci;
189 for (ci = clients.begin(); ci != clients.end(); ci++) {
190 if ((*ci)->getSock() == sock) {
191 (*ci)->flushSocket();
192 return;
193 }
194 }
195 throw rdr::Exception("invalid Socket in VNCServerST");
196}
197
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198int VNCServerST::checkTimeouts()
199{
200 int timeout = 0;
201 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000202
203 soonestTimeout(&timeout, Timer::checkTimeouts());
204
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
206 ci_next = ci; ci_next++;
207 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
208 }
209
210 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000211 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213 // Check MaxDisconnectionTime
214 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
215 if (now < lastDisconnectTime) {
216 // Someone must have set the time backwards.
217 slog.info("Time has gone backwards - resetting lastDisconnectTime");
218 lastDisconnectTime = now;
219 }
220 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
221 if (timeLeft < -60) {
222 // Someone must have set the time forwards.
223 slog.info("Time has gone forwards - resetting lastDisconnectTime");
224 lastDisconnectTime = now;
225 timeLeft = rfb::Server::maxDisconnectionTime;
226 }
227 if (timeLeft <= 0) {
228 slog.info("MaxDisconnectionTime reached, exiting");
229 exit(0);
230 }
231 soonestTimeout(&timeout, timeLeft * 1000);
232 }
233
234 // Check MaxConnectionTime
235 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
236 if (now < lastConnectionTime) {
237 // Someone must have set the time backwards.
238 slog.info("Time has gone backwards - resetting lastConnectionTime");
239 lastConnectionTime = now;
240 }
241 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
242 if (timeLeft < -60) {
243 // Someone must have set the time forwards.
244 slog.info("Time has gone forwards - resetting lastConnectionTime");
245 lastConnectionTime = now;
246 timeLeft = rfb::Server::maxConnectionTime;
247 }
248 if (timeLeft <= 0) {
249 slog.info("MaxConnectionTime reached, exiting");
250 exit(0);
251 }
252 soonestTimeout(&timeout, timeLeft * 1000);
253 }
254
255
256 // Check MaxIdleTime
257 if (rfb::Server::maxIdleTime) {
258 if (now < lastUserInputTime) {
259 // Someone must have set the time backwards.
260 slog.info("Time has gone backwards - resetting lastUserInputTime");
261 lastUserInputTime = now;
262 }
263 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
264 if (timeLeft < -60) {
265 // Someone must have set the time forwards.
266 slog.info("Time has gone forwards - resetting lastUserInputTime");
267 lastUserInputTime = now;
268 timeLeft = rfb::Server::maxIdleTime;
269 }
270 if (timeLeft <= 0) {
271 slog.info("MaxIdleTime reached, exiting");
272 exit(0);
273 }
274 soonestTimeout(&timeout, timeLeft * 1000);
275 }
276
277 return timeout;
278}
279
280
281// VNCServer methods
282
Pierre Ossman559a2e82012-01-23 15:54:11 +0000283void VNCServerST::blockUpdates()
284{
285 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200286
287 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000288}
289
290void VNCServerST::unblockUpdates()
291{
292 assert(blockCounter > 0);
293
294 blockCounter--;
295
Pierre Ossman6e49e952016-10-07 15:59:38 +0200296 // Restart the frame clock if we have updates
297 if (blockCounter == 0) {
298 if (!comparer->is_empty())
299 startFrameClock();
300 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000301}
302
Pierre Ossman04e62db2009-03-23 16:57:07 +0000303void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100305 if (comparer)
306 comparer->logStats();
307
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000308 pb = pb_;
309 delete comparer;
310 comparer = 0;
311
Pierre Ossman04e62db2009-03-23 16:57:07 +0000312 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000313
Pierre Ossman04e62db2009-03-23 16:57:07 +0000314 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000315 if (desktopStarted)
316 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000317 return;
318 }
319
320 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100321 renderedCursorInvalid = true;
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{
338 ScreenSet layout;
339
340 if (!pb_) {
341 if (desktopStarted)
342 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
343 return;
344 }
345
346 layout = screenLayout;
347
348 // Check that the screen layout is still valid
349 if (!layout.validate(pb_->width(), pb_->height())) {
350 Rect fbRect;
351 ScreenSet::iterator iter, iter_next;
352
353 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
354
355 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
356 iter_next = iter; ++iter_next;
357 if (iter->dimensions.enclosed_by(fbRect))
358 continue;
359 iter->dimensions = iter->dimensions.intersect(fbRect);
360 if (iter->dimensions.is_empty()) {
361 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
362 (int)iter->id, (unsigned)iter->id);
363 layout.remove_screen(iter->id);
364 }
365 }
366 }
367
368 setPixelBuffer(pb_, layout);
369}
370
371void VNCServerST::setScreenLayout(const ScreenSet& layout)
372{
373 if (!pb)
374 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
375 if (!layout.validate(pb->width(), pb->height()))
376 throw Exception("setScreenLayout: invalid screen layout");
377
Pierre Ossmandf453202009-04-02 14:26:45 +0000378 screenLayout = layout;
379
Pierre Ossman04e62db2009-03-23 16:57:07 +0000380 std::list<VNCSConnectionST*>::iterator ci, ci_next;
381 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
382 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000383 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000384 }
385}
386
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000387void VNCServerST::bell()
388{
389 std::list<VNCSConnectionST*>::iterator ci, ci_next;
390 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
391 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000392 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000393 }
394}
395
396void VNCServerST::serverCutText(const char* str, int len)
397{
398 std::list<VNCSConnectionST*>::iterator ci, ci_next;
399 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
400 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000401 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000402 }
403}
404
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000405void VNCServerST::setName(const char* name_)
406{
Adam Tkacd36b6262009-09-04 10:57:20 +0000407 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000408 std::list<VNCSConnectionST*>::iterator ci, ci_next;
409 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
410 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000411 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000412 }
413}
414
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000415void VNCServerST::add_changed(const Region& region)
416{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000417 if (comparer == NULL)
418 return;
419
420 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200421 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000422}
423
424void VNCServerST::add_copied(const Region& dest, const Point& delta)
425{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000426 if (comparer == NULL)
427 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000428
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000429 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200430 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000431}
432
433void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100434 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000435{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100436 delete cursor;
437 cursor = new Cursor(width, height, newHotspot, data);
438 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000439
440 renderedCursorInvalid = true;
441
442 std::list<VNCSConnectionST*>::iterator ci, ci_next;
443 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
444 ci_next = ci; ci_next++;
445 (*ci)->renderedCursorChange();
446 (*ci)->setCursorOrClose();
447 }
448}
449
450void VNCServerST::setCursorPos(const Point& pos)
451{
452 if (!cursorPos.equals(pos)) {
453 cursorPos = pos;
454 renderedCursorInvalid = true;
455 std::list<VNCSConnectionST*>::iterator ci;
456 for (ci = clients.begin(); ci != clients.end(); ci++)
457 (*ci)->renderedCursorChange();
458 }
459}
460
461// Other public methods
462
463void VNCServerST::approveConnection(network::Socket* sock, bool accept,
464 const char* reason)
465{
466 std::list<VNCSConnectionST*>::iterator ci;
467 for (ci = clients.begin(); ci != clients.end(); ci++) {
468 if ((*ci)->getSock() == sock) {
469 (*ci)->approveConnectionOrClose(accept, reason);
470 return;
471 }
472 }
473}
474
475void VNCServerST::closeClients(const char* reason, network::Socket* except)
476{
477 std::list<VNCSConnectionST*>::iterator i, next_i;
478 for (i=clients.begin(); i!=clients.end(); i=next_i) {
479 next_i = i; next_i++;
480 if ((*i)->getSock() != except)
481 (*i)->close(reason);
482 }
483}
484
485void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
486{
487 sockets->clear();
488 std::list<VNCSConnectionST*>::iterator ci;
489 for (ci = clients.begin(); ci != clients.end(); ci++) {
490 sockets->push_back((*ci)->getSock());
491 }
492 std::list<network::Socket*>::iterator si;
493 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
494 sockets->push_back(*si);
495 }
496}
497
498SConnection* VNCServerST::getSConnection(network::Socket* sock) {
499 std::list<VNCSConnectionST*>::iterator ci;
500 for (ci = clients.begin(); ci != clients.end(); ci++) {
501 if ((*ci)->getSock() == sock)
502 return *ci;
503 }
504 return 0;
505}
506
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000507bool VNCServerST::handleTimeout(Timer* t)
508{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200509 if (t == &frameTimer) {
510 // We keep running until we go a full interval without any updates
511 if (comparer->is_empty())
512 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000513
Pierre Ossman6e49e952016-10-07 15:59:38 +0200514 writeUpdate();
515 return true;
516 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000517
518 return false;
519}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000520
521// -=- Internal methods
522
523void VNCServerST::startDesktop()
524{
525 if (!desktopStarted) {
526 slog.debug("starting desktop");
527 desktop->start(this);
528 desktopStarted = true;
529 if (!pb)
530 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
531 }
532}
533
534int VNCServerST::authClientCount() {
535 int count = 0;
536 std::list<VNCSConnectionST*>::iterator ci;
537 for (ci = clients.begin(); ci != clients.end(); ci++) {
538 if ((*ci)->authenticated())
539 count++;
540 }
541 return count;
542}
543
544inline bool VNCServerST::needRenderedCursor()
545{
546 std::list<VNCSConnectionST*>::iterator ci;
547 for (ci = clients.begin(); ci != clients.end(); ci++)
548 if ((*ci)->needRenderedCursor()) return true;
549 return false;
550}
551
Pierre Ossman6e49e952016-10-07 15:59:38 +0200552void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000553{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200554 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000555 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000556 if (blockCounter > 0)
557 return;
558
Pierre Ossman6e49e952016-10-07 15:59:38 +0200559 frameTimer.start(1000/rfb::Server::frameRate);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000560}
561
Pierre Ossman6e49e952016-10-07 15:59:38 +0200562void VNCServerST::stopFrameClock()
563{
564 frameTimer.stop();
565}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000566
Pierre Ossman6e49e952016-10-07 15:59:38 +0200567// writeUpdate() is called on a regular interval in order to see what
568// updates are pending and propagates them to the update tracker for
569// each client. It uses the ComparingUpdateTracker's compare() method
570// to filter out areas of the screen which haven't actually changed. It
571// also checks the state of the (server-side) rendered cursor, if
572// necessary rendering it again with the correct background.
573
574void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000576 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200577 Region toCheck;
578
579 std::list<VNCSConnectionST*>::iterator ci, ci_next;
580
581 assert(blockCounter == 0);
582
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000583 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200584 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000585
Pierre Ossman6e49e952016-10-07 15:59:38 +0200586 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100587 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
588 .translate(cursorPos.subtract(cursor->hotspot()))
589 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000590
Pierre Ossman24684e52016-12-05 16:58:19 +0100591 if (!toCheck.intersect(clippedCursorRect).is_empty())
592 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593 }
594
595 pb->grabRegion(toCheck);
596
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000597 if (getComparerState())
598 comparer->enable();
599 else
600 comparer->disable();
601
602 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000603 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000604
Pierre Ossman6e49e952016-10-07 15:59:38 +0200605 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000607 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
608 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000609 (*ci)->add_copied(ui.copied, ui.copy_delta);
610 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200611 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000612 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200613}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000614
Pierre Ossman6e49e952016-10-07 15:59:38 +0200615// checkUpdate() is called by clients to see if it is safe to read from
616// the framebuffer at this time.
617
618bool VNCServerST::checkUpdate()
619{
620 // Block clients as the frame buffer cannot be safely accessed
621 if (blockCounter > 0)
622 return false;
623
624 // Block client from updating if there are pending updates
625 if (!comparer->is_empty())
626 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000627
628 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000629}
630
Pierre Ossman24684e52016-12-05 16:58:19 +0100631const RenderedCursor* VNCServerST::getRenderedCursor()
632{
633 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100634 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100635 renderedCursorInvalid = false;
636 }
637
638 return &renderedCursor;
639}
640
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000641void VNCServerST::getConnInfo(ListConnInfo * listConn)
642{
643 listConn->Clear();
644 listConn->setDisable(getDisable());
645 if (clients.empty())
646 return;
647 std::list<VNCSConnectionST*>::iterator i;
648 for (i = clients.begin(); i != clients.end(); i++)
649 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
650 (*i)->getStartTime(), (*i)->getStatus());
651}
652
653void VNCServerST::setConnStatus(ListConnInfo* listConn)
654{
655 setDisable(listConn->getDisable());
656 if (listConn->Empty() || clients.empty()) return;
657 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
658 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
659 std::list<VNCSConnectionST*>::iterator i;
660 for (i = clients.begin(); i != clients.end(); i++) {
661 if ((*i) == conn) {
662 int status = listConn->iGetStatus();
663 if (status == 3) {
664 (*i)->close(0);
665 } else {
666 (*i)->setStatus(status);
667 }
668 break;
669 }
670 }
671 }
672}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000673
Pierre Ossman04e62db2009-03-23 16:57:07 +0000674void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
675{
676 std::list<VNCSConnectionST*>::iterator ci, ci_next;
677 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
678 ci_next = ci; ci_next++;
679 if ((*ci) == requester)
680 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000681 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000682 }
683}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000684
685bool VNCServerST::getComparerState()
686{
687 if (rfb::Server::compareFB == 0)
688 return false;
689 if (rfb::Server::compareFB != 2)
690 return true;
691
692 std::list<VNCSConnectionST*>::iterator ci, ci_next;
693 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
694 ci_next = ci; ci_next++;
695 if ((*ci)->getComparerState())
696 return true;
697 }
698 return false;
699}