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