blob: 1ecd78c21119cbbbdcf9007325a80d190f51b3c2 [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),
69 syncFence(false), fenceFlags(0), fenceDataLen(0), fenceData(NULL),
Pierre Ossman1b478e52011-11-15 12:08:30 +000070 baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
71 ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
Pierre Ossman2c764942011-11-14 15:54:30 +000072 server(server_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 updates(false), image_getter(server->useEconomicTranslate),
74 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman1b478e52011-11-15 12:08:30 +000075 continuousUpdates(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000076 updateTimer(this), pointerEventTime(0),
77 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078{
79 setStreams(&sock->inStream(), &sock->outStream());
80 peerEndpoint.buf = sock->getPeerEndpoint();
81 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
82
83 // Configure the socket
84 setSocketTimeouts();
85 lastEventTime = time(0);
86
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 server->clients.push_front(this);
88}
89
90
91VNCSConnectionST::~VNCSConnectionST()
92{
93 // If we reach here then VNCServerST is deleting us!
94 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
95 peerEndpoint.buf,
96 (closeReason.buf) ? closeReason.buf : "");
97
98 // Release any keys the client still had pressed
99 std::set<rdr::U32>::iterator i;
100 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
101 server->desktop->keyEvent(*i, false);
102 if (server->pointerClient == this)
103 server->pointerClient = 0;
104
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 // Remove this client from the server
106 server->clients.remove(this);
107
Pierre Ossman2c764942011-11-14 15:54:30 +0000108 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109}
110
111
112// Methods called from VNCServerST
113
114bool VNCSConnectionST::init()
115{
116 try {
117 initialiseProtocol();
118 } catch (rdr::Exception& e) {
119 close(e.str());
120 return false;
121 }
122 return true;
123}
124
125void VNCSConnectionST::close(const char* reason)
126{
127 // Log the reason for the close
128 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000129 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 else
131 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
132
133 if (authenticated()) {
134 server->lastDisconnectTime = time(0);
135 }
136
137 // Just shutdown the socket and mark our state as closing. Eventually the
138 // calling code will call VNCServerST's removeSocket() method causing us to
139 // be deleted.
140 sock->shutdown();
141 setState(RFBSTATE_CLOSING);
142}
143
144
145void VNCSConnectionST::processMessages()
146{
147 if (state() == RFBSTATE_CLOSING) return;
148 try {
149 // - Now set appropriate socket timeouts and process data
150 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000151
152 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153
Pierre Ossmana830bec2011-11-08 12:12:02 +0000154 // Get the underlying TCP layer to build large packets if we send
155 // multiple small responses.
156 network::TcpSocket::cork(sock->getFd(), true);
157
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 while (getInStream()->checkNoWait(1)) {
159 processMsg();
Pierre Ossman2c764942011-11-14 15:54:30 +0000160 if (syncFence) {
161 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
162 syncFence = false;
163 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 }
165
Pierre Ossmana830bec2011-11-08 12:12:02 +0000166 // Flush out everything in case we go idle after this.
167 network::TcpSocket::cork(sock->getFd(), false);
168
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000169 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000170
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000171 // If there were anything requiring an update, try to send it here.
172 // We wait until now with this to aggregate responses and to give
173 // higher priority to user actions such as keyboard and pointer events.
174 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 } catch (rdr::EndOfStream&) {
176 close("Clean disconnection");
177 } catch (rdr::Exception &e) {
178 close(e.str());
179 }
180}
181
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182void VNCSConnectionST::pixelBufferChange()
183{
184 try {
185 if (!authenticated()) return;
186 if (cp.width && cp.height && (server->pb->width() != cp.width ||
187 server->pb->height() != cp.height))
188 {
189 // We need to clip the next update to the new size, but also add any
190 // extra bits if it's bigger. If we wanted to do this exactly, something
191 // like the code below would do it, but at the moment we just update the
192 // entire new size. However, we do need to clip the renderedCursorRect
193 // because that might be added to updates in writeFramebufferUpdate().
194
195 //updates.intersect(server->pb->getRect());
196 //
197 //if (server->pb->width() > cp.width)
198 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
199 // server->pb->height()));
200 //if (server->pb->height() > cp.height)
201 // updates.add_changed(Rect(0, cp.height, cp.width,
202 // server->pb->height()));
203
204 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
205
206 cp.width = server->pb->width();
207 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000208 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000210 // We should only send EDS to client asking for both
211 if (!writer()->writeExtendedDesktopSize()) {
212 if (!writer()->writeSetDesktopSize()) {
213 close("Client does not support desktop resize");
214 return;
215 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 }
217 }
218 }
219 // Just update the whole screen at the moment because we're too lazy to
220 // work out what's actually changed.
221 updates.clear();
222 updates.add_changed(server->pb->getRect());
223 vlog.debug("pixel buffer changed - re-initialising image getter");
224 image_getter.init(server->pb, cp.pf(), writer());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000225 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 } catch(rdr::Exception &e) {
227 close(e.str());
228 }
229}
230
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000231void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000232{
233 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000234 writeFramebufferUpdate();
235 } catch(rdr::Exception &e) {
236 close(e.str());
237 }
238}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000239
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000240void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
241{
242 try {
243 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000244 } catch(rdr::Exception &e) {
245 close(e.str());
246 }
247}
248
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours)
250{
251 try {
252 setColourMapEntries(firstColour, nColours);
253 } catch(rdr::Exception& e) {
254 close(e.str());
255 }
256}
257
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000258void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000259{
260 try {
261 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
262 } catch(rdr::Exception& e) {
263 close(e.str());
264 }
265}
266
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000267void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268{
269 try {
270 if (!(accessRights & AccessCutText)) return;
271 if (!rfb::Server::sendCutText) return;
272 if (state() == RFBSTATE_NORMAL)
273 writer()->writeServerCutText(str, len);
274 } catch(rdr::Exception& e) {
275 close(e.str());
276 }
277}
278
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000279
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000280void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000281{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000282 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000283 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000284 } catch(rdr::Exception& e) {
285 close(e.str());
286 }
287}
288
289
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000290void VNCSConnectionST::setCursorOrClose()
291{
292 try {
293 setCursor();
294 } catch(rdr::Exception& e) {
295 close(e.str());
296 }
297}
298
299
300int VNCSConnectionST::checkIdleTimeout()
301{
302 int idleTimeout = rfb::Server::idleTimeout;
303 if (idleTimeout == 0) return 0;
304 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
305 idleTimeout = 15; // minimum of 15 seconds while authenticating
306 time_t now = time(0);
307 if (now < lastEventTime) {
308 // Someone must have set the time backwards. Set lastEventTime so that the
309 // idleTimeout will count from now.
310 vlog.info("Time has gone backwards - resetting idle timeout");
311 lastEventTime = now;
312 }
313 int timeLeft = lastEventTime + idleTimeout - now;
314 if (timeLeft < -60) {
315 // Our callback is over a minute late - someone must have set the time
316 // forwards. Set lastEventTime so that the idleTimeout will count from
317 // now.
318 vlog.info("Time has gone forwards - resetting idle timeout");
319 lastEventTime = now;
320 return secsToMillis(idleTimeout);
321 }
322 if (timeLeft <= 0) {
323 close("Idle timeout");
324 return 0;
325 }
326 return secsToMillis(timeLeft);
327}
328
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000329
330bool VNCSConnectionST::getComparerState()
331{
332 // We interpret a low compression level as an indication that the client
333 // wants to prioritise CPU usage over bandwidth, and hence disable the
334 // comparing update tracker.
335 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
336}
337
338
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000339// renderedCursorChange() is called whenever the server-side rendered cursor
340// changes shape or position. It ensures that the next update will clean up
341// the old rendered cursor and if necessary draw the new rendered cursor.
342
343void VNCSConnectionST::renderedCursorChange()
344{
345 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000346 if (!renderedCursorRect.is_empty())
347 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000348 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000349 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000350 writeFramebufferUpdateOrClose();
351 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000352}
353
354// needRenderedCursor() returns true if this client needs the server-side
355// rendered cursor. This may be because it does not support local cursor or
356// because the current cursor position has not been set by this client.
357// Unfortunately we can't know for sure when the current cursor position has
358// been set by this client. We guess that this is the case when the current
359// cursor position is the same as the last pointer event from this client, or
360// if it is a very short time since this client's last pointer event (up to a
361// second). [ Ideally we should do finer-grained timing here and make the time
362// configurable, but I don't think it's that important. ]
363
364bool VNCSConnectionST::needRenderedCursor()
365{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000366 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000367 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000368 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000369}
370
371
372void VNCSConnectionST::approveConnectionOrClose(bool accept,
373 const char* reason)
374{
375 try {
376 approveConnection(accept, reason);
377 } catch (rdr::Exception& e) {
378 close(e.str());
379 }
380}
381
382
383
384// -=- Callbacks from SConnection
385
386void VNCSConnectionST::authSuccess()
387{
388 lastEventTime = time(0);
389
390 server->startDesktop();
391
392 // - Set the connection parameters appropriately
393 cp.width = server->pb->width();
394 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000395 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000396 cp.setName(server->getName());
397
398 // - Set the default pixel format
399 cp.setPF(server->pb->getPF());
400 char buffer[256];
401 cp.pf().print(buffer, 256);
402 vlog.info("Server default pixel format %s", buffer);
403 image_getter.init(server->pb, cp.pf(), 0);
404
405 // - Mark the entire display as "dirty"
406 updates.add_changed(server->pb->getRect());
407 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000408
409 // - Bootstrap the congestion control
410 ackedOffset = sock->outStream().length();
411 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412}
413
414void VNCSConnectionST::queryConnection(const char* userName)
415{
416 // - Authentication succeeded - clear from blacklist
417 CharArray name; name.buf = sock->getPeerAddress();
418 server->blHosts->clearBlackmark(name.buf);
419
420 // - Special case to provide a more useful error message
421 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
422 server->authClientCount() > 0) {
423 approveConnection(false, "The server is already in use");
424 return;
425 }
426
427 // - Does the client have the right to bypass the query?
428 if (reverseConnection ||
429 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
430 (accessRights & AccessNoQuery))
431 {
432 approveConnection(true);
433 return;
434 }
435
436 // - Get the server to display an Accept/Reject dialog, if required
437 // If a dialog is displayed, the result will be PENDING, and the
438 // server will call approveConnection at a later time
439 CharArray reason;
440 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
441 &reason.buf);
442 if (qr == VNCServerST::PENDING)
443 return;
444
445 // - If server returns ACCEPT/REJECT then pass result to SConnection
446 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
447}
448
449void VNCSConnectionST::clientInit(bool shared)
450{
451 lastEventTime = time(0);
452 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
453 if (rfb::Server::neverShared) shared = false;
454 if (!shared) {
455 if (rfb::Server::disconnectClients) {
456 // - Close all the other connected clients
457 vlog.debug("non-shared connection - closing clients");
458 server->closeClients("Non-shared connection requested", getSock());
459 } else {
460 // - Refuse this connection if there are existing clients, in addition to
461 // this one
462 if (server->authClientCount() > 1) {
463 close("Server is already in use");
464 return;
465 }
466 }
467 }
468 SConnection::clientInit(shared);
469}
470
471void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
472{
473 SConnection::setPixelFormat(pf);
474 char buffer[256];
475 pf.print(buffer, 256);
476 vlog.info("Client pixel format %s", buffer);
477 image_getter.init(server->pb, pf, writer());
478 setCursor();
479}
480
481void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
482{
483 pointerEventTime = lastEventTime = time(0);
484 server->lastUserInputTime = lastEventTime;
485 if (!(accessRights & AccessPtrEvents)) return;
486 if (!rfb::Server::acceptPointerEvents) return;
487 if (!server->pointerClient || server->pointerClient == this) {
488 pointerEventPos = pos;
489 if (buttonMask)
490 server->pointerClient = this;
491 else
492 server->pointerClient = 0;
493 server->desktop->pointerEvent(pointerEventPos, buttonMask);
494 }
495}
496
497
498class VNCSConnectionSTShiftPresser {
499public:
500 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
501 : desktop(desktop_), pressed(false) {}
502 ~VNCSConnectionSTShiftPresser() {
503 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
504 }
505 void press() {
506 desktop->keyEvent(XK_Shift_L, true);
507 pressed = true;
508 }
509 SDesktop* desktop;
510 bool pressed;
511};
512
513// keyEvent() - record in the pressedKeys which keys were pressed. Allow
514// multiple down events (for autorepeat), but only allow a single up event.
515void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
516 lastEventTime = time(0);
517 server->lastUserInputTime = lastEventTime;
518 if (!(accessRights & AccessKeyEvents)) return;
519 if (!rfb::Server::acceptKeyEvents) return;
520
521 // Remap the key if required
522 if (server->keyRemapper)
523 key = server->keyRemapper->remapKey(key);
524
525 // Turn ISO_Left_Tab into shifted Tab.
526 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
527 if (key == XK_ISO_Left_Tab) {
528 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
529 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
530 shiftPresser.press();
531 key = XK_Tab;
532 }
533
534 if (down) {
535 pressedKeys.insert(key);
536 } else {
537 if (!pressedKeys.erase(key)) return;
538 }
539 server->desktop->keyEvent(key, down);
540}
541
542void VNCSConnectionST::clientCutText(const char* str, int len)
543{
544 if (!(accessRights & AccessCutText)) return;
545 if (!rfb::Server::acceptCutText) return;
546 server->desktop->clientCutText(str, len);
547}
548
549void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
550{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000551 Rect safeRect;
552
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000553 if (!(accessRights & AccessView)) return;
554
555 SConnection::framebufferUpdateRequest(r, incremental);
556
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000557 // Check that the client isn't sending crappy requests
558 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
559 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
560 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000561 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
562 } else {
563 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000564 }
565
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000566 // Just update the requested region.
567 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000568 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000569 if (!incremental || !continuousUpdates)
570 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000571
572 if (!incremental) {
573 // Non-incremental update - treat as if area requested has changed
574 updates.add_changed(reqRgn);
575 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000576
577 // And send the screen layout to the client (which, unlike the
578 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000579 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000580
581 // We do not send a DesktopSize since it only contains the
582 // framebuffer size (which the client already should know) and
583 // because some clients don't handle extra DesktopSize events
584 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000585 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000586}
587
Pierre Ossman34bb0612009-03-21 21:16:14 +0000588void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
589 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000590{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000591 unsigned int result;
592
593 // Don't bother the desktop with an invalid configuration
594 if (!layout.validate(fb_width, fb_height)) {
595 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
596 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000597 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000598 return;
599 }
600
601 // FIXME: the desktop will call back to VNCServerST and an extra set
602 // of ExtendedDesktopSize messages will be sent. This is okay
603 // protocol-wise, but unnecessary.
604 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
605
Pierre Ossman04e62db2009-03-23 16:57:07 +0000606 writer()->writeExtendedDesktopSize(reasonClient, result,
607 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000608
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000609 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000610 if (result == resultSuccess) {
611 if (server->screenLayout != layout)
612 throw Exception("Desktop configured a different screen layout than requested");
613 server->notifyScreenLayoutChange(this);
614 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000615
616 // but always send back a reply to the requesting client
617 // (do this last as it might throw an exception on socket errors)
618 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000619}
620
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000621void VNCSConnectionST::setInitialColourMap()
622{
623 setColourMapEntries(0, 0);
624}
625
Pierre Ossman2c764942011-11-14 15:54:30 +0000626void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
627{
628 if (flags & fenceFlagRequest) {
629 if (flags & fenceFlagSyncNext) {
630 if (syncFence)
631 vlog.error("Fence trying to synchronise another fence");
632
633 syncFence = true;
634
635 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
636 fenceDataLen = len;
637 delete [] fenceData;
638 if (len > 0) {
639 fenceData = new char[len];
640 memcpy(fenceData, data, len);
641 }
642
643 return;
644 }
645
646 // We handle everything synchronously so we trivially honor these modes
647 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
648
649 writer()->writeFence(flags, len, data);
650 return;
651 }
652
Pierre Ossman1b478e52011-11-15 12:08:30 +0000653 struct RTTInfo rttInfo;
654
Pierre Ossman2c764942011-11-14 15:54:30 +0000655 switch (len) {
656 case 0:
657 // Initial dummy fence;
658 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000659 case sizeof(struct RTTInfo):
660 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
661 handleRTTPong(rttInfo);
662 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000663 default:
664 vlog.error("Fence response of unexpected size received");
665 }
666}
667
Pierre Ossman1b478e52011-11-15 12:08:30 +0000668void VNCSConnectionST::enableContinuousUpdates(bool enable,
669 int x, int y, int w, int h)
670{
671 Rect rect;
672
673 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
674 throw Exception("Client tried to enable continuous updates when not allowed");
675
676 continuousUpdates = enable;
677
678 rect.setXYWH(x, y, w, h);
679 cuRegion.reset(rect);
680
681 if (enable) {
682 requested.clear();
683 writeFramebufferUpdate();
684 } else {
685 writer()->writeEndOfContinuousUpdates();
686 }
687}
688
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000689// supportsLocalCursor() is called whenever the status of
690// cp.supportsLocalCursor has changed. If the client does now support local
691// cursor, we make sure that the old server-side rendered cursor is cleaned up
692// and the cursor is sent to the client.
693
694void VNCSConnectionST::supportsLocalCursor()
695{
696 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000697 if (!renderedCursorRect.is_empty())
698 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000699 drawRenderedCursor = false;
700 setCursor();
701 }
702}
703
Pierre Ossman2c764942011-11-14 15:54:30 +0000704void VNCSConnectionST::supportsFence()
705{
706 writer()->writeFence(fenceFlagRequest, 0, NULL);
707}
708
Pierre Ossman1b478e52011-11-15 12:08:30 +0000709void VNCSConnectionST::supportsContinuousUpdates()
710{
711 // We refuse to use continuous updates if we cannot monitor the buffer
712 // usage using fences.
713 if (!cp.supportsFence)
714 return;
715
716 writer()->writeEndOfContinuousUpdates();
717}
718
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000719void VNCSConnectionST::writeSetCursorCallback()
720{
721 if (cp.supportsLocalXCursor) {
722 Pixel pix0, pix1;
723 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
724 if (bitmap.buf) {
725 // The client supports XCursor and the cursor only has two
726 // colors. Use the XCursor encoding.
727 writer()->writeSetXCursor(server->cursor.width(),
728 server->cursor.height(),
729 server->cursor.hotspot.x,
730 server->cursor.hotspot.y,
731 bitmap.buf, server->cursor.mask.buf);
732 return;
733 } else {
734 // More than two colors
735 if (!cp.supportsLocalCursor) {
736 // FIXME: We could reduce to two colors.
737 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
738 return;
739 }
740 }
741 }
742
743 // Use RichCursor
744 rdr::U8* transData = writer()->getImageBuf(server->cursor.area());
745 image_getter.translatePixels(server->cursor.data, transData,
746 server->cursor.area());
747 writer()->writeSetCursor(server->cursor.width(),
748 server->cursor.height(),
749 server->cursor.hotspot,
750 transData, server->cursor.mask.buf);
751}
752
753
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000754bool VNCSConnectionST::handleTimeout(Timer* t)
755{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000756 try {
757 if (t == &updateTimer)
758 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000759 else if (t == &congestionTimer)
760 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000761 } catch (rdr::Exception& e) {
762 close(e.str());
763 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000764
765 return false;
766}
767
768
Pierre Ossman1b478e52011-11-15 12:08:30 +0000769void VNCSConnectionST::writeRTTPing()
770{
771 struct RTTInfo rttInfo;
772
773 if (!cp.supportsFence)
774 return;
775
776 memset(&rttInfo, 0, sizeof(struct RTTInfo));
777
778 gettimeofday(&rttInfo.tv, NULL);
779 rttInfo.offset = sock->outStream().length();
780 rttInfo.inFlight = rttInfo.offset - ackedOffset;
781
782 // We need to make sure any old update are already processed by the
783 // time we get the response back. This allows us to reliably throttle
784 // back on client overload, as well as network overload.
785 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
786 sizeof(struct RTTInfo), (const char*)&rttInfo);
787
788 pingCounter++;
789
790 sentOffset = rttInfo.offset;
791
792 // Let some data flow before we adjust the settings
793 if (!congestionTimer.isStarted())
794 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
795}
796
797void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
798{
799 unsigned rtt, delay;
800 int bdp;
801
802 pingCounter--;
803
804 rtt = msSince(&rttInfo.tv);
805 if (rtt < 1)
806 rtt = 1;
807
808 ackedOffset = rttInfo.offset;
809
810 // Try to estimate wire latency by tracking lowest seen latency
811 if (rtt < baseRTT)
812 baseRTT = rtt;
813
814 if (rttInfo.inFlight > congWindow) {
815 seenCongestion = true;
816
817 // Estimate added delay because of overtaxed buffers
818 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
819
820 if (delay < rtt)
821 rtt -= delay;
822 else
823 rtt = 1;
824
825 // If we underestimate the congestion window, then we'll get a latency
826 // that's less than the wire latency, which will confuse other portions
827 // of the code.
828 if (rtt < baseRTT)
829 rtt = baseRTT;
830 }
831
832 // We only keep track of the minimum latency seen (for a given interval)
833 // on the basis that we want to avoid continous buffer issue, but don't
834 // mind (or even approve of) bursts.
835 if (rtt < minRTT)
836 minRTT = rtt;
837}
838
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000839bool VNCSConnectionST::isCongested()
840{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000841 int offset;
842
843 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000844 if (sock->outStream().bufferUsage() > 0)
845 return true;
846
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847 if (!cp.supportsFence)
848 return false;
849
850 // Idle for too long? (and no data on the wire)
851 //
852 // FIXME: This should really just be one baseRTT, but we're getting
853 // problems with triggering the idle timeout on each update.
854 // Maybe we need to use a moving average for the wire latency
855 // instead of baseRTT.
856 if ((sentOffset == ackedOffset) &&
857 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
858
859#ifdef CONGESTION_DEBUG
860 if (congWindow > INITIAL_WINDOW)
861 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
862 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
863#endif
864
865 // Close congestion window and allow a transfer
866 // FIXME: Reset baseRTT like Linux Vegas?
867 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
868
869 return false;
870 }
871
872 offset = sock->outStream().length();
873
874 // FIXME: Should we compensate for non-update data?
875 // (i.e. use sentOffset instead of offset)
876 if ((offset - ackedOffset) < congWindow)
877 return false;
878
879 // If we just have one outstanding "ping", that means the client has
880 // started receiving our update. In order to not regress compared to
881 // before we had congestion avoidance, we allow another update here.
882 // This could further clog up the tubes, but congestion control isn't
883 // really working properly right now anyway as the wire would otherwise
884 // be idle for at least RTT/2.
885 if (pingCounter == 1)
886 return false;
887
888 return true;
889}
890
891
892void VNCSConnectionST::updateCongestion()
893{
894 unsigned diff;
895
896 if (!seenCongestion)
897 return;
898
899 diff = minRTT - baseRTT;
900
901 if (diff > __rfbmin(100, baseRTT)) {
902 // Way too fast
903 congWindow = congWindow * baseRTT / minRTT;
904 } else if (diff > __rfbmin(50, baseRTT/2)) {
905 // Slightly too fast
906 congWindow -= 4096;
907 } else if (diff < 5) {
908 // Way too slow
909 congWindow += 8192;
910 } else if (diff < 25) {
911 // Too slow
912 congWindow += 4096;
913 }
914
915 if (congWindow < MINIMUM_WINDOW)
916 congWindow = MINIMUM_WINDOW;
917 if (congWindow > MAXIMUM_WINDOW)
918 congWindow = MAXIMUM_WINDOW;
919
920#ifdef CONGESTION_DEBUG
921 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
922 minRTT, baseRTT, congWindow / 1024,
923 congWindow * 8.0 / baseRTT / 1000.0);
924
925#ifdef TCP_INFO
926 struct tcp_info tcp_info;
927 socklen_t tcp_info_length;
928
929 tcp_info_length = sizeof(tcp_info);
930 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
931 (void *)&tcp_info, &tcp_info_length) == 0) {
932 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
933 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
934 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
935 }
936#endif
937
938#endif
939
940 minRTT = -1;
941 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000942}
943
944
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000945void VNCSConnectionST::writeFramebufferUpdate()
946{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000947 Region req;
948 UpdateInfo ui;
949 bool needNewUpdateInfo;
950
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000951 updateTimer.stop();
952
Pierre Ossman2c764942011-11-14 15:54:30 +0000953 // We're in the middle of processing a command that's supposed to be
954 // synchronised. Allowing an update to slip out right now might violate
955 // that synchronisation.
956 if (syncFence)
957 return;
958
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000959 // We try to aggregate responses, so don't send out anything whilst we
960 // still have incoming messages. processMessages() will give us another
961 // chance to run once things are idle.
962 if (inProcessMessages)
963 return;
964
Pierre Ossman1b478e52011-11-15 12:08:30 +0000965 if (state() != RFBSTATE_NORMAL)
966 return;
967 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000968 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000969
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000970 // Check that we actually have some space on the link and retry in a
971 // bit if things are congested.
972 if (isCongested()) {
973 updateTimer.start(50);
974 return;
975 }
976
Pierre Ossman36dadf82011-11-15 12:11:32 +0000977 // In continuous mode, we will be outputting at least three distinct
978 // messages. We need to aggregate these in order to not clog up TCP's
979 // congestion window.
980 network::TcpSocket::cork(sock->getFd(), true);
981
Pierre Ossmane9962f72009-04-23 12:31:42 +0000982 // First take care of any updates that cannot contain framebuffer data
983 // changes.
984 if (writer()->needNoDataUpdate()) {
985 writer()->writeNoDataUpdate();
986 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000987 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000988 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000989 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000990
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000991 updates.enable_copyrect(cp.useCopyRect);
992
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000993 // Fetch updates from server object, and see if we are allowed to send
994 // anything right now (the framebuffer might have changed in ways we
995 // haven't yet been informed of).
996 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000997 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000998
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000999 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001000 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +00001001 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001002
Pierre Ossman1b478e52011-11-15 12:08:30 +00001003 if (continuousUpdates)
1004 req = cuRegion.union_(requested);
1005 else
1006 req = requested;
1007
1008 updates.getUpdateInfo(&ui, req);
1009 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001010
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001011 // If the previous position of the rendered cursor overlaps the source of the
1012 // copy, then when the copy happens the corresponding rectangle in the
1013 // destination will be wrong, so add it to the changed region.
1014
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001015 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
1016 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001017 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001018 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001019 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001020 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001021 }
1022 }
1023
1024 // If we need to remove the old rendered cursor, just add the rectangle to
1025 // the changed region.
1026
1027 if (removeRenderedCursor) {
1028 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001029 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001030 renderedCursorRect.clear();
1031 removeRenderedCursor = false;
1032 }
1033
1034 // Return if there is nothing to send the client.
1035
1036 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001037 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001038
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001039 // The `updates' object could change, make sure we have valid update info.
1040
1041 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001042 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001043
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001044 // If the client needs a server-side rendered cursor, work out the cursor
1045 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1046 // with the update region, we need to draw the rendered cursor regardless of
1047 // whether it has changed.
1048
1049 if (needRenderedCursor()) {
1050 renderedCursorRect
1051 = (server->renderedCursor.getRect(server->renderedCursorTL)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001052 .intersect(req.get_bounding_rect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001053
1054 if (renderedCursorRect.is_empty()) {
1055 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001056 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001057 .intersect(renderedCursorRect).is_empty()) {
1058 drawRenderedCursor = true;
1059 }
1060
1061 // We could remove the new cursor rect from updates here. It's not clear
1062 // whether this is worth it. If we do remove it, then we won't draw over
1063 // the same bit of screen twice, but we have the overhead of a more complex
1064 // region.
1065
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001066 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001067 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001068 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001069 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001070 }
1071
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001072 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001073 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001074 // complicated as compared to the original VNC4.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001075 writer()->setupCurrentEncoder();
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001076 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +00001077 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +00001078
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001079 std::vector<Rect> rects;
1080 std::vector<Rect>::const_iterator i;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001081 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001082 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +00001083 if (i->width() && i->height()) {
1084 int nUpdateRects = writer()->getNumRects(*i);
1085 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
Pierre Ossman65fb4b02012-02-28 11:54:01 +00001086 // With Tight encoding and LastRect support, the client does not
1087 // care about the number of rectangles in the update - it will
1088 // stop parsing when it encounters a LastRect "rectangle".
1089 // In this case, pretend to send 65535 rectangles.
DRCcd2c5d42011-08-11 11:18:34 +00001090 nRects = 0xFFFF; break;
1091 }
1092 else
1093 nRects += nUpdateRects;
1094 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001095 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001096
1097 writeRTTPing();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001098
1099 writer()->writeFramebufferUpdateStart(nRects);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001101 Region updatedRegion;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001102 writer()->writeRects(ui, &image_getter, &updatedRegion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001103 updates.subtract(updatedRegion);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001104
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001105 if (drawRenderedCursor)
1106 writeRenderedCursorRect();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001107
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001108 writer()->writeFramebufferUpdateEnd();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001109
1110 writeRTTPing();
1111
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001112 requested.clear();
1113 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001114
1115out:
1116 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001117}
1118
1119
1120// writeRenderedCursorRect() writes a single rectangle drawing the rendered
1121// cursor on the client.
1122
1123void VNCSConnectionST::writeRenderedCursorRect()
1124{
1125 image_getter.setPixelBuffer(&server->renderedCursor);
1126 image_getter.setOffset(server->renderedCursorTL);
1127
1128 Rect actual;
1129 writer()->writeRect(renderedCursorRect, &image_getter, &actual);
1130
1131 image_getter.setPixelBuffer(server->pb);
1132 image_getter.setOffset(Point(0,0));
1133
1134 drawRenderedCursor = false;
1135}
1136
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001137void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1138{
1139 if (!authenticated())
1140 return;
1141
1142 cp.screenLayout = server->screenLayout;
1143
1144 if (state() != RFBSTATE_NORMAL)
1145 return;
1146
1147 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1148 cp.screenLayout);
1149 writeFramebufferUpdate();
1150}
1151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001152void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours)
1153{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001154 if (!readyForSetColourMapEntries)
1155 return;
1156 if (server->pb->getPF().trueColour)
1157 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001158
Pierre Ossmana2739342011-03-08 16:53:07 +00001159 image_getter.setColourMapEntries(firstColour, nColours);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001160
1161 if (cp.pf().trueColour) {
1162 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001163 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001164 }
1165}
1166
1167
1168// setCursor() is called whenever the cursor has changed shape or pixel format.
1169// If the client supports local cursor then it will arrange for the cursor to
1170// be sent to the client.
1171
1172void VNCSConnectionST::setCursor()
1173{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001174 if (state() != RFBSTATE_NORMAL)
1175 return;
1176 if (!cp.supportsLocalCursor)
1177 return;
1178
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001179 writer()->cursorChange(this);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001180 writeFramebufferUpdate();
1181}
1182
1183void VNCSConnectionST::setDesktopName(const char *name)
1184{
1185 cp.setName(name);
1186
1187 if (state() != RFBSTATE_NORMAL)
1188 return;
1189
1190 if (!writer()->writeSetDesktopName()) {
1191 fprintf(stderr, "Client does not support desktop rename\n");
1192 return;
1193 }
1194
1195 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001196}
1197
1198void VNCSConnectionST::setSocketTimeouts()
1199{
1200 int timeoutms = rfb::Server::clientWaitTimeMillis;
1201 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1202 if (timeoutms == 0)
1203 timeoutms = -1;
1204 sock->inStream().setTimeout(timeoutms);
1205 sock->outStream().setTimeout(timeoutms);
1206}
1207
1208char* VNCSConnectionST::getStartTime()
1209{
1210 char* result = ctime(&startTime);
1211 result[24] = '\0';
1212 return result;
1213}
1214
1215void VNCSConnectionST::setStatus(int status)
1216{
1217 switch (status) {
1218 case 0:
1219 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1220 break;
1221 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001222 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001223 break;
1224 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001225 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001226 break;
1227 }
1228 framebufferUpdateRequest(server->pb->getRect(), false);
1229}
1230int VNCSConnectionST::getStatus()
1231{
1232 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1233 return 0;
1234 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1235 return 1;
1236 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1237 return 2;
1238 return 4;
1239}
1240