blob: 80c992f5c019403133dfeb6ad05734ada2729a44 [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
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),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000081 lastConnectionTime(0), disableclients(false),
Pierre Ossman6e49e952016-10-07 15:59:38 +020082 frameTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083{
84 lastUserInputTime = lastDisconnectTime = time(0);
85 slog.debug("creating single-threaded server %s", name.buf);
86}
87
88VNCServerST::~VNCServerST()
89{
90 slog.debug("shutting down server %s", name.buf);
91
92 // Close any active clients, with appropriate logging & cleanup
93 closeClients("Server shutdown");
94
Pierre Ossman6e49e952016-10-07 15:59:38 +020095 // Stop trying to render things
96 stopFrameClock();
97
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 // Delete all the clients, and their sockets, and any closing sockets
99 // NB: Deleting a client implicitly removes it from the clients list
100 while (!clients.empty()) {
101 delete clients.front();
102 }
103
104 // Stop the desktop object if active, *only* after deleting all clients!
105 if (desktopStarted) {
106 desktopStarted = false;
107 desktop->stop();
108 }
109
Pierre Ossman05338bc2016-11-08 14:57:11 +0100110 if (comparer)
111 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 delete comparer;
113}
114
115
116// SocketServer methods
117
118void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
119{
120 // - Check the connection isn't black-marked
121 // *** do this in getSecurity instead?
122 CharArray address(sock->getPeerAddress());
123 if (blHosts->isBlackmarked(address.buf)) {
124 connectionsLog.error("blacklisted: %s", address.buf);
125 try {
126 SConnection::writeConnFailedFromScratch("Too many security failures",
127 &sock->outStream());
128 } catch (rdr::Exception&) {
129 }
130 sock->shutdown();
131 closingSockets.push_back(sock);
132 return;
133 }
134
135 if (clients.empty()) {
136 lastConnectionTime = time(0);
137 }
138
139 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
140 client->init();
141}
142
143void VNCServerST::removeSocket(network::Socket* sock) {
144 // - If the socket has resources allocated to it, delete them
145 std::list<VNCSConnectionST*>::iterator ci;
146 for (ci = clients.begin(); ci != clients.end(); ci++) {
147 if ((*ci)->getSock() == sock) {
148 // - Delete the per-Socket resources
149 delete *ci;
150
151 // - Check that the desktop object is still required
152 if (authClientCount() == 0 && desktopStarted) {
153 slog.debug("no authenticated clients - stopping desktop");
154 desktopStarted = false;
155 desktop->stop();
156 }
Pierre Ossman05338bc2016-11-08 14:57:11 +0100157
158 if (comparer)
159 comparer->logStats();
160
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 return;
162 }
163 }
164
165 // - If the Socket has no resources, it may have been a closingSocket
166 closingSockets.remove(sock);
167}
168
Pierre Ossmand408ca52016-04-29 14:26:05 +0200169void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170{
171 // - Find the appropriate VNCSConnectionST and process the event
172 std::list<VNCSConnectionST*>::iterator ci;
173 for (ci = clients.begin(); ci != clients.end(); ci++) {
174 if ((*ci)->getSock() == sock) {
175 (*ci)->processMessages();
176 return;
177 }
178 }
179 throw rdr::Exception("invalid Socket in VNCServerST");
180}
181
Pierre Ossmand408ca52016-04-29 14:26:05 +0200182void VNCServerST::processSocketWriteEvent(network::Socket* sock)
183{
184 // - Find the appropriate VNCSConnectionST and process the event
185 std::list<VNCSConnectionST*>::iterator ci;
186 for (ci = clients.begin(); ci != clients.end(); ci++) {
187 if ((*ci)->getSock() == sock) {
188 (*ci)->flushSocket();
189 return;
190 }
191 }
192 throw rdr::Exception("invalid Socket in VNCServerST");
193}
194
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195int VNCServerST::checkTimeouts()
196{
197 int timeout = 0;
198 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000199
200 soonestTimeout(&timeout, Timer::checkTimeouts());
201
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
203 ci_next = ci; ci_next++;
204 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
205 }
206
207 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000208 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210 // Check MaxDisconnectionTime
211 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
212 if (now < lastDisconnectTime) {
213 // Someone must have set the time backwards.
214 slog.info("Time has gone backwards - resetting lastDisconnectTime");
215 lastDisconnectTime = now;
216 }
217 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
218 if (timeLeft < -60) {
219 // Someone must have set the time forwards.
220 slog.info("Time has gone forwards - resetting lastDisconnectTime");
221 lastDisconnectTime = now;
222 timeLeft = rfb::Server::maxDisconnectionTime;
223 }
224 if (timeLeft <= 0) {
225 slog.info("MaxDisconnectionTime reached, exiting");
226 exit(0);
227 }
228 soonestTimeout(&timeout, timeLeft * 1000);
229 }
230
231 // Check MaxConnectionTime
232 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
233 if (now < lastConnectionTime) {
234 // Someone must have set the time backwards.
235 slog.info("Time has gone backwards - resetting lastConnectionTime");
236 lastConnectionTime = now;
237 }
238 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
239 if (timeLeft < -60) {
240 // Someone must have set the time forwards.
241 slog.info("Time has gone forwards - resetting lastConnectionTime");
242 lastConnectionTime = now;
243 timeLeft = rfb::Server::maxConnectionTime;
244 }
245 if (timeLeft <= 0) {
246 slog.info("MaxConnectionTime reached, exiting");
247 exit(0);
248 }
249 soonestTimeout(&timeout, timeLeft * 1000);
250 }
251
252
253 // Check MaxIdleTime
254 if (rfb::Server::maxIdleTime) {
255 if (now < lastUserInputTime) {
256 // Someone must have set the time backwards.
257 slog.info("Time has gone backwards - resetting lastUserInputTime");
258 lastUserInputTime = now;
259 }
260 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
261 if (timeLeft < -60) {
262 // Someone must have set the time forwards.
263 slog.info("Time has gone forwards - resetting lastUserInputTime");
264 lastUserInputTime = now;
265 timeLeft = rfb::Server::maxIdleTime;
266 }
267 if (timeLeft <= 0) {
268 slog.info("MaxIdleTime reached, exiting");
269 exit(0);
270 }
271 soonestTimeout(&timeout, timeLeft * 1000);
272 }
273
274 return timeout;
275}
276
277
278// VNCServer methods
279
Pierre Ossman559a2e82012-01-23 15:54:11 +0000280void VNCServerST::blockUpdates()
281{
282 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200283
284 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000285}
286
287void VNCServerST::unblockUpdates()
288{
289 assert(blockCounter > 0);
290
291 blockCounter--;
292
Pierre Ossman6e49e952016-10-07 15:59:38 +0200293 // Restart the frame clock if we have updates
294 if (blockCounter == 0) {
295 if (!comparer->is_empty())
296 startFrameClock();
297 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000298}
299
Pierre Ossman04e62db2009-03-23 16:57:07 +0000300void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000301{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100302 if (comparer)
303 comparer->logStats();
304
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000305 pb = pb_;
306 delete comparer;
307 comparer = 0;
308
Pierre Ossman04e62db2009-03-23 16:57:07 +0000309 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000310
Pierre Ossman04e62db2009-03-23 16:57:07 +0000311 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000312 if (desktopStarted)
313 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000314 return;
315 }
316
317 comparer = new ComparingUpdateTracker(pb);
318 cursor.setPF(pb->getPF());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100319 renderedCursorInvalid = true;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000320
321 // Make sure that we have at least one screen
322 if (screenLayout.num_screens() == 0)
323 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
324
325 std::list<VNCSConnectionST*>::iterator ci, ci_next;
326 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
327 ci_next = ci; ci_next++;
328 (*ci)->pixelBufferChange();
329 // Since the new pixel buffer means an ExtendedDesktopSize needs to
330 // be sent anyway, we don't need to call screenLayoutChange.
331 }
332}
333
334void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
335{
336 ScreenSet layout;
337
338 if (!pb_) {
339 if (desktopStarted)
340 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
341 return;
342 }
343
344 layout = screenLayout;
345
346 // Check that the screen layout is still valid
347 if (!layout.validate(pb_->width(), pb_->height())) {
348 Rect fbRect;
349 ScreenSet::iterator iter, iter_next;
350
351 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
352
353 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
354 iter_next = iter; ++iter_next;
355 if (iter->dimensions.enclosed_by(fbRect))
356 continue;
357 iter->dimensions = iter->dimensions.intersect(fbRect);
358 if (iter->dimensions.is_empty()) {
359 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
360 (int)iter->id, (unsigned)iter->id);
361 layout.remove_screen(iter->id);
362 }
363 }
364 }
365
366 setPixelBuffer(pb_, layout);
367}
368
369void VNCServerST::setScreenLayout(const ScreenSet& layout)
370{
371 if (!pb)
372 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
373 if (!layout.validate(pb->width(), pb->height()))
374 throw Exception("setScreenLayout: invalid screen layout");
375
Pierre Ossmandf453202009-04-02 14:26:45 +0000376 screenLayout = layout;
377
Pierre Ossman04e62db2009-03-23 16:57:07 +0000378 std::list<VNCSConnectionST*>::iterator ci, ci_next;
379 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
380 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000381 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000382 }
383}
384
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000385void VNCServerST::bell()
386{
387 std::list<VNCSConnectionST*>::iterator ci, ci_next;
388 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
389 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000390 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000391 }
392}
393
394void VNCServerST::serverCutText(const char* str, int len)
395{
396 std::list<VNCSConnectionST*>::iterator ci, ci_next;
397 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
398 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000399 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000400 }
401}
402
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000403void VNCServerST::setName(const char* name_)
404{
Adam Tkacd36b6262009-09-04 10:57:20 +0000405 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000406 std::list<VNCSConnectionST*>::iterator ci, ci_next;
407 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
408 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000409 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000410 }
411}
412
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000413void VNCServerST::add_changed(const Region& region)
414{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000415 if (comparer == NULL)
416 return;
417
418 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200419 startFrameClock();
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);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200428 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000429}
430
431void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100432 const void* data, const void* mask)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000433{
434 cursor.hotspot = newHotspot;
435 cursor.setSize(width, height);
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100436 cursor.imageRect(cursor.getRect(), data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437 memcpy(cursor.mask.buf, mask, cursor.maskLen());
438
439 cursor.crop();
440
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{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200510 if (t == &frameTimer) {
511 // We keep running until we go a full interval without any updates
512 if (comparer->is_empty())
513 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000514
Pierre Ossman6e49e952016-10-07 15:59:38 +0200515 writeUpdate();
516 return true;
517 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000518
519 return false;
520}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000521
522// -=- Internal methods
523
524void VNCServerST::startDesktop()
525{
526 if (!desktopStarted) {
527 slog.debug("starting desktop");
528 desktop->start(this);
529 desktopStarted = true;
530 if (!pb)
531 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
532 }
533}
534
535int VNCServerST::authClientCount() {
536 int count = 0;
537 std::list<VNCSConnectionST*>::iterator ci;
538 for (ci = clients.begin(); ci != clients.end(); ci++) {
539 if ((*ci)->authenticated())
540 count++;
541 }
542 return count;
543}
544
545inline bool VNCServerST::needRenderedCursor()
546{
547 std::list<VNCSConnectionST*>::iterator ci;
548 for (ci = clients.begin(); ci != clients.end(); ci++)
549 if ((*ci)->needRenderedCursor()) return true;
550 return false;
551}
552
Pierre Ossman6e49e952016-10-07 15:59:38 +0200553void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000554{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200555 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000556 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000557 if (blockCounter > 0)
558 return;
559
Pierre Ossman6e49e952016-10-07 15:59:38 +0200560 frameTimer.start(1000/rfb::Server::frameRate);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000561}
562
Pierre Ossman6e49e952016-10-07 15:59:38 +0200563void VNCServerST::stopFrameClock()
564{
565 frameTimer.stop();
566}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000567
Pierre Ossman6e49e952016-10-07 15:59:38 +0200568// writeUpdate() is called on a regular interval in order to see what
569// updates are pending and propagates them to the update tracker for
570// each client. It uses the ComparingUpdateTracker's compare() method
571// to filter out areas of the screen which haven't actually changed. It
572// also checks the state of the (server-side) rendered cursor, if
573// necessary rendering it again with the correct background.
574
575void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000576{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000577 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200578 Region toCheck;
579
580 std::list<VNCSConnectionST*>::iterator ci, ci_next;
581
582 assert(blockCounter == 0);
583
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000584 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200585 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000586
Pierre Ossman6e49e952016-10-07 15:59:38 +0200587 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000588 Rect clippedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100589 = cursor.getRect(cursorPos.subtract(cursor.hotspot)).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();
606
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) {
634 renderedCursor.update(pb, &cursor, cursorPos);
635 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}