blob: e1424b720c1e306699afd139c2f15fd22424843f [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +01002 * Copyright 2009-2014 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 Ossman2c764942011-11-14 15:54:30 +000069 : SConnection(reverse), sock(s), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000070 pendingSyncFence(false), syncFence(false), fenceFlags(0),
71 fenceDataLen(0), fenceData(NULL),
Pierre Ossman1b478e52011-11-15 12:08:30 +000072 baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
73 ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020074 server(server_), updates(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman1b478e52011-11-15 12:08:30 +000076 continuousUpdates(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000077 updateTimer(this), pointerEventTime(0),
78 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079{
80 setStreams(&sock->inStream(), &sock->outStream());
81 peerEndpoint.buf = sock->getPeerEndpoint();
82 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
83
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010084 memset(encoders, 0, sizeof(encoders));
85
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 // 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 Ossmanfdba3fe2014-01-31 13:12:18 +0100111 for (int i = 0; i <= encodingMax; i++)
112 delete encoders[i];
113
Pierre Ossman2c764942011-11-14 15:54:30 +0000114 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115}
116
117
118// Methods called from VNCServerST
119
120bool VNCSConnectionST::init()
121{
122 try {
123 initialiseProtocol();
124 } catch (rdr::Exception& e) {
125 close(e.str());
126 return false;
127 }
128 return true;
129}
130
131void VNCSConnectionST::close(const char* reason)
132{
133 // Log the reason for the close
134 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000135 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136 else
137 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
138
139 if (authenticated()) {
140 server->lastDisconnectTime = time(0);
141 }
142
143 // Just shutdown the socket and mark our state as closing. Eventually the
144 // calling code will call VNCServerST's removeSocket() method causing us to
145 // be deleted.
146 sock->shutdown();
147 setState(RFBSTATE_CLOSING);
148}
149
150
151void VNCSConnectionST::processMessages()
152{
153 if (state() == RFBSTATE_CLOSING) return;
154 try {
155 // - Now set appropriate socket timeouts and process data
156 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000157
158 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159
Pierre Ossmana830bec2011-11-08 12:12:02 +0000160 // Get the underlying TCP layer to build large packets if we send
161 // multiple small responses.
162 network::TcpSocket::cork(sock->getFd(), true);
163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000165 if (pendingSyncFence) {
166 syncFence = true;
167 pendingSyncFence = false;
168 }
169
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000171
Pierre Ossman2c764942011-11-14 15:54:30 +0000172 if (syncFence) {
173 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
174 syncFence = false;
175 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176 }
177
Pierre Ossmana830bec2011-11-08 12:12:02 +0000178 // Flush out everything in case we go idle after this.
179 network::TcpSocket::cork(sock->getFd(), false);
180
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000181 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000182
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000183 // If there were anything requiring an update, try to send it here.
184 // We wait until now with this to aggregate responses and to give
185 // higher priority to user actions such as keyboard and pointer events.
186 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 } catch (rdr::EndOfStream&) {
188 close("Clean disconnection");
189 } catch (rdr::Exception &e) {
190 close(e.str());
191 }
192}
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194void VNCSConnectionST::pixelBufferChange()
195{
196 try {
197 if (!authenticated()) return;
198 if (cp.width && cp.height && (server->pb->width() != cp.width ||
199 server->pb->height() != cp.height))
200 {
201 // We need to clip the next update to the new size, but also add any
202 // extra bits if it's bigger. If we wanted to do this exactly, something
203 // like the code below would do it, but at the moment we just update the
204 // entire new size. However, we do need to clip the renderedCursorRect
205 // because that might be added to updates in writeFramebufferUpdate().
206
207 //updates.intersect(server->pb->getRect());
208 //
209 //if (server->pb->width() > cp.width)
210 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
211 // server->pb->height()));
212 //if (server->pb->height() > cp.height)
213 // updates.add_changed(Rect(0, cp.height, cp.width,
214 // server->pb->height()));
215
216 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
217
218 cp.width = server->pb->width();
219 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000220 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000222 // We should only send EDS to client asking for both
223 if (!writer()->writeExtendedDesktopSize()) {
224 if (!writer()->writeSetDesktopSize()) {
225 close("Client does not support desktop resize");
226 return;
227 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228 }
229 }
230 }
231 // Just update the whole screen at the moment because we're too lazy to
232 // work out what's actually changed.
233 updates.clear();
234 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000235 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 } catch(rdr::Exception &e) {
237 close(e.str());
238 }
239}
240
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000241void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000242{
243 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000244 writeFramebufferUpdate();
245 } catch(rdr::Exception &e) {
246 close(e.str());
247 }
248}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000249
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000250void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
251{
252 try {
253 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000254 } catch(rdr::Exception &e) {
255 close(e.str());
256 }
257}
258
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000259void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000260{
261 try {
262 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
263 } catch(rdr::Exception& e) {
264 close(e.str());
265 }
266}
267
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000268void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000269{
270 try {
271 if (!(accessRights & AccessCutText)) return;
272 if (!rfb::Server::sendCutText) return;
273 if (state() == RFBSTATE_NORMAL)
274 writer()->writeServerCutText(str, len);
275 } catch(rdr::Exception& e) {
276 close(e.str());
277 }
278}
279
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000280
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000281void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000282{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000283 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000284 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000285 } catch(rdr::Exception& e) {
286 close(e.str());
287 }
288}
289
290
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291void VNCSConnectionST::setCursorOrClose()
292{
293 try {
294 setCursor();
295 } catch(rdr::Exception& e) {
296 close(e.str());
297 }
298}
299
300
301int VNCSConnectionST::checkIdleTimeout()
302{
303 int idleTimeout = rfb::Server::idleTimeout;
304 if (idleTimeout == 0) return 0;
305 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
306 idleTimeout = 15; // minimum of 15 seconds while authenticating
307 time_t now = time(0);
308 if (now < lastEventTime) {
309 // Someone must have set the time backwards. Set lastEventTime so that the
310 // idleTimeout will count from now.
311 vlog.info("Time has gone backwards - resetting idle timeout");
312 lastEventTime = now;
313 }
314 int timeLeft = lastEventTime + idleTimeout - now;
315 if (timeLeft < -60) {
316 // Our callback is over a minute late - someone must have set the time
317 // forwards. Set lastEventTime so that the idleTimeout will count from
318 // now.
319 vlog.info("Time has gone forwards - resetting idle timeout");
320 lastEventTime = now;
321 return secsToMillis(idleTimeout);
322 }
323 if (timeLeft <= 0) {
324 close("Idle timeout");
325 return 0;
326 }
327 return secsToMillis(timeLeft);
328}
329
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000330
331bool VNCSConnectionST::getComparerState()
332{
333 // We interpret a low compression level as an indication that the client
334 // wants to prioritise CPU usage over bandwidth, and hence disable the
335 // comparing update tracker.
336 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
337}
338
339
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000340// renderedCursorChange() is called whenever the server-side rendered cursor
341// changes shape or position. It ensures that the next update will clean up
342// the old rendered cursor and if necessary draw the new rendered cursor.
343
344void VNCSConnectionST::renderedCursorChange()
345{
346 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000347 if (!renderedCursorRect.is_empty())
348 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000349 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000351 writeFramebufferUpdateOrClose();
352 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000353}
354
355// needRenderedCursor() returns true if this client needs the server-side
356// rendered cursor. This may be because it does not support local cursor or
357// because the current cursor position has not been set by this client.
358// Unfortunately we can't know for sure when the current cursor position has
359// been set by this client. We guess that this is the case when the current
360// cursor position is the same as the last pointer event from this client, or
361// if it is a very short time since this client's last pointer event (up to a
362// second). [ Ideally we should do finer-grained timing here and make the time
363// configurable, but I don't think it's that important. ]
364
365bool VNCSConnectionST::needRenderedCursor()
366{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000367 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000368 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000369 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000370}
371
372
373void VNCSConnectionST::approveConnectionOrClose(bool accept,
374 const char* reason)
375{
376 try {
377 approveConnection(accept, reason);
378 } catch (rdr::Exception& e) {
379 close(e.str());
380 }
381}
382
383
384
385// -=- Callbacks from SConnection
386
387void VNCSConnectionST::authSuccess()
388{
389 lastEventTime = time(0);
390
391 server->startDesktop();
392
393 // - Set the connection parameters appropriately
394 cp.width = server->pb->width();
395 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000396 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000397 cp.setName(server->getName());
398
399 // - Set the default pixel format
400 cp.setPF(server->pb->getPF());
401 char buffer[256];
402 cp.pf().print(buffer, 256);
403 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404
405 // - Mark the entire display as "dirty"
406 updates.add_changed(server->pb->getRect());
407 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000408
409 // - Bootstrap the congestion control
410 ackedOffset = sock->outStream().length();
411 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412}
413
414void VNCSConnectionST::queryConnection(const char* userName)
415{
416 // - Authentication succeeded - clear from blacklist
417 CharArray name; name.buf = sock->getPeerAddress();
418 server->blHosts->clearBlackmark(name.buf);
419
420 // - Special case to provide a more useful error message
421 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
422 server->authClientCount() > 0) {
423 approveConnection(false, "The server is already in use");
424 return;
425 }
426
427 // - Does the client have the right to bypass the query?
428 if (reverseConnection ||
429 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
430 (accessRights & AccessNoQuery))
431 {
432 approveConnection(true);
433 return;
434 }
435
436 // - Get the server to display an Accept/Reject dialog, if required
437 // If a dialog is displayed, the result will be PENDING, and the
438 // server will call approveConnection at a later time
439 CharArray reason;
440 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
441 &reason.buf);
442 if (qr == VNCServerST::PENDING)
443 return;
444
445 // - If server returns ACCEPT/REJECT then pass result to SConnection
446 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
447}
448
449void VNCSConnectionST::clientInit(bool shared)
450{
451 lastEventTime = time(0);
452 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
453 if (rfb::Server::neverShared) shared = false;
454 if (!shared) {
455 if (rfb::Server::disconnectClients) {
456 // - 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
592 // Don't bother the desktop with an invalid configuration
593 if (!layout.validate(fb_width, fb_height)) {
594 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
595 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000596 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000597 return;
598 }
599
600 // FIXME: the desktop will call back to VNCServerST and an extra set
601 // of ExtendedDesktopSize messages will be sent. This is okay
602 // protocol-wise, but unnecessary.
603 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
604
Pierre Ossman04e62db2009-03-23 16:57:07 +0000605 writer()->writeExtendedDesktopSize(reasonClient, result,
606 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000607
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000608 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000609 if (result == resultSuccess) {
610 if (server->screenLayout != layout)
611 throw Exception("Desktop configured a different screen layout than requested");
612 server->notifyScreenLayoutChange(this);
613 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000614
615 // but always send back a reply to the requesting client
616 // (do this last as it might throw an exception on socket errors)
617 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000618}
619
Pierre Ossman2c764942011-11-14 15:54:30 +0000620void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
621{
622 if (flags & fenceFlagRequest) {
623 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000624 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000625
626 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
627 fenceDataLen = len;
628 delete [] fenceData;
629 if (len > 0) {
630 fenceData = new char[len];
631 memcpy(fenceData, data, len);
632 }
633
634 return;
635 }
636
637 // We handle everything synchronously so we trivially honor these modes
638 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
639
640 writer()->writeFence(flags, len, data);
641 return;
642 }
643
Pierre Ossman1b478e52011-11-15 12:08:30 +0000644 struct RTTInfo rttInfo;
645
Pierre Ossman2c764942011-11-14 15:54:30 +0000646 switch (len) {
647 case 0:
648 // Initial dummy fence;
649 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000650 case sizeof(struct RTTInfo):
651 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
652 handleRTTPong(rttInfo);
653 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000654 default:
655 vlog.error("Fence response of unexpected size received");
656 }
657}
658
Pierre Ossman1b478e52011-11-15 12:08:30 +0000659void VNCSConnectionST::enableContinuousUpdates(bool enable,
660 int x, int y, int w, int h)
661{
662 Rect rect;
663
664 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
665 throw Exception("Client tried to enable continuous updates when not allowed");
666
667 continuousUpdates = enable;
668
669 rect.setXYWH(x, y, w, h);
670 cuRegion.reset(rect);
671
672 if (enable) {
673 requested.clear();
674 writeFramebufferUpdate();
675 } else {
676 writer()->writeEndOfContinuousUpdates();
677 }
678}
679
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000680// supportsLocalCursor() is called whenever the status of
681// cp.supportsLocalCursor has changed. If the client does now support local
682// cursor, we make sure that the old server-side rendered cursor is cleaned up
683// and the cursor is sent to the client.
684
685void VNCSConnectionST::supportsLocalCursor()
686{
687 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000688 if (!renderedCursorRect.is_empty())
689 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000690 drawRenderedCursor = false;
691 setCursor();
692 }
693}
694
Pierre Ossman2c764942011-11-14 15:54:30 +0000695void VNCSConnectionST::supportsFence()
696{
697 writer()->writeFence(fenceFlagRequest, 0, NULL);
698}
699
Pierre Ossman1b478e52011-11-15 12:08:30 +0000700void VNCSConnectionST::supportsContinuousUpdates()
701{
702 // We refuse to use continuous updates if we cannot monitor the buffer
703 // usage using fences.
704 if (!cp.supportsFence)
705 return;
706
707 writer()->writeEndOfContinuousUpdates();
708}
709
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000710
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000711bool VNCSConnectionST::handleTimeout(Timer* t)
712{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000713 try {
714 if (t == &updateTimer)
715 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000716 else if (t == &congestionTimer)
717 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000718 } catch (rdr::Exception& e) {
719 close(e.str());
720 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000721
722 return false;
723}
724
725
Pierre Ossman1b478e52011-11-15 12:08:30 +0000726void VNCSConnectionST::writeRTTPing()
727{
728 struct RTTInfo rttInfo;
729
730 if (!cp.supportsFence)
731 return;
732
733 memset(&rttInfo, 0, sizeof(struct RTTInfo));
734
735 gettimeofday(&rttInfo.tv, NULL);
736 rttInfo.offset = sock->outStream().length();
737 rttInfo.inFlight = rttInfo.offset - ackedOffset;
738
739 // We need to make sure any old update are already processed by the
740 // time we get the response back. This allows us to reliably throttle
741 // back on client overload, as well as network overload.
742 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
743 sizeof(struct RTTInfo), (const char*)&rttInfo);
744
745 pingCounter++;
746
747 sentOffset = rttInfo.offset;
748
749 // Let some data flow before we adjust the settings
750 if (!congestionTimer.isStarted())
751 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
752}
753
754void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
755{
756 unsigned rtt, delay;
757 int bdp;
758
759 pingCounter--;
760
761 rtt = msSince(&rttInfo.tv);
762 if (rtt < 1)
763 rtt = 1;
764
765 ackedOffset = rttInfo.offset;
766
767 // Try to estimate wire latency by tracking lowest seen latency
768 if (rtt < baseRTT)
769 baseRTT = rtt;
770
771 if (rttInfo.inFlight > congWindow) {
772 seenCongestion = true;
773
774 // Estimate added delay because of overtaxed buffers
775 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
776
777 if (delay < rtt)
778 rtt -= delay;
779 else
780 rtt = 1;
781
782 // If we underestimate the congestion window, then we'll get a latency
783 // that's less than the wire latency, which will confuse other portions
784 // of the code.
785 if (rtt < baseRTT)
786 rtt = baseRTT;
787 }
788
789 // We only keep track of the minimum latency seen (for a given interval)
790 // on the basis that we want to avoid continous buffer issue, but don't
791 // mind (or even approve of) bursts.
792 if (rtt < minRTT)
793 minRTT = rtt;
794}
795
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000796bool VNCSConnectionST::isCongested()
797{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000798 int offset;
799
800 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000801 if (sock->outStream().bufferUsage() > 0)
802 return true;
803
Pierre Ossman1b478e52011-11-15 12:08:30 +0000804 if (!cp.supportsFence)
805 return false;
806
807 // Idle for too long? (and no data on the wire)
808 //
809 // FIXME: This should really just be one baseRTT, but we're getting
810 // problems with triggering the idle timeout on each update.
811 // Maybe we need to use a moving average for the wire latency
812 // instead of baseRTT.
813 if ((sentOffset == ackedOffset) &&
814 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
815
816#ifdef CONGESTION_DEBUG
817 if (congWindow > INITIAL_WINDOW)
818 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
819 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
820#endif
821
822 // Close congestion window and allow a transfer
823 // FIXME: Reset baseRTT like Linux Vegas?
824 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
825
826 return false;
827 }
828
829 offset = sock->outStream().length();
830
831 // FIXME: Should we compensate for non-update data?
832 // (i.e. use sentOffset instead of offset)
833 if ((offset - ackedOffset) < congWindow)
834 return false;
835
836 // If we just have one outstanding "ping", that means the client has
837 // started receiving our update. In order to not regress compared to
838 // before we had congestion avoidance, we allow another update here.
839 // This could further clog up the tubes, but congestion control isn't
840 // really working properly right now anyway as the wire would otherwise
841 // be idle for at least RTT/2.
842 if (pingCounter == 1)
843 return false;
844
845 return true;
846}
847
848
849void VNCSConnectionST::updateCongestion()
850{
851 unsigned diff;
852
853 if (!seenCongestion)
854 return;
855
856 diff = minRTT - baseRTT;
857
858 if (diff > __rfbmin(100, baseRTT)) {
859 // Way too fast
860 congWindow = congWindow * baseRTT / minRTT;
861 } else if (diff > __rfbmin(50, baseRTT/2)) {
862 // Slightly too fast
863 congWindow -= 4096;
864 } else if (diff < 5) {
865 // Way too slow
866 congWindow += 8192;
867 } else if (diff < 25) {
868 // Too slow
869 congWindow += 4096;
870 }
871
872 if (congWindow < MINIMUM_WINDOW)
873 congWindow = MINIMUM_WINDOW;
874 if (congWindow > MAXIMUM_WINDOW)
875 congWindow = MAXIMUM_WINDOW;
876
877#ifdef CONGESTION_DEBUG
878 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
879 minRTT, baseRTT, congWindow / 1024,
880 congWindow * 8.0 / baseRTT / 1000.0);
881
882#ifdef TCP_INFO
883 struct tcp_info tcp_info;
884 socklen_t tcp_info_length;
885
886 tcp_info_length = sizeof(tcp_info);
887 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
888 (void *)&tcp_info, &tcp_info_length) == 0) {
889 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
890 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
891 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
892 }
893#endif
894
895#endif
896
897 minRTT = -1;
898 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000899}
900
901
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000902void VNCSConnectionST::writeFramebufferUpdate()
903{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000904 Region req;
905 UpdateInfo ui;
906 bool needNewUpdateInfo;
907
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000908 updateTimer.stop();
909
Pierre Ossman2c764942011-11-14 15:54:30 +0000910 // We're in the middle of processing a command that's supposed to be
911 // synchronised. Allowing an update to slip out right now might violate
912 // that synchronisation.
913 if (syncFence)
914 return;
915
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000916 // We try to aggregate responses, so don't send out anything whilst we
917 // still have incoming messages. processMessages() will give us another
918 // chance to run once things are idle.
919 if (inProcessMessages)
920 return;
921
Pierre Ossman1b478e52011-11-15 12:08:30 +0000922 if (state() != RFBSTATE_NORMAL)
923 return;
924 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000925 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000926
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000927 // Check that we actually have some space on the link and retry in a
928 // bit if things are congested.
929 if (isCongested()) {
930 updateTimer.start(50);
931 return;
932 }
933
Pierre Ossman36dadf82011-11-15 12:11:32 +0000934 // In continuous mode, we will be outputting at least three distinct
935 // messages. We need to aggregate these in order to not clog up TCP's
936 // congestion window.
937 network::TcpSocket::cork(sock->getFd(), true);
938
Pierre Ossmane9962f72009-04-23 12:31:42 +0000939 // First take care of any updates that cannot contain framebuffer data
940 // changes.
941 if (writer()->needNoDataUpdate()) {
942 writer()->writeNoDataUpdate();
943 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000944 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000945 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000946 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000947
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000948 updates.enable_copyrect(cp.useCopyRect);
949
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000950 // Fetch updates from server object, and see if we are allowed to send
951 // anything right now (the framebuffer might have changed in ways we
952 // haven't yet been informed of).
953 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000954 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000955
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000956 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000957 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000958 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000959
Pierre Ossman1b478e52011-11-15 12:08:30 +0000960 if (continuousUpdates)
961 req = cuRegion.union_(requested);
962 else
963 req = requested;
964
965 updates.getUpdateInfo(&ui, req);
966 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000967
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000968 // If the previous position of the rendered cursor overlaps the source of the
969 // copy, then when the copy happens the corresponding rectangle in the
970 // destination will be wrong, so add it to the changed region.
971
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000972 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
973 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000974 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000975 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000976 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000977 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000978 }
979 }
980
981 // If we need to remove the old rendered cursor, just add the rectangle to
982 // the changed region.
983
984 if (removeRenderedCursor) {
985 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000986 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000987 renderedCursorRect.clear();
988 removeRenderedCursor = false;
989 }
990
991 // Return if there is nothing to send the client.
992
993 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000994 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000995
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000996 // The `updates' object could change, make sure we have valid update info.
997
998 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000999 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001000
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001001 // If the client needs a server-side rendered cursor, work out the cursor
1002 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1003 // with the update region, we need to draw the rendered cursor regardless of
1004 // whether it has changed.
1005
1006 if (needRenderedCursor()) {
1007 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001008 = server->renderedCursor.getEffectiveRect()
1009 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001010
1011 if (renderedCursorRect.is_empty()) {
1012 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001013 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001014 .intersect(renderedCursorRect).is_empty()) {
1015 drawRenderedCursor = true;
1016 }
1017
1018 // We could remove the new cursor rect from updates here. It's not clear
1019 // whether this is worth it. If we do remove it, then we won't draw over
1020 // the same bit of screen twice, but we have the overhead of a more complex
1021 // region.
1022
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001023 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001024 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001025 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001026 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001027 }
1028
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001029 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001030 std::vector<Rect> rects;
1031 std::vector<Rect>::const_iterator i;
1032 int encoding;
1033
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001034 Encoder* encoder;
1035 PixelBuffer* pb;
1036
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001037 // Make sure the encoder has the latest settings
1038 encoding = cp.currentEncoding();
1039
1040 if (!encoders[encoding])
Pierre Ossman668468b2014-01-31 12:37:32 +01001041 encoders[encoding] = Encoder::createEncoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001042
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001043 encoder = encoders[encoding];
1044
1045 encoder->setCompressLevel(cp.compressLevel);
1046 encoder->setQualityLevel(cp.qualityLevel);
1047 encoder->setFineQualityLevel(cp.fineQualityLevel, cp.subsampling);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001048
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001049 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001050 // complicated as compared to the original VNC4.
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001051 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001052 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +00001053
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001054 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001055 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +00001056 if (i->width() && i->height()) {
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001057 int nUpdateRects = encoder->getNumRects(*i);
DRCcd2c5d42011-08-11 11:18:34 +00001058 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
Pierre Ossman65fb4b02012-02-28 11:54:01 +00001059 // With Tight encoding and LastRect support, the client does not
1060 // care about the number of rectangles in the update - it will
1061 // stop parsing when it encounters a LastRect "rectangle".
1062 // In this case, pretend to send 65535 rectangles.
DRCcd2c5d42011-08-11 11:18:34 +00001063 nRects = 0xFFFF; break;
1064 }
1065 else
1066 nRects += nUpdateRects;
1067 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001068 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001069
1070 writeRTTPing();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001071
1072 writer()->writeFramebufferUpdateStart(nRects);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001073
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001074 ui.copied.get_rects(&rects);
1075 for (i = rects.begin(); i != rects.end(); i++)
1076 writer()->writeCopyRect(*i, i->tl.x - ui.copy_delta.x,
1077 i->tl.y - ui.copy_delta.y);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001078
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001079 pb = server->getPixelBuffer();
1080
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001081 ui.changed.get_rects(&rects);
1082 for (i = rects.begin(); i != rects.end(); i++)
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001083 encoder->writeRect(*i, pb);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001084
1085 if (drawRenderedCursor) {
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +02001086 renderedCursorRect = server->renderedCursor.getEffectiveRect();
1087 encoder->writeRect(renderedCursorRect, &server->renderedCursor);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001088
1089 drawRenderedCursor = false;
1090 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001091
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001092 writer()->writeFramebufferUpdateEnd();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001093
1094 writeRTTPing();
1095
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001096 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001097 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001098 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001099
1100out:
1101 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001102}
1103
1104
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001105void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1106{
1107 if (!authenticated())
1108 return;
1109
1110 cp.screenLayout = server->screenLayout;
1111
1112 if (state() != RFBSTATE_NORMAL)
1113 return;
1114
1115 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1116 cp.screenLayout);
1117 writeFramebufferUpdate();
1118}
1119
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001120
1121// setCursor() is called whenever the cursor has changed shape or pixel format.
1122// If the client supports local cursor then it will arrange for the cursor to
1123// be sent to the client.
1124
1125void VNCSConnectionST::setCursor()
1126{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001127 if (state() != RFBSTATE_NORMAL)
1128 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001129
Pierre Ossman126e5642014-02-13 14:40:25 +01001130 cp.setCursor(server->cursor);
1131
1132 if (!writer()->writeSetCursor()) {
1133 if (!writer()->writeSetXCursor()) {
1134 // No client support
1135 return;
1136 }
1137 }
1138
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001139 writeFramebufferUpdate();
1140}
1141
1142void VNCSConnectionST::setDesktopName(const char *name)
1143{
1144 cp.setName(name);
1145
1146 if (state() != RFBSTATE_NORMAL)
1147 return;
1148
1149 if (!writer()->writeSetDesktopName()) {
1150 fprintf(stderr, "Client does not support desktop rename\n");
1151 return;
1152 }
1153
1154 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001155}
1156
1157void VNCSConnectionST::setSocketTimeouts()
1158{
1159 int timeoutms = rfb::Server::clientWaitTimeMillis;
1160 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1161 if (timeoutms == 0)
1162 timeoutms = -1;
1163 sock->inStream().setTimeout(timeoutms);
1164 sock->outStream().setTimeout(timeoutms);
1165}
1166
1167char* VNCSConnectionST::getStartTime()
1168{
1169 char* result = ctime(&startTime);
1170 result[24] = '\0';
1171 return result;
1172}
1173
1174void VNCSConnectionST::setStatus(int status)
1175{
1176 switch (status) {
1177 case 0:
1178 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1179 break;
1180 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001181 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001182 break;
1183 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001184 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001185 break;
1186 }
1187 framebufferUpdateRequest(server->pb->getRect(), false);
1188}
1189int VNCSConnectionST::getStatus()
1190{
1191 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1192 return 0;
1193 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1194 return 1;
1195 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1196 return 2;
1197 return 4;
1198}
1199