blob: 381ee21614ff1bd429e27a53865016bca76005cd [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00002 * Copyright 2009-2011 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>
40#define XK_MISCELLANY
41#define XK_XKB_KEYS
42#include <rfb/keysymdef.h>
43
44using namespace rfb;
45
46static LogWriter vlog("VNCSConnST");
47
Pierre Ossman1b478e52011-11-15 12:08:30 +000048// This window should get us going fairly fast on a decent bandwidth network.
49// If it's too high, it will rapidly be reduced and stay low.
50static const unsigned INITIAL_WINDOW = 16384;
51
52// TCP's minimal window is 3*MSS. But since we don't know the MSS, we
53// make a guess at 4 KiB (it's probaly a bit higher).
54static const unsigned MINIMUM_WINDOW = 4096;
55
56// The current default maximum window for Linux (4 MiB). Should be a good
57// limit for now...
58static const unsigned MAXIMUM_WINDOW = 4194304;
59
60struct RTTInfo {
61 struct timeval tv;
62 int offset;
63 unsigned inFlight;
64};
65
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
67 bool reverse)
Pierre Ossman2c764942011-11-14 15:54:30 +000068 : SConnection(reverse), sock(s), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000069 pendingSyncFence(false), syncFence(false), fenceFlags(0),
70 fenceDataLen(0), fenceData(NULL),
Pierre Ossman1b478e52011-11-15 12:08:30 +000071 baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
72 ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
Pierre Ossman2c764942011-11-14 15:54:30 +000073 server(server_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 updates(false), image_getter(server->useEconomicTranslate),
75 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
84 // Configure the socket
85 setSocketTimeouts();
86 lastEventTime = time(0);
87
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 server->clients.push_front(this);
89}
90
91
92VNCSConnectionST::~VNCSConnectionST()
93{
94 // If we reach here then VNCServerST is deleting us!
95 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
96 peerEndpoint.buf,
97 (closeReason.buf) ? closeReason.buf : "");
98
99 // Release any keys the client still had pressed
100 std::set<rdr::U32>::iterator i;
101 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
102 server->desktop->keyEvent(*i, false);
103 if (server->pointerClient == this)
104 server->pointerClient = 0;
105
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 // Remove this client from the server
107 server->clients.remove(this);
108
Pierre Ossman2c764942011-11-14 15:54:30 +0000109 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110}
111
112
113// Methods called from VNCServerST
114
115bool VNCSConnectionST::init()
116{
117 try {
118 initialiseProtocol();
119 } catch (rdr::Exception& e) {
120 close(e.str());
121 return false;
122 }
123 return true;
124}
125
126void VNCSConnectionST::close(const char* reason)
127{
128 // Log the reason for the close
129 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000130 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131 else
132 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
133
134 if (authenticated()) {
135 server->lastDisconnectTime = time(0);
136 }
137
138 // Just shutdown the socket and mark our state as closing. Eventually the
139 // calling code will call VNCServerST's removeSocket() method causing us to
140 // be deleted.
141 sock->shutdown();
142 setState(RFBSTATE_CLOSING);
143}
144
145
146void VNCSConnectionST::processMessages()
147{
148 if (state() == RFBSTATE_CLOSING) return;
149 try {
150 // - Now set appropriate socket timeouts and process data
151 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000152
153 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154
Pierre Ossmana830bec2011-11-08 12:12:02 +0000155 // Get the underlying TCP layer to build large packets if we send
156 // multiple small responses.
157 network::TcpSocket::cork(sock->getFd(), true);
158
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000160 if (pendingSyncFence) {
161 syncFence = true;
162 pendingSyncFence = false;
163 }
164
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000166
Pierre Ossman2c764942011-11-14 15:54:30 +0000167 if (syncFence) {
168 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
169 syncFence = false;
170 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 }
172
Pierre Ossmana830bec2011-11-08 12:12:02 +0000173 // Flush out everything in case we go idle after this.
174 network::TcpSocket::cork(sock->getFd(), false);
175
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000176 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000177
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000178 // If there were anything requiring an update, try to send it here.
179 // We wait until now with this to aggregate responses and to give
180 // higher priority to user actions such as keyboard and pointer events.
181 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182 } catch (rdr::EndOfStream&) {
183 close("Clean disconnection");
184 } catch (rdr::Exception &e) {
185 close(e.str());
186 }
187}
188
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189void VNCSConnectionST::pixelBufferChange()
190{
191 try {
192 if (!authenticated()) return;
193 if (cp.width && cp.height && (server->pb->width() != cp.width ||
194 server->pb->height() != cp.height))
195 {
196 // We need to clip the next update to the new size, but also add any
197 // extra bits if it's bigger. If we wanted to do this exactly, something
198 // like the code below would do it, but at the moment we just update the
199 // entire new size. However, we do need to clip the renderedCursorRect
200 // because that might be added to updates in writeFramebufferUpdate().
201
202 //updates.intersect(server->pb->getRect());
203 //
204 //if (server->pb->width() > cp.width)
205 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
206 // server->pb->height()));
207 //if (server->pb->height() > cp.height)
208 // updates.add_changed(Rect(0, cp.height, cp.width,
209 // server->pb->height()));
210
211 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
212
213 cp.width = server->pb->width();
214 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000215 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000217 // We should only send EDS to client asking for both
218 if (!writer()->writeExtendedDesktopSize()) {
219 if (!writer()->writeSetDesktopSize()) {
220 close("Client does not support desktop resize");
221 return;
222 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000223 }
224 }
225 }
226 // Just update the whole screen at the moment because we're too lazy to
227 // work out what's actually changed.
228 updates.clear();
229 updates.add_changed(server->pb->getRect());
230 vlog.debug("pixel buffer changed - re-initialising image getter");
231 image_getter.init(server->pb, cp.pf(), writer());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000232 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233 } catch(rdr::Exception &e) {
234 close(e.str());
235 }
236}
237
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000238void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000239{
240 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000241 writeFramebufferUpdate();
242 } catch(rdr::Exception &e) {
243 close(e.str());
244 }
245}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000246
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000247void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
248{
249 try {
250 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000251 } catch(rdr::Exception &e) {
252 close(e.str());
253 }
254}
255
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours)
257{
258 try {
259 setColourMapEntries(firstColour, nColours);
260 } catch(rdr::Exception& e) {
261 close(e.str());
262 }
263}
264
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000265void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000266{
267 try {
268 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
269 } catch(rdr::Exception& e) {
270 close(e.str());
271 }
272}
273
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000274void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000275{
276 try {
277 if (!(accessRights & AccessCutText)) return;
278 if (!rfb::Server::sendCutText) return;
279 if (state() == RFBSTATE_NORMAL)
280 writer()->writeServerCutText(str, len);
281 } catch(rdr::Exception& e) {
282 close(e.str());
283 }
284}
285
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000286
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000287void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000288{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000289 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000290 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000291 } catch(rdr::Exception& e) {
292 close(e.str());
293 }
294}
295
296
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000297void VNCSConnectionST::setCursorOrClose()
298{
299 try {
300 setCursor();
301 } catch(rdr::Exception& e) {
302 close(e.str());
303 }
304}
305
306
307int VNCSConnectionST::checkIdleTimeout()
308{
309 int idleTimeout = rfb::Server::idleTimeout;
310 if (idleTimeout == 0) return 0;
311 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
312 idleTimeout = 15; // minimum of 15 seconds while authenticating
313 time_t now = time(0);
314 if (now < lastEventTime) {
315 // Someone must have set the time backwards. Set lastEventTime so that the
316 // idleTimeout will count from now.
317 vlog.info("Time has gone backwards - resetting idle timeout");
318 lastEventTime = now;
319 }
320 int timeLeft = lastEventTime + idleTimeout - now;
321 if (timeLeft < -60) {
322 // Our callback is over a minute late - someone must have set the time
323 // forwards. Set lastEventTime so that the idleTimeout will count from
324 // now.
325 vlog.info("Time has gone forwards - resetting idle timeout");
326 lastEventTime = now;
327 return secsToMillis(idleTimeout);
328 }
329 if (timeLeft <= 0) {
330 close("Idle timeout");
331 return 0;
332 }
333 return secsToMillis(timeLeft);
334}
335
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000336
337bool VNCSConnectionST::getComparerState()
338{
339 // We interpret a low compression level as an indication that the client
340 // wants to prioritise CPU usage over bandwidth, and hence disable the
341 // comparing update tracker.
342 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
343}
344
345
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000346// renderedCursorChange() is called whenever the server-side rendered cursor
347// changes shape or position. It ensures that the next update will clean up
348// the old rendered cursor and if necessary draw the new rendered cursor.
349
350void VNCSConnectionST::renderedCursorChange()
351{
352 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000353 if (!renderedCursorRect.is_empty())
354 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000355 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000356 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000357 writeFramebufferUpdateOrClose();
358 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000359}
360
361// needRenderedCursor() returns true if this client needs the server-side
362// rendered cursor. This may be because it does not support local cursor or
363// because the current cursor position has not been set by this client.
364// Unfortunately we can't know for sure when the current cursor position has
365// been set by this client. We guess that this is the case when the current
366// cursor position is the same as the last pointer event from this client, or
367// if it is a very short time since this client's last pointer event (up to a
368// second). [ Ideally we should do finer-grained timing here and make the time
369// configurable, but I don't think it's that important. ]
370
371bool VNCSConnectionST::needRenderedCursor()
372{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000373 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000374 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000375 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000376}
377
378
379void VNCSConnectionST::approveConnectionOrClose(bool accept,
380 const char* reason)
381{
382 try {
383 approveConnection(accept, reason);
384 } catch (rdr::Exception& e) {
385 close(e.str());
386 }
387}
388
389
390
391// -=- Callbacks from SConnection
392
393void VNCSConnectionST::authSuccess()
394{
395 lastEventTime = time(0);
396
397 server->startDesktop();
398
399 // - Set the connection parameters appropriately
400 cp.width = server->pb->width();
401 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000402 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000403 cp.setName(server->getName());
404
405 // - Set the default pixel format
406 cp.setPF(server->pb->getPF());
407 char buffer[256];
408 cp.pf().print(buffer, 256);
409 vlog.info("Server default pixel format %s", buffer);
410 image_getter.init(server->pb, cp.pf(), 0);
411
412 // - Mark the entire display as "dirty"
413 updates.add_changed(server->pb->getRect());
414 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000415
416 // - Bootstrap the congestion control
417 ackedOffset = sock->outStream().length();
418 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000419}
420
421void VNCSConnectionST::queryConnection(const char* userName)
422{
423 // - Authentication succeeded - clear from blacklist
424 CharArray name; name.buf = sock->getPeerAddress();
425 server->blHosts->clearBlackmark(name.buf);
426
427 // - Special case to provide a more useful error message
428 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
429 server->authClientCount() > 0) {
430 approveConnection(false, "The server is already in use");
431 return;
432 }
433
434 // - Does the client have the right to bypass the query?
435 if (reverseConnection ||
436 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
437 (accessRights & AccessNoQuery))
438 {
439 approveConnection(true);
440 return;
441 }
442
443 // - Get the server to display an Accept/Reject dialog, if required
444 // If a dialog is displayed, the result will be PENDING, and the
445 // server will call approveConnection at a later time
446 CharArray reason;
447 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
448 &reason.buf);
449 if (qr == VNCServerST::PENDING)
450 return;
451
452 // - If server returns ACCEPT/REJECT then pass result to SConnection
453 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
454}
455
456void VNCSConnectionST::clientInit(bool shared)
457{
458 lastEventTime = time(0);
459 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
460 if (rfb::Server::neverShared) shared = false;
461 if (!shared) {
462 if (rfb::Server::disconnectClients) {
463 // - Close all the other connected clients
464 vlog.debug("non-shared connection - closing clients");
465 server->closeClients("Non-shared connection requested", getSock());
466 } else {
467 // - Refuse this connection if there are existing clients, in addition to
468 // this one
469 if (server->authClientCount() > 1) {
470 close("Server is already in use");
471 return;
472 }
473 }
474 }
475 SConnection::clientInit(shared);
476}
477
478void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
479{
480 SConnection::setPixelFormat(pf);
481 char buffer[256];
482 pf.print(buffer, 256);
483 vlog.info("Client pixel format %s", buffer);
484 image_getter.init(server->pb, pf, writer());
485 setCursor();
486}
487
488void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
489{
490 pointerEventTime = lastEventTime = time(0);
491 server->lastUserInputTime = lastEventTime;
492 if (!(accessRights & AccessPtrEvents)) return;
493 if (!rfb::Server::acceptPointerEvents) return;
494 if (!server->pointerClient || server->pointerClient == this) {
495 pointerEventPos = pos;
496 if (buttonMask)
497 server->pointerClient = this;
498 else
499 server->pointerClient = 0;
500 server->desktop->pointerEvent(pointerEventPos, buttonMask);
501 }
502}
503
504
505class VNCSConnectionSTShiftPresser {
506public:
507 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
508 : desktop(desktop_), pressed(false) {}
509 ~VNCSConnectionSTShiftPresser() {
510 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
511 }
512 void press() {
513 desktop->keyEvent(XK_Shift_L, true);
514 pressed = true;
515 }
516 SDesktop* desktop;
517 bool pressed;
518};
519
520// keyEvent() - record in the pressedKeys which keys were pressed. Allow
521// multiple down events (for autorepeat), but only allow a single up event.
522void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
523 lastEventTime = time(0);
524 server->lastUserInputTime = lastEventTime;
525 if (!(accessRights & AccessKeyEvents)) return;
526 if (!rfb::Server::acceptKeyEvents) return;
527
528 // Remap the key if required
529 if (server->keyRemapper)
530 key = server->keyRemapper->remapKey(key);
531
532 // Turn ISO_Left_Tab into shifted Tab.
533 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
534 if (key == XK_ISO_Left_Tab) {
535 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
536 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
537 shiftPresser.press();
538 key = XK_Tab;
539 }
540
541 if (down) {
542 pressedKeys.insert(key);
543 } else {
544 if (!pressedKeys.erase(key)) return;
545 }
546 server->desktop->keyEvent(key, down);
547}
548
549void VNCSConnectionST::clientCutText(const char* str, int len)
550{
551 if (!(accessRights & AccessCutText)) return;
552 if (!rfb::Server::acceptCutText) return;
553 server->desktop->clientCutText(str, len);
554}
555
556void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
557{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000558 Rect safeRect;
559
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000560 if (!(accessRights & AccessView)) return;
561
562 SConnection::framebufferUpdateRequest(r, incremental);
563
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000564 // Check that the client isn't sending crappy requests
565 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
566 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
567 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000568 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
569 } else {
570 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000571 }
572
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000573 // Just update the requested region.
574 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000576 if (!incremental || !continuousUpdates)
577 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000578
579 if (!incremental) {
580 // Non-incremental update - treat as if area requested has changed
581 updates.add_changed(reqRgn);
582 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000583
584 // And send the screen layout to the client (which, unlike the
585 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000586 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000587
588 // We do not send a DesktopSize since it only contains the
589 // framebuffer size (which the client already should know) and
590 // because some clients don't handle extra DesktopSize events
591 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000592 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593}
594
Pierre Ossman34bb0612009-03-21 21:16:14 +0000595void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
596 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000597{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000598 unsigned int result;
599
600 // Don't bother the desktop with an invalid configuration
601 if (!layout.validate(fb_width, fb_height)) {
602 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
603 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000604 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000605 return;
606 }
607
608 // FIXME: the desktop will call back to VNCServerST and an extra set
609 // of ExtendedDesktopSize messages will be sent. This is okay
610 // protocol-wise, but unnecessary.
611 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
612
Pierre Ossman04e62db2009-03-23 16:57:07 +0000613 writer()->writeExtendedDesktopSize(reasonClient, result,
614 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000615
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000616 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000617 if (result == resultSuccess) {
618 if (server->screenLayout != layout)
619 throw Exception("Desktop configured a different screen layout than requested");
620 server->notifyScreenLayoutChange(this);
621 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000622
623 // but always send back a reply to the requesting client
624 // (do this last as it might throw an exception on socket errors)
625 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000626}
627
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000628void VNCSConnectionST::setInitialColourMap()
629{
630 setColourMapEntries(0, 0);
631}
632
Pierre Ossman2c764942011-11-14 15:54:30 +0000633void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
634{
635 if (flags & fenceFlagRequest) {
636 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000637 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000638
639 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
640 fenceDataLen = len;
641 delete [] fenceData;
642 if (len > 0) {
643 fenceData = new char[len];
644 memcpy(fenceData, data, len);
645 }
646
647 return;
648 }
649
650 // We handle everything synchronously so we trivially honor these modes
651 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
652
653 writer()->writeFence(flags, len, data);
654 return;
655 }
656
Pierre Ossman1b478e52011-11-15 12:08:30 +0000657 struct RTTInfo rttInfo;
658
Pierre Ossman2c764942011-11-14 15:54:30 +0000659 switch (len) {
660 case 0:
661 // Initial dummy fence;
662 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000663 case sizeof(struct RTTInfo):
664 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
665 handleRTTPong(rttInfo);
666 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000667 default:
668 vlog.error("Fence response of unexpected size received");
669 }
670}
671
Pierre Ossman1b478e52011-11-15 12:08:30 +0000672void VNCSConnectionST::enableContinuousUpdates(bool enable,
673 int x, int y, int w, int h)
674{
675 Rect rect;
676
677 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
678 throw Exception("Client tried to enable continuous updates when not allowed");
679
680 continuousUpdates = enable;
681
682 rect.setXYWH(x, y, w, h);
683 cuRegion.reset(rect);
684
685 if (enable) {
686 requested.clear();
687 writeFramebufferUpdate();
688 } else {
689 writer()->writeEndOfContinuousUpdates();
690 }
691}
692
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000693// supportsLocalCursor() is called whenever the status of
694// cp.supportsLocalCursor has changed. If the client does now support local
695// cursor, we make sure that the old server-side rendered cursor is cleaned up
696// and the cursor is sent to the client.
697
698void VNCSConnectionST::supportsLocalCursor()
699{
700 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000701 if (!renderedCursorRect.is_empty())
702 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000703 drawRenderedCursor = false;
704 setCursor();
705 }
706}
707
Pierre Ossman2c764942011-11-14 15:54:30 +0000708void VNCSConnectionST::supportsFence()
709{
710 writer()->writeFence(fenceFlagRequest, 0, NULL);
711}
712
Pierre Ossman1b478e52011-11-15 12:08:30 +0000713void VNCSConnectionST::supportsContinuousUpdates()
714{
715 // We refuse to use continuous updates if we cannot monitor the buffer
716 // usage using fences.
717 if (!cp.supportsFence)
718 return;
719
720 writer()->writeEndOfContinuousUpdates();
721}
722
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000723void VNCSConnectionST::writeSetCursorCallback()
724{
725 if (cp.supportsLocalXCursor) {
726 Pixel pix0, pix1;
727 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
728 if (bitmap.buf) {
729 // The client supports XCursor and the cursor only has two
730 // colors. Use the XCursor encoding.
731 writer()->writeSetXCursor(server->cursor.width(),
732 server->cursor.height(),
733 server->cursor.hotspot.x,
734 server->cursor.hotspot.y,
735 bitmap.buf, server->cursor.mask.buf);
736 return;
737 } else {
738 // More than two colors
739 if (!cp.supportsLocalCursor) {
740 // FIXME: We could reduce to two colors.
741 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
742 return;
743 }
744 }
745 }
746
747 // Use RichCursor
748 rdr::U8* transData = writer()->getImageBuf(server->cursor.area());
749 image_getter.translatePixels(server->cursor.data, transData,
750 server->cursor.area());
751 writer()->writeSetCursor(server->cursor.width(),
752 server->cursor.height(),
753 server->cursor.hotspot,
754 transData, server->cursor.mask.buf);
755}
756
757
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000758bool VNCSConnectionST::handleTimeout(Timer* t)
759{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000760 try {
761 if (t == &updateTimer)
762 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000763 else if (t == &congestionTimer)
764 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000765 } catch (rdr::Exception& e) {
766 close(e.str());
767 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000768
769 return false;
770}
771
772
Pierre Ossman1b478e52011-11-15 12:08:30 +0000773void VNCSConnectionST::writeRTTPing()
774{
775 struct RTTInfo rttInfo;
776
777 if (!cp.supportsFence)
778 return;
779
780 memset(&rttInfo, 0, sizeof(struct RTTInfo));
781
782 gettimeofday(&rttInfo.tv, NULL);
783 rttInfo.offset = sock->outStream().length();
784 rttInfo.inFlight = rttInfo.offset - ackedOffset;
785
786 // We need to make sure any old update are already processed by the
787 // time we get the response back. This allows us to reliably throttle
788 // back on client overload, as well as network overload.
789 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
790 sizeof(struct RTTInfo), (const char*)&rttInfo);
791
792 pingCounter++;
793
794 sentOffset = rttInfo.offset;
795
796 // Let some data flow before we adjust the settings
797 if (!congestionTimer.isStarted())
798 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
799}
800
801void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
802{
803 unsigned rtt, delay;
804 int bdp;
805
806 pingCounter--;
807
808 rtt = msSince(&rttInfo.tv);
809 if (rtt < 1)
810 rtt = 1;
811
812 ackedOffset = rttInfo.offset;
813
814 // Try to estimate wire latency by tracking lowest seen latency
815 if (rtt < baseRTT)
816 baseRTT = rtt;
817
818 if (rttInfo.inFlight > congWindow) {
819 seenCongestion = true;
820
821 // Estimate added delay because of overtaxed buffers
822 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
823
824 if (delay < rtt)
825 rtt -= delay;
826 else
827 rtt = 1;
828
829 // If we underestimate the congestion window, then we'll get a latency
830 // that's less than the wire latency, which will confuse other portions
831 // of the code.
832 if (rtt < baseRTT)
833 rtt = baseRTT;
834 }
835
836 // We only keep track of the minimum latency seen (for a given interval)
837 // on the basis that we want to avoid continous buffer issue, but don't
838 // mind (or even approve of) bursts.
839 if (rtt < minRTT)
840 minRTT = rtt;
841}
842
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000843bool VNCSConnectionST::isCongested()
844{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000845 int offset;
846
847 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000848 if (sock->outStream().bufferUsage() > 0)
849 return true;
850
Pierre Ossman1b478e52011-11-15 12:08:30 +0000851 if (!cp.supportsFence)
852 return false;
853
854 // Idle for too long? (and no data on the wire)
855 //
856 // FIXME: This should really just be one baseRTT, but we're getting
857 // problems with triggering the idle timeout on each update.
858 // Maybe we need to use a moving average for the wire latency
859 // instead of baseRTT.
860 if ((sentOffset == ackedOffset) &&
861 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
862
863#ifdef CONGESTION_DEBUG
864 if (congWindow > INITIAL_WINDOW)
865 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
866 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
867#endif
868
869 // Close congestion window and allow a transfer
870 // FIXME: Reset baseRTT like Linux Vegas?
871 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
872
873 return false;
874 }
875
876 offset = sock->outStream().length();
877
878 // FIXME: Should we compensate for non-update data?
879 // (i.e. use sentOffset instead of offset)
880 if ((offset - ackedOffset) < congWindow)
881 return false;
882
883 // If we just have one outstanding "ping", that means the client has
884 // started receiving our update. In order to not regress compared to
885 // before we had congestion avoidance, we allow another update here.
886 // This could further clog up the tubes, but congestion control isn't
887 // really working properly right now anyway as the wire would otherwise
888 // be idle for at least RTT/2.
889 if (pingCounter == 1)
890 return false;
891
892 return true;
893}
894
895
896void VNCSConnectionST::updateCongestion()
897{
898 unsigned diff;
899
900 if (!seenCongestion)
901 return;
902
903 diff = minRTT - baseRTT;
904
905 if (diff > __rfbmin(100, baseRTT)) {
906 // Way too fast
907 congWindow = congWindow * baseRTT / minRTT;
908 } else if (diff > __rfbmin(50, baseRTT/2)) {
909 // Slightly too fast
910 congWindow -= 4096;
911 } else if (diff < 5) {
912 // Way too slow
913 congWindow += 8192;
914 } else if (diff < 25) {
915 // Too slow
916 congWindow += 4096;
917 }
918
919 if (congWindow < MINIMUM_WINDOW)
920 congWindow = MINIMUM_WINDOW;
921 if (congWindow > MAXIMUM_WINDOW)
922 congWindow = MAXIMUM_WINDOW;
923
924#ifdef CONGESTION_DEBUG
925 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
926 minRTT, baseRTT, congWindow / 1024,
927 congWindow * 8.0 / baseRTT / 1000.0);
928
929#ifdef TCP_INFO
930 struct tcp_info tcp_info;
931 socklen_t tcp_info_length;
932
933 tcp_info_length = sizeof(tcp_info);
934 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
935 (void *)&tcp_info, &tcp_info_length) == 0) {
936 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
937 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
938 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
939 }
940#endif
941
942#endif
943
944 minRTT = -1;
945 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000946}
947
948
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000949void VNCSConnectionST::writeFramebufferUpdate()
950{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000951 Region req;
952 UpdateInfo ui;
953 bool needNewUpdateInfo;
954
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000955 updateTimer.stop();
956
Pierre Ossman2c764942011-11-14 15:54:30 +0000957 // We're in the middle of processing a command that's supposed to be
958 // synchronised. Allowing an update to slip out right now might violate
959 // that synchronisation.
960 if (syncFence)
961 return;
962
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000963 // We try to aggregate responses, so don't send out anything whilst we
964 // still have incoming messages. processMessages() will give us another
965 // chance to run once things are idle.
966 if (inProcessMessages)
967 return;
968
Pierre Ossman1b478e52011-11-15 12:08:30 +0000969 if (state() != RFBSTATE_NORMAL)
970 return;
971 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000972 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000973
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000974 // Check that we actually have some space on the link and retry in a
975 // bit if things are congested.
976 if (isCongested()) {
977 updateTimer.start(50);
978 return;
979 }
980
Pierre Ossman36dadf82011-11-15 12:11:32 +0000981 // In continuous mode, we will be outputting at least three distinct
982 // messages. We need to aggregate these in order to not clog up TCP's
983 // congestion window.
984 network::TcpSocket::cork(sock->getFd(), true);
985
Pierre Ossmane9962f72009-04-23 12:31:42 +0000986 // First take care of any updates that cannot contain framebuffer data
987 // changes.
988 if (writer()->needNoDataUpdate()) {
989 writer()->writeNoDataUpdate();
990 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000991 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000992 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000993 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000994
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000995 updates.enable_copyrect(cp.useCopyRect);
996
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000997 // Fetch updates from server object, and see if we are allowed to send
998 // anything right now (the framebuffer might have changed in ways we
999 // haven't yet been informed of).
1000 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +00001001 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001002
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001003 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001004 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +00001005 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001006
Pierre Ossman1b478e52011-11-15 12:08:30 +00001007 if (continuousUpdates)
1008 req = cuRegion.union_(requested);
1009 else
1010 req = requested;
1011
1012 updates.getUpdateInfo(&ui, req);
1013 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001014
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001015 // If the previous position of the rendered cursor overlaps the source of the
1016 // copy, then when the copy happens the corresponding rectangle in the
1017 // destination will be wrong, so add it to the changed region.
1018
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001019 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
1020 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001021 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001022 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001023 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001024 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001025 }
1026 }
1027
1028 // If we need to remove the old rendered cursor, just add the rectangle to
1029 // the changed region.
1030
1031 if (removeRenderedCursor) {
1032 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001033 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001034 renderedCursorRect.clear();
1035 removeRenderedCursor = false;
1036 }
1037
1038 // Return if there is nothing to send the client.
1039
1040 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001041 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001042
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001043 // The `updates' object could change, make sure we have valid update info.
1044
1045 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001046 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001047
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001048 // If the client needs a server-side rendered cursor, work out the cursor
1049 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1050 // with the update region, we need to draw the rendered cursor regardless of
1051 // whether it has changed.
1052
1053 if (needRenderedCursor()) {
1054 renderedCursorRect
1055 = (server->renderedCursor.getRect(server->renderedCursorTL)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001056 .intersect(req.get_bounding_rect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001057
1058 if (renderedCursorRect.is_empty()) {
1059 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001060 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001061 .intersect(renderedCursorRect).is_empty()) {
1062 drawRenderedCursor = true;
1063 }
1064
1065 // We could remove the new cursor rect from updates here. It's not clear
1066 // whether this is worth it. If we do remove it, then we won't draw over
1067 // the same bit of screen twice, but we have the overhead of a more complex
1068 // region.
1069
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001070 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001071 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001072 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001073 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001074 }
1075
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001076 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001077 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001078 // complicated as compared to the original VNC4.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001079 writer()->setupCurrentEncoder();
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001080 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001081 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +00001082
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001083 std::vector<Rect> rects;
1084 std::vector<Rect>::const_iterator i;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001085 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001086 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +00001087 if (i->width() && i->height()) {
1088 int nUpdateRects = writer()->getNumRects(*i);
1089 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
Pierre Ossman65fb4b02012-02-28 11:54:01 +00001090 // With Tight encoding and LastRect support, the client does not
1091 // care about the number of rectangles in the update - it will
1092 // stop parsing when it encounters a LastRect "rectangle".
1093 // In this case, pretend to send 65535 rectangles.
DRCcd2c5d42011-08-11 11:18:34 +00001094 nRects = 0xFFFF; break;
1095 }
1096 else
1097 nRects += nUpdateRects;
1098 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001099 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001100
1101 writeRTTPing();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001102
1103 writer()->writeFramebufferUpdateStart(nRects);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001104
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001105 Region updatedRegion;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001106 writer()->writeRects(ui, &image_getter, &updatedRegion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001107 updates.subtract(updatedRegion);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001109 if (drawRenderedCursor)
1110 writeRenderedCursorRect();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001111
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001112 writer()->writeFramebufferUpdateEnd();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001113
1114 writeRTTPing();
1115
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001116 requested.clear();
1117 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001118
1119out:
1120 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001121}
1122
1123
1124// writeRenderedCursorRect() writes a single rectangle drawing the rendered
1125// cursor on the client.
1126
1127void VNCSConnectionST::writeRenderedCursorRect()
1128{
1129 image_getter.setPixelBuffer(&server->renderedCursor);
1130 image_getter.setOffset(server->renderedCursorTL);
1131
1132 Rect actual;
1133 writer()->writeRect(renderedCursorRect, &image_getter, &actual);
1134
1135 image_getter.setPixelBuffer(server->pb);
1136 image_getter.setOffset(Point(0,0));
1137
1138 drawRenderedCursor = false;
1139}
1140
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001141void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1142{
1143 if (!authenticated())
1144 return;
1145
1146 cp.screenLayout = server->screenLayout;
1147
1148 if (state() != RFBSTATE_NORMAL)
1149 return;
1150
1151 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1152 cp.screenLayout);
1153 writeFramebufferUpdate();
1154}
1155
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001156void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours)
1157{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001158 if (!readyForSetColourMapEntries)
1159 return;
1160 if (server->pb->getPF().trueColour)
1161 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001162
Pierre Ossmana2739342011-03-08 16:53:07 +00001163 image_getter.setColourMapEntries(firstColour, nColours);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001164
1165 if (cp.pf().trueColour) {
1166 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001167 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001168 }
1169}
1170
1171
1172// setCursor() is called whenever the cursor has changed shape or pixel format.
1173// If the client supports local cursor then it will arrange for the cursor to
1174// be sent to the client.
1175
1176void VNCSConnectionST::setCursor()
1177{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001178 if (state() != RFBSTATE_NORMAL)
1179 return;
1180 if (!cp.supportsLocalCursor)
1181 return;
1182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001183 writer()->cursorChange(this);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001184 writeFramebufferUpdate();
1185}
1186
1187void VNCSConnectionST::setDesktopName(const char *name)
1188{
1189 cp.setName(name);
1190
1191 if (state() != RFBSTATE_NORMAL)
1192 return;
1193
1194 if (!writer()->writeSetDesktopName()) {
1195 fprintf(stderr, "Client does not support desktop rename\n");
1196 return;
1197 }
1198
1199 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001200}
1201
1202void VNCSConnectionST::setSocketTimeouts()
1203{
1204 int timeoutms = rfb::Server::clientWaitTimeMillis;
1205 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1206 if (timeoutms == 0)
1207 timeoutms = -1;
1208 sock->inStream().setTimeout(timeoutms);
1209 sock->outStream().setTimeout(timeoutms);
1210}
1211
1212char* VNCSConnectionST::getStartTime()
1213{
1214 char* result = ctime(&startTime);
1215 result[24] = '\0';
1216 return result;
1217}
1218
1219void VNCSConnectionST::setStatus(int status)
1220{
1221 switch (status) {
1222 case 0:
1223 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1224 break;
1225 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001226 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001227 break;
1228 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001229 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001230 break;
1231 }
1232 framebufferUpdateRequest(server->pb->getRect(), false);
1233}
1234int VNCSConnectionST::getStatus()
1235{
1236 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1237 return 0;
1238 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1239 return 1;
1240 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1241 return 2;
1242 return 4;
1243}
1244