Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 2 | * Copyright 2009-2019 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 51 | #include <assert.h> |
Pierre Ossman | f99c571 | 2009-03-13 14:41:27 +0000 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 54 | #include <rfb/ComparingUpdateTracker.h> |
| 55 | #include <rfb/KeyRemapper.h> |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 56 | #include <rfb/LogWriter.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 57 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | #include <rfb/ServerCore.h> |
| 59 | #include <rfb/VNCServerST.h> |
| 60 | #include <rfb/VNCSConnectionST.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | #include <rfb/util.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 62 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | |
| 64 | #include <rdr/types.h> |
| 65 | |
| 66 | using namespace rfb; |
| 67 | |
| 68 | static LogWriter slog("VNCServerST"); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 69 | static LogWriter connectionsLog("Connections"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | |
| 71 | // |
| 72 | // -=- VNCServerST Implementation |
| 73 | // |
| 74 | |
| 75 | // -=- Constructors/Destructor |
| 76 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 77 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 78 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 79 | blockCounter(0), pb(0), ledState(ledUnknown), |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 80 | name(strDup(name_)), pointerClient(0), clipboardClient(0), |
| 81 | comparer(0), cursor(new Cursor(0, 0, Point(), NULL)), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | renderedCursorInvalid(false), |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 83 | keyRemapper(&KeyRemapper::defInstance), |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 84 | idleTimer(this), disconnectTimer(this), connectTimer(this), |
| 85 | frameTimer(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 86 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | slog.debug("creating single-threaded server %s", name.buf); |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 88 | |
| 89 | // FIXME: Do we really want to kick off these right away? |
| 90 | if (rfb::Server::maxIdleTime) |
| 91 | idleTimer.start(secsToMillis(rfb::Server::maxIdleTime)); |
| 92 | if (rfb::Server::maxDisconnectionTime) |
| 93 | disconnectTimer.start(secsToMillis(rfb::Server::maxDisconnectionTime)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | VNCServerST::~VNCServerST() |
| 97 | { |
| 98 | slog.debug("shutting down server %s", name.buf); |
| 99 | |
| 100 | // Close any active clients, with appropriate logging & cleanup |
| 101 | closeClients("Server shutdown"); |
| 102 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 103 | // Stop trying to render things |
| 104 | stopFrameClock(); |
| 105 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | // Delete all the clients, and their sockets, and any closing sockets |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | while (!clients.empty()) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 108 | VNCSConnectionST* client; |
| 109 | client = clients.front(); |
| 110 | clients.pop_front(); |
| 111 | delete client; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Stop the desktop object if active, *only* after deleting all clients! |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 115 | stopDesktop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 117 | if (comparer) |
| 118 | comparer->logStats(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | delete comparer; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 120 | |
| 121 | delete cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | // SocketServer methods |
| 126 | |
| 127 | void VNCServerST::addSocket(network::Socket* sock, bool outgoing) |
| 128 | { |
| 129 | // - Check the connection isn't black-marked |
| 130 | // *** do this in getSecurity instead? |
| 131 | CharArray address(sock->getPeerAddress()); |
| 132 | if (blHosts->isBlackmarked(address.buf)) { |
| 133 | connectionsLog.error("blacklisted: %s", address.buf); |
| 134 | try { |
Pierre Ossman | 4bba246 | 2018-10-11 07:52:50 +0200 | [diff] [blame] | 135 | rdr::OutStream& os = sock->outStream(); |
| 136 | |
| 137 | // Shortest possible way to tell a client it is not welcome |
| 138 | os.writeBytes("RFB 003.003\n", 12); |
| 139 | os.writeU32(0); |
| 140 | os.writeString("Too many security failures"); |
| 141 | os.flush(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 142 | } catch (rdr::Exception&) { |
| 143 | } |
| 144 | sock->shutdown(); |
| 145 | closingSockets.push_back(sock); |
| 146 | return; |
| 147 | } |
| 148 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 149 | CharArray name; |
| 150 | name.buf = sock->getPeerEndpoint(); |
| 151 | connectionsLog.status("accepted: %s", name.buf); |
| 152 | |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 153 | // Adjust the exit timers |
| 154 | if (rfb::Server::maxConnectionTime && clients.empty()) |
| 155 | connectTimer.start(secsToMillis(rfb::Server::maxConnectionTime)); |
| 156 | disconnectTimer.stop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | |
| 158 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 159 | clients.push_front(client); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 160 | client->init(); |
| 161 | } |
| 162 | |
| 163 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 164 | // - If the socket has resources allocated to it, delete them |
| 165 | std::list<VNCSConnectionST*>::iterator ci; |
| 166 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 167 | if ((*ci)->getSock() == sock) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 168 | clients.remove(*ci); |
| 169 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 170 | // - Remove any references to it |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 171 | if (pointerClient == *ci) |
| 172 | pointerClient = NULL; |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 173 | if (clipboardClient == *ci) |
| 174 | clipboardClient = NULL; |
| 175 | clipboardRequestors.remove(*ci); |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 176 | |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 177 | // Adjust the exit timers |
| 178 | connectTimer.stop(); |
| 179 | if (rfb::Server::maxDisconnectionTime && clients.empty()) |
| 180 | disconnectTimer.start(secsToMillis(rfb::Server::maxDisconnectionTime)); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 181 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 182 | // - Delete the per-Socket resources |
| 183 | delete *ci; |
| 184 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 185 | CharArray name; |
| 186 | name.buf = sock->getPeerEndpoint(); |
| 187 | connectionsLog.status("closed: %s", name.buf); |
| 188 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | // - Check that the desktop object is still required |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 190 | if (authClientCount() == 0) |
| 191 | stopDesktop(); |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 192 | |
| 193 | if (comparer) |
| 194 | comparer->logStats(); |
| 195 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // - If the Socket has no resources, it may have been a closingSocket |
| 201 | closingSockets.remove(sock); |
| 202 | } |
| 203 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 204 | void VNCServerST::processSocketReadEvent(network::Socket* sock) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 205 | { |
| 206 | // - Find the appropriate VNCSConnectionST and process the event |
| 207 | std::list<VNCSConnectionST*>::iterator ci; |
| 208 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 209 | if ((*ci)->getSock() == sock) { |
| 210 | (*ci)->processMessages(); |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 215 | } |
| 216 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 217 | void VNCServerST::processSocketWriteEvent(network::Socket* sock) |
| 218 | { |
| 219 | // - Find the appropriate VNCSConnectionST and process the event |
| 220 | std::list<VNCSConnectionST*>::iterator ci; |
| 221 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 222 | if ((*ci)->getSock() == sock) { |
| 223 | (*ci)->flushSocket(); |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 228 | } |
| 229 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | // VNCServer methods |
| 231 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 232 | void VNCServerST::blockUpdates() |
| 233 | { |
| 234 | blockCounter++; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 235 | |
| 236 | stopFrameClock(); |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void VNCServerST::unblockUpdates() |
| 240 | { |
| 241 | assert(blockCounter > 0); |
| 242 | |
| 243 | blockCounter--; |
| 244 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 245 | // Restart the frame clock if we have updates |
| 246 | if (blockCounter == 0) { |
| 247 | if (!comparer->is_empty()) |
| 248 | startFrameClock(); |
| 249 | } |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 252 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 253 | { |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 254 | if (comparer) |
| 255 | comparer->logStats(); |
| 256 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 257 | pb = pb_; |
| 258 | delete comparer; |
| 259 | comparer = 0; |
| 260 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 261 | if (!pb) { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 262 | screenLayout = ScreenSet(); |
| 263 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 264 | if (desktopStarted) |
| 265 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 266 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Pierre Ossman | c3826bb | 2018-11-01 16:10:19 +0100 | [diff] [blame] | 270 | if (!layout.validate(pb->width(), pb->height())) |
| 271 | throw Exception("setPixelBuffer: invalid screen layout"); |
| 272 | |
| 273 | screenLayout = layout; |
| 274 | |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 275 | // Assume the framebuffer contents wasn't saved and reset everything |
| 276 | // that tracks its contents |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 277 | comparer = new ComparingUpdateTracker(pb); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 278 | renderedCursorInvalid = true; |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 279 | add_changed(pb->getRect()); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 280 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 281 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 282 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 283 | ci_next = ci; ci_next++; |
| 284 | (*ci)->pixelBufferChange(); |
| 285 | // Since the new pixel buffer means an ExtendedDesktopSize needs to |
| 286 | // be sent anyway, we don't need to call screenLayoutChange. |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 291 | { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 292 | ScreenSet layout = screenLayout; |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 293 | |
| 294 | // Check that the screen layout is still valid |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 295 | if (pb_ && !layout.validate(pb_->width(), pb_->height())) { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 296 | Rect fbRect; |
| 297 | ScreenSet::iterator iter, iter_next; |
| 298 | |
| 299 | fbRect.setXYWH(0, 0, pb_->width(), pb_->height()); |
| 300 | |
| 301 | for (iter = layout.begin();iter != layout.end();iter = iter_next) { |
| 302 | iter_next = iter; ++iter_next; |
| 303 | if (iter->dimensions.enclosed_by(fbRect)) |
| 304 | continue; |
| 305 | iter->dimensions = iter->dimensions.intersect(fbRect); |
| 306 | if (iter->dimensions.is_empty()) { |
| 307 | slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer", |
| 308 | (int)iter->id, (unsigned)iter->id); |
| 309 | layout.remove_screen(iter->id); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Pierre Ossman | c3826bb | 2018-11-01 16:10:19 +0100 | [diff] [blame] | 314 | // Make sure that we have at least one screen |
| 315 | if (layout.num_screens() == 0) |
| 316 | layout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0)); |
| 317 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 318 | setPixelBuffer(pb_, layout); |
| 319 | } |
| 320 | |
| 321 | void VNCServerST::setScreenLayout(const ScreenSet& layout) |
| 322 | { |
| 323 | if (!pb) |
| 324 | throw Exception("setScreenLayout: new screen layout without a PixelBuffer"); |
| 325 | if (!layout.validate(pb->width(), pb->height())) |
| 326 | throw Exception("setScreenLayout: invalid screen layout"); |
| 327 | |
Pierre Ossman | df45320 | 2009-04-02 14:26:45 +0000 | [diff] [blame] | 328 | screenLayout = layout; |
| 329 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 330 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 331 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 332 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 333 | (*ci)->screenLayoutChangeOrClose(reasonServer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 337 | void VNCServerST::requestClipboard() |
| 338 | { |
| 339 | if (clipboardClient == NULL) |
| 340 | return; |
| 341 | |
| 342 | clipboardClient->requestClipboard(); |
| 343 | } |
| 344 | |
| 345 | void VNCServerST::announceClipboard(bool available) |
| 346 | { |
| 347 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 348 | |
| 349 | if (available) |
| 350 | clipboardClient = NULL; |
| 351 | |
| 352 | clipboardRequestors.clear(); |
| 353 | |
| 354 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 355 | ci_next = ci; ci_next++; |
| 356 | (*ci)->announceClipboard(available); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void VNCServerST::sendClipboardData(const char* data) |
| 361 | { |
| 362 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 363 | |
| 364 | if (strchr(data, '\r') != NULL) |
| 365 | throw Exception("Invalid carriage return in clipboard data"); |
| 366 | |
| 367 | for (ci = clipboardRequestors.begin(); |
| 368 | ci != clipboardRequestors.end(); ci = ci_next) { |
| 369 | ci_next = ci; ci_next++; |
| 370 | (*ci)->sendClipboardData(data); |
| 371 | } |
| 372 | |
| 373 | clipboardRequestors.clear(); |
| 374 | } |
| 375 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 376 | void VNCServerST::bell() |
| 377 | { |
| 378 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 379 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 380 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 381 | (*ci)->bellOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 385 | void VNCServerST::setName(const char* name_) |
| 386 | { |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 387 | name.replaceBuf(strDup(name_)); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 388 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 389 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 390 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 391 | (*ci)->setDesktopNameOrClose(name_); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 395 | void VNCServerST::add_changed(const Region& region) |
| 396 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 397 | if (comparer == NULL) |
| 398 | return; |
| 399 | |
| 400 | comparer->add_changed(region); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 401 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 405 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 406 | if (comparer == NULL) |
| 407 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 408 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 409 | comparer->add_copied(dest, delta); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 410 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 414 | const rdr::U8* data) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 415 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 416 | delete cursor; |
| 417 | cursor = new Cursor(width, height, newHotspot, data); |
| 418 | cursor->crop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 430 | void 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 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 441 | void VNCServerST::setLEDState(unsigned int state) |
| 442 | { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 443 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 444 | |
| 445 | if (state == ledState) |
| 446 | return; |
| 447 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 448 | ledState = state; |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 449 | |
| 450 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 451 | ci_next = ci; ci_next++; |
| 452 | (*ci)->setLEDStateOrClose(state); |
| 453 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 456 | // Event handlers |
| 457 | |
| 458 | void VNCServerST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) |
| 459 | { |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 460 | if (rfb::Server::maxIdleTime) |
| 461 | idleTimer.start(secsToMillis(rfb::Server::maxIdleTime)); |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 462 | |
| 463 | // Remap the key if required |
| 464 | if (keyRemapper) { |
| 465 | rdr::U32 newkey; |
| 466 | newkey = keyRemapper->remapKey(keysym); |
| 467 | if (newkey != keysym) { |
| 468 | slog.debug("Key remapped to 0x%x", newkey); |
| 469 | keysym = newkey; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | desktop->keyEvent(keysym, keycode, down); |
| 474 | } |
| 475 | |
| 476 | void VNCServerST::pointerEvent(VNCSConnectionST* client, |
| 477 | const Point& pos, int buttonMask) |
| 478 | { |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 479 | if (rfb::Server::maxIdleTime) |
| 480 | idleTimer.start(secsToMillis(rfb::Server::maxIdleTime)); |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 481 | |
| 482 | // Let one client own the cursor whilst buttons are pressed in order |
| 483 | // to provide a bit more sane user experience |
| 484 | if ((pointerClient != NULL) && (pointerClient != client)) |
| 485 | return; |
| 486 | |
| 487 | if (buttonMask) |
| 488 | pointerClient = client; |
| 489 | else |
| 490 | pointerClient = NULL; |
| 491 | |
| 492 | desktop->pointerEvent(pos, buttonMask); |
| 493 | } |
| 494 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 495 | void VNCServerST::handleClipboardRequest(VNCSConnectionST* client) |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 496 | { |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame^] | 497 | clipboardRequestors.push_back(client); |
| 498 | if (clipboardRequestors.size() == 1) |
| 499 | desktop->handleClipboardRequest(); |
| 500 | } |
| 501 | |
| 502 | void VNCServerST::handleClipboardAnnounce(VNCSConnectionST* client, |
| 503 | bool available) |
| 504 | { |
| 505 | if (available) |
| 506 | clipboardClient = client; |
| 507 | else { |
| 508 | if (client != clipboardClient) |
| 509 | return; |
| 510 | clipboardClient = NULL; |
| 511 | } |
| 512 | desktop->handleClipboardAnnounce(available); |
| 513 | } |
| 514 | |
| 515 | void VNCServerST::handleClipboardData(VNCSConnectionST* client, |
| 516 | const char* data) |
| 517 | { |
| 518 | if (client != clipboardClient) |
| 519 | return; |
| 520 | desktop->handleClipboardData(data); |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Pierre Ossman | 07e44cc | 2018-10-05 17:32:57 +0200 | [diff] [blame] | 523 | unsigned int VNCServerST::setDesktopSize(VNCSConnectionST* requester, |
| 524 | int fb_width, int fb_height, |
| 525 | const ScreenSet& layout) |
| 526 | { |
| 527 | unsigned int result; |
| 528 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 529 | |
| 530 | // Don't bother the desktop with an invalid configuration |
| 531 | if (!layout.validate(fb_width, fb_height)) |
| 532 | return resultInvalid; |
| 533 | |
| 534 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 535 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 536 | // protocol-wise, but unnecessary. |
| 537 | result = desktop->setScreenLayout(fb_width, fb_height, layout); |
| 538 | if (result != resultSuccess) |
| 539 | return result; |
| 540 | |
| 541 | // Sanity check |
| 542 | if (screenLayout != layout) |
| 543 | throw Exception("Desktop configured a different screen layout than requested"); |
| 544 | |
| 545 | // Notify other clients |
| 546 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 547 | ci_next = ci; ci_next++; |
| 548 | if ((*ci) == requester) |
| 549 | continue; |
| 550 | (*ci)->screenLayoutChangeOrClose(reasonOtherClient); |
| 551 | } |
| 552 | |
| 553 | return resultSuccess; |
| 554 | } |
| 555 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 556 | // Other public methods |
| 557 | |
| 558 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 559 | const char* reason) |
| 560 | { |
| 561 | std::list<VNCSConnectionST*>::iterator ci; |
| 562 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 563 | if ((*ci)->getSock() == sock) { |
| 564 | (*ci)->approveConnectionOrClose(accept, reason); |
| 565 | return; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 571 | { |
| 572 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 573 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 574 | next_i = i; next_i++; |
| 575 | if ((*i)->getSock() != except) |
| 576 | (*i)->close(reason); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 581 | { |
| 582 | sockets->clear(); |
| 583 | std::list<VNCSConnectionST*>::iterator ci; |
| 584 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 585 | sockets->push_back((*ci)->getSock()); |
| 586 | } |
| 587 | std::list<network::Socket*>::iterator si; |
| 588 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 589 | sockets->push_back(*si); |
| 590 | } |
| 591 | } |
| 592 | |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 593 | SConnection* VNCServerST::getConnection(network::Socket* sock) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 594 | std::list<VNCSConnectionST*>::iterator ci; |
| 595 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 596 | if ((*ci)->getSock() == sock) |
| 597 | return *ci; |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 602 | bool VNCServerST::handleTimeout(Timer* t) |
| 603 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 604 | if (t == &frameTimer) { |
| 605 | // We keep running until we go a full interval without any updates |
| 606 | if (comparer->is_empty()) |
| 607 | return false; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 608 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 609 | writeUpdate(); |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 610 | |
| 611 | // If this is the first iteration then we need to adjust the timeout |
| 612 | if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) { |
| 613 | frameTimer.start(1000/rfb::Server::frameRate); |
| 614 | return false; |
| 615 | } |
| 616 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 617 | return true; |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 618 | } else if (t == &idleTimer) { |
| 619 | slog.info("MaxIdleTime reached, exiting"); |
Pierre Ossman | 10688ef | 2018-09-29 11:24:19 +0200 | [diff] [blame] | 620 | desktop->terminate(); |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 621 | } else if (t == &disconnectTimer) { |
| 622 | slog.info("MaxDisconnectionTime reached, exiting"); |
Pierre Ossman | 10688ef | 2018-09-29 11:24:19 +0200 | [diff] [blame] | 623 | desktop->terminate(); |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 624 | } else if (t == &connectTimer) { |
| 625 | slog.info("MaxConnectionTime reached, exiting"); |
Pierre Ossman | 10688ef | 2018-09-29 11:24:19 +0200 | [diff] [blame] | 626 | desktop->terminate(); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 627 | } |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 628 | |
| 629 | return false; |
| 630 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 631 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 632 | void VNCServerST::queryConnection(VNCSConnectionST* client, |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 633 | const char* userName) |
| 634 | { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 635 | // - Authentication succeeded - clear from blacklist |
| 636 | CharArray name; |
| 637 | name.buf = client->getSock()->getPeerAddress(); |
| 638 | blHosts->clearBlackmark(name.buf); |
| 639 | |
| 640 | // - Prepare the desktop for that the client will start requiring |
| 641 | // resources after this |
| 642 | startDesktop(); |
| 643 | |
| 644 | // - Special case to provide a more useful error message |
| 645 | if (rfb::Server::neverShared && |
| 646 | !rfb::Server::disconnectClients && |
| 647 | authClientCount() > 0) { |
| 648 | approveConnection(client->getSock(), false, |
| 649 | "The server is already in use"); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | // - Are we configured to do queries? |
| 654 | if (!rfb::Server::queryConnect && |
| 655 | !client->getSock()->requiresQuery()) { |
| 656 | approveConnection(client->getSock(), true, NULL); |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | // - Does the client have the right to bypass the query? |
| 661 | if (client->accessCheck(SConnection::AccessNoQuery)) |
| 662 | { |
| 663 | approveConnection(client->getSock(), true, NULL); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | desktop->queryConnection(client->getSock(), userName); |
| 668 | } |
| 669 | |
| 670 | void VNCServerST::clientReady(VNCSConnectionST* client, bool shared) |
| 671 | { |
| 672 | if (!shared) { |
| 673 | if (rfb::Server::disconnectClients && |
| 674 | client->accessCheck(SConnection::AccessNonShared)) { |
| 675 | // - Close all the other connected clients |
| 676 | slog.debug("non-shared connection - closing clients"); |
| 677 | closeClients("Non-shared connection requested", client->getSock()); |
| 678 | } else { |
| 679 | // - Refuse this connection if there are existing clients, in addition to |
| 680 | // this one |
| 681 | if (authClientCount() > 1) { |
| 682 | client->close("Server is already in use"); |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | } |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 689 | // -=- Internal methods |
| 690 | |
| 691 | void VNCServerST::startDesktop() |
| 692 | { |
| 693 | if (!desktopStarted) { |
| 694 | slog.debug("starting desktop"); |
| 695 | desktop->start(this); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 696 | if (!pb) |
| 697 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 698 | desktopStarted = true; |
| 699 | // The tracker might have accumulated changes whilst we were |
| 700 | // stopped, so flush those out |
| 701 | if (!comparer->is_empty()) |
| 702 | writeUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 706 | void VNCServerST::stopDesktop() |
| 707 | { |
| 708 | if (desktopStarted) { |
| 709 | slog.debug("stopping desktop"); |
| 710 | desktopStarted = false; |
| 711 | desktop->stop(); |
| 712 | stopFrameClock(); |
| 713 | } |
| 714 | } |
| 715 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 716 | int VNCServerST::authClientCount() { |
| 717 | int count = 0; |
| 718 | std::list<VNCSConnectionST*>::iterator ci; |
| 719 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 720 | if ((*ci)->authenticated()) |
| 721 | count++; |
| 722 | } |
| 723 | return count; |
| 724 | } |
| 725 | |
| 726 | inline bool VNCServerST::needRenderedCursor() |
| 727 | { |
| 728 | std::list<VNCSConnectionST*>::iterator ci; |
| 729 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 730 | if ((*ci)->needRenderedCursor()) return true; |
| 731 | return false; |
| 732 | } |
| 733 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 734 | void VNCServerST::startFrameClock() |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 735 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 736 | if (frameTimer.isStarted()) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 737 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 738 | if (blockCounter > 0) |
| 739 | return; |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 740 | if (!desktopStarted) |
| 741 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 742 | |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 743 | // The first iteration will be just half a frame as we get a very |
| 744 | // unstable update rate if we happen to be perfectly in sync with |
| 745 | // the application's update rate |
| 746 | frameTimer.start(1000/rfb::Server::frameRate/2); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 749 | void VNCServerST::stopFrameClock() |
| 750 | { |
| 751 | frameTimer.stop(); |
| 752 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 753 | |
Pierre Ossman | a2b80d6 | 2018-03-23 09:30:09 +0100 | [diff] [blame] | 754 | int VNCServerST::msToNextUpdate() |
| 755 | { |
| 756 | // FIXME: If the application is updating slower than frameRate then |
| 757 | // we could allow the clients more time here |
| 758 | |
| 759 | if (!frameTimer.isStarted()) |
| 760 | return 1000/rfb::Server::frameRate/2; |
| 761 | else |
| 762 | return frameTimer.getRemainingMs(); |
| 763 | } |
| 764 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 765 | // writeUpdate() is called on a regular interval in order to see what |
| 766 | // updates are pending and propagates them to the update tracker for |
| 767 | // each client. It uses the ComparingUpdateTracker's compare() method |
| 768 | // to filter out areas of the screen which haven't actually changed. It |
| 769 | // also checks the state of the (server-side) rendered cursor, if |
| 770 | // necessary rendering it again with the correct background. |
| 771 | |
| 772 | void VNCServerST::writeUpdate() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 773 | { |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 774 | UpdateInfo ui; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 775 | Region toCheck; |
| 776 | |
| 777 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 778 | |
| 779 | assert(blockCounter == 0); |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 780 | assert(desktopStarted); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 781 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 782 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 783 | toCheck = ui.changed.union_(ui.copied); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 784 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 785 | if (needRenderedCursor()) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 786 | Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 787 | .translate(cursorPos.subtract(cursor->hotspot())) |
| 788 | .intersect(pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 789 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 790 | if (!toCheck.intersect(clippedCursorRect).is_empty()) |
| 791 | renderedCursorInvalid = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | pb->grabRegion(toCheck); |
| 795 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 796 | if (getComparerState()) |
| 797 | comparer->enable(); |
| 798 | else |
| 799 | comparer->disable(); |
| 800 | |
| 801 | if (comparer->compare()) |
Constantin Kaplinsky | f0b3be7 | 2008-08-21 05:22:04 +0000 | [diff] [blame] | 802 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 803 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 804 | comparer->clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 805 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 806 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 807 | ci_next = ci; ci_next++; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 808 | (*ci)->add_copied(ui.copied, ui.copy_delta); |
| 809 | (*ci)->add_changed(ui.changed); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 810 | (*ci)->writeFramebufferUpdateOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 811 | } |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 812 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 813 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 814 | // checkUpdate() is called by clients to see if it is safe to read from |
| 815 | // the framebuffer at this time. |
| 816 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 817 | Region VNCServerST::getPendingRegion() |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 818 | { |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 819 | UpdateInfo ui; |
| 820 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 821 | // Block clients as the frame buffer cannot be safely accessed |
| 822 | if (blockCounter > 0) |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 823 | return pb->getRect(); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 824 | |
| 825 | // Block client from updating if there are pending updates |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 826 | if (comparer->is_empty()) |
| 827 | return Region(); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 828 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 829 | comparer->getUpdateInfo(&ui, pb->getRect()); |
| 830 | |
| 831 | return ui.changed.union_(ui.copied); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 834 | const RenderedCursor* VNCServerST::getRenderedCursor() |
| 835 | { |
| 836 | if (renderedCursorInvalid) { |
Pierre Ossman | 7cb4f31 | 2017-02-24 13:25:00 +0100 | [diff] [blame] | 837 | renderedCursor.update(pb, cursor, cursorPos); |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 838 | renderedCursorInvalid = false; |
| 839 | } |
| 840 | |
| 841 | return &renderedCursor; |
| 842 | } |
| 843 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 844 | bool VNCServerST::getComparerState() |
| 845 | { |
| 846 | if (rfb::Server::compareFB == 0) |
| 847 | return false; |
| 848 | if (rfb::Server::compareFB != 2) |
| 849 | return true; |
| 850 | |
| 851 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 852 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 853 | ci_next = ci; ci_next++; |
| 854 | if ((*ci)->getComparerState()) |
| 855 | return true; |
| 856 | } |
| 857 | return false; |
| 858 | } |