blob: f45ee7e0ecd1259cae2a062738a9169e4e75ebbc [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;
103 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
104 server->desktop->keyEvent(*i, false);
105 if (server->pointerClient == this)
106 server->pointerClient = 0;
107
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108 // Remove this client from the server
109 server->clients.remove(this);
110
Pierre Ossman2c764942011-11-14 15:54:30 +0000111 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112}
113
114
115// Methods called from VNCServerST
116
117bool VNCSConnectionST::init()
118{
119 try {
120 initialiseProtocol();
121 } catch (rdr::Exception& e) {
122 close(e.str());
123 return false;
124 }
125 return true;
126}
127
128void VNCSConnectionST::close(const char* reason)
129{
130 // Log the reason for the close
131 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000132 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 else
134 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
135
136 if (authenticated()) {
137 server->lastDisconnectTime = time(0);
138 }
139
140 // Just shutdown the socket and mark our state as closing. Eventually the
141 // calling code will call VNCServerST's removeSocket() method causing us to
142 // be deleted.
143 sock->shutdown();
144 setState(RFBSTATE_CLOSING);
145}
146
147
148void VNCSConnectionST::processMessages()
149{
150 if (state() == RFBSTATE_CLOSING) return;
151 try {
152 // - Now set appropriate socket timeouts and process data
153 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000154
155 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156
Pierre Ossmana830bec2011-11-08 12:12:02 +0000157 // Get the underlying TCP layer to build large packets if we send
158 // multiple small responses.
159 network::TcpSocket::cork(sock->getFd(), true);
160
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000162 if (pendingSyncFence) {
163 syncFence = true;
164 pendingSyncFence = false;
165 }
166
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000167 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000168
Pierre Ossman2c764942011-11-14 15:54:30 +0000169 if (syncFence) {
170 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
171 syncFence = false;
172 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173 }
174
Pierre Ossmana830bec2011-11-08 12:12:02 +0000175 // Flush out everything in case we go idle after this.
176 network::TcpSocket::cork(sock->getFd(), false);
177
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000178 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000179
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000180 // If there were anything requiring an update, try to send it here.
181 // We wait until now with this to aggregate responses and to give
182 // higher priority to user actions such as keyboard and pointer events.
183 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 } catch (rdr::EndOfStream&) {
185 close("Clean disconnection");
186 } catch (rdr::Exception &e) {
187 close(e.str());
188 }
189}
190
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191void VNCSConnectionST::pixelBufferChange()
192{
193 try {
194 if (!authenticated()) return;
195 if (cp.width && cp.height && (server->pb->width() != cp.width ||
196 server->pb->height() != cp.height))
197 {
198 // We need to clip the next update to the new size, but also add any
199 // extra bits if it's bigger. If we wanted to do this exactly, something
200 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200201 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 // because that might be added to updates in writeFramebufferUpdate().
203
204 //updates.intersect(server->pb->getRect());
205 //
206 //if (server->pb->width() > cp.width)
207 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
208 // server->pb->height()));
209 //if (server->pb->height() > cp.height)
210 // updates.add_changed(Rect(0, cp.height, cp.width,
211 // server->pb->height()));
212
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200213 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214
215 cp.width = server->pb->width();
216 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000217 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000219 // We should only send EDS to client asking for both
220 if (!writer()->writeExtendedDesktopSize()) {
221 if (!writer()->writeSetDesktopSize()) {
222 close("Client does not support desktop resize");
223 return;
224 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225 }
226 }
227 }
228 // Just update the whole screen at the moment because we're too lazy to
229 // work out what's actually changed.
230 updates.clear();
231 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000232 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233 } catch(rdr::Exception &e) {
234 close(e.str());
235 }
236}
237
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000238void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000239{
240 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000241 writeFramebufferUpdate();
242 } catch(rdr::Exception &e) {
243 close(e.str());
244 }
245}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000246
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000247void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
248{
249 try {
250 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000251 } catch(rdr::Exception &e) {
252 close(e.str());
253 }
254}
255
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000256void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000257{
258 try {
259 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
260 } catch(rdr::Exception& e) {
261 close(e.str());
262 }
263}
264
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000265void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000266{
267 try {
268 if (!(accessRights & AccessCutText)) return;
269 if (!rfb::Server::sendCutText) return;
270 if (state() == RFBSTATE_NORMAL)
271 writer()->writeServerCutText(str, len);
272 } catch(rdr::Exception& e) {
273 close(e.str());
274 }
275}
276
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000277
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000278void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000279{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000280 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000281 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000282 } catch(rdr::Exception& e) {
283 close(e.str());
284 }
285}
286
287
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000288void VNCSConnectionST::setCursorOrClose()
289{
290 try {
291 setCursor();
292 } catch(rdr::Exception& e) {
293 close(e.str());
294 }
295}
296
297
298int VNCSConnectionST::checkIdleTimeout()
299{
300 int idleTimeout = rfb::Server::idleTimeout;
301 if (idleTimeout == 0) return 0;
302 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
303 idleTimeout = 15; // minimum of 15 seconds while authenticating
304 time_t now = time(0);
305 if (now < lastEventTime) {
306 // Someone must have set the time backwards. Set lastEventTime so that the
307 // idleTimeout will count from now.
308 vlog.info("Time has gone backwards - resetting idle timeout");
309 lastEventTime = now;
310 }
311 int timeLeft = lastEventTime + idleTimeout - now;
312 if (timeLeft < -60) {
313 // Our callback is over a minute late - someone must have set the time
314 // forwards. Set lastEventTime so that the idleTimeout will count from
315 // now.
316 vlog.info("Time has gone forwards - resetting idle timeout");
317 lastEventTime = now;
318 return secsToMillis(idleTimeout);
319 }
320 if (timeLeft <= 0) {
321 close("Idle timeout");
322 return 0;
323 }
324 return secsToMillis(timeLeft);
325}
326
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000327
328bool VNCSConnectionST::getComparerState()
329{
330 // We interpret a low compression level as an indication that the client
331 // wants to prioritise CPU usage over bandwidth, and hence disable the
332 // comparing update tracker.
333 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
334}
335
336
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000337// renderedCursorChange() is called whenever the server-side rendered cursor
338// changes shape or position. It ensures that the next update will clean up
339// the old rendered cursor and if necessary draw the new rendered cursor.
340
341void VNCSConnectionST::renderedCursorChange()
342{
343 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200344 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000345 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000346 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200347 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000348 writeFramebufferUpdateOrClose();
349 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350}
351
352// needRenderedCursor() returns true if this client needs the server-side
353// rendered cursor. This may be because it does not support local cursor or
354// because the current cursor position has not been set by this client.
355// Unfortunately we can't know for sure when the current cursor position has
356// been set by this client. We guess that this is the case when the current
357// cursor position is the same as the last pointer event from this client, or
358// if it is a very short time since this client's last pointer event (up to a
359// second). [ Ideally we should do finer-grained timing here and make the time
360// configurable, but I don't think it's that important. ]
361
362bool VNCSConnectionST::needRenderedCursor()
363{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000364 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000365 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000366 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000367}
368
369
370void VNCSConnectionST::approveConnectionOrClose(bool accept,
371 const char* reason)
372{
373 try {
374 approveConnection(accept, reason);
375 } catch (rdr::Exception& e) {
376 close(e.str());
377 }
378}
379
380
381
382// -=- Callbacks from SConnection
383
384void VNCSConnectionST::authSuccess()
385{
386 lastEventTime = time(0);
387
388 server->startDesktop();
389
390 // - Set the connection parameters appropriately
391 cp.width = server->pb->width();
392 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000393 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000394 cp.setName(server->getName());
395
396 // - Set the default pixel format
397 cp.setPF(server->pb->getPF());
398 char buffer[256];
399 cp.pf().print(buffer, 256);
400 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000401
402 // - Mark the entire display as "dirty"
403 updates.add_changed(server->pb->getRect());
404 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000405
406 // - Bootstrap the congestion control
407 ackedOffset = sock->outStream().length();
408 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000409}
410
411void VNCSConnectionST::queryConnection(const char* userName)
412{
413 // - Authentication succeeded - clear from blacklist
414 CharArray name; name.buf = sock->getPeerAddress();
415 server->blHosts->clearBlackmark(name.buf);
416
417 // - Special case to provide a more useful error message
418 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
419 server->authClientCount() > 0) {
420 approveConnection(false, "The server is already in use");
421 return;
422 }
423
424 // - Does the client have the right to bypass the query?
425 if (reverseConnection ||
426 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
427 (accessRights & AccessNoQuery))
428 {
429 approveConnection(true);
430 return;
431 }
432
433 // - Get the server to display an Accept/Reject dialog, if required
434 // If a dialog is displayed, the result will be PENDING, and the
435 // server will call approveConnection at a later time
436 CharArray reason;
437 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
438 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100439 if (qr == VNCServerST::PENDING) {
440 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000441 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100442 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000443
444 // - If server returns ACCEPT/REJECT then pass result to SConnection
445 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
446}
447
448void VNCSConnectionST::clientInit(bool shared)
449{
450 lastEventTime = time(0);
451 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100452 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000453 if (rfb::Server::neverShared) shared = false;
454 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100455 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000456 // - Close all the other connected clients
457 vlog.debug("non-shared connection - closing clients");
458 server->closeClients("Non-shared connection requested", getSock());
459 } else {
460 // - Refuse this connection if there are existing clients, in addition to
461 // this one
462 if (server->authClientCount() > 1) {
463 close("Server is already in use");
464 return;
465 }
466 }
467 }
468 SConnection::clientInit(shared);
469}
470
471void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
472{
473 SConnection::setPixelFormat(pf);
474 char buffer[256];
475 pf.print(buffer, 256);
476 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000477 setCursor();
478}
479
480void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
481{
482 pointerEventTime = lastEventTime = time(0);
483 server->lastUserInputTime = lastEventTime;
484 if (!(accessRights & AccessPtrEvents)) return;
485 if (!rfb::Server::acceptPointerEvents) return;
486 if (!server->pointerClient || server->pointerClient == this) {
487 pointerEventPos = pos;
488 if (buttonMask)
489 server->pointerClient = this;
490 else
491 server->pointerClient = 0;
492 server->desktop->pointerEvent(pointerEventPos, buttonMask);
493 }
494}
495
496
497class VNCSConnectionSTShiftPresser {
498public:
499 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
500 : desktop(desktop_), pressed(false) {}
501 ~VNCSConnectionSTShiftPresser() {
502 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
503 }
504 void press() {
505 desktop->keyEvent(XK_Shift_L, true);
506 pressed = true;
507 }
508 SDesktop* desktop;
509 bool pressed;
510};
511
512// keyEvent() - record in the pressedKeys which keys were pressed. Allow
513// multiple down events (for autorepeat), but only allow a single up event.
514void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
515 lastEventTime = time(0);
516 server->lastUserInputTime = lastEventTime;
517 if (!(accessRights & AccessKeyEvents)) return;
518 if (!rfb::Server::acceptKeyEvents) return;
519
520 // Remap the key if required
521 if (server->keyRemapper)
522 key = server->keyRemapper->remapKey(key);
523
524 // Turn ISO_Left_Tab into shifted Tab.
525 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
526 if (key == XK_ISO_Left_Tab) {
527 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
528 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
529 shiftPresser.press();
530 key = XK_Tab;
531 }
532
533 if (down) {
534 pressedKeys.insert(key);
535 } else {
536 if (!pressedKeys.erase(key)) return;
537 }
538 server->desktop->keyEvent(key, down);
539}
540
541void VNCSConnectionST::clientCutText(const char* str, int len)
542{
543 if (!(accessRights & AccessCutText)) return;
544 if (!rfb::Server::acceptCutText) return;
545 server->desktop->clientCutText(str, len);
546}
547
548void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
549{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000550 Rect safeRect;
551
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000552 if (!(accessRights & AccessView)) return;
553
554 SConnection::framebufferUpdateRequest(r, incremental);
555
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000556 // Check that the client isn't sending crappy requests
557 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
558 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
559 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000560 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
561 } else {
562 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000563 }
564
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000565 // Just update the requested region.
566 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000567 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000568 if (!incremental || !continuousUpdates)
569 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000570
571 if (!incremental) {
572 // Non-incremental update - treat as if area requested has changed
573 updates.add_changed(reqRgn);
574 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000575
576 // And send the screen layout to the client (which, unlike the
577 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000578 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000579
580 // We do not send a DesktopSize since it only contains the
581 // framebuffer size (which the client already should know) and
582 // because some clients don't handle extra DesktopSize events
583 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000584 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000585}
586
Pierre Ossman34bb0612009-03-21 21:16:14 +0000587void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
588 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000589{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000590 unsigned int result;
591
Michal Srbb318b8f2014-11-24 13:18:28 +0200592 if (!(accessRights & AccessSetDesktopSize)) return;
593 if (!rfb::Server::acceptSetDesktopSize) return;
594
Pierre Ossman04e62db2009-03-23 16:57:07 +0000595 // Don't bother the desktop with an invalid configuration
596 if (!layout.validate(fb_width, fb_height)) {
597 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
598 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000599 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000600 return;
601 }
602
603 // FIXME: the desktop will call back to VNCServerST and an extra set
604 // of ExtendedDesktopSize messages will be sent. This is okay
605 // protocol-wise, but unnecessary.
606 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
607
Pierre Ossman04e62db2009-03-23 16:57:07 +0000608 writer()->writeExtendedDesktopSize(reasonClient, result,
609 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000610
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000611 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000612 if (result == resultSuccess) {
613 if (server->screenLayout != layout)
614 throw Exception("Desktop configured a different screen layout than requested");
615 server->notifyScreenLayoutChange(this);
616 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000617
618 // but always send back a reply to the requesting client
619 // (do this last as it might throw an exception on socket errors)
620 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000621}
622
Pierre Ossman2c764942011-11-14 15:54:30 +0000623void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
624{
625 if (flags & fenceFlagRequest) {
626 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000627 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000628
629 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
630 fenceDataLen = len;
631 delete [] fenceData;
632 if (len > 0) {
633 fenceData = new char[len];
634 memcpy(fenceData, data, len);
635 }
636
637 return;
638 }
639
640 // We handle everything synchronously so we trivially honor these modes
641 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
642
643 writer()->writeFence(flags, len, data);
644 return;
645 }
646
Pierre Ossman1b478e52011-11-15 12:08:30 +0000647 struct RTTInfo rttInfo;
648
Pierre Ossman2c764942011-11-14 15:54:30 +0000649 switch (len) {
650 case 0:
651 // Initial dummy fence;
652 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000653 case sizeof(struct RTTInfo):
654 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
655 handleRTTPong(rttInfo);
656 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000657 default:
658 vlog.error("Fence response of unexpected size received");
659 }
660}
661
Pierre Ossman1b478e52011-11-15 12:08:30 +0000662void VNCSConnectionST::enableContinuousUpdates(bool enable,
663 int x, int y, int w, int h)
664{
665 Rect rect;
666
667 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
668 throw Exception("Client tried to enable continuous updates when not allowed");
669
670 continuousUpdates = enable;
671
672 rect.setXYWH(x, y, w, h);
673 cuRegion.reset(rect);
674
675 if (enable) {
676 requested.clear();
677 writeFramebufferUpdate();
678 } else {
679 writer()->writeEndOfContinuousUpdates();
680 }
681}
682
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000683// supportsLocalCursor() is called whenever the status of
684// cp.supportsLocalCursor has changed. If the client does now support local
685// cursor, we make sure that the old server-side rendered cursor is cleaned up
686// and the cursor is sent to the client.
687
688void VNCSConnectionST::supportsLocalCursor()
689{
690 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200691 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000692 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000693 setCursor();
694 }
695}
696
Pierre Ossman2c764942011-11-14 15:54:30 +0000697void VNCSConnectionST::supportsFence()
698{
699 writer()->writeFence(fenceFlagRequest, 0, NULL);
700}
701
Pierre Ossman1b478e52011-11-15 12:08:30 +0000702void VNCSConnectionST::supportsContinuousUpdates()
703{
704 // We refuse to use continuous updates if we cannot monitor the buffer
705 // usage using fences.
706 if (!cp.supportsFence)
707 return;
708
709 writer()->writeEndOfContinuousUpdates();
710}
711
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000712
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000713bool VNCSConnectionST::handleTimeout(Timer* t)
714{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000715 try {
716 if (t == &updateTimer)
717 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000718 else if (t == &congestionTimer)
719 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100720 else if (t == &queryConnectTimer) {
721 if (state() == RFBSTATE_QUERYING)
722 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
723 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000724 } catch (rdr::Exception& e) {
725 close(e.str());
726 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000727
728 return false;
729}
730
731
Pierre Ossman1b478e52011-11-15 12:08:30 +0000732void VNCSConnectionST::writeRTTPing()
733{
734 struct RTTInfo rttInfo;
735
736 if (!cp.supportsFence)
737 return;
738
739 memset(&rttInfo, 0, sizeof(struct RTTInfo));
740
741 gettimeofday(&rttInfo.tv, NULL);
742 rttInfo.offset = sock->outStream().length();
743 rttInfo.inFlight = rttInfo.offset - ackedOffset;
744
745 // We need to make sure any old update are already processed by the
746 // time we get the response back. This allows us to reliably throttle
747 // back on client overload, as well as network overload.
748 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
749 sizeof(struct RTTInfo), (const char*)&rttInfo);
750
751 pingCounter++;
752
753 sentOffset = rttInfo.offset;
754
755 // Let some data flow before we adjust the settings
756 if (!congestionTimer.isStarted())
757 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
758}
759
760void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
761{
762 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000763
764 pingCounter--;
765
766 rtt = msSince(&rttInfo.tv);
767 if (rtt < 1)
768 rtt = 1;
769
770 ackedOffset = rttInfo.offset;
771
772 // Try to estimate wire latency by tracking lowest seen latency
773 if (rtt < baseRTT)
774 baseRTT = rtt;
775
776 if (rttInfo.inFlight > congWindow) {
777 seenCongestion = true;
778
779 // Estimate added delay because of overtaxed buffers
780 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
781
782 if (delay < rtt)
783 rtt -= delay;
784 else
785 rtt = 1;
786
787 // If we underestimate the congestion window, then we'll get a latency
788 // that's less than the wire latency, which will confuse other portions
789 // of the code.
790 if (rtt < baseRTT)
791 rtt = baseRTT;
792 }
793
794 // We only keep track of the minimum latency seen (for a given interval)
795 // on the basis that we want to avoid continous buffer issue, but don't
796 // mind (or even approve of) bursts.
797 if (rtt < minRTT)
798 minRTT = rtt;
799}
800
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000801bool VNCSConnectionST::isCongested()
802{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000803 int offset;
804
805 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000806 if (sock->outStream().bufferUsage() > 0)
807 return true;
808
Pierre Ossman1b478e52011-11-15 12:08:30 +0000809 if (!cp.supportsFence)
810 return false;
811
812 // Idle for too long? (and no data on the wire)
813 //
814 // FIXME: This should really just be one baseRTT, but we're getting
815 // problems with triggering the idle timeout on each update.
816 // Maybe we need to use a moving average for the wire latency
817 // instead of baseRTT.
818 if ((sentOffset == ackedOffset) &&
819 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
820
821#ifdef CONGESTION_DEBUG
822 if (congWindow > INITIAL_WINDOW)
823 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
824 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
825#endif
826
827 // Close congestion window and allow a transfer
828 // FIXME: Reset baseRTT like Linux Vegas?
829 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
830
831 return false;
832 }
833
834 offset = sock->outStream().length();
835
836 // FIXME: Should we compensate for non-update data?
837 // (i.e. use sentOffset instead of offset)
838 if ((offset - ackedOffset) < congWindow)
839 return false;
840
841 // If we just have one outstanding "ping", that means the client has
842 // started receiving our update. In order to not regress compared to
843 // before we had congestion avoidance, we allow another update here.
844 // This could further clog up the tubes, but congestion control isn't
845 // really working properly right now anyway as the wire would otherwise
846 // be idle for at least RTT/2.
847 if (pingCounter == 1)
848 return false;
849
850 return true;
851}
852
853
854void VNCSConnectionST::updateCongestion()
855{
856 unsigned diff;
857
858 if (!seenCongestion)
859 return;
860
861 diff = minRTT - baseRTT;
862
863 if (diff > __rfbmin(100, baseRTT)) {
864 // Way too fast
865 congWindow = congWindow * baseRTT / minRTT;
866 } else if (diff > __rfbmin(50, baseRTT/2)) {
867 // Slightly too fast
868 congWindow -= 4096;
869 } else if (diff < 5) {
870 // Way too slow
871 congWindow += 8192;
872 } else if (diff < 25) {
873 // Too slow
874 congWindow += 4096;
875 }
876
877 if (congWindow < MINIMUM_WINDOW)
878 congWindow = MINIMUM_WINDOW;
879 if (congWindow > MAXIMUM_WINDOW)
880 congWindow = MAXIMUM_WINDOW;
881
882#ifdef CONGESTION_DEBUG
883 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
884 minRTT, baseRTT, congWindow / 1024,
885 congWindow * 8.0 / baseRTT / 1000.0);
886
887#ifdef TCP_INFO
888 struct tcp_info tcp_info;
889 socklen_t tcp_info_length;
890
891 tcp_info_length = sizeof(tcp_info);
892 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
893 (void *)&tcp_info, &tcp_info_length) == 0) {
894 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
895 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
896 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
897 }
898#endif
899
900#endif
901
902 minRTT = -1;
903 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000904}
905
906
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000907void VNCSConnectionST::writeFramebufferUpdate()
908{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000909 Region req;
910 UpdateInfo ui;
911 bool needNewUpdateInfo;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200912 bool drawRenderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000913
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000914 updateTimer.stop();
915
Pierre Ossman2c764942011-11-14 15:54:30 +0000916 // We're in the middle of processing a command that's supposed to be
917 // synchronised. Allowing an update to slip out right now might violate
918 // that synchronisation.
919 if (syncFence)
920 return;
921
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000922 // We try to aggregate responses, so don't send out anything whilst we
923 // still have incoming messages. processMessages() will give us another
924 // chance to run once things are idle.
925 if (inProcessMessages)
926 return;
927
Pierre Ossman1b478e52011-11-15 12:08:30 +0000928 if (state() != RFBSTATE_NORMAL)
929 return;
930 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000931 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000932
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000933 // Check that we actually have some space on the link and retry in a
934 // bit if things are congested.
935 if (isCongested()) {
936 updateTimer.start(50);
937 return;
938 }
939
Pierre Ossman36dadf82011-11-15 12:11:32 +0000940 // In continuous mode, we will be outputting at least three distinct
941 // messages. We need to aggregate these in order to not clog up TCP's
942 // congestion window.
943 network::TcpSocket::cork(sock->getFd(), true);
944
Pierre Ossmane9962f72009-04-23 12:31:42 +0000945 // First take care of any updates that cannot contain framebuffer data
946 // changes.
947 if (writer()->needNoDataUpdate()) {
948 writer()->writeNoDataUpdate();
949 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000950 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000951 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000952 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000953
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000954 updates.enable_copyrect(cp.useCopyRect);
955
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000956 // Fetch updates from server object, and see if we are allowed to send
957 // anything right now (the framebuffer might have changed in ways we
958 // haven't yet been informed of).
959 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000960 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000961
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000962 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000963 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000964 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000965
Pierre Ossman1b478e52011-11-15 12:08:30 +0000966 if (continuousUpdates)
967 req = cuRegion.union_(requested);
968 else
969 req = requested;
970
971 updates.getUpdateInfo(&ui, req);
972 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000973
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000974 // If the previous position of the rendered cursor overlaps the source of the
975 // copy, then when the copy happens the corresponding rectangle in the
976 // destination will be wrong, so add it to the changed region.
977
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200978 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
979 Region bogusCopiedCursor;
980
981 bogusCopiedCursor.copyFrom(damagedCursorRegion);
982 bogusCopiedCursor.translate(ui.copy_delta);
983 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000984 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000985 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000986 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000987 }
988 }
989
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200990 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000991 // the changed region.
992
993 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200994 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000995 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200996 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000997 removeRenderedCursor = false;
998 }
999
1000 // Return if there is nothing to send the client.
1001
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001002 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001003 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001004
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001005 // The `updates' object could change, make sure we have valid update info.
1006
1007 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001008 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001009
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001010 // If the client needs a server-side rendered cursor, work out the cursor
1011 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1012 // with the update region, we need to draw the rendered cursor regardless of
1013 // whether it has changed.
1014
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001015 drawRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001016 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001017 Rect renderedCursorRect;
1018
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001019 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001020 = server->renderedCursor.getEffectiveRect()
1021 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001022
1023 if (renderedCursorRect.is_empty()) {
1024 drawRenderedCursor = false;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001025 } else if (updateRenderedCursor) {
1026 drawRenderedCursor = true;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001027 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001028 .intersect(renderedCursorRect).is_empty()) {
1029 drawRenderedCursor = true;
1030 }
1031
1032 // We could remove the new cursor rect from updates here. It's not clear
1033 // whether this is worth it. If we do remove it, then we won't draw over
1034 // the same bit of screen twice, but we have the overhead of a more complex
1035 // region.
1036
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001037 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001038 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001039 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001040 //}
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001041
1042 damagedCursorRegion.assign_union(renderedCursorRect);
1043 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001044 }
1045
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001046 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanc0397262014-03-14 15:59:46 +01001047 RenderedCursor *cursor;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001048
Pierre Ossmanc0397262014-03-14 15:59:46 +01001049 cursor = NULL;
1050 if (drawRenderedCursor)
1051 cursor = &server->renderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +00001052
1053 writeRTTPing();
1054
Pierre Ossmanc0397262014-03-14 15:59:46 +01001055 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
1056
1057 writeRTTPing();
1058
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001059 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001060 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001061 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001062
1063out:
1064 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001065}
1066
1067
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001068void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1069{
1070 if (!authenticated())
1071 return;
1072
1073 cp.screenLayout = server->screenLayout;
1074
1075 if (state() != RFBSTATE_NORMAL)
1076 return;
1077
1078 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1079 cp.screenLayout);
1080 writeFramebufferUpdate();
1081}
1082
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001083
1084// setCursor() is called whenever the cursor has changed shape or pixel format.
1085// If the client supports local cursor then it will arrange for the cursor to
1086// be sent to the client.
1087
1088void VNCSConnectionST::setCursor()
1089{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001090 if (state() != RFBSTATE_NORMAL)
1091 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001092
Pierre Ossman126e5642014-02-13 14:40:25 +01001093 cp.setCursor(server->cursor);
1094
1095 if (!writer()->writeSetCursor()) {
1096 if (!writer()->writeSetXCursor()) {
1097 // No client support
1098 return;
1099 }
1100 }
1101
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001102 writeFramebufferUpdate();
1103}
1104
1105void VNCSConnectionST::setDesktopName(const char *name)
1106{
1107 cp.setName(name);
1108
1109 if (state() != RFBSTATE_NORMAL)
1110 return;
1111
1112 if (!writer()->writeSetDesktopName()) {
1113 fprintf(stderr, "Client does not support desktop rename\n");
1114 return;
1115 }
1116
1117 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001118}
1119
1120void VNCSConnectionST::setSocketTimeouts()
1121{
1122 int timeoutms = rfb::Server::clientWaitTimeMillis;
1123 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1124 if (timeoutms == 0)
1125 timeoutms = -1;
1126 sock->inStream().setTimeout(timeoutms);
1127 sock->outStream().setTimeout(timeoutms);
1128}
1129
1130char* VNCSConnectionST::getStartTime()
1131{
1132 char* result = ctime(&startTime);
1133 result[24] = '\0';
1134 return result;
1135}
1136
1137void VNCSConnectionST::setStatus(int status)
1138{
1139 switch (status) {
1140 case 0:
1141 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1142 break;
1143 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001144 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001145 break;
1146 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001147 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001148 break;
1149 }
1150 framebufferUpdateRequest(server->pb->getRect(), false);
1151}
1152int VNCSConnectionST::getStatus()
1153{
1154 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1155 return 0;
1156 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1157 return 1;
1158 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1159 return 2;
1160 return 4;
1161}
1162