blob: 46c4be9b0f4e66cc2cb3720370eb3d1cee8393e2 [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 Ossmanb1e80f72017-09-22 16:48:14 +0200323 startFrameClock();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000324
325 // Make sure that we have at least one screen
326 if (screenLayout.num_screens() == 0)
327 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
328
329 std::list<VNCSConnectionST*>::iterator ci, ci_next;
330 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
331 ci_next = ci; ci_next++;
332 (*ci)->pixelBufferChange();
333 // Since the new pixel buffer means an ExtendedDesktopSize needs to
334 // be sent anyway, we don't need to call screenLayoutChange.
335 }
336}
337
338void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
339{
340 ScreenSet layout;
341
342 if (!pb_) {
343 if (desktopStarted)
344 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
345 return;
346 }
347
348 layout = screenLayout;
349
350 // Check that the screen layout is still valid
351 if (!layout.validate(pb_->width(), pb_->height())) {
352 Rect fbRect;
353 ScreenSet::iterator iter, iter_next;
354
355 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
356
357 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
358 iter_next = iter; ++iter_next;
359 if (iter->dimensions.enclosed_by(fbRect))
360 continue;
361 iter->dimensions = iter->dimensions.intersect(fbRect);
362 if (iter->dimensions.is_empty()) {
363 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
364 (int)iter->id, (unsigned)iter->id);
365 layout.remove_screen(iter->id);
366 }
367 }
368 }
369
370 setPixelBuffer(pb_, layout);
371}
372
373void VNCServerST::setScreenLayout(const ScreenSet& layout)
374{
375 if (!pb)
376 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
377 if (!layout.validate(pb->width(), pb->height()))
378 throw Exception("setScreenLayout: invalid screen layout");
379
Pierre Ossmandf453202009-04-02 14:26:45 +0000380 screenLayout = layout;
381
Pierre Ossman04e62db2009-03-23 16:57:07 +0000382 std::list<VNCSConnectionST*>::iterator ci, ci_next;
383 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
384 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000385 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000386 }
387}
388
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000389void VNCServerST::bell()
390{
391 std::list<VNCSConnectionST*>::iterator ci, ci_next;
392 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
393 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000394 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000395 }
396}
397
398void VNCServerST::serverCutText(const char* str, int len)
399{
400 std::list<VNCSConnectionST*>::iterator ci, ci_next;
401 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
402 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000403 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404 }
405}
406
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000407void VNCServerST::setName(const char* name_)
408{
Adam Tkacd36b6262009-09-04 10:57:20 +0000409 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000410 std::list<VNCSConnectionST*>::iterator ci, ci_next;
411 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
412 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000413 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000414 }
415}
416
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000417void VNCServerST::add_changed(const Region& region)
418{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000419 if (comparer == NULL)
420 return;
421
422 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200423 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000424}
425
426void VNCServerST::add_copied(const Region& dest, const Point& delta)
427{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000428 if (comparer == NULL)
429 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000431 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200432 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000433}
434
435void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100436 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100438 delete cursor;
439 cursor = new Cursor(width, height, newHotspot, data);
440 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000441
442 renderedCursorInvalid = true;
443
444 std::list<VNCSConnectionST*>::iterator ci, ci_next;
445 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
446 ci_next = ci; ci_next++;
447 (*ci)->renderedCursorChange();
448 (*ci)->setCursorOrClose();
449 }
450}
451
452void VNCServerST::setCursorPos(const Point& pos)
453{
454 if (!cursorPos.equals(pos)) {
455 cursorPos = pos;
456 renderedCursorInvalid = true;
457 std::list<VNCSConnectionST*>::iterator ci;
458 for (ci = clients.begin(); ci != clients.end(); ci++)
459 (*ci)->renderedCursorChange();
460 }
461}
462
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100463void VNCServerST::setLEDState(unsigned int state)
464{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100465 std::list<VNCSConnectionST*>::iterator ci, ci_next;
466
467 if (state == ledState)
468 return;
469
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100470 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100471
472 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
473 ci_next = ci; ci_next++;
474 (*ci)->setLEDStateOrClose(state);
475 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100476}
477
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000478// Other public methods
479
480void VNCServerST::approveConnection(network::Socket* sock, bool accept,
481 const char* reason)
482{
483 std::list<VNCSConnectionST*>::iterator ci;
484 for (ci = clients.begin(); ci != clients.end(); ci++) {
485 if ((*ci)->getSock() == sock) {
486 (*ci)->approveConnectionOrClose(accept, reason);
487 return;
488 }
489 }
490}
491
492void VNCServerST::closeClients(const char* reason, network::Socket* except)
493{
494 std::list<VNCSConnectionST*>::iterator i, next_i;
495 for (i=clients.begin(); i!=clients.end(); i=next_i) {
496 next_i = i; next_i++;
497 if ((*i)->getSock() != except)
498 (*i)->close(reason);
499 }
500}
501
502void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
503{
504 sockets->clear();
505 std::list<VNCSConnectionST*>::iterator ci;
506 for (ci = clients.begin(); ci != clients.end(); ci++) {
507 sockets->push_back((*ci)->getSock());
508 }
509 std::list<network::Socket*>::iterator si;
510 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
511 sockets->push_back(*si);
512 }
513}
514
515SConnection* VNCServerST::getSConnection(network::Socket* sock) {
516 std::list<VNCSConnectionST*>::iterator ci;
517 for (ci = clients.begin(); ci != clients.end(); ci++) {
518 if ((*ci)->getSock() == sock)
519 return *ci;
520 }
521 return 0;
522}
523
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000524bool VNCServerST::handleTimeout(Timer* t)
525{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200526 if (t == &frameTimer) {
527 // We keep running until we go a full interval without any updates
528 if (comparer->is_empty())
529 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000530
Pierre Ossman6e49e952016-10-07 15:59:38 +0200531 writeUpdate();
532 return true;
533 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000534
535 return false;
536}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000537
538// -=- Internal methods
539
540void VNCServerST::startDesktop()
541{
542 if (!desktopStarted) {
543 slog.debug("starting desktop");
544 desktop->start(this);
545 desktopStarted = true;
546 if (!pb)
547 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
548 }
549}
550
551int VNCServerST::authClientCount() {
552 int count = 0;
553 std::list<VNCSConnectionST*>::iterator ci;
554 for (ci = clients.begin(); ci != clients.end(); ci++) {
555 if ((*ci)->authenticated())
556 count++;
557 }
558 return count;
559}
560
561inline bool VNCServerST::needRenderedCursor()
562{
563 std::list<VNCSConnectionST*>::iterator ci;
564 for (ci = clients.begin(); ci != clients.end(); ci++)
565 if ((*ci)->needRenderedCursor()) return true;
566 return false;
567}
568
Pierre Ossman6e49e952016-10-07 15:59:38 +0200569void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000570{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200571 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000572 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000573 if (blockCounter > 0)
574 return;
575
Pierre Ossman6e49e952016-10-07 15:59:38 +0200576 frameTimer.start(1000/rfb::Server::frameRate);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000577}
578
Pierre Ossman6e49e952016-10-07 15:59:38 +0200579void VNCServerST::stopFrameClock()
580{
581 frameTimer.stop();
582}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000583
Pierre Ossman6e49e952016-10-07 15:59:38 +0200584// writeUpdate() is called on a regular interval in order to see what
585// updates are pending and propagates them to the update tracker for
586// each client. It uses the ComparingUpdateTracker's compare() method
587// to filter out areas of the screen which haven't actually changed. It
588// also checks the state of the (server-side) rendered cursor, if
589// necessary rendering it again with the correct background.
590
591void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000592{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000593 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200594 Region toCheck;
595
596 std::list<VNCSConnectionST*>::iterator ci, ci_next;
597
598 assert(blockCounter == 0);
599
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000600 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200601 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000602
Pierre Ossman6e49e952016-10-07 15:59:38 +0200603 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100604 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
605 .translate(cursorPos.subtract(cursor->hotspot()))
606 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000607
Pierre Ossman24684e52016-12-05 16:58:19 +0100608 if (!toCheck.intersect(clippedCursorRect).is_empty())
609 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000610 }
611
612 pb->grabRegion(toCheck);
613
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000614 if (getComparerState())
615 comparer->enable();
616 else
617 comparer->disable();
618
619 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000620 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000621
Pierre Ossman6e49e952016-10-07 15:59:38 +0200622 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
625 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000626 (*ci)->add_copied(ui.copied, ui.copy_delta);
627 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200628 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000629 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200630}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000631
Pierre Ossman6e49e952016-10-07 15:59:38 +0200632// checkUpdate() is called by clients to see if it is safe to read from
633// the framebuffer at this time.
634
635bool VNCServerST::checkUpdate()
636{
637 // Block clients as the frame buffer cannot be safely accessed
638 if (blockCounter > 0)
639 return false;
640
641 // Block client from updating if there are pending updates
642 if (!comparer->is_empty())
643 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000644
645 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000646}
647
Pierre Ossman24684e52016-12-05 16:58:19 +0100648const RenderedCursor* VNCServerST::getRenderedCursor()
649{
650 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100651 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100652 renderedCursorInvalid = false;
653 }
654
655 return &renderedCursor;
656}
657
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000658void VNCServerST::getConnInfo(ListConnInfo * listConn)
659{
660 listConn->Clear();
661 listConn->setDisable(getDisable());
662 if (clients.empty())
663 return;
664 std::list<VNCSConnectionST*>::iterator i;
665 for (i = clients.begin(); i != clients.end(); i++)
666 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
667 (*i)->getStartTime(), (*i)->getStatus());
668}
669
670void VNCServerST::setConnStatus(ListConnInfo* listConn)
671{
672 setDisable(listConn->getDisable());
673 if (listConn->Empty() || clients.empty()) return;
674 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
675 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
676 std::list<VNCSConnectionST*>::iterator i;
677 for (i = clients.begin(); i != clients.end(); i++) {
678 if ((*i) == conn) {
679 int status = listConn->iGetStatus();
680 if (status == 3) {
681 (*i)->close(0);
682 } else {
683 (*i)->setStatus(status);
684 }
685 break;
686 }
687 }
688 }
689}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000690
Pierre Ossman04e62db2009-03-23 16:57:07 +0000691void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
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) == requester)
697 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000698 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000699 }
700}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000701
702bool VNCServerST::getComparerState()
703{
704 if (rfb::Server::compareFB == 0)
705 return false;
706 if (rfb::Server::compareFB != 2)
707 return true;
708
709 std::list<VNCSConnectionST*>::iterator ci, ci_next;
710 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
711 ci_next = ci; ci_next++;
712 if ((*ci)->getComparerState())
713 return true;
714 }
715 return false;
716}