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