blob: 43e8f3edafb2c3292ed15e3b55e58d22058399c4 [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>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010061#include <rfb/ledStates.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062
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),
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010078 blockCounter(0), pb(0), ledState(ledUnknown),
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
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100462void VNCServerST::setLEDState(unsigned int state)
463{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100464 std::list<VNCSConnectionST*>::iterator ci, ci_next;
465
466 if (state == ledState)
467 return;
468
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100469 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100470
471 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
472 ci_next = ci; ci_next++;
473 (*ci)->setLEDStateOrClose(state);
474 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100475}
476
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000477// Other public methods
478
479void VNCServerST::approveConnection(network::Socket* sock, bool accept,
480 const char* reason)
481{
482 std::list<VNCSConnectionST*>::iterator ci;
483 for (ci = clients.begin(); ci != clients.end(); ci++) {
484 if ((*ci)->getSock() == sock) {
485 (*ci)->approveConnectionOrClose(accept, reason);
486 return;
487 }
488 }
489}
490
491void VNCServerST::closeClients(const char* reason, network::Socket* except)
492{
493 std::list<VNCSConnectionST*>::iterator i, next_i;
494 for (i=clients.begin(); i!=clients.end(); i=next_i) {
495 next_i = i; next_i++;
496 if ((*i)->getSock() != except)
497 (*i)->close(reason);
498 }
499}
500
501void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
502{
503 sockets->clear();
504 std::list<VNCSConnectionST*>::iterator ci;
505 for (ci = clients.begin(); ci != clients.end(); ci++) {
506 sockets->push_back((*ci)->getSock());
507 }
508 std::list<network::Socket*>::iterator si;
509 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
510 sockets->push_back(*si);
511 }
512}
513
514SConnection* VNCServerST::getSConnection(network::Socket* sock) {
515 std::list<VNCSConnectionST*>::iterator ci;
516 for (ci = clients.begin(); ci != clients.end(); ci++) {
517 if ((*ci)->getSock() == sock)
518 return *ci;
519 }
520 return 0;
521}
522
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000523bool VNCServerST::handleTimeout(Timer* t)
524{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200525 if (t == &frameTimer) {
526 // We keep running until we go a full interval without any updates
527 if (comparer->is_empty())
528 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000529
Pierre Ossman6e49e952016-10-07 15:59:38 +0200530 writeUpdate();
531 return true;
532 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000533
534 return false;
535}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000536
537// -=- Internal methods
538
539void VNCServerST::startDesktop()
540{
541 if (!desktopStarted) {
542 slog.debug("starting desktop");
543 desktop->start(this);
544 desktopStarted = true;
545 if (!pb)
546 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
547 }
548}
549
550int VNCServerST::authClientCount() {
551 int count = 0;
552 std::list<VNCSConnectionST*>::iterator ci;
553 for (ci = clients.begin(); ci != clients.end(); ci++) {
554 if ((*ci)->authenticated())
555 count++;
556 }
557 return count;
558}
559
560inline bool VNCServerST::needRenderedCursor()
561{
562 std::list<VNCSConnectionST*>::iterator ci;
563 for (ci = clients.begin(); ci != clients.end(); ci++)
564 if ((*ci)->needRenderedCursor()) return true;
565 return false;
566}
567
Pierre Ossman6e49e952016-10-07 15:59:38 +0200568void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000569{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200570 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000571 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000572 if (blockCounter > 0)
573 return;
574
Pierre Ossman6e49e952016-10-07 15:59:38 +0200575 frameTimer.start(1000/rfb::Server::frameRate);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000576}
577
Pierre Ossman6e49e952016-10-07 15:59:38 +0200578void VNCServerST::stopFrameClock()
579{
580 frameTimer.stop();
581}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000582
Pierre Ossman6e49e952016-10-07 15:59:38 +0200583// writeUpdate() is called on a regular interval in order to see what
584// updates are pending and propagates them to the update tracker for
585// each client. It uses the ComparingUpdateTracker's compare() method
586// to filter out areas of the screen which haven't actually changed. It
587// also checks the state of the (server-side) rendered cursor, if
588// necessary rendering it again with the correct background.
589
590void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000591{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000592 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200593 Region toCheck;
594
595 std::list<VNCSConnectionST*>::iterator ci, ci_next;
596
597 assert(blockCounter == 0);
598
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000599 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200600 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000601
Pierre Ossman6e49e952016-10-07 15:59:38 +0200602 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100603 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
604 .translate(cursorPos.subtract(cursor->hotspot()))
605 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606
Pierre Ossman24684e52016-12-05 16:58:19 +0100607 if (!toCheck.intersect(clippedCursorRect).is_empty())
608 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000609 }
610
611 pb->grabRegion(toCheck);
612
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000613 if (getComparerState())
614 comparer->enable();
615 else
616 comparer->disable();
617
618 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000619 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000620
Pierre Ossman6e49e952016-10-07 15:59:38 +0200621 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000622
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
624 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000625 (*ci)->add_copied(ui.copied, ui.copy_delta);
626 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200627 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000628 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200629}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630
Pierre Ossman6e49e952016-10-07 15:59:38 +0200631// checkUpdate() is called by clients to see if it is safe to read from
632// the framebuffer at this time.
633
634bool VNCServerST::checkUpdate()
635{
636 // Block clients as the frame buffer cannot be safely accessed
637 if (blockCounter > 0)
638 return false;
639
640 // Block client from updating if there are pending updates
641 if (!comparer->is_empty())
642 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000643
644 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000645}
646
Pierre Ossman24684e52016-12-05 16:58:19 +0100647const RenderedCursor* VNCServerST::getRenderedCursor()
648{
649 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100650 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100651 renderedCursorInvalid = false;
652 }
653
654 return &renderedCursor;
655}
656
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657void VNCServerST::getConnInfo(ListConnInfo * listConn)
658{
659 listConn->Clear();
660 listConn->setDisable(getDisable());
661 if (clients.empty())
662 return;
663 std::list<VNCSConnectionST*>::iterator i;
664 for (i = clients.begin(); i != clients.end(); i++)
665 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
666 (*i)->getStartTime(), (*i)->getStatus());
667}
668
669void VNCServerST::setConnStatus(ListConnInfo* listConn)
670{
671 setDisable(listConn->getDisable());
672 if (listConn->Empty() || clients.empty()) return;
673 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
674 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
675 std::list<VNCSConnectionST*>::iterator i;
676 for (i = clients.begin(); i != clients.end(); i++) {
677 if ((*i) == conn) {
678 int status = listConn->iGetStatus();
679 if (status == 3) {
680 (*i)->close(0);
681 } else {
682 (*i)->setStatus(status);
683 }
684 break;
685 }
686 }
687 }
688}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000689
Pierre Ossman04e62db2009-03-23 16:57:07 +0000690void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
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) == requester)
696 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000697 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000698 }
699}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000700
701bool VNCServerST::getComparerState()
702{
703 if (rfb::Server::compareFB == 0)
704 return false;
705 if (rfb::Server::compareFB != 2)
706 return true;
707
708 std::list<VNCSConnectionST*>::iterator ci, ci_next;
709 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
710 ci_next = ci; ci_next++;
711 if ((*ci)->getComparerState())
712 return true;
713 }
714 return false;
715}