blob: 80c79fc3a6bd3b2d8685e8cf4a3077d6fedfed9c [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman24684e52016-12-05 16:58:19 +01002 * Copyright 2009-2016 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
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000069rfb::IntParameter deferUpdateTime("DeferUpdate",
DRC4548f302011-12-22 15:57:59 +000070 "Time in milliseconds to defer updates",1);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000071
72rfb::BoolParameter alwaysSetDeferUpdateTimer("AlwaysSetDeferUpdateTimer",
73 "Always reset the defer update timer on every change",false);
74
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075//
76// -=- VNCServerST Implementation
77//
78
79// -=- Constructors/Destructor
80
Adam Tkaca6578bf2010-04-23 14:07:41 +000081VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000082 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
83 blockCounter(0), pb(0),
Adam Tkacd36b6262009-09-04 10:57:20 +000084 name(strDup(name_)), pointerClient(0), comparer(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000087 lastConnectionTime(0), disableclients(false),
88 deferTimer(this), deferPending(false)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089{
90 lastUserInputTime = lastDisconnectTime = time(0);
91 slog.debug("creating single-threaded server %s", name.buf);
92}
93
94VNCServerST::~VNCServerST()
95{
96 slog.debug("shutting down server %s", name.buf);
97
98 // Close any active clients, with appropriate logging & cleanup
99 closeClients("Server shutdown");
100
101 // Delete all the clients, and their sockets, and any closing sockets
102 // NB: Deleting a client implicitly removes it from the clients list
103 while (!clients.empty()) {
104 delete clients.front();
105 }
106
107 // Stop the desktop object if active, *only* after deleting all clients!
108 if (desktopStarted) {
109 desktopStarted = false;
110 desktop->stop();
111 }
112
Pierre Ossman05338bc2016-11-08 14:57:11 +0100113 if (comparer)
114 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 delete comparer;
116}
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++;
286}
287
288void VNCServerST::unblockUpdates()
289{
290 assert(blockCounter > 0);
291
292 blockCounter--;
293
294 // Flush out any updates we might have blocked
295 if (blockCounter == 0)
296 tryUpdate();
297}
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) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000311 if (desktopStarted)
312 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000313 return;
314 }
315
316 comparer = new ComparingUpdateTracker(pb);
317 cursor.setPF(pb->getPF());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100318 renderedCursorInvalid = true;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000319
320 // Make sure that we have at least one screen
321 if (screenLayout.num_screens() == 0)
322 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
323
324 std::list<VNCSConnectionST*>::iterator ci, ci_next;
325 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
326 ci_next = ci; ci_next++;
327 (*ci)->pixelBufferChange();
328 // Since the new pixel buffer means an ExtendedDesktopSize needs to
329 // be sent anyway, we don't need to call screenLayoutChange.
330 }
331}
332
333void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
334{
335 ScreenSet layout;
336
337 if (!pb_) {
338 if (desktopStarted)
339 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
340 return;
341 }
342
343 layout = screenLayout;
344
345 // Check that the screen layout is still valid
346 if (!layout.validate(pb_->width(), pb_->height())) {
347 Rect fbRect;
348 ScreenSet::iterator iter, iter_next;
349
350 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
351
352 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
353 iter_next = iter; ++iter_next;
354 if (iter->dimensions.enclosed_by(fbRect))
355 continue;
356 iter->dimensions = iter->dimensions.intersect(fbRect);
357 if (iter->dimensions.is_empty()) {
358 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
359 (int)iter->id, (unsigned)iter->id);
360 layout.remove_screen(iter->id);
361 }
362 }
363 }
364
365 setPixelBuffer(pb_, layout);
366}
367
368void VNCServerST::setScreenLayout(const ScreenSet& layout)
369{
370 if (!pb)
371 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
372 if (!layout.validate(pb->width(), pb->height()))
373 throw Exception("setScreenLayout: invalid screen layout");
374
Pierre Ossmandf453202009-04-02 14:26:45 +0000375 screenLayout = layout;
376
Pierre Ossman04e62db2009-03-23 16:57:07 +0000377 std::list<VNCSConnectionST*>::iterator ci, ci_next;
378 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
379 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000380 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000381 }
382}
383
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000384void VNCServerST::bell()
385{
386 std::list<VNCSConnectionST*>::iterator ci, ci_next;
387 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
388 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000389 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000390 }
391}
392
393void VNCServerST::serverCutText(const char* str, int len)
394{
395 std::list<VNCSConnectionST*>::iterator ci, ci_next;
396 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
397 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000398 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000399 }
400}
401
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000402void VNCServerST::setName(const char* name_)
403{
Adam Tkacd36b6262009-09-04 10:57:20 +0000404 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000405 std::list<VNCSConnectionST*>::iterator ci, ci_next;
406 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
407 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000408 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000409 }
410}
411
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412void VNCServerST::add_changed(const Region& region)
413{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000414 if (comparer == NULL)
415 return;
416
417 comparer->add_changed(region);
418 startDefer();
419 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000420}
421
422void VNCServerST::add_copied(const Region& dest, const Point& delta)
423{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000424 if (comparer == NULL)
425 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000426
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000427 comparer->add_copied(dest, delta);
428 startDefer();
429 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430}
431
432void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100433 const void* data, const void* mask)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000434{
435 cursor.hotspot = newHotspot;
436 cursor.setSize(width, height);
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100437 cursor.imageRect(cursor.getRect(), data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000438 memcpy(cursor.mask.buf, mask, cursor.maskLen());
439
440 cursor.crop();
441
442 renderedCursorInvalid = true;
443
444 std::list<VNCSConnectionST*>::iterator ci, ci_next;
445 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
446 ci_next = ci; ci_next++;
447 (*ci)->renderedCursorChange();
448 (*ci)->setCursorOrClose();
449 }
450}
451
452void VNCServerST::setCursorPos(const Point& pos)
453{
454 if (!cursorPos.equals(pos)) {
455 cursorPos = pos;
456 renderedCursorInvalid = true;
457 std::list<VNCSConnectionST*>::iterator ci;
458 for (ci = clients.begin(); ci != clients.end(); ci++)
459 (*ci)->renderedCursorChange();
460 }
461}
462
463// Other public methods
464
465void VNCServerST::approveConnection(network::Socket* sock, bool accept,
466 const char* reason)
467{
468 std::list<VNCSConnectionST*>::iterator ci;
469 for (ci = clients.begin(); ci != clients.end(); ci++) {
470 if ((*ci)->getSock() == sock) {
471 (*ci)->approveConnectionOrClose(accept, reason);
472 return;
473 }
474 }
475}
476
477void VNCServerST::closeClients(const char* reason, network::Socket* except)
478{
479 std::list<VNCSConnectionST*>::iterator i, next_i;
480 for (i=clients.begin(); i!=clients.end(); i=next_i) {
481 next_i = i; next_i++;
482 if ((*i)->getSock() != except)
483 (*i)->close(reason);
484 }
485}
486
487void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
488{
489 sockets->clear();
490 std::list<VNCSConnectionST*>::iterator ci;
491 for (ci = clients.begin(); ci != clients.end(); ci++) {
492 sockets->push_back((*ci)->getSock());
493 }
494 std::list<network::Socket*>::iterator si;
495 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
496 sockets->push_back(*si);
497 }
498}
499
500SConnection* VNCServerST::getSConnection(network::Socket* sock) {
501 std::list<VNCSConnectionST*>::iterator ci;
502 for (ci = clients.begin(); ci != clients.end(); ci++) {
503 if ((*ci)->getSock() == sock)
504 return *ci;
505 }
506 return 0;
507}
508
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000509bool VNCServerST::handleTimeout(Timer* t)
510{
511 if (t != &deferTimer)
512 return false;
513
514 tryUpdate();
515
516 return false;
517}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000518
519// -=- Internal methods
520
521void VNCServerST::startDesktop()
522{
523 if (!desktopStarted) {
524 slog.debug("starting desktop");
525 desktop->start(this);
526 desktopStarted = true;
527 if (!pb)
528 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
529 }
530}
531
532int VNCServerST::authClientCount() {
533 int count = 0;
534 std::list<VNCSConnectionST*>::iterator ci;
535 for (ci = clients.begin(); ci != clients.end(); ci++) {
536 if ((*ci)->authenticated())
537 count++;
538 }
539 return count;
540}
541
542inline bool VNCServerST::needRenderedCursor()
543{
544 std::list<VNCSConnectionST*>::iterator ci;
545 for (ci = clients.begin(); ci != clients.end(); ci++)
546 if ((*ci)->needRenderedCursor()) return true;
547 return false;
548}
549
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000550inline void VNCServerST::startDefer()
551{
552 if (deferUpdateTime == 0)
553 return;
554
555 if (deferPending && !alwaysSetDeferUpdateTimer)
556 return;
557
558 gettimeofday(&deferStart, NULL);
559 deferTimer.start(deferUpdateTime);
560
561 deferPending = true;
562}
563
564inline bool VNCServerST::checkDefer()
565{
566 if (!deferPending)
567 return true;
568
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100569 if (msSince(&deferStart) >= (unsigned)deferUpdateTime)
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000570 return true;
571
572 return false;
573}
574
575void VNCServerST::tryUpdate()
576{
577 std::list<VNCSConnectionST*>::iterator ci, ci_next;
578
Pierre Ossman559a2e82012-01-23 15:54:11 +0000579 if (blockCounter > 0)
580 return;
581
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000582 if (!checkDefer())
583 return;
584
585 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
586 ci_next = ci; ci_next++;
587 (*ci)->writeFramebufferUpdateOrClose();
588 }
589}
590
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000591// checkUpdate() is called just before sending an update. It checks to see
592// what updates are pending and propagates them to the update tracker for each
593// client. It uses the ComparingUpdateTracker's compare() method to filter out
594// areas of the screen which haven't actually changed. It also checks the
595// state of the (server-side) rendered cursor, if necessary rendering it again
596// with the correct background.
597
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000598bool VNCServerST::checkUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000599{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000600 UpdateInfo ui;
601 comparer->getUpdateInfo(&ui, pb->getRect());
602
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000603 bool renderCursor = needRenderedCursor();
604
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000605 if (ui.is_empty() && !(renderCursor && renderedCursorInvalid))
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000606 return true;
607
Pierre Ossman559a2e82012-01-23 15:54:11 +0000608 // Block clients as the frame buffer cannot be safely accessed
609 if (blockCounter > 0)
610 return false;
611
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000612 // Block client from updating if we are currently deferring updates
613 if (!checkDefer())
614 return false;
615
616 deferPending = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000617
Pierre Ossman02e43d72009-03-05 11:57:11 +0000618 Region toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000619
620 if (renderCursor) {
621 Rect clippedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100622 = cursor.getRect(cursorPos.subtract(cursor.hotspot)).intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623
Pierre Ossman24684e52016-12-05 16:58:19 +0100624 if (!toCheck.intersect(clippedCursorRect).is_empty())
625 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000626 }
627
628 pb->grabRegion(toCheck);
629
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000630 if (getComparerState())
631 comparer->enable();
632 else
633 comparer->disable();
634
635 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000636 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000638 std::list<VNCSConnectionST*>::iterator ci, ci_next;
639 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
640 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000641 (*ci)->add_copied(ui.copied, ui.copy_delta);
642 (*ci)->add_changed(ui.changed);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000643 }
644
645 comparer->clear();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000646
647 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000648}
649
Pierre Ossman24684e52016-12-05 16:58:19 +0100650const RenderedCursor* VNCServerST::getRenderedCursor()
651{
652 if (renderedCursorInvalid) {
653 renderedCursor.update(pb, &cursor, cursorPos);
654 renderedCursorInvalid = false;
655 }
656
657 return &renderedCursor;
658}
659
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000660void VNCServerST::getConnInfo(ListConnInfo * listConn)
661{
662 listConn->Clear();
663 listConn->setDisable(getDisable());
664 if (clients.empty())
665 return;
666 std::list<VNCSConnectionST*>::iterator i;
667 for (i = clients.begin(); i != clients.end(); i++)
668 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
669 (*i)->getStartTime(), (*i)->getStatus());
670}
671
672void VNCServerST::setConnStatus(ListConnInfo* listConn)
673{
674 setDisable(listConn->getDisable());
675 if (listConn->Empty() || clients.empty()) return;
676 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
677 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
678 std::list<VNCSConnectionST*>::iterator i;
679 for (i = clients.begin(); i != clients.end(); i++) {
680 if ((*i) == conn) {
681 int status = listConn->iGetStatus();
682 if (status == 3) {
683 (*i)->close(0);
684 } else {
685 (*i)->setStatus(status);
686 }
687 break;
688 }
689 }
690 }
691}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000692
Pierre Ossman04e62db2009-03-23 16:57:07 +0000693void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
694{
695 std::list<VNCSConnectionST*>::iterator ci, ci_next;
696 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
697 ci_next = ci; ci_next++;
698 if ((*ci) == requester)
699 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000700 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000701 }
702}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000703
704bool VNCServerST::getComparerState()
705{
706 if (rfb::Server::compareFB == 0)
707 return false;
708 if (rfb::Server::compareFB != 2)
709 return true;
710
711 std::list<VNCSConnectionST*>::iterator ci, ci_next;
712 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
713 ci_next = ci; ci_next++;
714 if ((*ci)->getComparerState())
715 return true;
716 }
717 return false;
718}