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