blob: f8674deae20d02a9288911d87dc5fb368f58bc69 [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 Ossman2c764942011-11-14 15:54:30 +000074 server(server_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 updates(false), image_getter(server->useEconomicTranslate),
76 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman1b478e52011-11-15 12:08:30 +000077 continuousUpdates(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000078 updateTimer(this), pointerEventTime(0),
79 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080{
81 setStreams(&sock->inStream(), &sock->outStream());
82 peerEndpoint.buf = sock->getPeerEndpoint();
83 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
84
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010085 memset(encoders, 0, sizeof(encoders));
86
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 // Configure the socket
88 setSocketTimeouts();
89 lastEventTime = time(0);
90
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 server->clients.push_front(this);
92}
93
94
95VNCSConnectionST::~VNCSConnectionST()
96{
97 // If we reach here then VNCServerST is deleting us!
98 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
99 peerEndpoint.buf,
100 (closeReason.buf) ? closeReason.buf : "");
101
102 // Release any keys the client still had pressed
103 std::set<rdr::U32>::iterator i;
104 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
105 server->desktop->keyEvent(*i, false);
106 if (server->pointerClient == this)
107 server->pointerClient = 0;
108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 // Remove this client from the server
110 server->clients.remove(this);
111
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100112 for (int i = 0; i <= encodingMax; i++)
113 delete encoders[i];
114
Pierre Ossman2c764942011-11-14 15:54:30 +0000115 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116}
117
118
119// Methods called from VNCServerST
120
121bool VNCSConnectionST::init()
122{
123 try {
124 initialiseProtocol();
125 } catch (rdr::Exception& e) {
126 close(e.str());
127 return false;
128 }
129 return true;
130}
131
132void VNCSConnectionST::close(const char* reason)
133{
134 // Log the reason for the close
135 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000136 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 else
138 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
139
140 if (authenticated()) {
141 server->lastDisconnectTime = time(0);
142 }
143
144 // Just shutdown the socket and mark our state as closing. Eventually the
145 // calling code will call VNCServerST's removeSocket() method causing us to
146 // be deleted.
147 sock->shutdown();
148 setState(RFBSTATE_CLOSING);
149}
150
151
152void VNCSConnectionST::processMessages()
153{
154 if (state() == RFBSTATE_CLOSING) return;
155 try {
156 // - Now set appropriate socket timeouts and process data
157 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000158
159 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160
Pierre Ossmana830bec2011-11-08 12:12:02 +0000161 // Get the underlying TCP layer to build large packets if we send
162 // multiple small responses.
163 network::TcpSocket::cork(sock->getFd(), true);
164
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000166 if (pendingSyncFence) {
167 syncFence = true;
168 pendingSyncFence = false;
169 }
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000172
Pierre Ossman2c764942011-11-14 15:54:30 +0000173 if (syncFence) {
174 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
175 syncFence = false;
176 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 }
178
Pierre Ossmana830bec2011-11-08 12:12:02 +0000179 // Flush out everything in case we go idle after this.
180 network::TcpSocket::cork(sock->getFd(), false);
181
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000182 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000183
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000184 // If there were anything requiring an update, try to send it here.
185 // We wait until now with this to aggregate responses and to give
186 // higher priority to user actions such as keyboard and pointer events.
187 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 } catch (rdr::EndOfStream&) {
189 close("Clean disconnection");
190 } catch (rdr::Exception &e) {
191 close(e.str());
192 }
193}
194
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195void VNCSConnectionST::pixelBufferChange()
196{
197 try {
198 if (!authenticated()) return;
199 if (cp.width && cp.height && (server->pb->width() != cp.width ||
200 server->pb->height() != cp.height))
201 {
202 // We need to clip the next update to the new size, but also add any
203 // extra bits if it's bigger. If we wanted to do this exactly, something
204 // like the code below would do it, but at the moment we just update the
205 // entire new size. However, we do need to clip the renderedCursorRect
206 // because that might be added to updates in writeFramebufferUpdate().
207
208 //updates.intersect(server->pb->getRect());
209 //
210 //if (server->pb->width() > cp.width)
211 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
212 // server->pb->height()));
213 //if (server->pb->height() > cp.height)
214 // updates.add_changed(Rect(0, cp.height, cp.width,
215 // server->pb->height()));
216
217 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
218
219 cp.width = server->pb->width();
220 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000221 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000223 // We should only send EDS to client asking for both
224 if (!writer()->writeExtendedDesktopSize()) {
225 if (!writer()->writeSetDesktopSize()) {
226 close("Client does not support desktop resize");
227 return;
228 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 }
230 }
231 }
232 // Just update the whole screen at the moment because we're too lazy to
233 // work out what's actually changed.
234 updates.clear();
235 updates.add_changed(server->pb->getRect());
236 vlog.debug("pixel buffer changed - re-initialising image getter");
237 image_getter.init(server->pb, cp.pf(), writer());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000238 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 } catch(rdr::Exception &e) {
240 close(e.str());
241 }
242}
243
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000244void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000245{
246 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000247 writeFramebufferUpdate();
248 } catch(rdr::Exception &e) {
249 close(e.str());
250 }
251}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000252
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000253void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
254{
255 try {
256 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000257 } catch(rdr::Exception &e) {
258 close(e.str());
259 }
260}
261
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000262void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000263{
264 try {
265 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
266 } catch(rdr::Exception& e) {
267 close(e.str());
268 }
269}
270
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000271void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000272{
273 try {
274 if (!(accessRights & AccessCutText)) return;
275 if (!rfb::Server::sendCutText) return;
276 if (state() == RFBSTATE_NORMAL)
277 writer()->writeServerCutText(str, len);
278 } catch(rdr::Exception& e) {
279 close(e.str());
280 }
281}
282
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000283
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000284void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000285{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000286 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000287 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000288 } catch(rdr::Exception& e) {
289 close(e.str());
290 }
291}
292
293
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000294void VNCSConnectionST::setCursorOrClose()
295{
296 try {
297 setCursor();
298 } catch(rdr::Exception& e) {
299 close(e.str());
300 }
301}
302
303
304int VNCSConnectionST::checkIdleTimeout()
305{
306 int idleTimeout = rfb::Server::idleTimeout;
307 if (idleTimeout == 0) return 0;
308 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
309 idleTimeout = 15; // minimum of 15 seconds while authenticating
310 time_t now = time(0);
311 if (now < lastEventTime) {
312 // Someone must have set the time backwards. Set lastEventTime so that the
313 // idleTimeout will count from now.
314 vlog.info("Time has gone backwards - resetting idle timeout");
315 lastEventTime = now;
316 }
317 int timeLeft = lastEventTime + idleTimeout - now;
318 if (timeLeft < -60) {
319 // Our callback is over a minute late - someone must have set the time
320 // forwards. Set lastEventTime so that the idleTimeout will count from
321 // now.
322 vlog.info("Time has gone forwards - resetting idle timeout");
323 lastEventTime = now;
324 return secsToMillis(idleTimeout);
325 }
326 if (timeLeft <= 0) {
327 close("Idle timeout");
328 return 0;
329 }
330 return secsToMillis(timeLeft);
331}
332
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000333
334bool VNCSConnectionST::getComparerState()
335{
336 // We interpret a low compression level as an indication that the client
337 // wants to prioritise CPU usage over bandwidth, and hence disable the
338 // comparing update tracker.
339 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
340}
341
342
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000343// renderedCursorChange() is called whenever the server-side rendered cursor
344// changes shape or position. It ensures that the next update will clean up
345// the old rendered cursor and if necessary draw the new rendered cursor.
346
347void VNCSConnectionST::renderedCursorChange()
348{
349 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000350 if (!renderedCursorRect.is_empty())
351 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000352 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000353 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000354 writeFramebufferUpdateOrClose();
355 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000356}
357
358// needRenderedCursor() returns true if this client needs the server-side
359// rendered cursor. This may be because it does not support local cursor or
360// because the current cursor position has not been set by this client.
361// Unfortunately we can't know for sure when the current cursor position has
362// been set by this client. We guess that this is the case when the current
363// cursor position is the same as the last pointer event from this client, or
364// if it is a very short time since this client's last pointer event (up to a
365// second). [ Ideally we should do finer-grained timing here and make the time
366// configurable, but I don't think it's that important. ]
367
368bool VNCSConnectionST::needRenderedCursor()
369{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000370 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000371 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000372 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000373}
374
375
376void VNCSConnectionST::approveConnectionOrClose(bool accept,
377 const char* reason)
378{
379 try {
380 approveConnection(accept, reason);
381 } catch (rdr::Exception& e) {
382 close(e.str());
383 }
384}
385
386
387
388// -=- Callbacks from SConnection
389
390void VNCSConnectionST::authSuccess()
391{
392 lastEventTime = time(0);
393
394 server->startDesktop();
395
396 // - Set the connection parameters appropriately
397 cp.width = server->pb->width();
398 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000399 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000400 cp.setName(server->getName());
401
402 // - Set the default pixel format
403 cp.setPF(server->pb->getPF());
404 char buffer[256];
405 cp.pf().print(buffer, 256);
406 vlog.info("Server default pixel format %s", buffer);
407 image_getter.init(server->pb, cp.pf(), 0);
408
409 // - Mark the entire display as "dirty"
410 updates.add_changed(server->pb->getRect());
411 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000412
413 // - Bootstrap the congestion control
414 ackedOffset = sock->outStream().length();
415 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000416}
417
418void VNCSConnectionST::queryConnection(const char* userName)
419{
420 // - Authentication succeeded - clear from blacklist
421 CharArray name; name.buf = sock->getPeerAddress();
422 server->blHosts->clearBlackmark(name.buf);
423
424 // - Special case to provide a more useful error message
425 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
426 server->authClientCount() > 0) {
427 approveConnection(false, "The server is already in use");
428 return;
429 }
430
431 // - Does the client have the right to bypass the query?
432 if (reverseConnection ||
433 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
434 (accessRights & AccessNoQuery))
435 {
436 approveConnection(true);
437 return;
438 }
439
440 // - Get the server to display an Accept/Reject dialog, if required
441 // If a dialog is displayed, the result will be PENDING, and the
442 // server will call approveConnection at a later time
443 CharArray reason;
444 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
445 &reason.buf);
446 if (qr == VNCServerST::PENDING)
447 return;
448
449 // - If server returns ACCEPT/REJECT then pass result to SConnection
450 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
451}
452
453void VNCSConnectionST::clientInit(bool shared)
454{
455 lastEventTime = time(0);
456 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
457 if (rfb::Server::neverShared) shared = false;
458 if (!shared) {
459 if (rfb::Server::disconnectClients) {
460 // - Close all the other connected clients
461 vlog.debug("non-shared connection - closing clients");
462 server->closeClients("Non-shared connection requested", getSock());
463 } else {
464 // - Refuse this connection if there are existing clients, in addition to
465 // this one
466 if (server->authClientCount() > 1) {
467 close("Server is already in use");
468 return;
469 }
470 }
471 }
472 SConnection::clientInit(shared);
473}
474
475void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
476{
477 SConnection::setPixelFormat(pf);
478 char buffer[256];
479 pf.print(buffer, 256);
480 vlog.info("Client pixel format %s", buffer);
481 image_getter.init(server->pb, pf, writer());
482 setCursor();
483}
484
485void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
486{
487 pointerEventTime = lastEventTime = time(0);
488 server->lastUserInputTime = lastEventTime;
489 if (!(accessRights & AccessPtrEvents)) return;
490 if (!rfb::Server::acceptPointerEvents) return;
491 if (!server->pointerClient || server->pointerClient == this) {
492 pointerEventPos = pos;
493 if (buttonMask)
494 server->pointerClient = this;
495 else
496 server->pointerClient = 0;
497 server->desktop->pointerEvent(pointerEventPos, buttonMask);
498 }
499}
500
501
502class VNCSConnectionSTShiftPresser {
503public:
504 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
505 : desktop(desktop_), pressed(false) {}
506 ~VNCSConnectionSTShiftPresser() {
507 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
508 }
509 void press() {
510 desktop->keyEvent(XK_Shift_L, true);
511 pressed = true;
512 }
513 SDesktop* desktop;
514 bool pressed;
515};
516
517// keyEvent() - record in the pressedKeys which keys were pressed. Allow
518// multiple down events (for autorepeat), but only allow a single up event.
519void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
520 lastEventTime = time(0);
521 server->lastUserInputTime = lastEventTime;
522 if (!(accessRights & AccessKeyEvents)) return;
523 if (!rfb::Server::acceptKeyEvents) return;
524
525 // Remap the key if required
526 if (server->keyRemapper)
527 key = server->keyRemapper->remapKey(key);
528
529 // Turn ISO_Left_Tab into shifted Tab.
530 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
531 if (key == XK_ISO_Left_Tab) {
532 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
533 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
534 shiftPresser.press();
535 key = XK_Tab;
536 }
537
538 if (down) {
539 pressedKeys.insert(key);
540 } else {
541 if (!pressedKeys.erase(key)) return;
542 }
543 server->desktop->keyEvent(key, down);
544}
545
546void VNCSConnectionST::clientCutText(const char* str, int len)
547{
548 if (!(accessRights & AccessCutText)) return;
549 if (!rfb::Server::acceptCutText) return;
550 server->desktop->clientCutText(str, len);
551}
552
553void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
554{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000555 Rect safeRect;
556
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000557 if (!(accessRights & AccessView)) return;
558
559 SConnection::framebufferUpdateRequest(r, incremental);
560
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000561 // Check that the client isn't sending crappy requests
562 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
563 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
564 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000565 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
566 } else {
567 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000568 }
569
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000570 // Just update the requested region.
571 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000572 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000573 if (!incremental || !continuousUpdates)
574 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575
576 if (!incremental) {
577 // Non-incremental update - treat as if area requested has changed
578 updates.add_changed(reqRgn);
579 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000580
581 // And send the screen layout to the client (which, unlike the
582 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000583 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000584
585 // We do not send a DesktopSize since it only contains the
586 // framebuffer size (which the client already should know) and
587 // because some clients don't handle extra DesktopSize events
588 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000589 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000590}
591
Pierre Ossman34bb0612009-03-21 21:16:14 +0000592void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
593 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000594{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000595 unsigned int result;
596
597 // Don't bother the desktop with an invalid configuration
598 if (!layout.validate(fb_width, fb_height)) {
599 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
600 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000601 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000602 return;
603 }
604
605 // FIXME: the desktop will call back to VNCServerST and an extra set
606 // of ExtendedDesktopSize messages will be sent. This is okay
607 // protocol-wise, but unnecessary.
608 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
609
Pierre Ossman04e62db2009-03-23 16:57:07 +0000610 writer()->writeExtendedDesktopSize(reasonClient, result,
611 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000612
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000613 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000614 if (result == resultSuccess) {
615 if (server->screenLayout != layout)
616 throw Exception("Desktop configured a different screen layout than requested");
617 server->notifyScreenLayoutChange(this);
618 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000619
620 // but always send back a reply to the requesting client
621 // (do this last as it might throw an exception on socket errors)
622 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000623}
624
Pierre Ossman2c764942011-11-14 15:54:30 +0000625void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
626{
627 if (flags & fenceFlagRequest) {
628 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000629 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000630
631 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
632 fenceDataLen = len;
633 delete [] fenceData;
634 if (len > 0) {
635 fenceData = new char[len];
636 memcpy(fenceData, data, len);
637 }
638
639 return;
640 }
641
642 // We handle everything synchronously so we trivially honor these modes
643 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
644
645 writer()->writeFence(flags, len, data);
646 return;
647 }
648
Pierre Ossman1b478e52011-11-15 12:08:30 +0000649 struct RTTInfo rttInfo;
650
Pierre Ossman2c764942011-11-14 15:54:30 +0000651 switch (len) {
652 case 0:
653 // Initial dummy fence;
654 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000655 case sizeof(struct RTTInfo):
656 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
657 handleRTTPong(rttInfo);
658 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000659 default:
660 vlog.error("Fence response of unexpected size received");
661 }
662}
663
Pierre Ossman1b478e52011-11-15 12:08:30 +0000664void VNCSConnectionST::enableContinuousUpdates(bool enable,
665 int x, int y, int w, int h)
666{
667 Rect rect;
668
669 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
670 throw Exception("Client tried to enable continuous updates when not allowed");
671
672 continuousUpdates = enable;
673
674 rect.setXYWH(x, y, w, h);
675 cuRegion.reset(rect);
676
677 if (enable) {
678 requested.clear();
679 writeFramebufferUpdate();
680 } else {
681 writer()->writeEndOfContinuousUpdates();
682 }
683}
684
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000685// supportsLocalCursor() is called whenever the status of
686// cp.supportsLocalCursor has changed. If the client does now support local
687// cursor, we make sure that the old server-side rendered cursor is cleaned up
688// and the cursor is sent to the client.
689
690void VNCSConnectionST::supportsLocalCursor()
691{
692 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000693 if (!renderedCursorRect.is_empty())
694 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000695 drawRenderedCursor = false;
696 setCursor();
697 }
698}
699
Pierre Ossman2c764942011-11-14 15:54:30 +0000700void VNCSConnectionST::supportsFence()
701{
702 writer()->writeFence(fenceFlagRequest, 0, NULL);
703}
704
Pierre Ossman1b478e52011-11-15 12:08:30 +0000705void VNCSConnectionST::supportsContinuousUpdates()
706{
707 // We refuse to use continuous updates if we cannot monitor the buffer
708 // usage using fences.
709 if (!cp.supportsFence)
710 return;
711
712 writer()->writeEndOfContinuousUpdates();
713}
714
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000715void VNCSConnectionST::writeSetCursorCallback()
716{
717 if (cp.supportsLocalXCursor) {
718 Pixel pix0, pix1;
719 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
720 if (bitmap.buf) {
721 // The client supports XCursor and the cursor only has two
722 // colors. Use the XCursor encoding.
723 writer()->writeSetXCursor(server->cursor.width(),
724 server->cursor.height(),
725 server->cursor.hotspot.x,
726 server->cursor.hotspot.y,
727 bitmap.buf, server->cursor.mask.buf);
728 return;
729 } else {
730 // More than two colors
731 if (!cp.supportsLocalCursor) {
732 // FIXME: We could reduce to two colors.
733 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
734 return;
735 }
736 }
737 }
738
739 // Use RichCursor
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100740 rdr::U8* transBuffer;
741 int stride;
742 const rdr::U8* buffer;
743
744 transBuffer = writer()->getImageBuf(server->cursor.area());
745
746 buffer = server->cursor.getBuffer(server->cursor.getRect(), &stride);
747 image_getter.translatePixels(buffer, transBuffer, server->cursor.area());
748
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000749 writer()->writeSetCursor(server->cursor.width(),
750 server->cursor.height(),
751 server->cursor.hotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100752 transBuffer, server->cursor.mask.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000753}
754
755
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000756bool VNCSConnectionST::handleTimeout(Timer* t)
757{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000758 try {
759 if (t == &updateTimer)
760 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000761 else if (t == &congestionTimer)
762 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000763 } catch (rdr::Exception& e) {
764 close(e.str());
765 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000766
767 return false;
768}
769
770
Pierre Ossman1b478e52011-11-15 12:08:30 +0000771void VNCSConnectionST::writeRTTPing()
772{
773 struct RTTInfo rttInfo;
774
775 if (!cp.supportsFence)
776 return;
777
778 memset(&rttInfo, 0, sizeof(struct RTTInfo));
779
780 gettimeofday(&rttInfo.tv, NULL);
781 rttInfo.offset = sock->outStream().length();
782 rttInfo.inFlight = rttInfo.offset - ackedOffset;
783
784 // We need to make sure any old update are already processed by the
785 // time we get the response back. This allows us to reliably throttle
786 // back on client overload, as well as network overload.
787 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
788 sizeof(struct RTTInfo), (const char*)&rttInfo);
789
790 pingCounter++;
791
792 sentOffset = rttInfo.offset;
793
794 // Let some data flow before we adjust the settings
795 if (!congestionTimer.isStarted())
796 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
797}
798
799void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
800{
801 unsigned rtt, delay;
802 int bdp;
803
804 pingCounter--;
805
806 rtt = msSince(&rttInfo.tv);
807 if (rtt < 1)
808 rtt = 1;
809
810 ackedOffset = rttInfo.offset;
811
812 // Try to estimate wire latency by tracking lowest seen latency
813 if (rtt < baseRTT)
814 baseRTT = rtt;
815
816 if (rttInfo.inFlight > congWindow) {
817 seenCongestion = true;
818
819 // Estimate added delay because of overtaxed buffers
820 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
821
822 if (delay < rtt)
823 rtt -= delay;
824 else
825 rtt = 1;
826
827 // If we underestimate the congestion window, then we'll get a latency
828 // that's less than the wire latency, which will confuse other portions
829 // of the code.
830 if (rtt < baseRTT)
831 rtt = baseRTT;
832 }
833
834 // We only keep track of the minimum latency seen (for a given interval)
835 // on the basis that we want to avoid continous buffer issue, but don't
836 // mind (or even approve of) bursts.
837 if (rtt < minRTT)
838 minRTT = rtt;
839}
840
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000841bool VNCSConnectionST::isCongested()
842{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000843 int offset;
844
845 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000846 if (sock->outStream().bufferUsage() > 0)
847 return true;
848
Pierre Ossman1b478e52011-11-15 12:08:30 +0000849 if (!cp.supportsFence)
850 return false;
851
852 // Idle for too long? (and no data on the wire)
853 //
854 // FIXME: This should really just be one baseRTT, but we're getting
855 // problems with triggering the idle timeout on each update.
856 // Maybe we need to use a moving average for the wire latency
857 // instead of baseRTT.
858 if ((sentOffset == ackedOffset) &&
859 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
860
861#ifdef CONGESTION_DEBUG
862 if (congWindow > INITIAL_WINDOW)
863 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
864 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
865#endif
866
867 // Close congestion window and allow a transfer
868 // FIXME: Reset baseRTT like Linux Vegas?
869 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
870
871 return false;
872 }
873
874 offset = sock->outStream().length();
875
876 // FIXME: Should we compensate for non-update data?
877 // (i.e. use sentOffset instead of offset)
878 if ((offset - ackedOffset) < congWindow)
879 return false;
880
881 // If we just have one outstanding "ping", that means the client has
882 // started receiving our update. In order to not regress compared to
883 // before we had congestion avoidance, we allow another update here.
884 // This could further clog up the tubes, but congestion control isn't
885 // really working properly right now anyway as the wire would otherwise
886 // be idle for at least RTT/2.
887 if (pingCounter == 1)
888 return false;
889
890 return true;
891}
892
893
894void VNCSConnectionST::updateCongestion()
895{
896 unsigned diff;
897
898 if (!seenCongestion)
899 return;
900
901 diff = minRTT - baseRTT;
902
903 if (diff > __rfbmin(100, baseRTT)) {
904 // Way too fast
905 congWindow = congWindow * baseRTT / minRTT;
906 } else if (diff > __rfbmin(50, baseRTT/2)) {
907 // Slightly too fast
908 congWindow -= 4096;
909 } else if (diff < 5) {
910 // Way too slow
911 congWindow += 8192;
912 } else if (diff < 25) {
913 // Too slow
914 congWindow += 4096;
915 }
916
917 if (congWindow < MINIMUM_WINDOW)
918 congWindow = MINIMUM_WINDOW;
919 if (congWindow > MAXIMUM_WINDOW)
920 congWindow = MAXIMUM_WINDOW;
921
922#ifdef CONGESTION_DEBUG
923 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
924 minRTT, baseRTT, congWindow / 1024,
925 congWindow * 8.0 / baseRTT / 1000.0);
926
927#ifdef TCP_INFO
928 struct tcp_info tcp_info;
929 socklen_t tcp_info_length;
930
931 tcp_info_length = sizeof(tcp_info);
932 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
933 (void *)&tcp_info, &tcp_info_length) == 0) {
934 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
935 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
936 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
937 }
938#endif
939
940#endif
941
942 minRTT = -1;
943 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000944}
945
946
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000947void VNCSConnectionST::writeFramebufferUpdate()
948{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000949 Region req;
950 UpdateInfo ui;
951 bool needNewUpdateInfo;
952
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000953 updateTimer.stop();
954
Pierre Ossman2c764942011-11-14 15:54:30 +0000955 // We're in the middle of processing a command that's supposed to be
956 // synchronised. Allowing an update to slip out right now might violate
957 // that synchronisation.
958 if (syncFence)
959 return;
960
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000961 // We try to aggregate responses, so don't send out anything whilst we
962 // still have incoming messages. processMessages() will give us another
963 // chance to run once things are idle.
964 if (inProcessMessages)
965 return;
966
Pierre Ossman1b478e52011-11-15 12:08:30 +0000967 if (state() != RFBSTATE_NORMAL)
968 return;
969 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000970 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000971
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000972 // Check that we actually have some space on the link and retry in a
973 // bit if things are congested.
974 if (isCongested()) {
975 updateTimer.start(50);
976 return;
977 }
978
Pierre Ossman36dadf82011-11-15 12:11:32 +0000979 // In continuous mode, we will be outputting at least three distinct
980 // messages. We need to aggregate these in order to not clog up TCP's
981 // congestion window.
982 network::TcpSocket::cork(sock->getFd(), true);
983
Pierre Ossmane9962f72009-04-23 12:31:42 +0000984 // First take care of any updates that cannot contain framebuffer data
985 // changes.
986 if (writer()->needNoDataUpdate()) {
987 writer()->writeNoDataUpdate();
988 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000989 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000990 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000991 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000992
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000993 updates.enable_copyrect(cp.useCopyRect);
994
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000995 // Fetch updates from server object, and see if we are allowed to send
996 // anything right now (the framebuffer might have changed in ways we
997 // haven't yet been informed of).
998 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000999 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001000
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001001 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001002 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +00001003 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001004
Pierre Ossman1b478e52011-11-15 12:08:30 +00001005 if (continuousUpdates)
1006 req = cuRegion.union_(requested);
1007 else
1008 req = requested;
1009
1010 updates.getUpdateInfo(&ui, req);
1011 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001012
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013 // If the previous position of the rendered cursor overlaps the source of the
1014 // copy, then when the copy happens the corresponding rectangle in the
1015 // destination will be wrong, so add it to the changed region.
1016
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001017 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
1018 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001019 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001020 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001021 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001022 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001023 }
1024 }
1025
1026 // If we need to remove the old rendered cursor, just add the rectangle to
1027 // the changed region.
1028
1029 if (removeRenderedCursor) {
1030 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001031 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001032 renderedCursorRect.clear();
1033 removeRenderedCursor = false;
1034 }
1035
1036 // Return if there is nothing to send the client.
1037
1038 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001039 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001040
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001041 // The `updates' object could change, make sure we have valid update info.
1042
1043 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001044 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001045
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001046 // If the client needs a server-side rendered cursor, work out the cursor
1047 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1048 // with the update region, we need to draw the rendered cursor regardless of
1049 // whether it has changed.
1050
1051 if (needRenderedCursor()) {
1052 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001053 = server->renderedCursor.getEffectiveRect()
1054 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001055
1056 if (renderedCursorRect.is_empty()) {
1057 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001058 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001059 .intersect(renderedCursorRect).is_empty()) {
1060 drawRenderedCursor = true;
1061 }
1062
1063 // We could remove the new cursor rect from updates here. It's not clear
1064 // whether this is worth it. If we do remove it, then we won't draw over
1065 // the same bit of screen twice, but we have the overhead of a more complex
1066 // region.
1067
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001068 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001069 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001070 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001071 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001072 }
1073
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001074 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001075 std::vector<Rect> rects;
1076 std::vector<Rect>::const_iterator i;
1077 int encoding;
1078
1079 // Make sure the encoder has the latest settings
1080 encoding = cp.currentEncoding();
1081
1082 if (!encoders[encoding])
Pierre Ossman668468b2014-01-31 12:37:32 +01001083 encoders[encoding] = Encoder::createEncoder(encoding, this);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001084
1085 encoders[encoding]->setCompressLevel(cp.compressLevel);
1086 encoders[encoding]->setQualityLevel(cp.qualityLevel);
1087 encoders[encoding]->setFineQualityLevel(cp.fineQualityLevel,
1088 cp.subsampling);
1089
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001090 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001091 // complicated as compared to the original VNC4.
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001092 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001093 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +00001094
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001095 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001096 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +00001097 if (i->width() && i->height()) {
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001098 int nUpdateRects = encoders[encoding]->getNumRects(*i);
DRCcd2c5d42011-08-11 11:18:34 +00001099 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
Pierre Ossman65fb4b02012-02-28 11:54:01 +00001100 // With Tight encoding and LastRect support, the client does not
1101 // care about the number of rectangles in the update - it will
1102 // stop parsing when it encounters a LastRect "rectangle".
1103 // In this case, pretend to send 65535 rectangles.
DRCcd2c5d42011-08-11 11:18:34 +00001104 nRects = 0xFFFF; break;
1105 }
1106 else
1107 nRects += nUpdateRects;
1108 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001109 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001110
1111 writeRTTPing();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001112
1113 writer()->writeFramebufferUpdateStart(nRects);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001114
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001115 ui.copied.get_rects(&rects);
1116 for (i = rects.begin(); i != rects.end(); i++)
1117 writer()->writeCopyRect(*i, i->tl.x - ui.copy_delta.x,
1118 i->tl.y - ui.copy_delta.y);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001119
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001120 ui.changed.get_rects(&rects);
1121 for (i = rects.begin(); i != rects.end(); i++)
1122 encoders[encoding]->writeRect(*i, &image_getter);
1123
1124 if (drawRenderedCursor) {
1125 image_getter.setPixelBuffer(&server->renderedCursor);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001126 encoders[encoding]->writeRect(renderedCursorRect, &image_getter);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001127 image_getter.setPixelBuffer(server->pb);
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001128
1129 drawRenderedCursor = false;
1130 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001131
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001132 writer()->writeFramebufferUpdateEnd();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001133
1134 writeRTTPing();
1135
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001136 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001137 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001138 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001139
1140out:
1141 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001142}
1143
1144
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001145void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1146{
1147 if (!authenticated())
1148 return;
1149
1150 cp.screenLayout = server->screenLayout;
1151
1152 if (state() != RFBSTATE_NORMAL)
1153 return;
1154
1155 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1156 cp.screenLayout);
1157 writeFramebufferUpdate();
1158}
1159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001160
1161// setCursor() is called whenever the cursor has changed shape or pixel format.
1162// If the client supports local cursor then it will arrange for the cursor to
1163// be sent to the client.
1164
1165void VNCSConnectionST::setCursor()
1166{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001167 if (state() != RFBSTATE_NORMAL)
1168 return;
1169 if (!cp.supportsLocalCursor)
1170 return;
1171
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001172 writer()->cursorChange(this);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001173 writeFramebufferUpdate();
1174}
1175
1176void VNCSConnectionST::setDesktopName(const char *name)
1177{
1178 cp.setName(name);
1179
1180 if (state() != RFBSTATE_NORMAL)
1181 return;
1182
1183 if (!writer()->writeSetDesktopName()) {
1184 fprintf(stderr, "Client does not support desktop rename\n");
1185 return;
1186 }
1187
1188 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001189}
1190
1191void VNCSConnectionST::setSocketTimeouts()
1192{
1193 int timeoutms = rfb::Server::clientWaitTimeMillis;
1194 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1195 if (timeoutms == 0)
1196 timeoutms = -1;
1197 sock->inStream().setTimeout(timeoutms);
1198 sock->outStream().setTimeout(timeoutms);
1199}
1200
1201char* VNCSConnectionST::getStartTime()
1202{
1203 char* result = ctime(&startTime);
1204 result[24] = '\0';
1205 return result;
1206}
1207
1208void VNCSConnectionST::setStatus(int status)
1209{
1210 switch (status) {
1211 case 0:
1212 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1213 break;
1214 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001215 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001216 break;
1217 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001218 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001219 break;
1220 }
1221 framebufferUpdateRequest(server->pb->getRect(), false);
1222}
1223int VNCSConnectionST::getStatus()
1224{
1225 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1226 return 0;
1227 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1228 return 1;
1229 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1230 return 2;
1231 return 4;
1232}
1233