blob: 81eed37a200e3a05529722091100c83c48a1ba74 [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
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),
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010085 cursor(new Cursor(0, 0, Point(), NULL)),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000088 lastConnectionTime(0), disableclients(false),
89 deferTimer(this), deferPending(false)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090{
91 lastUserInputTime = lastDisconnectTime = time(0);
92 slog.debug("creating single-threaded server %s", name.buf);
93}
94
95VNCServerST::~VNCServerST()
96{
97 slog.debug("shutting down server %s", name.buf);
98
99 // Close any active clients, with appropriate logging & cleanup
100 closeClients("Server shutdown");
101
102 // Delete all the clients, and their sockets, and any closing sockets
103 // NB: Deleting a client implicitly removes it from the clients list
104 while (!clients.empty()) {
105 delete clients.front();
106 }
107
108 // Stop the desktop object if active, *only* after deleting all clients!
109 if (desktopStarted) {
110 desktopStarted = false;
111 desktop->stop();
112 }
113
Pierre Ossman05338bc2016-11-08 14:57:11 +0100114 if (comparer)
115 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100117
118 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119}
120
121
122// SocketServer methods
123
124void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
125{
126 // - Check the connection isn't black-marked
127 // *** do this in getSecurity instead?
128 CharArray address(sock->getPeerAddress());
129 if (blHosts->isBlackmarked(address.buf)) {
130 connectionsLog.error("blacklisted: %s", address.buf);
131 try {
132 SConnection::writeConnFailedFromScratch("Too many security failures",
133 &sock->outStream());
134 } catch (rdr::Exception&) {
135 }
136 sock->shutdown();
137 closingSockets.push_back(sock);
138 return;
139 }
140
141 if (clients.empty()) {
142 lastConnectionTime = time(0);
143 }
144
145 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
146 client->init();
147}
148
149void VNCServerST::removeSocket(network::Socket* sock) {
150 // - If the socket has resources allocated to it, delete them
151 std::list<VNCSConnectionST*>::iterator ci;
152 for (ci = clients.begin(); ci != clients.end(); ci++) {
153 if ((*ci)->getSock() == sock) {
154 // - Delete the per-Socket resources
155 delete *ci;
156
157 // - Check that the desktop object is still required
158 if (authClientCount() == 0 && desktopStarted) {
159 slog.debug("no authenticated clients - stopping desktop");
160 desktopStarted = false;
161 desktop->stop();
162 }
Pierre Ossman05338bc2016-11-08 14:57:11 +0100163
164 if (comparer)
165 comparer->logStats();
166
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000167 return;
168 }
169 }
170
171 // - If the Socket has no resources, it may have been a closingSocket
172 closingSockets.remove(sock);
173}
174
Pierre Ossmand408ca52016-04-29 14:26:05 +0200175void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176{
177 // - Find the appropriate VNCSConnectionST and process the event
178 std::list<VNCSConnectionST*>::iterator ci;
179 for (ci = clients.begin(); ci != clients.end(); ci++) {
180 if ((*ci)->getSock() == sock) {
181 (*ci)->processMessages();
182 return;
183 }
184 }
185 throw rdr::Exception("invalid Socket in VNCServerST");
186}
187
Pierre Ossmand408ca52016-04-29 14:26:05 +0200188void VNCServerST::processSocketWriteEvent(network::Socket* sock)
189{
190 // - Find the appropriate VNCSConnectionST and process the event
191 std::list<VNCSConnectionST*>::iterator ci;
192 for (ci = clients.begin(); ci != clients.end(); ci++) {
193 if ((*ci)->getSock() == sock) {
194 (*ci)->flushSocket();
195 return;
196 }
197 }
198 throw rdr::Exception("invalid Socket in VNCServerST");
199}
200
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201int VNCServerST::checkTimeouts()
202{
203 int timeout = 0;
204 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000205
206 soonestTimeout(&timeout, Timer::checkTimeouts());
207
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
209 ci_next = ci; ci_next++;
210 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
211 }
212
213 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000214 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 // Check MaxDisconnectionTime
217 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
218 if (now < lastDisconnectTime) {
219 // Someone must have set the time backwards.
220 slog.info("Time has gone backwards - resetting lastDisconnectTime");
221 lastDisconnectTime = now;
222 }
223 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
224 if (timeLeft < -60) {
225 // Someone must have set the time forwards.
226 slog.info("Time has gone forwards - resetting lastDisconnectTime");
227 lastDisconnectTime = now;
228 timeLeft = rfb::Server::maxDisconnectionTime;
229 }
230 if (timeLeft <= 0) {
231 slog.info("MaxDisconnectionTime reached, exiting");
232 exit(0);
233 }
234 soonestTimeout(&timeout, timeLeft * 1000);
235 }
236
237 // Check MaxConnectionTime
238 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
239 if (now < lastConnectionTime) {
240 // Someone must have set the time backwards.
241 slog.info("Time has gone backwards - resetting lastConnectionTime");
242 lastConnectionTime = now;
243 }
244 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
245 if (timeLeft < -60) {
246 // Someone must have set the time forwards.
247 slog.info("Time has gone forwards - resetting lastConnectionTime");
248 lastConnectionTime = now;
249 timeLeft = rfb::Server::maxConnectionTime;
250 }
251 if (timeLeft <= 0) {
252 slog.info("MaxConnectionTime reached, exiting");
253 exit(0);
254 }
255 soonestTimeout(&timeout, timeLeft * 1000);
256 }
257
258
259 // Check MaxIdleTime
260 if (rfb::Server::maxIdleTime) {
261 if (now < lastUserInputTime) {
262 // Someone must have set the time backwards.
263 slog.info("Time has gone backwards - resetting lastUserInputTime");
264 lastUserInputTime = now;
265 }
266 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
267 if (timeLeft < -60) {
268 // Someone must have set the time forwards.
269 slog.info("Time has gone forwards - resetting lastUserInputTime");
270 lastUserInputTime = now;
271 timeLeft = rfb::Server::maxIdleTime;
272 }
273 if (timeLeft <= 0) {
274 slog.info("MaxIdleTime reached, exiting");
275 exit(0);
276 }
277 soonestTimeout(&timeout, timeLeft * 1000);
278 }
279
280 return timeout;
281}
282
283
284// VNCServer methods
285
Pierre Ossman559a2e82012-01-23 15:54:11 +0000286void VNCServerST::blockUpdates()
287{
288 blockCounter++;
289}
290
291void VNCServerST::unblockUpdates()
292{
293 assert(blockCounter > 0);
294
295 blockCounter--;
296
297 // Flush out any updates we might have blocked
298 if (blockCounter == 0)
299 tryUpdate();
300}
301
Pierre Ossman04e62db2009-03-23 16:57:07 +0000302void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000303{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100304 if (comparer)
305 comparer->logStats();
306
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000307 pb = pb_;
308 delete comparer;
309 comparer = 0;
310
Pierre Ossman04e62db2009-03-23 16:57:07 +0000311 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000312
Pierre Ossman04e62db2009-03-23 16:57:07 +0000313 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000314 if (desktopStarted)
315 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
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 Ossman04e62db2009-03-23 16:57:07 +0000321
322 // Make sure that we have at least one screen
323 if (screenLayout.num_screens() == 0)
324 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
325
326 std::list<VNCSConnectionST*>::iterator ci, ci_next;
327 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
328 ci_next = ci; ci_next++;
329 (*ci)->pixelBufferChange();
330 // Since the new pixel buffer means an ExtendedDesktopSize needs to
331 // be sent anyway, we don't need to call screenLayoutChange.
332 }
333}
334
335void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
336{
337 ScreenSet layout;
338
339 if (!pb_) {
340 if (desktopStarted)
341 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
342 return;
343 }
344
345 layout = screenLayout;
346
347 // Check that the screen layout is still valid
348 if (!layout.validate(pb_->width(), pb_->height())) {
349 Rect fbRect;
350 ScreenSet::iterator iter, iter_next;
351
352 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
353
354 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
355 iter_next = iter; ++iter_next;
356 if (iter->dimensions.enclosed_by(fbRect))
357 continue;
358 iter->dimensions = iter->dimensions.intersect(fbRect);
359 if (iter->dimensions.is_empty()) {
360 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
361 (int)iter->id, (unsigned)iter->id);
362 layout.remove_screen(iter->id);
363 }
364 }
365 }
366
367 setPixelBuffer(pb_, layout);
368}
369
370void VNCServerST::setScreenLayout(const ScreenSet& layout)
371{
372 if (!pb)
373 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
374 if (!layout.validate(pb->width(), pb->height()))
375 throw Exception("setScreenLayout: invalid screen layout");
376
Pierre Ossmandf453202009-04-02 14:26:45 +0000377 screenLayout = layout;
378
Pierre Ossman04e62db2009-03-23 16:57:07 +0000379 std::list<VNCSConnectionST*>::iterator ci, ci_next;
380 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
381 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000382 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000383 }
384}
385
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000386void VNCServerST::bell()
387{
388 std::list<VNCSConnectionST*>::iterator ci, ci_next;
389 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
390 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000391 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000392 }
393}
394
395void VNCServerST::serverCutText(const char* str, int len)
396{
397 std::list<VNCSConnectionST*>::iterator ci, ci_next;
398 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
399 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000400 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000401 }
402}
403
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000404void VNCServerST::setName(const char* name_)
405{
Adam Tkacd36b6262009-09-04 10:57:20 +0000406 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000407 std::list<VNCSConnectionST*>::iterator ci, ci_next;
408 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
409 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000410 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000411 }
412}
413
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000414void VNCServerST::add_changed(const Region& region)
415{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000416 if (comparer == NULL)
417 return;
418
419 comparer->add_changed(region);
420 startDefer();
421 tryUpdate();
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);
430 startDefer();
431 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432}
433
434void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100435 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000436{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100437 delete cursor;
438 cursor = new Cursor(width, height, newHotspot, data);
439 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000440
441 renderedCursorInvalid = true;
442
443 std::list<VNCSConnectionST*>::iterator ci, ci_next;
444 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
445 ci_next = ci; ci_next++;
446 (*ci)->renderedCursorChange();
447 (*ci)->setCursorOrClose();
448 }
449}
450
451void VNCServerST::setCursorPos(const Point& pos)
452{
453 if (!cursorPos.equals(pos)) {
454 cursorPos = pos;
455 renderedCursorInvalid = true;
456 std::list<VNCSConnectionST*>::iterator ci;
457 for (ci = clients.begin(); ci != clients.end(); ci++)
458 (*ci)->renderedCursorChange();
459 }
460}
461
462// Other public methods
463
464void VNCServerST::approveConnection(network::Socket* sock, bool accept,
465 const char* reason)
466{
467 std::list<VNCSConnectionST*>::iterator ci;
468 for (ci = clients.begin(); ci != clients.end(); ci++) {
469 if ((*ci)->getSock() == sock) {
470 (*ci)->approveConnectionOrClose(accept, reason);
471 return;
472 }
473 }
474}
475
476void VNCServerST::closeClients(const char* reason, network::Socket* except)
477{
478 std::list<VNCSConnectionST*>::iterator i, next_i;
479 for (i=clients.begin(); i!=clients.end(); i=next_i) {
480 next_i = i; next_i++;
481 if ((*i)->getSock() != except)
482 (*i)->close(reason);
483 }
484}
485
486void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
487{
488 sockets->clear();
489 std::list<VNCSConnectionST*>::iterator ci;
490 for (ci = clients.begin(); ci != clients.end(); ci++) {
491 sockets->push_back((*ci)->getSock());
492 }
493 std::list<network::Socket*>::iterator si;
494 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
495 sockets->push_back(*si);
496 }
497}
498
499SConnection* VNCServerST::getSConnection(network::Socket* sock) {
500 std::list<VNCSConnectionST*>::iterator ci;
501 for (ci = clients.begin(); ci != clients.end(); ci++) {
502 if ((*ci)->getSock() == sock)
503 return *ci;
504 }
505 return 0;
506}
507
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000508bool VNCServerST::handleTimeout(Timer* t)
509{
510 if (t != &deferTimer)
511 return false;
512
513 tryUpdate();
514
515 return false;
516}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000517
518// -=- Internal methods
519
520void VNCServerST::startDesktop()
521{
522 if (!desktopStarted) {
523 slog.debug("starting desktop");
524 desktop->start(this);
525 desktopStarted = true;
526 if (!pb)
527 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
528 }
529}
530
531int VNCServerST::authClientCount() {
532 int count = 0;
533 std::list<VNCSConnectionST*>::iterator ci;
534 for (ci = clients.begin(); ci != clients.end(); ci++) {
535 if ((*ci)->authenticated())
536 count++;
537 }
538 return count;
539}
540
541inline bool VNCServerST::needRenderedCursor()
542{
543 std::list<VNCSConnectionST*>::iterator ci;
544 for (ci = clients.begin(); ci != clients.end(); ci++)
545 if ((*ci)->needRenderedCursor()) return true;
546 return false;
547}
548
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000549inline void VNCServerST::startDefer()
550{
551 if (deferUpdateTime == 0)
552 return;
553
554 if (deferPending && !alwaysSetDeferUpdateTimer)
555 return;
556
557 gettimeofday(&deferStart, NULL);
558 deferTimer.start(deferUpdateTime);
559
560 deferPending = true;
561}
562
563inline bool VNCServerST::checkDefer()
564{
565 if (!deferPending)
566 return true;
567
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100568 if (msSince(&deferStart) >= (unsigned)deferUpdateTime)
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000569 return true;
570
571 return false;
572}
573
574void VNCServerST::tryUpdate()
575{
576 std::list<VNCSConnectionST*>::iterator ci, ci_next;
577
Pierre Ossman559a2e82012-01-23 15:54:11 +0000578 if (blockCounter > 0)
579 return;
580
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000581 if (!checkDefer())
582 return;
583
584 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
585 ci_next = ci; ci_next++;
586 (*ci)->writeFramebufferUpdateOrClose();
587 }
588}
589
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000590// checkUpdate() is called just before sending an update. It checks to see
591// what updates are pending and propagates them to the update tracker for each
592// client. It uses the ComparingUpdateTracker's compare() method to filter out
593// areas of the screen which haven't actually changed. It also checks the
594// state of the (server-side) rendered cursor, if necessary rendering it again
595// with the correct background.
596
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000597bool VNCServerST::checkUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000598{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000599 UpdateInfo ui;
600 comparer->getUpdateInfo(&ui, pb->getRect());
601
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000602 bool renderCursor = needRenderedCursor();
603
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000604 if (ui.is_empty() && !(renderCursor && renderedCursorInvalid))
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000605 return true;
606
Pierre Ossman559a2e82012-01-23 15:54:11 +0000607 // Block clients as the frame buffer cannot be safely accessed
608 if (blockCounter > 0)
609 return false;
610
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000611 // Block client from updating if we are currently deferring updates
612 if (!checkDefer())
613 return false;
614
615 deferPending = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000616
Pierre Ossman02e43d72009-03-05 11:57:11 +0000617 Region toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000618
619 if (renderCursor) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100620 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
621 .translate(cursorPos.subtract(cursor->hotspot()))
622 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623
624 if (!renderedCursorInvalid && (toCheck.intersect(clippedCursorRect)
625 .is_empty())) {
626 renderCursor = false;
627 } else {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000628 toCheck.assign_union(clippedCursorRect);
629 }
630 }
631
632 pb->grabRegion(toCheck);
633
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000634 if (getComparerState())
635 comparer->enable();
636 else
637 comparer->disable();
638
639 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000640 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000641
642 if (renderCursor) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100643 renderedCursor.update(pb, cursor, cursorPos);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000644 renderedCursorInvalid = false;
645 }
646
647 std::list<VNCSConnectionST*>::iterator ci, ci_next;
648 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);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000652 }
653
654 comparer->clear();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000655
656 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657}
658
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659void VNCServerST::getConnInfo(ListConnInfo * listConn)
660{
661 listConn->Clear();
662 listConn->setDisable(getDisable());
663 if (clients.empty())
664 return;
665 std::list<VNCSConnectionST*>::iterator i;
666 for (i = clients.begin(); i != clients.end(); i++)
667 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
668 (*i)->getStartTime(), (*i)->getStatus());
669}
670
671void VNCServerST::setConnStatus(ListConnInfo* listConn)
672{
673 setDisable(listConn->getDisable());
674 if (listConn->Empty() || clients.empty()) return;
675 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
676 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
677 std::list<VNCSConnectionST*>::iterator i;
678 for (i = clients.begin(); i != clients.end(); i++) {
679 if ((*i) == conn) {
680 int status = listConn->iGetStatus();
681 if (status == 3) {
682 (*i)->close(0);
683 } else {
684 (*i)->setStatus(status);
685 }
686 break;
687 }
688 }
689 }
690}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000691
Pierre Ossman04e62db2009-03-23 16:57:07 +0000692void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
693{
694 std::list<VNCSConnectionST*>::iterator ci, ci_next;
695 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
696 ci_next = ci; ci_next++;
697 if ((*ci) == requester)
698 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000699 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000700 }
701}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000702
703bool VNCServerST::getComparerState()
704{
705 if (rfb::Server::compareFB == 0)
706 return false;
707 if (rfb::Server::compareFB != 2)
708 return true;
709
710 std::list<VNCSConnectionST*>::iterator ci, ci_next;
711 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
712 ci_next = ci; ci_next++;
713 if ((*ci)->getComparerState())
714 return true;
715 }
716 return false;
717}