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