blob: 654391928cb51d132b75bbaea42bc5486aa3a075 [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
Pierre Ossman707fa122015-12-11 20:21:20 +010054#include <rfb/ComparingUpdateTracker.h>
55#include <rfb/KeyRemapper.h>
56#include <rfb/ListConnInfo.h>
57#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058#include <rfb/ServerCore.h>
59#include <rfb/VNCServerST.h>
60#include <rfb/VNCSConnectionST.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061#include <rfb/util.h>
62
63#include <rdr/types.h>
64
65using namespace rfb;
66
67static LogWriter slog("VNCServerST");
68LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069
70//
71// -=- VNCServerST Implementation
72//
73
74// -=- Constructors/Destructor
75
Adam Tkaca6578bf2010-04-23 14:07:41 +000076VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000077 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
78 blockCounter(0), pb(0),
Adam Tkacd36b6262009-09-04 10:57:20 +000079 name(strDup(name_)), pointerClient(0), comparer(0),
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010080 cursor(new Cursor(0, 0, Point(), NULL)),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000083 lastConnectionTime(0), disableclients(false),
Pierre Ossman6e49e952016-10-07 15:59:38 +020084 frameTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085{
86 lastUserInputTime = lastDisconnectTime = time(0);
87 slog.debug("creating single-threaded server %s", name.buf);
88}
89
90VNCServerST::~VNCServerST()
91{
92 slog.debug("shutting down server %s", name.buf);
93
94 // Close any active clients, with appropriate logging & cleanup
95 closeClients("Server shutdown");
96
Pierre Ossman6e49e952016-10-07 15:59:38 +020097 // Stop trying to render things
98 stopFrameClock();
99
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 // Delete all the clients, and their sockets, and any closing sockets
101 // NB: Deleting a client implicitly removes it from the clients list
102 while (!clients.empty()) {
103 delete clients.front();
104 }
105
106 // Stop the desktop object if active, *only* after deleting all clients!
107 if (desktopStarted) {
108 desktopStarted = false;
109 desktop->stop();
110 }
111
Pierre Ossman05338bc2016-11-08 14:57:11 +0100112 if (comparer)
113 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100115
116 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117}
118
119
120// SocketServer methods
121
122void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
123{
124 // - Check the connection isn't black-marked
125 // *** do this in getSecurity instead?
126 CharArray address(sock->getPeerAddress());
127 if (blHosts->isBlackmarked(address.buf)) {
128 connectionsLog.error("blacklisted: %s", address.buf);
129 try {
130 SConnection::writeConnFailedFromScratch("Too many security failures",
131 &sock->outStream());
132 } catch (rdr::Exception&) {
133 }
134 sock->shutdown();
135 closingSockets.push_back(sock);
136 return;
137 }
138
139 if (clients.empty()) {
140 lastConnectionTime = time(0);
141 }
142
143 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
144 client->init();
145}
146
147void VNCServerST::removeSocket(network::Socket* sock) {
148 // - If the socket has resources allocated to it, delete them
149 std::list<VNCSConnectionST*>::iterator ci;
150 for (ci = clients.begin(); ci != clients.end(); ci++) {
151 if ((*ci)->getSock() == sock) {
152 // - Delete the per-Socket resources
153 delete *ci;
154
155 // - Check that the desktop object is still required
156 if (authClientCount() == 0 && desktopStarted) {
157 slog.debug("no authenticated clients - stopping desktop");
158 desktopStarted = false;
159 desktop->stop();
160 }
Pierre Ossman05338bc2016-11-08 14:57:11 +0100161
162 if (comparer)
163 comparer->logStats();
164
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 return;
166 }
167 }
168
169 // - If the Socket has no resources, it may have been a closingSocket
170 closingSockets.remove(sock);
171}
172
Pierre Ossmand408ca52016-04-29 14:26:05 +0200173void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174{
175 // - Find the appropriate VNCSConnectionST and process the event
176 std::list<VNCSConnectionST*>::iterator ci;
177 for (ci = clients.begin(); ci != clients.end(); ci++) {
178 if ((*ci)->getSock() == sock) {
179 (*ci)->processMessages();
180 return;
181 }
182 }
183 throw rdr::Exception("invalid Socket in VNCServerST");
184}
185
Pierre Ossmand408ca52016-04-29 14:26:05 +0200186void VNCServerST::processSocketWriteEvent(network::Socket* sock)
187{
188 // - Find the appropriate VNCSConnectionST and process the event
189 std::list<VNCSConnectionST*>::iterator ci;
190 for (ci = clients.begin(); ci != clients.end(); ci++) {
191 if ((*ci)->getSock() == sock) {
192 (*ci)->flushSocket();
193 return;
194 }
195 }
196 throw rdr::Exception("invalid Socket in VNCServerST");
197}
198
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000199int VNCServerST::checkTimeouts()
200{
201 int timeout = 0;
202 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000203
204 soonestTimeout(&timeout, Timer::checkTimeouts());
205
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
207 ci_next = ci; ci_next++;
208 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
209 }
210
211 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000212 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 // Check MaxDisconnectionTime
215 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
216 if (now < lastDisconnectTime) {
217 // Someone must have set the time backwards.
218 slog.info("Time has gone backwards - resetting lastDisconnectTime");
219 lastDisconnectTime = now;
220 }
221 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
222 if (timeLeft < -60) {
223 // Someone must have set the time forwards.
224 slog.info("Time has gone forwards - resetting lastDisconnectTime");
225 lastDisconnectTime = now;
226 timeLeft = rfb::Server::maxDisconnectionTime;
227 }
228 if (timeLeft <= 0) {
229 slog.info("MaxDisconnectionTime reached, exiting");
230 exit(0);
231 }
232 soonestTimeout(&timeout, timeLeft * 1000);
233 }
234
235 // Check MaxConnectionTime
236 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
237 if (now < lastConnectionTime) {
238 // Someone must have set the time backwards.
239 slog.info("Time has gone backwards - resetting lastConnectionTime");
240 lastConnectionTime = now;
241 }
242 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
243 if (timeLeft < -60) {
244 // Someone must have set the time forwards.
245 slog.info("Time has gone forwards - resetting lastConnectionTime");
246 lastConnectionTime = now;
247 timeLeft = rfb::Server::maxConnectionTime;
248 }
249 if (timeLeft <= 0) {
250 slog.info("MaxConnectionTime reached, exiting");
251 exit(0);
252 }
253 soonestTimeout(&timeout, timeLeft * 1000);
254 }
255
256
257 // Check MaxIdleTime
258 if (rfb::Server::maxIdleTime) {
259 if (now < lastUserInputTime) {
260 // Someone must have set the time backwards.
261 slog.info("Time has gone backwards - resetting lastUserInputTime");
262 lastUserInputTime = now;
263 }
264 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
265 if (timeLeft < -60) {
266 // Someone must have set the time forwards.
267 slog.info("Time has gone forwards - resetting lastUserInputTime");
268 lastUserInputTime = now;
269 timeLeft = rfb::Server::maxIdleTime;
270 }
271 if (timeLeft <= 0) {
272 slog.info("MaxIdleTime reached, exiting");
273 exit(0);
274 }
275 soonestTimeout(&timeout, timeLeft * 1000);
276 }
277
278 return timeout;
279}
280
281
282// VNCServer methods
283
Pierre Ossman559a2e82012-01-23 15:54:11 +0000284void VNCServerST::blockUpdates()
285{
286 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200287
288 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000289}
290
291void VNCServerST::unblockUpdates()
292{
293 assert(blockCounter > 0);
294
295 blockCounter--;
296
Pierre Ossman6e49e952016-10-07 15:59:38 +0200297 // Restart the frame clock if we have updates
298 if (blockCounter == 0) {
299 if (!comparer->is_empty())
300 startFrameClock();
301 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000302}
303
Pierre Ossman04e62db2009-03-23 16:57:07 +0000304void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000305{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100306 if (comparer)
307 comparer->logStats();
308
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309 pb = pb_;
310 delete comparer;
311 comparer = 0;
312
Pierre Ossman04e62db2009-03-23 16:57:07 +0000313 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000314
Pierre Ossman04e62db2009-03-23 16:57:07 +0000315 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000316 if (desktopStarted)
317 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000318 return;
319 }
320
321 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100322 renderedCursorInvalid = true;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000323
324 // Make sure that we have at least one screen
325 if (screenLayout.num_screens() == 0)
326 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
327
328 std::list<VNCSConnectionST*>::iterator ci, ci_next;
329 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
330 ci_next = ci; ci_next++;
331 (*ci)->pixelBufferChange();
332 // Since the new pixel buffer means an ExtendedDesktopSize needs to
333 // be sent anyway, we don't need to call screenLayoutChange.
334 }
335}
336
337void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
338{
339 ScreenSet layout;
340
341 if (!pb_) {
342 if (desktopStarted)
343 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
344 return;
345 }
346
347 layout = screenLayout;
348
349 // Check that the screen layout is still valid
350 if (!layout.validate(pb_->width(), pb_->height())) {
351 Rect fbRect;
352 ScreenSet::iterator iter, iter_next;
353
354 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
355
356 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
357 iter_next = iter; ++iter_next;
358 if (iter->dimensions.enclosed_by(fbRect))
359 continue;
360 iter->dimensions = iter->dimensions.intersect(fbRect);
361 if (iter->dimensions.is_empty()) {
362 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
363 (int)iter->id, (unsigned)iter->id);
364 layout.remove_screen(iter->id);
365 }
366 }
367 }
368
369 setPixelBuffer(pb_, layout);
370}
371
372void VNCServerST::setScreenLayout(const ScreenSet& layout)
373{
374 if (!pb)
375 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
376 if (!layout.validate(pb->width(), pb->height()))
377 throw Exception("setScreenLayout: invalid screen layout");
378
Pierre Ossmandf453202009-04-02 14:26:45 +0000379 screenLayout = layout;
380
Pierre Ossman04e62db2009-03-23 16:57:07 +0000381 std::list<VNCSConnectionST*>::iterator ci, ci_next;
382 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
383 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000384 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000385 }
386}
387
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000388void VNCServerST::bell()
389{
390 std::list<VNCSConnectionST*>::iterator ci, ci_next;
391 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
392 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000393 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000394 }
395}
396
397void VNCServerST::serverCutText(const char* str, int len)
398{
399 std::list<VNCSConnectionST*>::iterator ci, ci_next;
400 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
401 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000402 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000403 }
404}
405
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000406void VNCServerST::setName(const char* name_)
407{
Adam Tkacd36b6262009-09-04 10:57:20 +0000408 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000409 std::list<VNCSConnectionST*>::iterator ci, ci_next;
410 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
411 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000412 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000413 }
414}
415
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000416void VNCServerST::add_changed(const Region& region)
417{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000418 if (comparer == NULL)
419 return;
420
421 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200422 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423}
424
425void VNCServerST::add_copied(const Region& dest, const Point& delta)
426{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000427 if (comparer == NULL)
428 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000429
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000430 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200431 startFrameClock();
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{
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()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100588 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
589 .translate(cursorPos.subtract(cursor->hotspot()))
590 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000591
Pierre Ossman24684e52016-12-05 16:58:19 +0100592 if (!toCheck.intersect(clippedCursorRect).is_empty())
593 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000594 }
595
596 pb->grabRegion(toCheck);
597
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000598 if (getComparerState())
599 comparer->enable();
600 else
601 comparer->disable();
602
603 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000604 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000605
Pierre Ossman6e49e952016-10-07 15:59:38 +0200606 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000607
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000608 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
609 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000610 (*ci)->add_copied(ui.copied, ui.copy_delta);
611 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200612 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000613 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200614}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000615
Pierre Ossman6e49e952016-10-07 15:59:38 +0200616// checkUpdate() is called by clients to see if it is safe to read from
617// the framebuffer at this time.
618
619bool VNCServerST::checkUpdate()
620{
621 // Block clients as the frame buffer cannot be safely accessed
622 if (blockCounter > 0)
623 return false;
624
625 // Block client from updating if there are pending updates
626 if (!comparer->is_empty())
627 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000628
629 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630}
631
Pierre Ossman24684e52016-12-05 16:58:19 +0100632const RenderedCursor* VNCServerST::getRenderedCursor()
633{
634 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100635 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100636 renderedCursorInvalid = false;
637 }
638
639 return &renderedCursor;
640}
641
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000642void VNCServerST::getConnInfo(ListConnInfo * listConn)
643{
644 listConn->Clear();
645 listConn->setDisable(getDisable());
646 if (clients.empty())
647 return;
648 std::list<VNCSConnectionST*>::iterator i;
649 for (i = clients.begin(); i != clients.end(); i++)
650 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
651 (*i)->getStartTime(), (*i)->getStatus());
652}
653
654void VNCServerST::setConnStatus(ListConnInfo* listConn)
655{
656 setDisable(listConn->getDisable());
657 if (listConn->Empty() || clients.empty()) return;
658 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
659 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
660 std::list<VNCSConnectionST*>::iterator i;
661 for (i = clients.begin(); i != clients.end(); i++) {
662 if ((*i) == conn) {
663 int status = listConn->iGetStatus();
664 if (status == 3) {
665 (*i)->close(0);
666 } else {
667 (*i)->setStatus(status);
668 }
669 break;
670 }
671 }
672 }
673}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000674
Pierre Ossman04e62db2009-03-23 16:57:07 +0000675void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
676{
677 std::list<VNCSConnectionST*>::iterator ci, ci_next;
678 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
679 ci_next = ci; ci_next++;
680 if ((*ci) == requester)
681 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000682 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000683 }
684}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000685
686bool VNCServerST::getComparerState()
687{
688 if (rfb::Server::compareFB == 0)
689 return false;
690 if (rfb::Server::compareFB != 2)
691 return true;
692
693 std::list<VNCSConnectionST*>::iterator ci, ci_next;
694 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
695 ci_next = ci; ci_next++;
696 if ((*ci)->getComparerState())
697 return true;
698 }
699 return false;
700}