blob: 51cb86cfda3d632ea74157f0d1c9d52fc477238b [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +01002 * Copyright 2009-2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20// -=- Single-Threaded VNC Server implementation
21
22
23// Note about how sockets get closed:
24//
25// Closing sockets to clients is non-trivial because the code which calls
26// VNCServerST must explicitly know about all the sockets (so that it can block
27// on them appropriately). However, VNCServerST may want to close clients for
28// a number of reasons, and from a variety of entry points. The simplest is
29// when processSocketEvent() is called for a client, and the remote end has
30// closed its socket. A more complex reason is when processSocketEvent() is
31// called for a client which has just sent a ClientInit with the shared flag
32// set to false - in this case we want to close all other clients. Yet another
33// reason for disconnecting clients is when the desktop size has changed as a
34// result of a call to setPixelBuffer().
35//
36// The responsibility for creating and deleting sockets is entirely with the
37// calling code. When VNCServerST wants to close a connection to a client it
38// calls the VNCSConnectionST's close() method which calls shutdown() on the
39// socket. Eventually the calling code will notice that the socket has been
40// shut down and call removeSocket() so that we can delete the
41// VNCSConnectionST. Note that the socket must not be deleted by the calling
42// code until after removeSocket() has been called.
43//
44// One minor complication is that we don't allocate a VNCSConnectionST object
45// for a blacklisted host (since we want to minimise the resources used for
46// dealing with such a connection). In order to properly implement the
47// getSockets function, we must maintain a separate closingSockets list,
48// otherwise blacklisted connections might be "forgotten".
49
50
Pierre Ossman559a2e82012-01-23 15:54:11 +000051#include <assert.h>
Pierre Ossmanf99c5712009-03-13 14:41:27 +000052#include <stdlib.h>
53
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054#include <rfb/ServerCore.h>
55#include <rfb/VNCServerST.h>
56#include <rfb/VNCSConnectionST.h>
57#include <rfb/ComparingUpdateTracker.h>
Adam Tkaca6578bf2010-04-23 14:07:41 +000058#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059#include <rfb/KeyRemapper.h>
60#include <rfb/util.h>
61
62#include <rdr/types.h>
63
64using namespace rfb;
65
66static LogWriter slog("VNCServerST");
67LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000069rfb::IntParameter deferUpdateTime("DeferUpdate",
DRC4548f302011-12-22 15:57:59 +000070 "Time in milliseconds to defer updates",1);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000071
72rfb::BoolParameter alwaysSetDeferUpdateTimer("AlwaysSetDeferUpdateTimer",
73 "Always reset the defer update timer on every change",false);
74
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075//
76// -=- VNCServerST Implementation
77//
78
79// -=- Constructors/Destructor
80
Adam Tkaca6578bf2010-04-23 14:07:41 +000081VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000082 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
83 blockCounter(0), pb(0),
Adam Tkacd36b6262009-09-04 10:57:20 +000084 name(strDup(name_)), pointerClient(0), comparer(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000087 lastConnectionTime(0), disableclients(false),
88 deferTimer(this), deferPending(false)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089{
90 lastUserInputTime = lastDisconnectTime = time(0);
91 slog.debug("creating single-threaded server %s", name.buf);
92}
93
94VNCServerST::~VNCServerST()
95{
96 slog.debug("shutting down server %s", name.buf);
97
98 // Close any active clients, with appropriate logging & cleanup
99 closeClients("Server shutdown");
100
101 // Delete all the clients, and their sockets, and any closing sockets
102 // NB: Deleting a client implicitly removes it from the clients list
103 while (!clients.empty()) {
104 delete clients.front();
105 }
106
107 // Stop the desktop object if active, *only* after deleting all clients!
108 if (desktopStarted) {
109 desktopStarted = false;
110 desktop->stop();
111 }
112
113 delete comparer;
114}
115
116
117// SocketServer methods
118
119void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
120{
121 // - Check the connection isn't black-marked
122 // *** do this in getSecurity instead?
123 CharArray address(sock->getPeerAddress());
124 if (blHosts->isBlackmarked(address.buf)) {
125 connectionsLog.error("blacklisted: %s", address.buf);
126 try {
127 SConnection::writeConnFailedFromScratch("Too many security failures",
128 &sock->outStream());
129 } catch (rdr::Exception&) {
130 }
131 sock->shutdown();
132 closingSockets.push_back(sock);
133 return;
134 }
135
136 if (clients.empty()) {
137 lastConnectionTime = time(0);
138 }
139
140 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
141 client->init();
142}
143
144void VNCServerST::removeSocket(network::Socket* sock) {
145 // - If the socket has resources allocated to it, delete them
146 std::list<VNCSConnectionST*>::iterator ci;
147 for (ci = clients.begin(); ci != clients.end(); ci++) {
148 if ((*ci)->getSock() == sock) {
149 // - Delete the per-Socket resources
150 delete *ci;
151
152 // - Check that the desktop object is still required
153 if (authClientCount() == 0 && desktopStarted) {
154 slog.debug("no authenticated clients - stopping desktop");
155 desktopStarted = false;
156 desktop->stop();
157 }
158 return;
159 }
160 }
161
162 // - If the Socket has no resources, it may have been a closingSocket
163 closingSockets.remove(sock);
164}
165
166void VNCServerST::processSocketEvent(network::Socket* sock)
167{
168 // - Find the appropriate VNCSConnectionST and process the event
169 std::list<VNCSConnectionST*>::iterator ci;
170 for (ci = clients.begin(); ci != clients.end(); ci++) {
171 if ((*ci)->getSock() == sock) {
172 (*ci)->processMessages();
173 return;
174 }
175 }
176 throw rdr::Exception("invalid Socket in VNCServerST");
177}
178
179int VNCServerST::checkTimeouts()
180{
181 int timeout = 0;
182 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000183
184 soonestTimeout(&timeout, Timer::checkTimeouts());
185
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
187 ci_next = ci; ci_next++;
188 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
189 }
190
191 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000192 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 // Check MaxDisconnectionTime
195 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
196 if (now < lastDisconnectTime) {
197 // Someone must have set the time backwards.
198 slog.info("Time has gone backwards - resetting lastDisconnectTime");
199 lastDisconnectTime = now;
200 }
201 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
202 if (timeLeft < -60) {
203 // Someone must have set the time forwards.
204 slog.info("Time has gone forwards - resetting lastDisconnectTime");
205 lastDisconnectTime = now;
206 timeLeft = rfb::Server::maxDisconnectionTime;
207 }
208 if (timeLeft <= 0) {
209 slog.info("MaxDisconnectionTime reached, exiting");
210 exit(0);
211 }
212 soonestTimeout(&timeout, timeLeft * 1000);
213 }
214
215 // Check MaxConnectionTime
216 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
217 if (now < lastConnectionTime) {
218 // Someone must have set the time backwards.
219 slog.info("Time has gone backwards - resetting lastConnectionTime");
220 lastConnectionTime = now;
221 }
222 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
223 if (timeLeft < -60) {
224 // Someone must have set the time forwards.
225 slog.info("Time has gone forwards - resetting lastConnectionTime");
226 lastConnectionTime = now;
227 timeLeft = rfb::Server::maxConnectionTime;
228 }
229 if (timeLeft <= 0) {
230 slog.info("MaxConnectionTime reached, exiting");
231 exit(0);
232 }
233 soonestTimeout(&timeout, timeLeft * 1000);
234 }
235
236
237 // Check MaxIdleTime
238 if (rfb::Server::maxIdleTime) {
239 if (now < lastUserInputTime) {
240 // Someone must have set the time backwards.
241 slog.info("Time has gone backwards - resetting lastUserInputTime");
242 lastUserInputTime = now;
243 }
244 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
245 if (timeLeft < -60) {
246 // Someone must have set the time forwards.
247 slog.info("Time has gone forwards - resetting lastUserInputTime");
248 lastUserInputTime = now;
249 timeLeft = rfb::Server::maxIdleTime;
250 }
251 if (timeLeft <= 0) {
252 slog.info("MaxIdleTime reached, exiting");
253 exit(0);
254 }
255 soonestTimeout(&timeout, timeLeft * 1000);
256 }
257
258 return timeout;
259}
260
261
262// VNCServer methods
263
Pierre Ossman559a2e82012-01-23 15:54:11 +0000264void VNCServerST::blockUpdates()
265{
266 blockCounter++;
267}
268
269void VNCServerST::unblockUpdates()
270{
271 assert(blockCounter > 0);
272
273 blockCounter--;
274
275 // Flush out any updates we might have blocked
276 if (blockCounter == 0)
277 tryUpdate();
278}
279
Pierre Ossman04e62db2009-03-23 16:57:07 +0000280void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000281{
282 pb = pb_;
283 delete comparer;
284 comparer = 0;
285
Pierre Ossman04e62db2009-03-23 16:57:07 +0000286 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000287
Pierre Ossman04e62db2009-03-23 16:57:07 +0000288 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000289 if (desktopStarted)
290 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000291 return;
292 }
293
294 comparer = new ComparingUpdateTracker(pb);
295 cursor.setPF(pb->getPF());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100296 renderedCursorInvalid = true;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000297
298 // Make sure that we have at least one screen
299 if (screenLayout.num_screens() == 0)
300 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
301
302 std::list<VNCSConnectionST*>::iterator ci, ci_next;
303 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
304 ci_next = ci; ci_next++;
305 (*ci)->pixelBufferChange();
306 // Since the new pixel buffer means an ExtendedDesktopSize needs to
307 // be sent anyway, we don't need to call screenLayoutChange.
308 }
309}
310
311void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
312{
313 ScreenSet layout;
314
315 if (!pb_) {
316 if (desktopStarted)
317 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
318 return;
319 }
320
321 layout = screenLayout;
322
323 // Check that the screen layout is still valid
324 if (!layout.validate(pb_->width(), pb_->height())) {
325 Rect fbRect;
326 ScreenSet::iterator iter, iter_next;
327
328 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
329
330 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
331 iter_next = iter; ++iter_next;
332 if (iter->dimensions.enclosed_by(fbRect))
333 continue;
334 iter->dimensions = iter->dimensions.intersect(fbRect);
335 if (iter->dimensions.is_empty()) {
336 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
337 (int)iter->id, (unsigned)iter->id);
338 layout.remove_screen(iter->id);
339 }
340 }
341 }
342
343 setPixelBuffer(pb_, layout);
344}
345
346void VNCServerST::setScreenLayout(const ScreenSet& layout)
347{
348 if (!pb)
349 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
350 if (!layout.validate(pb->width(), pb->height()))
351 throw Exception("setScreenLayout: invalid screen layout");
352
Pierre Ossmandf453202009-04-02 14:26:45 +0000353 screenLayout = layout;
354
Pierre Ossman04e62db2009-03-23 16:57:07 +0000355 std::list<VNCSConnectionST*>::iterator ci, ci_next;
356 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
357 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000358 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000359 }
360}
361
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000362void VNCServerST::bell()
363{
364 std::list<VNCSConnectionST*>::iterator ci, ci_next;
365 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
366 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000367 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000368 }
369}
370
371void VNCServerST::serverCutText(const char* str, int len)
372{
373 std::list<VNCSConnectionST*>::iterator ci, ci_next;
374 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
375 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000376 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000377 }
378}
379
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000380void VNCServerST::setName(const char* name_)
381{
Adam Tkacd36b6262009-09-04 10:57:20 +0000382 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000383 std::list<VNCSConnectionST*>::iterator ci, ci_next;
384 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
385 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000386 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000387 }
388}
389
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000390void VNCServerST::add_changed(const Region& region)
391{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000392 if (comparer == NULL)
393 return;
394
395 comparer->add_changed(region);
396 startDefer();
397 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000398}
399
400void VNCServerST::add_copied(const Region& dest, const Point& delta)
401{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000402 if (comparer == NULL)
403 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000405 comparer->add_copied(dest, delta);
406 startDefer();
407 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000408}
409
410void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100411 const void* data, const void* mask)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412{
413 cursor.hotspot = newHotspot;
414 cursor.setSize(width, height);
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100415 cursor.imageRect(cursor.getRect(), data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000416 memcpy(cursor.mask.buf, mask, cursor.maskLen());
417
418 cursor.crop();
419
420 renderedCursorInvalid = true;
421
422 std::list<VNCSConnectionST*>::iterator ci, ci_next;
423 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
424 ci_next = ci; ci_next++;
425 (*ci)->renderedCursorChange();
426 (*ci)->setCursorOrClose();
427 }
428}
429
430void VNCServerST::setCursorPos(const Point& pos)
431{
432 if (!cursorPos.equals(pos)) {
433 cursorPos = pos;
434 renderedCursorInvalid = true;
435 std::list<VNCSConnectionST*>::iterator ci;
436 for (ci = clients.begin(); ci != clients.end(); ci++)
437 (*ci)->renderedCursorChange();
438 }
439}
440
441// Other public methods
442
443void VNCServerST::approveConnection(network::Socket* sock, bool accept,
444 const char* reason)
445{
446 std::list<VNCSConnectionST*>::iterator ci;
447 for (ci = clients.begin(); ci != clients.end(); ci++) {
448 if ((*ci)->getSock() == sock) {
449 (*ci)->approveConnectionOrClose(accept, reason);
450 return;
451 }
452 }
453}
454
455void VNCServerST::closeClients(const char* reason, network::Socket* except)
456{
457 std::list<VNCSConnectionST*>::iterator i, next_i;
458 for (i=clients.begin(); i!=clients.end(); i=next_i) {
459 next_i = i; next_i++;
460 if ((*i)->getSock() != except)
461 (*i)->close(reason);
462 }
463}
464
465void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
466{
467 sockets->clear();
468 std::list<VNCSConnectionST*>::iterator ci;
469 for (ci = clients.begin(); ci != clients.end(); ci++) {
470 sockets->push_back((*ci)->getSock());
471 }
472 std::list<network::Socket*>::iterator si;
473 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
474 sockets->push_back(*si);
475 }
476}
477
478SConnection* VNCServerST::getSConnection(network::Socket* sock) {
479 std::list<VNCSConnectionST*>::iterator ci;
480 for (ci = clients.begin(); ci != clients.end(); ci++) {
481 if ((*ci)->getSock() == sock)
482 return *ci;
483 }
484 return 0;
485}
486
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000487bool VNCServerST::handleTimeout(Timer* t)
488{
489 if (t != &deferTimer)
490 return false;
491
492 tryUpdate();
493
494 return false;
495}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000496
497// -=- Internal methods
498
499void VNCServerST::startDesktop()
500{
501 if (!desktopStarted) {
502 slog.debug("starting desktop");
503 desktop->start(this);
504 desktopStarted = true;
505 if (!pb)
506 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
507 }
508}
509
510int VNCServerST::authClientCount() {
511 int count = 0;
512 std::list<VNCSConnectionST*>::iterator ci;
513 for (ci = clients.begin(); ci != clients.end(); ci++) {
514 if ((*ci)->authenticated())
515 count++;
516 }
517 return count;
518}
519
520inline bool VNCServerST::needRenderedCursor()
521{
522 std::list<VNCSConnectionST*>::iterator ci;
523 for (ci = clients.begin(); ci != clients.end(); ci++)
524 if ((*ci)->needRenderedCursor()) return true;
525 return false;
526}
527
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000528inline void VNCServerST::startDefer()
529{
530 if (deferUpdateTime == 0)
531 return;
532
533 if (deferPending && !alwaysSetDeferUpdateTimer)
534 return;
535
536 gettimeofday(&deferStart, NULL);
537 deferTimer.start(deferUpdateTime);
538
539 deferPending = true;
540}
541
542inline bool VNCServerST::checkDefer()
543{
544 if (!deferPending)
545 return true;
546
547 if (msSince(&deferStart) >= deferUpdateTime)
548 return true;
549
550 return false;
551}
552
553void VNCServerST::tryUpdate()
554{
555 std::list<VNCSConnectionST*>::iterator ci, ci_next;
556
Pierre Ossman559a2e82012-01-23 15:54:11 +0000557 if (blockCounter > 0)
558 return;
559
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000560 if (!checkDefer())
561 return;
562
563 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
564 ci_next = ci; ci_next++;
565 (*ci)->writeFramebufferUpdateOrClose();
566 }
567}
568
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000569// checkUpdate() is called just before sending an update. It checks to see
570// what updates are pending and propagates them to the update tracker for each
571// client. It uses the ComparingUpdateTracker's compare() method to filter out
572// areas of the screen which haven't actually changed. It also checks the
573// state of the (server-side) rendered cursor, if necessary rendering it again
574// with the correct background.
575
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000576bool VNCServerST::checkUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000577{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000578 UpdateInfo ui;
579 comparer->getUpdateInfo(&ui, pb->getRect());
580
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000581 bool renderCursor = needRenderedCursor();
582
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000583 if (ui.is_empty() && !(renderCursor && renderedCursorInvalid))
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000584 return true;
585
Pierre Ossman559a2e82012-01-23 15:54:11 +0000586 // Block clients as the frame buffer cannot be safely accessed
587 if (blockCounter > 0)
588 return false;
589
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000590 // Block client from updating if we are currently deferring updates
591 if (!checkDefer())
592 return false;
593
594 deferPending = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000595
Pierre Ossman02e43d72009-03-05 11:57:11 +0000596 Region toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000597
598 if (renderCursor) {
599 Rect clippedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100600 = cursor.getRect(cursorPos.subtract(cursor.hotspot)).intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000601
602 if (!renderedCursorInvalid && (toCheck.intersect(clippedCursorRect)
603 .is_empty())) {
604 renderCursor = false;
605 } else {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606 toCheck.assign_union(clippedCursorRect);
607 }
608 }
609
610 pb->grabRegion(toCheck);
611
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000612 if (getComparerState())
613 comparer->enable();
614 else
615 comparer->disable();
616
617 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000618 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000619
620 if (renderCursor) {
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100621 renderedCursor.update(pb, &cursor, cursorPos);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000622 renderedCursorInvalid = false;
623 }
624
625 std::list<VNCSConnectionST*>::iterator ci, ci_next;
626 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
627 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000628 (*ci)->add_copied(ui.copied, ui.copy_delta);
629 (*ci)->add_changed(ui.changed);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630 }
631
632 comparer->clear();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000633
634 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000635}
636
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637void VNCServerST::getConnInfo(ListConnInfo * listConn)
638{
639 listConn->Clear();
640 listConn->setDisable(getDisable());
641 if (clients.empty())
642 return;
643 std::list<VNCSConnectionST*>::iterator i;
644 for (i = clients.begin(); i != clients.end(); i++)
645 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
646 (*i)->getStartTime(), (*i)->getStatus());
647}
648
649void VNCServerST::setConnStatus(ListConnInfo* listConn)
650{
651 setDisable(listConn->getDisable());
652 if (listConn->Empty() || clients.empty()) return;
653 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
654 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
655 std::list<VNCSConnectionST*>::iterator i;
656 for (i = clients.begin(); i != clients.end(); i++) {
657 if ((*i) == conn) {
658 int status = listConn->iGetStatus();
659 if (status == 3) {
660 (*i)->close(0);
661 } else {
662 (*i)->setStatus(status);
663 }
664 break;
665 }
666 }
667 }
668}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000669
Pierre Ossman04e62db2009-03-23 16:57:07 +0000670void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
671{
672 std::list<VNCSConnectionST*>::iterator ci, ci_next;
673 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
674 ci_next = ci; ci_next++;
675 if ((*ci) == requester)
676 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000677 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000678 }
679}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000680
681bool VNCServerST::getComparerState()
682{
683 if (rfb::Server::compareFB == 0)
684 return false;
685 if (rfb::Server::compareFB != 2)
686 return true;
687
688 std::list<VNCSConnectionST*>::iterator ci, ci_next;
689 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
690 ci_next = ci; ci_next++;
691 if ((*ci)->getComparerState())
692 return true;
693 }
694 return false;
695}