blob: 0f4ca94204b6e3602d9d5da3484ce5e5fe9e08fe [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanf8e3b342015-01-26 14:37:04 +01002 * Copyright 2009-2015 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 Ossman1b478e52011-11-15 12:08:30 +000020// 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 Ossmana830bec2011-11-08 12:12:02 +000031#include <network/TcpSocket.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032#include <rfb/VNCSConnectionST.h>
33#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000034#include <rfb/Security.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000035#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000036#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <rfb/ServerCore.h>
38#include <rfb/ComparingUpdateTracker.h>
39#include <rfb/KeyRemapper.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010040#include <rfb/Encoder.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041#define XK_MISCELLANY
42#define XK_XKB_KEYS
43#include <rfb/keysymdef.h>
44
45using namespace rfb;
46
47static LogWriter vlog("VNCSConnST");
48
Pierre Ossman1b478e52011-11-15 12:08:30 +000049// 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.
51static 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).
55static const unsigned MINIMUM_WINDOW = 4096;
56
57// The current default maximum window for Linux (4 MiB). Should be a good
58// limit for now...
59static const unsigned MAXIMUM_WINDOW = 4194304;
60
61struct RTTInfo {
62 struct timeval tv;
63 int offset;
64 unsigned inFlight;
65};
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
68 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010069 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010070 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000071 pendingSyncFence(false), syncFence(false), fenceFlags(0),
72 fenceDataLen(0), fenceData(NULL),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010073 baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0),
74 minRTT(-1), seenCongestion(false),
75 pingCounter(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020076 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020077 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmanc0397262014-03-14 15:59:46 +010078 continuousUpdates(false), encodeManager(this),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000079 updateTimer(this), pointerEventTime(0),
80 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081{
82 setStreams(&sock->inStream(), &sock->outStream());
83 peerEndpoint.buf = sock->getPeerEndpoint();
84 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
85
86 // Configure the socket
87 setSocketTimeouts();
88 lastEventTime = time(0);
89
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 server->clients.push_front(this);
91}
92
93
94VNCSConnectionST::~VNCSConnectionST()
95{
96 // If we reach here then VNCServerST is deleting us!
97 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
98 peerEndpoint.buf,
99 (closeReason.buf) ? closeReason.buf : "");
100
101 // Release any keys the client still had pressed
102 std::set<rdr::U32>::iterator i;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200103 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) {
104 vlog.debug("Releasing key 0x%x on client disconnect", *i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 server->desktop->keyEvent(*i, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200106 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 if (server->pointerClient == this)
108 server->pointerClient = 0;
109
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110 // Remove this client from the server
111 server->clients.remove(this);
112
Pierre Ossman2c764942011-11-14 15:54:30 +0000113 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114}
115
116
117// Methods called from VNCServerST
118
119bool VNCSConnectionST::init()
120{
121 try {
122 initialiseProtocol();
123 } catch (rdr::Exception& e) {
124 close(e.str());
125 return false;
126 }
127 return true;
128}
129
130void VNCSConnectionST::close(const char* reason)
131{
132 // Log the reason for the close
133 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000134 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 else
136 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
137
138 if (authenticated()) {
139 server->lastDisconnectTime = time(0);
140 }
141
142 // Just shutdown the socket and mark our state as closing. Eventually the
143 // calling code will call VNCServerST's removeSocket() method causing us to
144 // be deleted.
145 sock->shutdown();
146 setState(RFBSTATE_CLOSING);
147}
148
149
150void VNCSConnectionST::processMessages()
151{
152 if (state() == RFBSTATE_CLOSING) return;
153 try {
154 // - Now set appropriate socket timeouts and process data
155 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000156
157 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158
Pierre Ossmana830bec2011-11-08 12:12:02 +0000159 // Get the underlying TCP layer to build large packets if we send
160 // multiple small responses.
161 network::TcpSocket::cork(sock->getFd(), true);
162
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000163 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000164 if (pendingSyncFence) {
165 syncFence = true;
166 pendingSyncFence = false;
167 }
168
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000170
Pierre Ossman2c764942011-11-14 15:54:30 +0000171 if (syncFence) {
172 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
173 syncFence = false;
174 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 }
176
Pierre Ossmana830bec2011-11-08 12:12:02 +0000177 // Flush out everything in case we go idle after this.
178 network::TcpSocket::cork(sock->getFd(), false);
179
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000180 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000181
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000182 // If there were anything requiring an update, try to send it here.
183 // We wait until now with this to aggregate responses and to give
184 // higher priority to user actions such as keyboard and pointer events.
185 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 } catch (rdr::EndOfStream&) {
187 close("Clean disconnection");
188 } catch (rdr::Exception &e) {
189 close(e.str());
190 }
191}
192
Pierre Ossmand408ca52016-04-29 14:26:05 +0200193void VNCSConnectionST::flushSocket()
194{
195 if (state() == RFBSTATE_CLOSING) return;
196 try {
197 setSocketTimeouts();
198 sock->outStream().flush();
199 } catch (rdr::Exception &e) {
200 close(e.str());
201 }
202}
203
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204void VNCSConnectionST::pixelBufferChange()
205{
206 try {
207 if (!authenticated()) return;
208 if (cp.width && cp.height && (server->pb->width() != cp.width ||
209 server->pb->height() != cp.height))
210 {
211 // We need to clip the next update to the new size, but also add any
212 // extra bits if it's bigger. If we wanted to do this exactly, something
213 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200214 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 // because that might be added to updates in writeFramebufferUpdate().
216
217 //updates.intersect(server->pb->getRect());
218 //
219 //if (server->pb->width() > cp.width)
220 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
221 // server->pb->height()));
222 //if (server->pb->height() > cp.height)
223 // updates.add_changed(Rect(0, cp.height, cp.width,
224 // server->pb->height()));
225
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200226 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000227
228 cp.width = server->pb->width();
229 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000230 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000231 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000232 // We should only send EDS to client asking for both
233 if (!writer()->writeExtendedDesktopSize()) {
234 if (!writer()->writeSetDesktopSize()) {
235 close("Client does not support desktop resize");
236 return;
237 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238 }
239 }
240 }
241 // Just update the whole screen at the moment because we're too lazy to
242 // work out what's actually changed.
243 updates.clear();
244 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000245 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 } catch(rdr::Exception &e) {
247 close(e.str());
248 }
249}
250
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000251void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000252{
253 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000254 writeFramebufferUpdate();
255 } catch(rdr::Exception &e) {
256 close(e.str());
257 }
258}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000259
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000260void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
261{
262 try {
263 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000264 } catch(rdr::Exception &e) {
265 close(e.str());
266 }
267}
268
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000269void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000270{
271 try {
272 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
273 } catch(rdr::Exception& e) {
274 close(e.str());
275 }
276}
277
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000278void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000279{
280 try {
281 if (!(accessRights & AccessCutText)) return;
282 if (!rfb::Server::sendCutText) return;
283 if (state() == RFBSTATE_NORMAL)
284 writer()->writeServerCutText(str, len);
285 } catch(rdr::Exception& e) {
286 close(e.str());
287 }
288}
289
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000290
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000291void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000292{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000293 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000294 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000295 } catch(rdr::Exception& e) {
296 close(e.str());
297 }
298}
299
300
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000301void VNCSConnectionST::setCursorOrClose()
302{
303 try {
304 setCursor();
305 } catch(rdr::Exception& e) {
306 close(e.str());
307 }
308}
309
310
311int VNCSConnectionST::checkIdleTimeout()
312{
313 int idleTimeout = rfb::Server::idleTimeout;
314 if (idleTimeout == 0) return 0;
315 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
316 idleTimeout = 15; // minimum of 15 seconds while authenticating
317 time_t now = time(0);
318 if (now < lastEventTime) {
319 // Someone must have set the time backwards. Set lastEventTime so that the
320 // idleTimeout will count from now.
321 vlog.info("Time has gone backwards - resetting idle timeout");
322 lastEventTime = now;
323 }
324 int timeLeft = lastEventTime + idleTimeout - now;
325 if (timeLeft < -60) {
326 // Our callback is over a minute late - someone must have set the time
327 // forwards. Set lastEventTime so that the idleTimeout will count from
328 // now.
329 vlog.info("Time has gone forwards - resetting idle timeout");
330 lastEventTime = now;
331 return secsToMillis(idleTimeout);
332 }
333 if (timeLeft <= 0) {
334 close("Idle timeout");
335 return 0;
336 }
337 return secsToMillis(timeLeft);
338}
339
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000340
341bool VNCSConnectionST::getComparerState()
342{
343 // We interpret a low compression level as an indication that the client
344 // wants to prioritise CPU usage over bandwidth, and hence disable the
345 // comparing update tracker.
346 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
347}
348
349
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350// renderedCursorChange() is called whenever the server-side rendered cursor
351// changes shape or position. It ensures that the next update will clean up
352// the old rendered cursor and if necessary draw the new rendered cursor.
353
354void VNCSConnectionST::renderedCursorChange()
355{
356 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200357 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000358 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000359 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200360 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000361 writeFramebufferUpdateOrClose();
362 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000363}
364
365// needRenderedCursor() returns true if this client needs the server-side
366// rendered cursor. This may be because it does not support local cursor or
367// because the current cursor position has not been set by this client.
368// Unfortunately we can't know for sure when the current cursor position has
369// been set by this client. We guess that this is the case when the current
370// cursor position is the same as the last pointer event from this client, or
371// if it is a very short time since this client's last pointer event (up to a
372// second). [ Ideally we should do finer-grained timing here and make the time
373// configurable, but I don't think it's that important. ]
374
375bool VNCSConnectionST::needRenderedCursor()
376{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000377 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000378 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000379 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000380}
381
382
383void VNCSConnectionST::approveConnectionOrClose(bool accept,
384 const char* reason)
385{
386 try {
387 approveConnection(accept, reason);
388 } catch (rdr::Exception& e) {
389 close(e.str());
390 }
391}
392
393
394
395// -=- Callbacks from SConnection
396
397void VNCSConnectionST::authSuccess()
398{
399 lastEventTime = time(0);
400
401 server->startDesktop();
402
403 // - Set the connection parameters appropriately
404 cp.width = server->pb->width();
405 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000406 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000407 cp.setName(server->getName());
408
409 // - Set the default pixel format
410 cp.setPF(server->pb->getPF());
411 char buffer[256];
412 cp.pf().print(buffer, 256);
413 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000414
415 // - Mark the entire display as "dirty"
416 updates.add_changed(server->pb->getRect());
417 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000418
419 // - Bootstrap the congestion control
420 ackedOffset = sock->outStream().length();
421 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000422}
423
424void VNCSConnectionST::queryConnection(const char* userName)
425{
426 // - Authentication succeeded - clear from blacklist
427 CharArray name; name.buf = sock->getPeerAddress();
428 server->blHosts->clearBlackmark(name.buf);
429
430 // - Special case to provide a more useful error message
431 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
432 server->authClientCount() > 0) {
433 approveConnection(false, "The server is already in use");
434 return;
435 }
436
437 // - Does the client have the right to bypass the query?
438 if (reverseConnection ||
439 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
440 (accessRights & AccessNoQuery))
441 {
442 approveConnection(true);
443 return;
444 }
445
446 // - Get the server to display an Accept/Reject dialog, if required
447 // If a dialog is displayed, the result will be PENDING, and the
448 // server will call approveConnection at a later time
449 CharArray reason;
450 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
451 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100452 if (qr == VNCServerST::PENDING) {
453 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000454 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100455 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000456
457 // - If server returns ACCEPT/REJECT then pass result to SConnection
458 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
459}
460
461void VNCSConnectionST::clientInit(bool shared)
462{
463 lastEventTime = time(0);
464 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100465 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000466 if (rfb::Server::neverShared) shared = false;
467 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100468 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000469 // - Close all the other connected clients
470 vlog.debug("non-shared connection - closing clients");
471 server->closeClients("Non-shared connection requested", getSock());
472 } else {
473 // - Refuse this connection if there are existing clients, in addition to
474 // this one
475 if (server->authClientCount() > 1) {
476 close("Server is already in use");
477 return;
478 }
479 }
480 }
481 SConnection::clientInit(shared);
482}
483
484void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
485{
486 SConnection::setPixelFormat(pf);
487 char buffer[256];
488 pf.print(buffer, 256);
489 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000490 setCursor();
491}
492
493void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
494{
495 pointerEventTime = lastEventTime = time(0);
496 server->lastUserInputTime = lastEventTime;
497 if (!(accessRights & AccessPtrEvents)) return;
498 if (!rfb::Server::acceptPointerEvents) return;
499 if (!server->pointerClient || server->pointerClient == this) {
500 pointerEventPos = pos;
501 if (buttonMask)
502 server->pointerClient = this;
503 else
504 server->pointerClient = 0;
505 server->desktop->pointerEvent(pointerEventPos, buttonMask);
506 }
507}
508
509
510class VNCSConnectionSTShiftPresser {
511public:
512 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
513 : desktop(desktop_), pressed(false) {}
514 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200515 if (pressed) {
516 vlog.debug("Releasing fake Shift_L");
517 desktop->keyEvent(XK_Shift_L, false);
518 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000519 }
520 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200521 vlog.debug("Pressing fake Shift_L");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000522 desktop->keyEvent(XK_Shift_L, true);
523 pressed = true;
524 }
525 SDesktop* desktop;
526 bool pressed;
527};
528
529// keyEvent() - record in the pressedKeys which keys were pressed. Allow
530// multiple down events (for autorepeat), but only allow a single up event.
531void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
532 lastEventTime = time(0);
533 server->lastUserInputTime = lastEventTime;
534 if (!(accessRights & AccessKeyEvents)) return;
535 if (!rfb::Server::acceptKeyEvents) return;
536
Pierre Ossman9a153b02015-08-31 10:01:14 +0200537 if (down)
538 vlog.debug("Key pressed: 0x%x", key);
539 else
540 vlog.debug("Key released: 0x%x", key);
541
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000542 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200543 if (server->keyRemapper) {
544 rdr::U32 newkey;
545 newkey = server->keyRemapper->remapKey(key);
546 if (newkey != key) {
547 vlog.debug("Key remapped to 0x%x", newkey);
548 key = newkey;
549 }
550 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000551
552 // Turn ISO_Left_Tab into shifted Tab.
553 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
554 if (key == XK_ISO_Left_Tab) {
555 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
556 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
557 shiftPresser.press();
558 key = XK_Tab;
559 }
560
561 if (down) {
562 pressedKeys.insert(key);
563 } else {
564 if (!pressedKeys.erase(key)) return;
565 }
566 server->desktop->keyEvent(key, down);
567}
568
569void VNCSConnectionST::clientCutText(const char* str, int len)
570{
571 if (!(accessRights & AccessCutText)) return;
572 if (!rfb::Server::acceptCutText) return;
573 server->desktop->clientCutText(str, len);
574}
575
576void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
577{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000578 Rect safeRect;
579
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000580 if (!(accessRights & AccessView)) return;
581
582 SConnection::framebufferUpdateRequest(r, incremental);
583
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000584 // Check that the client isn't sending crappy requests
585 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
586 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
587 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000588 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
589 } else {
590 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000591 }
592
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000593 // Just update the requested region.
594 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000595 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000596 if (!incremental || !continuousUpdates)
597 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000598
599 if (!incremental) {
600 // Non-incremental update - treat as if area requested has changed
601 updates.add_changed(reqRgn);
602 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000603
604 // And send the screen layout to the client (which, unlike the
605 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000606 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000607
608 // We do not send a DesktopSize since it only contains the
609 // framebuffer size (which the client already should know) and
610 // because some clients don't handle extra DesktopSize events
611 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000612 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000613}
614
Pierre Ossman34bb0612009-03-21 21:16:14 +0000615void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
616 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000617{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000618 unsigned int result;
619
Michal Srbb318b8f2014-11-24 13:18:28 +0200620 if (!(accessRights & AccessSetDesktopSize)) return;
621 if (!rfb::Server::acceptSetDesktopSize) return;
622
Pierre Ossman04e62db2009-03-23 16:57:07 +0000623 // Don't bother the desktop with an invalid configuration
624 if (!layout.validate(fb_width, fb_height)) {
625 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
626 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000627 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000628 return;
629 }
630
631 // FIXME: the desktop will call back to VNCServerST and an extra set
632 // of ExtendedDesktopSize messages will be sent. This is okay
633 // protocol-wise, but unnecessary.
634 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
635
Pierre Ossman04e62db2009-03-23 16:57:07 +0000636 writer()->writeExtendedDesktopSize(reasonClient, result,
637 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000638
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000639 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000640 if (result == resultSuccess) {
641 if (server->screenLayout != layout)
642 throw Exception("Desktop configured a different screen layout than requested");
643 server->notifyScreenLayoutChange(this);
644 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000645
646 // but always send back a reply to the requesting client
647 // (do this last as it might throw an exception on socket errors)
648 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000649}
650
Pierre Ossman2c764942011-11-14 15:54:30 +0000651void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
652{
653 if (flags & fenceFlagRequest) {
654 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000655 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000656
657 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
658 fenceDataLen = len;
659 delete [] fenceData;
660 if (len > 0) {
661 fenceData = new char[len];
662 memcpy(fenceData, data, len);
663 }
664
665 return;
666 }
667
668 // We handle everything synchronously so we trivially honor these modes
669 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
670
671 writer()->writeFence(flags, len, data);
672 return;
673 }
674
Pierre Ossman1b478e52011-11-15 12:08:30 +0000675 struct RTTInfo rttInfo;
676
Pierre Ossman2c764942011-11-14 15:54:30 +0000677 switch (len) {
678 case 0:
679 // Initial dummy fence;
680 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000681 case sizeof(struct RTTInfo):
682 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
683 handleRTTPong(rttInfo);
684 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000685 default:
686 vlog.error("Fence response of unexpected size received");
687 }
688}
689
Pierre Ossman1b478e52011-11-15 12:08:30 +0000690void VNCSConnectionST::enableContinuousUpdates(bool enable,
691 int x, int y, int w, int h)
692{
693 Rect rect;
694
695 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
696 throw Exception("Client tried to enable continuous updates when not allowed");
697
698 continuousUpdates = enable;
699
700 rect.setXYWH(x, y, w, h);
701 cuRegion.reset(rect);
702
703 if (enable) {
704 requested.clear();
705 writeFramebufferUpdate();
706 } else {
707 writer()->writeEndOfContinuousUpdates();
708 }
709}
710
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000711// supportsLocalCursor() is called whenever the status of
712// cp.supportsLocalCursor has changed. If the client does now support local
713// cursor, we make sure that the old server-side rendered cursor is cleaned up
714// and the cursor is sent to the client.
715
716void VNCSConnectionST::supportsLocalCursor()
717{
718 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200719 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000720 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000721 setCursor();
722 }
723}
724
Pierre Ossman2c764942011-11-14 15:54:30 +0000725void VNCSConnectionST::supportsFence()
726{
727 writer()->writeFence(fenceFlagRequest, 0, NULL);
728}
729
Pierre Ossman1b478e52011-11-15 12:08:30 +0000730void VNCSConnectionST::supportsContinuousUpdates()
731{
732 // We refuse to use continuous updates if we cannot monitor the buffer
733 // usage using fences.
734 if (!cp.supportsFence)
735 return;
736
737 writer()->writeEndOfContinuousUpdates();
738}
739
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000740
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000741bool VNCSConnectionST::handleTimeout(Timer* t)
742{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000743 try {
744 if (t == &updateTimer)
745 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000746 else if (t == &congestionTimer)
747 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100748 else if (t == &queryConnectTimer) {
749 if (state() == RFBSTATE_QUERYING)
750 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
751 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000752 } catch (rdr::Exception& e) {
753 close(e.str());
754 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000755
756 return false;
757}
758
759
Pierre Ossman1b478e52011-11-15 12:08:30 +0000760void VNCSConnectionST::writeRTTPing()
761{
762 struct RTTInfo rttInfo;
763
764 if (!cp.supportsFence)
765 return;
766
767 memset(&rttInfo, 0, sizeof(struct RTTInfo));
768
769 gettimeofday(&rttInfo.tv, NULL);
770 rttInfo.offset = sock->outStream().length();
771 rttInfo.inFlight = rttInfo.offset - ackedOffset;
772
773 // We need to make sure any old update are already processed by the
774 // time we get the response back. This allows us to reliably throttle
775 // back on client overload, as well as network overload.
776 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
777 sizeof(struct RTTInfo), (const char*)&rttInfo);
778
779 pingCounter++;
780
781 sentOffset = rttInfo.offset;
782
783 // Let some data flow before we adjust the settings
784 if (!congestionTimer.isStarted())
785 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
786}
787
788void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
789{
790 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000791
792 pingCounter--;
793
794 rtt = msSince(&rttInfo.tv);
795 if (rtt < 1)
796 rtt = 1;
797
798 ackedOffset = rttInfo.offset;
799
800 // Try to estimate wire latency by tracking lowest seen latency
801 if (rtt < baseRTT)
802 baseRTT = rtt;
803
804 if (rttInfo.inFlight > congWindow) {
805 seenCongestion = true;
806
807 // Estimate added delay because of overtaxed buffers
808 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
809
810 if (delay < rtt)
811 rtt -= delay;
812 else
813 rtt = 1;
814
815 // If we underestimate the congestion window, then we'll get a latency
816 // that's less than the wire latency, which will confuse other portions
817 // of the code.
818 if (rtt < baseRTT)
819 rtt = baseRTT;
820 }
821
822 // We only keep track of the minimum latency seen (for a given interval)
823 // on the basis that we want to avoid continous buffer issue, but don't
824 // mind (or even approve of) bursts.
825 if (rtt < minRTT)
826 minRTT = rtt;
827}
828
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000829bool VNCSConnectionST::isCongested()
830{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000831 int offset;
832
833 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000834 if (sock->outStream().bufferUsage() > 0)
835 return true;
836
Pierre Ossman1b478e52011-11-15 12:08:30 +0000837 if (!cp.supportsFence)
838 return false;
839
840 // Idle for too long? (and no data on the wire)
841 //
842 // FIXME: This should really just be one baseRTT, but we're getting
843 // problems with triggering the idle timeout on each update.
844 // Maybe we need to use a moving average for the wire latency
845 // instead of baseRTT.
846 if ((sentOffset == ackedOffset) &&
847 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
848
849#ifdef CONGESTION_DEBUG
850 if (congWindow > INITIAL_WINDOW)
851 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
852 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
853#endif
854
855 // Close congestion window and allow a transfer
856 // FIXME: Reset baseRTT like Linux Vegas?
857 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
858
859 return false;
860 }
861
862 offset = sock->outStream().length();
863
864 // FIXME: Should we compensate for non-update data?
865 // (i.e. use sentOffset instead of offset)
866 if ((offset - ackedOffset) < congWindow)
867 return false;
868
869 // If we just have one outstanding "ping", that means the client has
870 // started receiving our update. In order to not regress compared to
871 // before we had congestion avoidance, we allow another update here.
872 // This could further clog up the tubes, but congestion control isn't
873 // really working properly right now anyway as the wire would otherwise
874 // be idle for at least RTT/2.
875 if (pingCounter == 1)
876 return false;
877
878 return true;
879}
880
881
882void VNCSConnectionST::updateCongestion()
883{
884 unsigned diff;
885
886 if (!seenCongestion)
887 return;
888
889 diff = minRTT - baseRTT;
890
891 if (diff > __rfbmin(100, baseRTT)) {
892 // Way too fast
893 congWindow = congWindow * baseRTT / minRTT;
894 } else if (diff > __rfbmin(50, baseRTT/2)) {
895 // Slightly too fast
896 congWindow -= 4096;
897 } else if (diff < 5) {
898 // Way too slow
899 congWindow += 8192;
900 } else if (diff < 25) {
901 // Too slow
902 congWindow += 4096;
903 }
904
905 if (congWindow < MINIMUM_WINDOW)
906 congWindow = MINIMUM_WINDOW;
907 if (congWindow > MAXIMUM_WINDOW)
908 congWindow = MAXIMUM_WINDOW;
909
910#ifdef CONGESTION_DEBUG
911 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
912 minRTT, baseRTT, congWindow / 1024,
913 congWindow * 8.0 / baseRTT / 1000.0);
914
915#ifdef TCP_INFO
916 struct tcp_info tcp_info;
917 socklen_t tcp_info_length;
918
919 tcp_info_length = sizeof(tcp_info);
920 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
921 (void *)&tcp_info, &tcp_info_length) == 0) {
922 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
923 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
924 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
925 }
926#endif
927
928#endif
929
930 minRTT = -1;
931 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000932}
933
934
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000935void VNCSConnectionST::writeFramebufferUpdate()
936{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000937 Region req;
938 UpdateInfo ui;
939 bool needNewUpdateInfo;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200940 bool drawRenderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000941
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000942 updateTimer.stop();
943
Pierre Ossman2c764942011-11-14 15:54:30 +0000944 // We're in the middle of processing a command that's supposed to be
945 // synchronised. Allowing an update to slip out right now might violate
946 // that synchronisation.
947 if (syncFence)
948 return;
949
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000950 // We try to aggregate responses, so don't send out anything whilst we
951 // still have incoming messages. processMessages() will give us another
952 // chance to run once things are idle.
953 if (inProcessMessages)
954 return;
955
Pierre Ossman1b478e52011-11-15 12:08:30 +0000956 if (state() != RFBSTATE_NORMAL)
957 return;
958 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000959 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000960
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000961 // Check that we actually have some space on the link and retry in a
962 // bit if things are congested.
963 if (isCongested()) {
964 updateTimer.start(50);
965 return;
966 }
967
Pierre Ossman36dadf82011-11-15 12:11:32 +0000968 // In continuous mode, we will be outputting at least three distinct
969 // messages. We need to aggregate these in order to not clog up TCP's
970 // congestion window.
971 network::TcpSocket::cork(sock->getFd(), true);
972
Pierre Ossmane9962f72009-04-23 12:31:42 +0000973 // First take care of any updates that cannot contain framebuffer data
974 // changes.
975 if (writer()->needNoDataUpdate()) {
976 writer()->writeNoDataUpdate();
977 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000978 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000979 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000980 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000981
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000982 updates.enable_copyrect(cp.useCopyRect);
983
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000984 // Fetch updates from server object, and see if we are allowed to send
985 // anything right now (the framebuffer might have changed in ways we
986 // haven't yet been informed of).
987 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000988 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000989
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000990 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000991 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000992 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000993
Pierre Ossman1b478e52011-11-15 12:08:30 +0000994 if (continuousUpdates)
995 req = cuRegion.union_(requested);
996 else
997 req = requested;
998
999 updates.getUpdateInfo(&ui, req);
1000 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001001
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001002 // If the previous position of the rendered cursor overlaps the source of the
1003 // copy, then when the copy happens the corresponding rectangle in the
1004 // destination will be wrong, so add it to the changed region.
1005
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001006 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1007 Region bogusCopiedCursor;
1008
1009 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1010 bogusCopiedCursor.translate(ui.copy_delta);
1011 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001012 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001014 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001015 }
1016 }
1017
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001018 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001019 // the changed region.
1020
1021 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001022 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001023 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001024 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001025 removeRenderedCursor = false;
1026 }
1027
1028 // Return if there is nothing to send the client.
1029
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001030 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001031 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001032
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001033 // The `updates' object could change, make sure we have valid update info.
1034
1035 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001036 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001037
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001038 // If the client needs a server-side rendered cursor, work out the cursor
1039 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1040 // with the update region, we need to draw the rendered cursor regardless of
1041 // whether it has changed.
1042
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001043 drawRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001044 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001045 Rect renderedCursorRect;
1046
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001047 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001048 = server->renderedCursor.getEffectiveRect()
1049 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001050
1051 if (renderedCursorRect.is_empty()) {
1052 drawRenderedCursor = false;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001053 } else if (updateRenderedCursor) {
1054 drawRenderedCursor = true;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001055 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001056 .intersect(renderedCursorRect).is_empty()) {
1057 drawRenderedCursor = true;
1058 }
1059
1060 // We could remove the new cursor rect from updates here. It's not clear
1061 // whether this is worth it. If we do remove it, then we won't draw over
1062 // the same bit of screen twice, but we have the overhead of a more complex
1063 // region.
1064
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001065 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001066 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001067 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001068 //}
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001069
1070 damagedCursorRegion.assign_union(renderedCursorRect);
1071 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001072 }
1073
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001074 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanc0397262014-03-14 15:59:46 +01001075 RenderedCursor *cursor;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001076
Pierre Ossmanc0397262014-03-14 15:59:46 +01001077 cursor = NULL;
1078 if (drawRenderedCursor)
1079 cursor = &server->renderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +00001080
1081 writeRTTPing();
1082
Pierre Ossmanc0397262014-03-14 15:59:46 +01001083 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
1084
1085 writeRTTPing();
1086
Pierre Ossmanb64dbf22015-06-09 16:10:39 +02001087 // The request might be for just part of the screen, so we cannot
1088 // just clear the entire update tracker.
Pierre Ossman3c56d4f2015-06-23 15:29:37 +02001089 updates.subtract(req);
Pierre Ossmanb64dbf22015-06-09 16:10:39 +02001090
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001091 requested.clear();
1092 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001093
1094out:
1095 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001096}
1097
1098
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001099void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1100{
1101 if (!authenticated())
1102 return;
1103
1104 cp.screenLayout = server->screenLayout;
1105
1106 if (state() != RFBSTATE_NORMAL)
1107 return;
1108
1109 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1110 cp.screenLayout);
1111 writeFramebufferUpdate();
1112}
1113
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001114
1115// setCursor() is called whenever the cursor has changed shape or pixel format.
1116// If the client supports local cursor then it will arrange for the cursor to
1117// be sent to the client.
1118
1119void VNCSConnectionST::setCursor()
1120{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001121 if (state() != RFBSTATE_NORMAL)
1122 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001123
Pierre Ossman126e5642014-02-13 14:40:25 +01001124 cp.setCursor(server->cursor);
1125
1126 if (!writer()->writeSetCursor()) {
1127 if (!writer()->writeSetXCursor()) {
1128 // No client support
1129 return;
1130 }
1131 }
1132
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001133 writeFramebufferUpdate();
1134}
1135
1136void VNCSConnectionST::setDesktopName(const char *name)
1137{
1138 cp.setName(name);
1139
1140 if (state() != RFBSTATE_NORMAL)
1141 return;
1142
1143 if (!writer()->writeSetDesktopName()) {
1144 fprintf(stderr, "Client does not support desktop rename\n");
1145 return;
1146 }
1147
1148 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001149}
1150
1151void VNCSConnectionST::setSocketTimeouts()
1152{
1153 int timeoutms = rfb::Server::clientWaitTimeMillis;
1154 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1155 if (timeoutms == 0)
1156 timeoutms = -1;
1157 sock->inStream().setTimeout(timeoutms);
1158 sock->outStream().setTimeout(timeoutms);
1159}
1160
1161char* VNCSConnectionST::getStartTime()
1162{
1163 char* result = ctime(&startTime);
1164 result[24] = '\0';
1165 return result;
1166}
1167
1168void VNCSConnectionST::setStatus(int status)
1169{
1170 switch (status) {
1171 case 0:
1172 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1173 break;
1174 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001175 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001176 break;
1177 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001178 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001179 break;
1180 }
1181 framebufferUpdateRequest(server->pb->getRect(), false);
1182}
1183int VNCSConnectionST::getStatus()
1184{
1185 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1186 return 0;
1187 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1188 return 1;
1189 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1190 return 2;
1191 return 4;
1192}
1193