Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 2 | * Copyright 2009-2015 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 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 20 | // Debug output on what the congestion control is up to |
| 21 | #undef CONGESTION_DEBUG |
| 22 | |
| 23 | #include <sys/time.h> |
| 24 | |
| 25 | #ifdef CONGESTION_DEBUG |
| 26 | #include <sys/socket.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <netinet/tcp.h> |
| 29 | #endif |
| 30 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 31 | #include <network/TcpSocket.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 32 | #include <rfb/VNCSConnectionST.h> |
| 33 | #include <rfb/LogWriter.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 34 | #include <rfb/Security.h> |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 35 | #include <rfb/screenTypes.h> |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 36 | #include <rfb/fenceTypes.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | #include <rfb/ServerCore.h> |
| 38 | #include <rfb/ComparingUpdateTracker.h> |
| 39 | #include <rfb/KeyRemapper.h> |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 40 | #include <rfb/Encoder.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | #define XK_MISCELLANY |
| 42 | #define XK_XKB_KEYS |
| 43 | #include <rfb/keysymdef.h> |
| 44 | |
| 45 | using namespace rfb; |
| 46 | |
| 47 | static LogWriter vlog("VNCSConnST"); |
| 48 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 49 | // This window should get us going fairly fast on a decent bandwidth network. |
| 50 | // If it's too high, it will rapidly be reduced and stay low. |
| 51 | static const unsigned INITIAL_WINDOW = 16384; |
| 52 | |
| 53 | // TCP's minimal window is 3*MSS. But since we don't know the MSS, we |
klemens | 0536d09 | 2017-01-28 20:56:56 +0100 | [diff] [blame] | 54 | // make a guess at 4 KiB (it's probably a bit higher). |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 55 | static const unsigned MINIMUM_WINDOW = 4096; |
| 56 | |
| 57 | // The current default maximum window for Linux (4 MiB). Should be a good |
| 58 | // limit for now... |
| 59 | static const unsigned MAXIMUM_WINDOW = 4194304; |
| 60 | |
| 61 | struct RTTInfo { |
| 62 | struct timeval tv; |
| 63 | int offset; |
| 64 | unsigned inFlight; |
| 65 | }; |
| 66 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 67 | VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s, |
| 68 | bool reverse) |
Pierre Ossman | 7069bdd | 2015-02-06 14:41:58 +0100 | [diff] [blame] | 69 | : sock(s), reverseConnection(reverse), |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 70 | queryConnectTimer(this), inProcessMessages(false), |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 71 | pendingSyncFence(false), syncFence(false), fenceFlags(0), |
| 72 | fenceDataLen(0), fenceData(NULL), |
Pierre Ossman | b1cd6ca | 2015-03-03 16:37:43 +0100 | [diff] [blame] | 73 | baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0), |
| 74 | minRTT(-1), seenCongestion(false), |
| 75 | pingCounter(0), congestionTimer(this), |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 76 | server(server_), updates(false), |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 77 | updateRenderedCursor(false), removeRenderedCursor(false), |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 78 | continuousUpdates(false), encodeManager(this), pointerEventTime(0), |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 79 | accessRights(AccessDefault), startTime(time(0)) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | { |
| 81 | setStreams(&sock->inStream(), &sock->outStream()); |
| 82 | peerEndpoint.buf = sock->getPeerEndpoint(); |
| 83 | VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf); |
| 84 | |
| 85 | // Configure the socket |
| 86 | setSocketTimeouts(); |
| 87 | lastEventTime = time(0); |
| 88 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 89 | server->clients.push_front(this); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | VNCSConnectionST::~VNCSConnectionST() |
| 94 | { |
| 95 | // If we reach here then VNCServerST is deleting us! |
| 96 | VNCServerST::connectionsLog.write(1,"closed: %s (%s)", |
| 97 | peerEndpoint.buf, |
| 98 | (closeReason.buf) ? closeReason.buf : ""); |
| 99 | |
| 100 | // Release any keys the client still had pressed |
| 101 | std::set<rdr::U32>::iterator i; |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 102 | for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) { |
| 103 | vlog.debug("Releasing key 0x%x on client disconnect", *i); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | server->desktop->keyEvent(*i, false); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 105 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | if (server->pointerClient == this) |
| 107 | server->pointerClient = 0; |
| 108 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | // Remove this client from the server |
| 110 | server->clients.remove(this); |
| 111 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 112 | delete [] fenceData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | // Methods called from VNCServerST |
| 117 | |
| 118 | bool VNCSConnectionST::init() |
| 119 | { |
| 120 | try { |
| 121 | initialiseProtocol(); |
| 122 | } catch (rdr::Exception& e) { |
| 123 | close(e.str()); |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void VNCSConnectionST::close(const char* reason) |
| 130 | { |
| 131 | // Log the reason for the close |
| 132 | if (!closeReason.buf) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 133 | closeReason.buf = strDup(reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 134 | else |
| 135 | vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason); |
| 136 | |
| 137 | if (authenticated()) { |
| 138 | server->lastDisconnectTime = time(0); |
| 139 | } |
| 140 | |
| 141 | // Just shutdown the socket and mark our state as closing. Eventually the |
| 142 | // calling code will call VNCServerST's removeSocket() method causing us to |
| 143 | // be deleted. |
| 144 | sock->shutdown(); |
| 145 | setState(RFBSTATE_CLOSING); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void VNCSConnectionST::processMessages() |
| 150 | { |
| 151 | if (state() == RFBSTATE_CLOSING) return; |
| 152 | try { |
| 153 | // - Now set appropriate socket timeouts and process data |
| 154 | setSocketTimeouts(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 155 | |
| 156 | inProcessMessages = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 158 | // Get the underlying TCP layer to build large packets if we send |
| 159 | // multiple small responses. |
| 160 | network::TcpSocket::cork(sock->getFd(), true); |
| 161 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | while (getInStream()->checkNoWait(1)) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 163 | if (pendingSyncFence) { |
| 164 | syncFence = true; |
| 165 | pendingSyncFence = false; |
| 166 | } |
| 167 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | processMsg(); |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 169 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 170 | if (syncFence) { |
| 171 | writer()->writeFence(fenceFlags, fenceDataLen, fenceData); |
| 172 | syncFence = false; |
| 173 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 176 | // Flush out everything in case we go idle after this. |
| 177 | network::TcpSocket::cork(sock->getFd(), false); |
| 178 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 179 | inProcessMessages = false; |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 180 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 181 | // If there were anything requiring an update, try to send it here. |
| 182 | // We wait until now with this to aggregate responses and to give |
| 183 | // higher priority to user actions such as keyboard and pointer events. |
| 184 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | } catch (rdr::EndOfStream&) { |
| 186 | close("Clean disconnection"); |
| 187 | } catch (rdr::Exception &e) { |
| 188 | close(e.str()); |
| 189 | } |
| 190 | } |
| 191 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 192 | void VNCSConnectionST::flushSocket() |
| 193 | { |
| 194 | if (state() == RFBSTATE_CLOSING) return; |
| 195 | try { |
| 196 | setSocketTimeouts(); |
| 197 | sock->outStream().flush(); |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 198 | // Flushing the socket might release an update that was previously |
| 199 | // delayed because of congestion. |
| 200 | if (sock->outStream().bufferUsage() == 0) |
| 201 | writeFramebufferUpdate(); |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 202 | } catch (rdr::Exception &e) { |
| 203 | close(e.str()); |
| 204 | } |
| 205 | } |
| 206 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 207 | void VNCSConnectionST::pixelBufferChange() |
| 208 | { |
| 209 | try { |
| 210 | if (!authenticated()) return; |
| 211 | if (cp.width && cp.height && (server->pb->width() != cp.width || |
| 212 | server->pb->height() != cp.height)) |
| 213 | { |
| 214 | // We need to clip the next update to the new size, but also add any |
| 215 | // extra bits if it's bigger. If we wanted to do this exactly, something |
| 216 | // like the code below would do it, but at the moment we just update the |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 217 | // entire new size. However, we do need to clip the damagedCursorRegion |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | // because that might be added to updates in writeFramebufferUpdate(). |
| 219 | |
| 220 | //updates.intersect(server->pb->getRect()); |
| 221 | // |
| 222 | //if (server->pb->width() > cp.width) |
| 223 | // updates.add_changed(Rect(cp.width, 0, server->pb->width(), |
| 224 | // server->pb->height())); |
| 225 | //if (server->pb->height() > cp.height) |
| 226 | // updates.add_changed(Rect(0, cp.height, cp.width, |
| 227 | // server->pb->height())); |
| 228 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 229 | damagedCursorRegion.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | |
| 231 | cp.width = server->pb->width(); |
| 232 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 233 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 234 | if (state() == RFBSTATE_NORMAL) { |
Pierre Ossman | 2ee430a | 2009-05-28 12:54:24 +0000 | [diff] [blame] | 235 | // We should only send EDS to client asking for both |
| 236 | if (!writer()->writeExtendedDesktopSize()) { |
| 237 | if (!writer()->writeSetDesktopSize()) { |
| 238 | close("Client does not support desktop resize"); |
| 239 | return; |
| 240 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | // Just update the whole screen at the moment because we're too lazy to |
| 245 | // work out what's actually changed. |
| 246 | updates.clear(); |
| 247 | updates.add_changed(server->pb->getRect()); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 248 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 249 | } catch(rdr::Exception &e) { |
| 250 | close(e.str()); |
| 251 | } |
| 252 | } |
| 253 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 254 | void VNCSConnectionST::writeFramebufferUpdateOrClose() |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 255 | { |
| 256 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 257 | writeFramebufferUpdate(); |
| 258 | } catch(rdr::Exception &e) { |
| 259 | close(e.str()); |
| 260 | } |
| 261 | } |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 262 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 263 | void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason) |
| 264 | { |
| 265 | try { |
| 266 | screenLayoutChange(reason); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 267 | } catch(rdr::Exception &e) { |
| 268 | close(e.str()); |
| 269 | } |
| 270 | } |
| 271 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 272 | void VNCSConnectionST::bellOrClose() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 273 | { |
| 274 | try { |
| 275 | if (state() == RFBSTATE_NORMAL) writer()->writeBell(); |
| 276 | } catch(rdr::Exception& e) { |
| 277 | close(e.str()); |
| 278 | } |
| 279 | } |
| 280 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 281 | void VNCSConnectionST::serverCutTextOrClose(const char *str, int len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 282 | { |
| 283 | try { |
| 284 | if (!(accessRights & AccessCutText)) return; |
| 285 | if (!rfb::Server::sendCutText) return; |
| 286 | if (state() == RFBSTATE_NORMAL) |
| 287 | writer()->writeServerCutText(str, len); |
| 288 | } catch(rdr::Exception& e) { |
| 289 | close(e.str()); |
| 290 | } |
| 291 | } |
| 292 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 293 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 294 | void VNCSConnectionST::setDesktopNameOrClose(const char *name) |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 295 | { |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 296 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 297 | setDesktopName(name); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 298 | } catch(rdr::Exception& e) { |
| 299 | close(e.str()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 304 | void VNCSConnectionST::setCursorOrClose() |
| 305 | { |
| 306 | try { |
| 307 | setCursor(); |
| 308 | } catch(rdr::Exception& e) { |
| 309 | close(e.str()); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | int VNCSConnectionST::checkIdleTimeout() |
| 315 | { |
| 316 | int idleTimeout = rfb::Server::idleTimeout; |
| 317 | if (idleTimeout == 0) return 0; |
| 318 | if (state() != RFBSTATE_NORMAL && idleTimeout < 15) |
| 319 | idleTimeout = 15; // minimum of 15 seconds while authenticating |
| 320 | time_t now = time(0); |
| 321 | if (now < lastEventTime) { |
| 322 | // Someone must have set the time backwards. Set lastEventTime so that the |
| 323 | // idleTimeout will count from now. |
| 324 | vlog.info("Time has gone backwards - resetting idle timeout"); |
| 325 | lastEventTime = now; |
| 326 | } |
| 327 | int timeLeft = lastEventTime + idleTimeout - now; |
| 328 | if (timeLeft < -60) { |
| 329 | // Our callback is over a minute late - someone must have set the time |
| 330 | // forwards. Set lastEventTime so that the idleTimeout will count from |
| 331 | // now. |
| 332 | vlog.info("Time has gone forwards - resetting idle timeout"); |
| 333 | lastEventTime = now; |
| 334 | return secsToMillis(idleTimeout); |
| 335 | } |
| 336 | if (timeLeft <= 0) { |
| 337 | close("Idle timeout"); |
| 338 | return 0; |
| 339 | } |
| 340 | return secsToMillis(timeLeft); |
| 341 | } |
| 342 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 343 | |
| 344 | bool VNCSConnectionST::getComparerState() |
| 345 | { |
| 346 | // We interpret a low compression level as an indication that the client |
| 347 | // wants to prioritise CPU usage over bandwidth, and hence disable the |
| 348 | // comparing update tracker. |
| 349 | return (cp.compressLevel == -1) || (cp.compressLevel > 1); |
| 350 | } |
| 351 | |
| 352 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 353 | // renderedCursorChange() is called whenever the server-side rendered cursor |
| 354 | // changes shape or position. It ensures that the next update will clean up |
| 355 | // the old rendered cursor and if necessary draw the new rendered cursor. |
| 356 | |
| 357 | void VNCSConnectionST::renderedCursorChange() |
| 358 | { |
| 359 | if (state() != RFBSTATE_NORMAL) return; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 360 | if (!damagedCursorRegion.is_empty()) |
Pierre Ossman | 5c9e1e5 | 2011-11-08 10:32:05 +0000 | [diff] [blame] | 361 | removeRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 362 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 363 | updateRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 364 | writeFramebufferUpdateOrClose(); |
| 365 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // needRenderedCursor() returns true if this client needs the server-side |
| 369 | // rendered cursor. This may be because it does not support local cursor or |
| 370 | // because the current cursor position has not been set by this client. |
| 371 | // Unfortunately we can't know for sure when the current cursor position has |
| 372 | // been set by this client. We guess that this is the case when the current |
| 373 | // cursor position is the same as the last pointer event from this client, or |
| 374 | // if it is a very short time since this client's last pointer event (up to a |
| 375 | // second). [ Ideally we should do finer-grained timing here and make the time |
| 376 | // configurable, but I don't think it's that important. ] |
| 377 | |
| 378 | bool VNCSConnectionST::needRenderedCursor() |
| 379 | { |
Peter Ã…strand | ffeeb26 | 2010-02-10 09:29:00 +0000 | [diff] [blame] | 380 | bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 381 | return (state() == RFBSTATE_NORMAL |
Peter Ã…strand | ffeeb26 | 2010-02-10 09:29:00 +0000 | [diff] [blame] | 382 | && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
| 386 | void VNCSConnectionST::approveConnectionOrClose(bool accept, |
| 387 | const char* reason) |
| 388 | { |
| 389 | try { |
| 390 | approveConnection(accept, reason); |
| 391 | } catch (rdr::Exception& e) { |
| 392 | close(e.str()); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | |
| 398 | // -=- Callbacks from SConnection |
| 399 | |
| 400 | void VNCSConnectionST::authSuccess() |
| 401 | { |
| 402 | lastEventTime = time(0); |
| 403 | |
| 404 | server->startDesktop(); |
| 405 | |
| 406 | // - Set the connection parameters appropriately |
| 407 | cp.width = server->pb->width(); |
| 408 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 409 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 410 | cp.setName(server->getName()); |
| 411 | |
| 412 | // - Set the default pixel format |
| 413 | cp.setPF(server->pb->getPF()); |
| 414 | char buffer[256]; |
| 415 | cp.pf().print(buffer, 256); |
| 416 | vlog.info("Server default pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 417 | |
| 418 | // - Mark the entire display as "dirty" |
| 419 | updates.add_changed(server->pb->getRect()); |
| 420 | startTime = time(0); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 421 | |
| 422 | // - Bootstrap the congestion control |
| 423 | ackedOffset = sock->outStream().length(); |
| 424 | congWindow = INITIAL_WINDOW; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void VNCSConnectionST::queryConnection(const char* userName) |
| 428 | { |
| 429 | // - Authentication succeeded - clear from blacklist |
| 430 | CharArray name; name.buf = sock->getPeerAddress(); |
| 431 | server->blHosts->clearBlackmark(name.buf); |
| 432 | |
| 433 | // - Special case to provide a more useful error message |
| 434 | if (rfb::Server::neverShared && !rfb::Server::disconnectClients && |
| 435 | server->authClientCount() > 0) { |
| 436 | approveConnection(false, "The server is already in use"); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | // - Does the client have the right to bypass the query? |
| 441 | if (reverseConnection || |
| 442 | !(rfb::Server::queryConnect || sock->requiresQuery()) || |
| 443 | (accessRights & AccessNoQuery)) |
| 444 | { |
| 445 | approveConnection(true); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | // - Get the server to display an Accept/Reject dialog, if required |
| 450 | // If a dialog is displayed, the result will be PENDING, and the |
| 451 | // server will call approveConnection at a later time |
| 452 | CharArray reason; |
| 453 | VNCServerST::queryResult qr = server->queryConnection(sock, userName, |
| 454 | &reason.buf); |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 455 | if (qr == VNCServerST::PENDING) { |
| 456 | queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 457 | return; |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 458 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 459 | |
| 460 | // - If server returns ACCEPT/REJECT then pass result to SConnection |
| 461 | approveConnection(qr == VNCServerST::ACCEPT, reason.buf); |
| 462 | } |
| 463 | |
| 464 | void VNCSConnectionST::clientInit(bool shared) |
| 465 | { |
| 466 | lastEventTime = time(0); |
| 467 | if (rfb::Server::alwaysShared || reverseConnection) shared = true; |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 468 | if (!(accessRights & AccessNonShared)) shared = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 469 | if (rfb::Server::neverShared) shared = false; |
| 470 | if (!shared) { |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 471 | if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 472 | // - Close all the other connected clients |
| 473 | vlog.debug("non-shared connection - closing clients"); |
| 474 | server->closeClients("Non-shared connection requested", getSock()); |
| 475 | } else { |
| 476 | // - Refuse this connection if there are existing clients, in addition to |
| 477 | // this one |
| 478 | if (server->authClientCount() > 1) { |
| 479 | close("Server is already in use"); |
| 480 | return; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | SConnection::clientInit(shared); |
| 485 | } |
| 486 | |
| 487 | void VNCSConnectionST::setPixelFormat(const PixelFormat& pf) |
| 488 | { |
| 489 | SConnection::setPixelFormat(pf); |
| 490 | char buffer[256]; |
| 491 | pf.print(buffer, 256); |
| 492 | vlog.info("Client pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 493 | setCursor(); |
| 494 | } |
| 495 | |
| 496 | void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask) |
| 497 | { |
| 498 | pointerEventTime = lastEventTime = time(0); |
| 499 | server->lastUserInputTime = lastEventTime; |
| 500 | if (!(accessRights & AccessPtrEvents)) return; |
| 501 | if (!rfb::Server::acceptPointerEvents) return; |
| 502 | if (!server->pointerClient || server->pointerClient == this) { |
| 503 | pointerEventPos = pos; |
| 504 | if (buttonMask) |
| 505 | server->pointerClient = this; |
| 506 | else |
| 507 | server->pointerClient = 0; |
| 508 | server->desktop->pointerEvent(pointerEventPos, buttonMask); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | class VNCSConnectionSTShiftPresser { |
| 514 | public: |
| 515 | VNCSConnectionSTShiftPresser(SDesktop* desktop_) |
| 516 | : desktop(desktop_), pressed(false) {} |
| 517 | ~VNCSConnectionSTShiftPresser() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 518 | if (pressed) { |
| 519 | vlog.debug("Releasing fake Shift_L"); |
| 520 | desktop->keyEvent(XK_Shift_L, false); |
| 521 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 522 | } |
| 523 | void press() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 524 | vlog.debug("Pressing fake Shift_L"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 525 | desktop->keyEvent(XK_Shift_L, true); |
| 526 | pressed = true; |
| 527 | } |
| 528 | SDesktop* desktop; |
| 529 | bool pressed; |
| 530 | }; |
| 531 | |
| 532 | // keyEvent() - record in the pressedKeys which keys were pressed. Allow |
| 533 | // multiple down events (for autorepeat), but only allow a single up event. |
| 534 | void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) { |
| 535 | lastEventTime = time(0); |
| 536 | server->lastUserInputTime = lastEventTime; |
| 537 | if (!(accessRights & AccessKeyEvents)) return; |
| 538 | if (!rfb::Server::acceptKeyEvents) return; |
| 539 | |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 540 | if (down) |
| 541 | vlog.debug("Key pressed: 0x%x", key); |
| 542 | else |
| 543 | vlog.debug("Key released: 0x%x", key); |
| 544 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 545 | // Remap the key if required |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 546 | if (server->keyRemapper) { |
| 547 | rdr::U32 newkey; |
| 548 | newkey = server->keyRemapper->remapKey(key); |
| 549 | if (newkey != key) { |
| 550 | vlog.debug("Key remapped to 0x%x", newkey); |
| 551 | key = newkey; |
| 552 | } |
| 553 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 554 | |
| 555 | // Turn ISO_Left_Tab into shifted Tab. |
| 556 | VNCSConnectionSTShiftPresser shiftPresser(server->desktop); |
| 557 | if (key == XK_ISO_Left_Tab) { |
| 558 | if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() && |
| 559 | pressedKeys.find(XK_Shift_R) == pressedKeys.end()) |
| 560 | shiftPresser.press(); |
| 561 | key = XK_Tab; |
| 562 | } |
| 563 | |
| 564 | if (down) { |
| 565 | pressedKeys.insert(key); |
| 566 | } else { |
| 567 | if (!pressedKeys.erase(key)) return; |
| 568 | } |
| 569 | server->desktop->keyEvent(key, down); |
| 570 | } |
| 571 | |
| 572 | void VNCSConnectionST::clientCutText(const char* str, int len) |
| 573 | { |
| 574 | if (!(accessRights & AccessCutText)) return; |
| 575 | if (!rfb::Server::acceptCutText) return; |
| 576 | server->desktop->clientCutText(str, len); |
| 577 | } |
| 578 | |
| 579 | void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental) |
| 580 | { |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 581 | Rect safeRect; |
| 582 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 583 | if (!(accessRights & AccessView)) return; |
| 584 | |
| 585 | SConnection::framebufferUpdateRequest(r, incremental); |
| 586 | |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 587 | // Check that the client isn't sending crappy requests |
| 588 | if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) { |
| 589 | vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 590 | r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height); |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 591 | safeRect = r.intersect(Rect(0, 0, cp.width, cp.height)); |
| 592 | } else { |
| 593 | safeRect = r; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 596 | // Just update the requested region. |
| 597 | // Framebuffer update will be sent a bit later, see processMessages(). |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 598 | Region reqRgn(r); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 599 | if (!incremental || !continuousUpdates) |
| 600 | requested.assign_union(reqRgn); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 601 | |
| 602 | if (!incremental) { |
| 603 | // Non-incremental update - treat as if area requested has changed |
| 604 | updates.add_changed(reqRgn); |
| 605 | server->comparer->add_changed(reqRgn); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 606 | |
| 607 | // And send the screen layout to the client (which, unlike the |
| 608 | // framebuffer dimensions, the client doesn't get during init) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 609 | writer()->writeExtendedDesktopSize(); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 610 | |
| 611 | // We do not send a DesktopSize since it only contains the |
| 612 | // framebuffer size (which the client already should know) and |
| 613 | // because some clients don't handle extra DesktopSize events |
| 614 | // very well. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 615 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 618 | void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height, |
| 619 | const ScreenSet& layout) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 620 | { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 621 | unsigned int result; |
| 622 | |
Michal Srb | b318b8f | 2014-11-24 13:18:28 +0200 | [diff] [blame] | 623 | if (!(accessRights & AccessSetDesktopSize)) return; |
| 624 | if (!rfb::Server::acceptSetDesktopSize) return; |
| 625 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 626 | // Don't bother the desktop with an invalid configuration |
| 627 | if (!layout.validate(fb_width, fb_height)) { |
| 628 | writer()->writeExtendedDesktopSize(reasonClient, resultInvalid, |
| 629 | fb_width, fb_height, layout); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 630 | writeFramebufferUpdate(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 631 | return; |
| 632 | } |
| 633 | |
| 634 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 635 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 636 | // protocol-wise, but unnecessary. |
| 637 | result = server->desktop->setScreenLayout(fb_width, fb_height, layout); |
| 638 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 639 | writer()->writeExtendedDesktopSize(reasonClient, result, |
| 640 | fb_width, fb_height, layout); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 641 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 642 | // Only notify other clients on success |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 643 | if (result == resultSuccess) { |
| 644 | if (server->screenLayout != layout) |
| 645 | throw Exception("Desktop configured a different screen layout than requested"); |
| 646 | server->notifyScreenLayoutChange(this); |
| 647 | } |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 648 | |
| 649 | // but always send back a reply to the requesting client |
| 650 | // (do this last as it might throw an exception on socket errors) |
| 651 | writeFramebufferUpdate(); |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 654 | void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 655 | { |
| 656 | if (flags & fenceFlagRequest) { |
| 657 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 658 | pendingSyncFence = true; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 659 | |
| 660 | fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext); |
| 661 | fenceDataLen = len; |
| 662 | delete [] fenceData; |
| 663 | if (len > 0) { |
| 664 | fenceData = new char[len]; |
| 665 | memcpy(fenceData, data, len); |
| 666 | } |
| 667 | |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | // We handle everything synchronously so we trivially honor these modes |
| 672 | flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter); |
| 673 | |
| 674 | writer()->writeFence(flags, len, data); |
| 675 | return; |
| 676 | } |
| 677 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 678 | struct RTTInfo rttInfo; |
| 679 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 680 | switch (len) { |
| 681 | case 0: |
| 682 | // Initial dummy fence; |
| 683 | break; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 684 | case sizeof(struct RTTInfo): |
| 685 | memcpy(&rttInfo, data, sizeof(struct RTTInfo)); |
| 686 | handleRTTPong(rttInfo); |
| 687 | break; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 688 | default: |
| 689 | vlog.error("Fence response of unexpected size received"); |
| 690 | } |
| 691 | } |
| 692 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 693 | void VNCSConnectionST::enableContinuousUpdates(bool enable, |
| 694 | int x, int y, int w, int h) |
| 695 | { |
| 696 | Rect rect; |
| 697 | |
| 698 | if (!cp.supportsFence || !cp.supportsContinuousUpdates) |
| 699 | throw Exception("Client tried to enable continuous updates when not allowed"); |
| 700 | |
| 701 | continuousUpdates = enable; |
| 702 | |
| 703 | rect.setXYWH(x, y, w, h); |
| 704 | cuRegion.reset(rect); |
| 705 | |
| 706 | if (enable) { |
| 707 | requested.clear(); |
| 708 | writeFramebufferUpdate(); |
| 709 | } else { |
| 710 | writer()->writeEndOfContinuousUpdates(); |
| 711 | } |
| 712 | } |
| 713 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 714 | // supportsLocalCursor() is called whenever the status of |
| 715 | // cp.supportsLocalCursor has changed. If the client does now support local |
| 716 | // cursor, we make sure that the old server-side rendered cursor is cleaned up |
| 717 | // and the cursor is sent to the client. |
| 718 | |
| 719 | void VNCSConnectionST::supportsLocalCursor() |
| 720 | { |
| 721 | if (cp.supportsLocalCursor || cp.supportsLocalXCursor) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 722 | if (!damagedCursorRegion.is_empty()) |
Pierre Ossman | 5c9e1e5 | 2011-11-08 10:32:05 +0000 | [diff] [blame] | 723 | removeRenderedCursor = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 724 | setCursor(); |
| 725 | } |
| 726 | } |
| 727 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 728 | void VNCSConnectionST::supportsFence() |
| 729 | { |
| 730 | writer()->writeFence(fenceFlagRequest, 0, NULL); |
| 731 | } |
| 732 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 733 | void VNCSConnectionST::supportsContinuousUpdates() |
| 734 | { |
| 735 | // We refuse to use continuous updates if we cannot monitor the buffer |
| 736 | // usage using fences. |
| 737 | if (!cp.supportsFence) |
| 738 | return; |
| 739 | |
| 740 | writer()->writeEndOfContinuousUpdates(); |
| 741 | } |
| 742 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 743 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 744 | bool VNCSConnectionST::handleTimeout(Timer* t) |
| 745 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 746 | try { |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 747 | if (t == &congestionTimer) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 748 | updateCongestion(); |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 749 | else if (t == &queryConnectTimer) { |
| 750 | if (state() == RFBSTATE_QUERYING) |
| 751 | approveConnection(false, "The attempt to prompt the user to accept the connection failed"); |
| 752 | } |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 753 | } catch (rdr::Exception& e) { |
| 754 | close(e.str()); |
| 755 | } |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 756 | |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 761 | void VNCSConnectionST::writeRTTPing() |
| 762 | { |
| 763 | struct RTTInfo rttInfo; |
| 764 | |
| 765 | if (!cp.supportsFence) |
| 766 | return; |
| 767 | |
| 768 | memset(&rttInfo, 0, sizeof(struct RTTInfo)); |
| 769 | |
| 770 | gettimeofday(&rttInfo.tv, NULL); |
| 771 | rttInfo.offset = sock->outStream().length(); |
| 772 | rttInfo.inFlight = rttInfo.offset - ackedOffset; |
| 773 | |
| 774 | // We need to make sure any old update are already processed by the |
| 775 | // time we get the response back. This allows us to reliably throttle |
| 776 | // back on client overload, as well as network overload. |
| 777 | writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore, |
| 778 | sizeof(struct RTTInfo), (const char*)&rttInfo); |
| 779 | |
| 780 | pingCounter++; |
| 781 | |
| 782 | sentOffset = rttInfo.offset; |
| 783 | |
| 784 | // Let some data flow before we adjust the settings |
| 785 | if (!congestionTimer.isStarted()) |
| 786 | congestionTimer.start(__rfbmin(baseRTT * 2, 100)); |
| 787 | } |
| 788 | |
| 789 | void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo) |
| 790 | { |
| 791 | unsigned rtt, delay; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 792 | |
| 793 | pingCounter--; |
| 794 | |
| 795 | rtt = msSince(&rttInfo.tv); |
| 796 | if (rtt < 1) |
| 797 | rtt = 1; |
| 798 | |
| 799 | ackedOffset = rttInfo.offset; |
| 800 | |
| 801 | // Try to estimate wire latency by tracking lowest seen latency |
| 802 | if (rtt < baseRTT) |
| 803 | baseRTT = rtt; |
| 804 | |
| 805 | if (rttInfo.inFlight > congWindow) { |
| 806 | seenCongestion = true; |
| 807 | |
| 808 | // Estimate added delay because of overtaxed buffers |
| 809 | delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow; |
| 810 | |
| 811 | if (delay < rtt) |
| 812 | rtt -= delay; |
| 813 | else |
| 814 | rtt = 1; |
| 815 | |
| 816 | // If we underestimate the congestion window, then we'll get a latency |
| 817 | // that's less than the wire latency, which will confuse other portions |
| 818 | // of the code. |
| 819 | if (rtt < baseRTT) |
| 820 | rtt = baseRTT; |
| 821 | } |
| 822 | |
| 823 | // We only keep track of the minimum latency seen (for a given interval) |
klemens | 0536d09 | 2017-01-28 20:56:56 +0100 | [diff] [blame] | 824 | // on the basis that we want to avoid continuous buffer issue, but don't |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 825 | // mind (or even approve of) bursts. |
| 826 | if (rtt < minRTT) |
| 827 | minRTT = rtt; |
| 828 | } |
| 829 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 830 | bool VNCSConnectionST::isCongested() |
| 831 | { |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 832 | int offset; |
| 833 | |
| 834 | // Stuff still waiting in the send buffer? |
Pierre Ossman | 352d062 | 2016-04-29 15:50:54 +0200 | [diff] [blame] | 835 | sock->outStream().flush(); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 836 | if (sock->outStream().bufferUsage() > 0) |
| 837 | return true; |
| 838 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 839 | if (!cp.supportsFence) |
| 840 | return false; |
| 841 | |
| 842 | // Idle for too long? (and no data on the wire) |
| 843 | // |
| 844 | // FIXME: This should really just be one baseRTT, but we're getting |
| 845 | // problems with triggering the idle timeout on each update. |
| 846 | // Maybe we need to use a moving average for the wire latency |
| 847 | // instead of baseRTT. |
| 848 | if ((sentOffset == ackedOffset) && |
| 849 | (sock->outStream().getIdleTime() > 2 * baseRTT)) { |
| 850 | |
| 851 | #ifdef CONGESTION_DEBUG |
| 852 | if (congWindow > INITIAL_WINDOW) |
| 853 | fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n", |
| 854 | INITIAL_WINDOW / 1024, sock->outStream().getIdleTime()); |
| 855 | #endif |
| 856 | |
| 857 | // Close congestion window and allow a transfer |
| 858 | // FIXME: Reset baseRTT like Linux Vegas? |
| 859 | congWindow = __rfbmin(INITIAL_WINDOW, congWindow); |
| 860 | |
| 861 | return false; |
| 862 | } |
| 863 | |
| 864 | offset = sock->outStream().length(); |
| 865 | |
| 866 | // FIXME: Should we compensate for non-update data? |
| 867 | // (i.e. use sentOffset instead of offset) |
| 868 | if ((offset - ackedOffset) < congWindow) |
| 869 | return false; |
| 870 | |
| 871 | // If we just have one outstanding "ping", that means the client has |
| 872 | // started receiving our update. In order to not regress compared to |
| 873 | // before we had congestion avoidance, we allow another update here. |
| 874 | // This could further clog up the tubes, but congestion control isn't |
| 875 | // really working properly right now anyway as the wire would otherwise |
| 876 | // be idle for at least RTT/2. |
| 877 | if (pingCounter == 1) |
| 878 | return false; |
| 879 | |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | void VNCSConnectionST::updateCongestion() |
| 885 | { |
| 886 | unsigned diff; |
| 887 | |
| 888 | if (!seenCongestion) |
| 889 | return; |
| 890 | |
| 891 | diff = minRTT - baseRTT; |
| 892 | |
| 893 | if (diff > __rfbmin(100, baseRTT)) { |
| 894 | // Way too fast |
| 895 | congWindow = congWindow * baseRTT / minRTT; |
| 896 | } else if (diff > __rfbmin(50, baseRTT/2)) { |
| 897 | // Slightly too fast |
| 898 | congWindow -= 4096; |
| 899 | } else if (diff < 5) { |
| 900 | // Way too slow |
| 901 | congWindow += 8192; |
| 902 | } else if (diff < 25) { |
| 903 | // Too slow |
| 904 | congWindow += 4096; |
| 905 | } |
| 906 | |
| 907 | if (congWindow < MINIMUM_WINDOW) |
| 908 | congWindow = MINIMUM_WINDOW; |
| 909 | if (congWindow > MAXIMUM_WINDOW) |
| 910 | congWindow = MAXIMUM_WINDOW; |
| 911 | |
| 912 | #ifdef CONGESTION_DEBUG |
| 913 | fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n", |
| 914 | minRTT, baseRTT, congWindow / 1024, |
| 915 | congWindow * 8.0 / baseRTT / 1000.0); |
| 916 | |
| 917 | #ifdef TCP_INFO |
| 918 | struct tcp_info tcp_info; |
| 919 | socklen_t tcp_info_length; |
| 920 | |
| 921 | tcp_info_length = sizeof(tcp_info); |
| 922 | if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO, |
| 923 | (void *)&tcp_info, &tcp_info_length) == 0) { |
| 924 | fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n", |
| 925 | tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000, |
| 926 | tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024); |
| 927 | } |
| 928 | #endif |
| 929 | |
| 930 | #endif |
| 931 | |
| 932 | minRTT = -1; |
| 933 | seenCongestion = false; |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 937 | void VNCSConnectionST::writeFramebufferUpdate() |
| 938 | { |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 939 | Region req; |
| 940 | UpdateInfo ui; |
| 941 | bool needNewUpdateInfo; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 942 | bool drawRenderedCursor; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 943 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 944 | // We're in the middle of processing a command that's supposed to be |
| 945 | // synchronised. Allowing an update to slip out right now might violate |
| 946 | // that synchronisation. |
| 947 | if (syncFence) |
| 948 | return; |
| 949 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 950 | // We try to aggregate responses, so don't send out anything whilst we |
| 951 | // still have incoming messages. processMessages() will give us another |
| 952 | // chance to run once things are idle. |
| 953 | if (inProcessMessages) |
| 954 | return; |
| 955 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 956 | if (state() != RFBSTATE_NORMAL) |
| 957 | return; |
| 958 | if (requested.is_empty() && !continuousUpdates) |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 959 | return; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 960 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 961 | // Check that we actually have some space on the link and retry in a |
| 962 | // bit if things are congested. |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 963 | if (isCongested()) |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 964 | return; |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 965 | |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 966 | // In continuous mode, we will be outputting at least three distinct |
| 967 | // messages. We need to aggregate these in order to not clog up TCP's |
| 968 | // congestion window. |
| 969 | network::TcpSocket::cork(sock->getFd(), true); |
| 970 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 971 | // First take care of any updates that cannot contain framebuffer data |
| 972 | // changes. |
| 973 | if (writer()->needNoDataUpdate()) { |
| 974 | writer()->writeNoDataUpdate(); |
| 975 | requested.clear(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 976 | if (!continuousUpdates) |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 977 | goto out; |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 978 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 979 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 980 | updates.enable_copyrect(cp.useCopyRect); |
| 981 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 982 | // Fetch updates from server object, and see if we are allowed to send |
| 983 | // anything right now (the framebuffer might have changed in ways we |
| 984 | // haven't yet been informed of). |
| 985 | if (!server->checkUpdate()) |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 986 | goto out; |
Constantin Kaplinsky | a09dc14 | 2008-12-18 12:08:15 +0000 | [diff] [blame] | 987 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 988 | // Get the lists of updates. Prior to exporting the data to the `ui' object, |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 989 | // getUpdateInfo() will normalize the `updates' object such way that its |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 990 | // `changed' and `copied' regions would not intersect. |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 991 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 992 | if (continuousUpdates) |
| 993 | req = cuRegion.union_(requested); |
| 994 | else |
| 995 | req = requested; |
| 996 | |
| 997 | updates.getUpdateInfo(&ui, req); |
| 998 | needNewUpdateInfo = false; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 999 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1000 | // If the previous position of the rendered cursor overlaps the source of the |
| 1001 | // copy, then when the copy happens the corresponding rectangle in the |
| 1002 | // destination will be wrong, so add it to the changed region. |
| 1003 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1004 | if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) { |
| 1005 | Region bogusCopiedCursor; |
| 1006 | |
| 1007 | bogusCopiedCursor.copyFrom(damagedCursorRegion); |
| 1008 | bogusCopiedCursor.translate(ui.copy_delta); |
| 1009 | bogusCopiedCursor.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 1010 | if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1011 | updates.add_changed(bogusCopiedCursor); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1012 | needNewUpdateInfo = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1016 | // If we need to remove the old rendered cursor, just add the region to |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1017 | // the changed region. |
| 1018 | |
| 1019 | if (removeRenderedCursor) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1020 | updates.add_changed(damagedCursorRegion); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1021 | needNewUpdateInfo = true; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1022 | damagedCursorRegion.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1023 | removeRenderedCursor = false; |
| 1024 | } |
| 1025 | |
| 1026 | // Return if there is nothing to send the client. |
| 1027 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1028 | if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor) |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 1029 | goto out; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1030 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1031 | // The `updates' object could change, make sure we have valid update info. |
| 1032 | |
| 1033 | if (needNewUpdateInfo) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1034 | updates.getUpdateInfo(&ui, req); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1035 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1036 | // If the client needs a server-side rendered cursor, work out the cursor |
| 1037 | // rectangle. If it's empty then don't bother drawing it, but if it overlaps |
| 1038 | // with the update region, we need to draw the rendered cursor regardless of |
| 1039 | // whether it has changed. |
| 1040 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1041 | drawRenderedCursor = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1042 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1043 | Rect renderedCursorRect; |
| 1044 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1045 | renderedCursorRect |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 1046 | = server->renderedCursor.getEffectiveRect() |
| 1047 | .intersect(req.get_bounding_rect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1048 | |
| 1049 | if (renderedCursorRect.is_empty()) { |
| 1050 | drawRenderedCursor = false; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1051 | } else if (updateRenderedCursor) { |
| 1052 | drawRenderedCursor = true; |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 1053 | } else if (!ui.changed.union_(ui.copied) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1054 | .intersect(renderedCursorRect).is_empty()) { |
| 1055 | drawRenderedCursor = true; |
| 1056 | } |
| 1057 | |
| 1058 | // We could remove the new cursor rect from updates here. It's not clear |
| 1059 | // whether this is worth it. If we do remove it, then we won't draw over |
| 1060 | // the same bit of screen twice, but we have the overhead of a more complex |
| 1061 | // region. |
| 1062 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1063 | //if (drawRenderedCursor) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1064 | // updates.subtract(renderedCursorRect); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1065 | // updates.getUpdateInfo(&ui, req); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1066 | //} |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1067 | |
| 1068 | damagedCursorRegion.assign_union(renderedCursorRect); |
| 1069 | updateRenderedCursor = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 1072 | if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) { |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1073 | RenderedCursor *cursor; |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 1074 | |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1075 | cursor = NULL; |
| 1076 | if (drawRenderedCursor) |
| 1077 | cursor = &server->renderedCursor; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1078 | |
| 1079 | writeRTTPing(); |
| 1080 | |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1081 | encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor); |
| 1082 | |
| 1083 | writeRTTPing(); |
| 1084 | |
Pierre Ossman | b64dbf2 | 2015-06-09 16:10:39 +0200 | [diff] [blame] | 1085 | // The request might be for just part of the screen, so we cannot |
| 1086 | // just clear the entire update tracker. |
Pierre Ossman | 3c56d4f | 2015-06-23 15:29:37 +0200 | [diff] [blame] | 1087 | updates.subtract(req); |
Pierre Ossman | b64dbf2 | 2015-06-09 16:10:39 +0200 | [diff] [blame] | 1088 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1089 | requested.clear(); |
| 1090 | } |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 1091 | |
| 1092 | out: |
| 1093 | network::TcpSocket::cork(sock->getFd(), false); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1097 | void VNCSConnectionST::screenLayoutChange(rdr::U16 reason) |
| 1098 | { |
| 1099 | if (!authenticated()) |
| 1100 | return; |
| 1101 | |
| 1102 | cp.screenLayout = server->screenLayout; |
| 1103 | |
| 1104 | if (state() != RFBSTATE_NORMAL) |
| 1105 | return; |
| 1106 | |
| 1107 | writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height, |
| 1108 | cp.screenLayout); |
| 1109 | writeFramebufferUpdate(); |
| 1110 | } |
| 1111 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1112 | |
| 1113 | // setCursor() is called whenever the cursor has changed shape or pixel format. |
| 1114 | // If the client supports local cursor then it will arrange for the cursor to |
| 1115 | // be sent to the client. |
| 1116 | |
| 1117 | void VNCSConnectionST::setCursor() |
| 1118 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1119 | if (state() != RFBSTATE_NORMAL) |
| 1120 | return; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1121 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 1122 | cp.setCursor(*server->cursor); |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1123 | |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame^] | 1124 | if (!writer()->writeSetCursorWithAlpha()) { |
| 1125 | if (!writer()->writeSetCursor()) { |
| 1126 | if (!writer()->writeSetXCursor()) { |
| 1127 | // No client support |
| 1128 | return; |
| 1129 | } |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1133 | writeFramebufferUpdate(); |
| 1134 | } |
| 1135 | |
| 1136 | void VNCSConnectionST::setDesktopName(const char *name) |
| 1137 | { |
| 1138 | cp.setName(name); |
| 1139 | |
| 1140 | if (state() != RFBSTATE_NORMAL) |
| 1141 | return; |
| 1142 | |
| 1143 | if (!writer()->writeSetDesktopName()) { |
| 1144 | fprintf(stderr, "Client does not support desktop rename\n"); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | void VNCSConnectionST::setSocketTimeouts() |
| 1152 | { |
| 1153 | int timeoutms = rfb::Server::clientWaitTimeMillis; |
| 1154 | soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout)); |
| 1155 | if (timeoutms == 0) |
| 1156 | timeoutms = -1; |
| 1157 | sock->inStream().setTimeout(timeoutms); |
| 1158 | sock->outStream().setTimeout(timeoutms); |
| 1159 | } |
| 1160 | |
| 1161 | char* VNCSConnectionST::getStartTime() |
| 1162 | { |
| 1163 | char* result = ctime(&startTime); |
| 1164 | result[24] = '\0'; |
| 1165 | return result; |
| 1166 | } |
| 1167 | |
| 1168 | void VNCSConnectionST::setStatus(int status) |
| 1169 | { |
| 1170 | switch (status) { |
| 1171 | case 0: |
| 1172 | accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView; |
| 1173 | break; |
| 1174 | case 1: |
Pierre Ossman | 7728be2 | 2015-03-03 16:39:37 +0100 | [diff] [blame] | 1175 | accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | case 2: |
Adam Tkac | 8e98506 | 2011-02-07 11:33:57 +0000 | [diff] [blame] | 1178 | accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | framebufferUpdateRequest(server->pb->getRect(), false); |
| 1182 | } |
| 1183 | int VNCSConnectionST::getStatus() |
| 1184 | { |
| 1185 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007) |
| 1186 | return 0; |
| 1187 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001) |
| 1188 | return 1; |
| 1189 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000) |
| 1190 | return 2; |
| 1191 | return 4; |
| 1192 | } |
| 1193 | |