blob: e30b4f429d0b7038e21923b7f443f1cf247abb0d [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +01002 * Copyright 2009-2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Pierre Ossman1b478e52011-11-15 12:08:30 +000020// Debug output on what the congestion control is up to
21#undef CONGESTION_DEBUG
22
23#include <sys/time.h>
24
25#ifdef CONGESTION_DEBUG
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#endif
30
Pierre Ossmana830bec2011-11-08 12:12:02 +000031#include <network/TcpSocket.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032#include <rfb/VNCSConnectionST.h>
33#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000034#include <rfb/Security.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000035#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000036#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <rfb/ServerCore.h>
38#include <rfb/ComparingUpdateTracker.h>
39#include <rfb/KeyRemapper.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010040#include <rfb/Encoder.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041#define XK_MISCELLANY
42#define XK_XKB_KEYS
43#include <rfb/keysymdef.h>
44
45using namespace rfb;
46
47static LogWriter vlog("VNCSConnST");
48
Pierre Ossman1b478e52011-11-15 12:08:30 +000049// This window should get us going fairly fast on a decent bandwidth network.
50// If it's too high, it will rapidly be reduced and stay low.
51static const unsigned INITIAL_WINDOW = 16384;
52
53// TCP's minimal window is 3*MSS. But since we don't know the MSS, we
54// make a guess at 4 KiB (it's probaly a bit higher).
55static const unsigned MINIMUM_WINDOW = 4096;
56
57// The current default maximum window for Linux (4 MiB). Should be a good
58// limit for now...
59static const unsigned MAXIMUM_WINDOW = 4194304;
60
61struct RTTInfo {
62 struct timeval tv;
63 int offset;
64 unsigned inFlight;
65};
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
68 bool reverse)
Pierre Ossman2c764942011-11-14 15:54:30 +000069 : SConnection(reverse), sock(s), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000070 pendingSyncFence(false), syncFence(false), fenceFlags(0),
71 fenceDataLen(0), fenceData(NULL),
Pierre Ossman1b478e52011-11-15 12:08:30 +000072 baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
73 ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020074 server(server_), updates(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmanc0397262014-03-14 15:59:46 +010076 continuousUpdates(false), encodeManager(this),
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());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000230 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000231 } catch(rdr::Exception &e) {
232 close(e.str());
233 }
234}
235
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000236void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000237{
238 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000239 writeFramebufferUpdate();
240 } catch(rdr::Exception &e) {
241 close(e.str());
242 }
243}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000244
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000245void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
246{
247 try {
248 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000249 } catch(rdr::Exception &e) {
250 close(e.str());
251 }
252}
253
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000254void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000255{
256 try {
257 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
258 } catch(rdr::Exception& e) {
259 close(e.str());
260 }
261}
262
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000263void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000264{
265 try {
266 if (!(accessRights & AccessCutText)) return;
267 if (!rfb::Server::sendCutText) return;
268 if (state() == RFBSTATE_NORMAL)
269 writer()->writeServerCutText(str, len);
270 } catch(rdr::Exception& e) {
271 close(e.str());
272 }
273}
274
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000275
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000276void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000277{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000278 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000279 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000280 } catch(rdr::Exception& e) {
281 close(e.str());
282 }
283}
284
285
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000286void VNCSConnectionST::setCursorOrClose()
287{
288 try {
289 setCursor();
290 } catch(rdr::Exception& e) {
291 close(e.str());
292 }
293}
294
295
296int VNCSConnectionST::checkIdleTimeout()
297{
298 int idleTimeout = rfb::Server::idleTimeout;
299 if (idleTimeout == 0) return 0;
300 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
301 idleTimeout = 15; // minimum of 15 seconds while authenticating
302 time_t now = time(0);
303 if (now < lastEventTime) {
304 // Someone must have set the time backwards. Set lastEventTime so that the
305 // idleTimeout will count from now.
306 vlog.info("Time has gone backwards - resetting idle timeout");
307 lastEventTime = now;
308 }
309 int timeLeft = lastEventTime + idleTimeout - now;
310 if (timeLeft < -60) {
311 // Our callback is over a minute late - someone must have set the time
312 // forwards. Set lastEventTime so that the idleTimeout will count from
313 // now.
314 vlog.info("Time has gone forwards - resetting idle timeout");
315 lastEventTime = now;
316 return secsToMillis(idleTimeout);
317 }
318 if (timeLeft <= 0) {
319 close("Idle timeout");
320 return 0;
321 }
322 return secsToMillis(timeLeft);
323}
324
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000325
326bool VNCSConnectionST::getComparerState()
327{
328 // We interpret a low compression level as an indication that the client
329 // wants to prioritise CPU usage over bandwidth, and hence disable the
330 // comparing update tracker.
331 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
332}
333
334
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000335// renderedCursorChange() is called whenever the server-side rendered cursor
336// changes shape or position. It ensures that the next update will clean up
337// the old rendered cursor and if necessary draw the new rendered cursor.
338
339void VNCSConnectionST::renderedCursorChange()
340{
341 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000342 if (!renderedCursorRect.is_empty())
343 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000344 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000345 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000346 writeFramebufferUpdateOrClose();
347 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000348}
349
350// needRenderedCursor() returns true if this client needs the server-side
351// rendered cursor. This may be because it does not support local cursor or
352// because the current cursor position has not been set by this client.
353// Unfortunately we can't know for sure when the current cursor position has
354// been set by this client. We guess that this is the case when the current
355// cursor position is the same as the last pointer event from this client, or
356// if it is a very short time since this client's last pointer event (up to a
357// second). [ Ideally we should do finer-grained timing here and make the time
358// configurable, but I don't think it's that important. ]
359
360bool VNCSConnectionST::needRenderedCursor()
361{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000362 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000363 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000364 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000365}
366
367
368void VNCSConnectionST::approveConnectionOrClose(bool accept,
369 const char* reason)
370{
371 try {
372 approveConnection(accept, reason);
373 } catch (rdr::Exception& e) {
374 close(e.str());
375 }
376}
377
378
379
380// -=- Callbacks from SConnection
381
382void VNCSConnectionST::authSuccess()
383{
384 lastEventTime = time(0);
385
386 server->startDesktop();
387
388 // - Set the connection parameters appropriately
389 cp.width = server->pb->width();
390 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000391 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000392 cp.setName(server->getName());
393
394 // - Set the default pixel format
395 cp.setPF(server->pb->getPF());
396 char buffer[256];
397 cp.pf().print(buffer, 256);
398 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000399
400 // - Mark the entire display as "dirty"
401 updates.add_changed(server->pb->getRect());
402 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000403
404 // - Bootstrap the congestion control
405 ackedOffset = sock->outStream().length();
406 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000407}
408
409void VNCSConnectionST::queryConnection(const char* userName)
410{
411 // - Authentication succeeded - clear from blacklist
412 CharArray name; name.buf = sock->getPeerAddress();
413 server->blHosts->clearBlackmark(name.buf);
414
415 // - Special case to provide a more useful error message
416 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
417 server->authClientCount() > 0) {
418 approveConnection(false, "The server is already in use");
419 return;
420 }
421
422 // - Does the client have the right to bypass the query?
423 if (reverseConnection ||
424 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
425 (accessRights & AccessNoQuery))
426 {
427 approveConnection(true);
428 return;
429 }
430
431 // - Get the server to display an Accept/Reject dialog, if required
432 // If a dialog is displayed, the result will be PENDING, and the
433 // server will call approveConnection at a later time
434 CharArray reason;
435 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
436 &reason.buf);
437 if (qr == VNCServerST::PENDING)
438 return;
439
440 // - If server returns ACCEPT/REJECT then pass result to SConnection
441 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
442}
443
444void VNCSConnectionST::clientInit(bool shared)
445{
446 lastEventTime = time(0);
447 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100448 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000449 if (rfb::Server::neverShared) shared = false;
450 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100451 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000452 // - Close all the other connected clients
453 vlog.debug("non-shared connection - closing clients");
454 server->closeClients("Non-shared connection requested", getSock());
455 } else {
456 // - Refuse this connection if there are existing clients, in addition to
457 // this one
458 if (server->authClientCount() > 1) {
459 close("Server is already in use");
460 return;
461 }
462 }
463 }
464 SConnection::clientInit(shared);
465}
466
467void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
468{
469 SConnection::setPixelFormat(pf);
470 char buffer[256];
471 pf.print(buffer, 256);
472 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000473 setCursor();
474}
475
476void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
477{
478 pointerEventTime = lastEventTime = time(0);
479 server->lastUserInputTime = lastEventTime;
480 if (!(accessRights & AccessPtrEvents)) return;
481 if (!rfb::Server::acceptPointerEvents) return;
482 if (!server->pointerClient || server->pointerClient == this) {
483 pointerEventPos = pos;
484 if (buttonMask)
485 server->pointerClient = this;
486 else
487 server->pointerClient = 0;
488 server->desktop->pointerEvent(pointerEventPos, buttonMask);
489 }
490}
491
492
493class VNCSConnectionSTShiftPresser {
494public:
495 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
496 : desktop(desktop_), pressed(false) {}
497 ~VNCSConnectionSTShiftPresser() {
498 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
499 }
500 void press() {
501 desktop->keyEvent(XK_Shift_L, true);
502 pressed = true;
503 }
504 SDesktop* desktop;
505 bool pressed;
506};
507
508// keyEvent() - record in the pressedKeys which keys were pressed. Allow
509// multiple down events (for autorepeat), but only allow a single up event.
510void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
511 lastEventTime = time(0);
512 server->lastUserInputTime = lastEventTime;
513 if (!(accessRights & AccessKeyEvents)) return;
514 if (!rfb::Server::acceptKeyEvents) return;
515
516 // Remap the key if required
517 if (server->keyRemapper)
518 key = server->keyRemapper->remapKey(key);
519
520 // Turn ISO_Left_Tab into shifted Tab.
521 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
522 if (key == XK_ISO_Left_Tab) {
523 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
524 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
525 shiftPresser.press();
526 key = XK_Tab;
527 }
528
529 if (down) {
530 pressedKeys.insert(key);
531 } else {
532 if (!pressedKeys.erase(key)) return;
533 }
534 server->desktop->keyEvent(key, down);
535}
536
537void VNCSConnectionST::clientCutText(const char* str, int len)
538{
539 if (!(accessRights & AccessCutText)) return;
540 if (!rfb::Server::acceptCutText) return;
541 server->desktop->clientCutText(str, len);
542}
543
544void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
545{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000546 Rect safeRect;
547
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000548 if (!(accessRights & AccessView)) return;
549
550 SConnection::framebufferUpdateRequest(r, incremental);
551
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000552 // Check that the client isn't sending crappy requests
553 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
554 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
555 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000556 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
557 } else {
558 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000559 }
560
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000561 // Just update the requested region.
562 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000563 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000564 if (!incremental || !continuousUpdates)
565 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000566
567 if (!incremental) {
568 // Non-incremental update - treat as if area requested has changed
569 updates.add_changed(reqRgn);
570 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000571
572 // And send the screen layout to the client (which, unlike the
573 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000574 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000575
576 // We do not send a DesktopSize since it only contains the
577 // framebuffer size (which the client already should know) and
578 // because some clients don't handle extra DesktopSize events
579 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000580 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000581}
582
Pierre Ossman34bb0612009-03-21 21:16:14 +0000583void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
584 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000585{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000586 unsigned int result;
587
Michal Srbb318b8f2014-11-24 13:18:28 +0200588 if (!(accessRights & AccessSetDesktopSize)) return;
589 if (!rfb::Server::acceptSetDesktopSize) return;
590
Pierre Ossman04e62db2009-03-23 16:57:07 +0000591 // Don't bother the desktop with an invalid configuration
592 if (!layout.validate(fb_width, fb_height)) {
593 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
594 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000595 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000596 return;
597 }
598
599 // FIXME: the desktop will call back to VNCServerST and an extra set
600 // of ExtendedDesktopSize messages will be sent. This is okay
601 // protocol-wise, but unnecessary.
602 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
603
Pierre Ossman04e62db2009-03-23 16:57:07 +0000604 writer()->writeExtendedDesktopSize(reasonClient, result,
605 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000606
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000607 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000608 if (result == resultSuccess) {
609 if (server->screenLayout != layout)
610 throw Exception("Desktop configured a different screen layout than requested");
611 server->notifyScreenLayoutChange(this);
612 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000613
614 // but always send back a reply to the requesting client
615 // (do this last as it might throw an exception on socket errors)
616 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000617}
618
Pierre Ossman2c764942011-11-14 15:54:30 +0000619void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
620{
621 if (flags & fenceFlagRequest) {
622 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000623 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000624
625 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
626 fenceDataLen = len;
627 delete [] fenceData;
628 if (len > 0) {
629 fenceData = new char[len];
630 memcpy(fenceData, data, len);
631 }
632
633 return;
634 }
635
636 // We handle everything synchronously so we trivially honor these modes
637 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
638
639 writer()->writeFence(flags, len, data);
640 return;
641 }
642
Pierre Ossman1b478e52011-11-15 12:08:30 +0000643 struct RTTInfo rttInfo;
644
Pierre Ossman2c764942011-11-14 15:54:30 +0000645 switch (len) {
646 case 0:
647 // Initial dummy fence;
648 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000649 case sizeof(struct RTTInfo):
650 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
651 handleRTTPong(rttInfo);
652 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000653 default:
654 vlog.error("Fence response of unexpected size received");
655 }
656}
657
Pierre Ossman1b478e52011-11-15 12:08:30 +0000658void VNCSConnectionST::enableContinuousUpdates(bool enable,
659 int x, int y, int w, int h)
660{
661 Rect rect;
662
663 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
664 throw Exception("Client tried to enable continuous updates when not allowed");
665
666 continuousUpdates = enable;
667
668 rect.setXYWH(x, y, w, h);
669 cuRegion.reset(rect);
670
671 if (enable) {
672 requested.clear();
673 writeFramebufferUpdate();
674 } else {
675 writer()->writeEndOfContinuousUpdates();
676 }
677}
678
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000679// supportsLocalCursor() is called whenever the status of
680// cp.supportsLocalCursor has changed. If the client does now support local
681// cursor, we make sure that the old server-side rendered cursor is cleaned up
682// and the cursor is sent to the client.
683
684void VNCSConnectionST::supportsLocalCursor()
685{
686 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000687 if (!renderedCursorRect.is_empty())
688 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000689 drawRenderedCursor = false;
690 setCursor();
691 }
692}
693
Pierre Ossman2c764942011-11-14 15:54:30 +0000694void VNCSConnectionST::supportsFence()
695{
696 writer()->writeFence(fenceFlagRequest, 0, NULL);
697}
698
Pierre Ossman1b478e52011-11-15 12:08:30 +0000699void VNCSConnectionST::supportsContinuousUpdates()
700{
701 // We refuse to use continuous updates if we cannot monitor the buffer
702 // usage using fences.
703 if (!cp.supportsFence)
704 return;
705
706 writer()->writeEndOfContinuousUpdates();
707}
708
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000709
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000710bool VNCSConnectionST::handleTimeout(Timer* t)
711{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000712 try {
713 if (t == &updateTimer)
714 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000715 else if (t == &congestionTimer)
716 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000717 } catch (rdr::Exception& e) {
718 close(e.str());
719 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000720
721 return false;
722}
723
724
Pierre Ossman1b478e52011-11-15 12:08:30 +0000725void VNCSConnectionST::writeRTTPing()
726{
727 struct RTTInfo rttInfo;
728
729 if (!cp.supportsFence)
730 return;
731
732 memset(&rttInfo, 0, sizeof(struct RTTInfo));
733
734 gettimeofday(&rttInfo.tv, NULL);
735 rttInfo.offset = sock->outStream().length();
736 rttInfo.inFlight = rttInfo.offset - ackedOffset;
737
738 // We need to make sure any old update are already processed by the
739 // time we get the response back. This allows us to reliably throttle
740 // back on client overload, as well as network overload.
741 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
742 sizeof(struct RTTInfo), (const char*)&rttInfo);
743
744 pingCounter++;
745
746 sentOffset = rttInfo.offset;
747
748 // Let some data flow before we adjust the settings
749 if (!congestionTimer.isStarted())
750 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
751}
752
753void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
754{
755 unsigned rtt, delay;
756 int bdp;
757
758 pingCounter--;
759
760 rtt = msSince(&rttInfo.tv);
761 if (rtt < 1)
762 rtt = 1;
763
764 ackedOffset = rttInfo.offset;
765
766 // Try to estimate wire latency by tracking lowest seen latency
767 if (rtt < baseRTT)
768 baseRTT = rtt;
769
770 if (rttInfo.inFlight > congWindow) {
771 seenCongestion = true;
772
773 // Estimate added delay because of overtaxed buffers
774 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
775
776 if (delay < rtt)
777 rtt -= delay;
778 else
779 rtt = 1;
780
781 // If we underestimate the congestion window, then we'll get a latency
782 // that's less than the wire latency, which will confuse other portions
783 // of the code.
784 if (rtt < baseRTT)
785 rtt = baseRTT;
786 }
787
788 // We only keep track of the minimum latency seen (for a given interval)
789 // on the basis that we want to avoid continous buffer issue, but don't
790 // mind (or even approve of) bursts.
791 if (rtt < minRTT)
792 minRTT = rtt;
793}
794
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000795bool VNCSConnectionST::isCongested()
796{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000797 int offset;
798
799 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000800 if (sock->outStream().bufferUsage() > 0)
801 return true;
802
Pierre Ossman1b478e52011-11-15 12:08:30 +0000803 if (!cp.supportsFence)
804 return false;
805
806 // Idle for too long? (and no data on the wire)
807 //
808 // FIXME: This should really just be one baseRTT, but we're getting
809 // problems with triggering the idle timeout on each update.
810 // Maybe we need to use a moving average for the wire latency
811 // instead of baseRTT.
812 if ((sentOffset == ackedOffset) &&
813 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
814
815#ifdef CONGESTION_DEBUG
816 if (congWindow > INITIAL_WINDOW)
817 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
818 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
819#endif
820
821 // Close congestion window and allow a transfer
822 // FIXME: Reset baseRTT like Linux Vegas?
823 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
824
825 return false;
826 }
827
828 offset = sock->outStream().length();
829
830 // FIXME: Should we compensate for non-update data?
831 // (i.e. use sentOffset instead of offset)
832 if ((offset - ackedOffset) < congWindow)
833 return false;
834
835 // If we just have one outstanding "ping", that means the client has
836 // started receiving our update. In order to not regress compared to
837 // before we had congestion avoidance, we allow another update here.
838 // This could further clog up the tubes, but congestion control isn't
839 // really working properly right now anyway as the wire would otherwise
840 // be idle for at least RTT/2.
841 if (pingCounter == 1)
842 return false;
843
844 return true;
845}
846
847
848void VNCSConnectionST::updateCongestion()
849{
850 unsigned diff;
851
852 if (!seenCongestion)
853 return;
854
855 diff = minRTT - baseRTT;
856
857 if (diff > __rfbmin(100, baseRTT)) {
858 // Way too fast
859 congWindow = congWindow * baseRTT / minRTT;
860 } else if (diff > __rfbmin(50, baseRTT/2)) {
861 // Slightly too fast
862 congWindow -= 4096;
863 } else if (diff < 5) {
864 // Way too slow
865 congWindow += 8192;
866 } else if (diff < 25) {
867 // Too slow
868 congWindow += 4096;
869 }
870
871 if (congWindow < MINIMUM_WINDOW)
872 congWindow = MINIMUM_WINDOW;
873 if (congWindow > MAXIMUM_WINDOW)
874 congWindow = MAXIMUM_WINDOW;
875
876#ifdef CONGESTION_DEBUG
877 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
878 minRTT, baseRTT, congWindow / 1024,
879 congWindow * 8.0 / baseRTT / 1000.0);
880
881#ifdef TCP_INFO
882 struct tcp_info tcp_info;
883 socklen_t tcp_info_length;
884
885 tcp_info_length = sizeof(tcp_info);
886 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
887 (void *)&tcp_info, &tcp_info_length) == 0) {
888 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
889 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
890 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
891 }
892#endif
893
894#endif
895
896 minRTT = -1;
897 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000898}
899
900
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000901void VNCSConnectionST::writeFramebufferUpdate()
902{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000903 Region req;
904 UpdateInfo ui;
905 bool needNewUpdateInfo;
906
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000907 updateTimer.stop();
908
Pierre Ossman2c764942011-11-14 15:54:30 +0000909 // We're in the middle of processing a command that's supposed to be
910 // synchronised. Allowing an update to slip out right now might violate
911 // that synchronisation.
912 if (syncFence)
913 return;
914
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000915 // We try to aggregate responses, so don't send out anything whilst we
916 // still have incoming messages. processMessages() will give us another
917 // chance to run once things are idle.
918 if (inProcessMessages)
919 return;
920
Pierre Ossman1b478e52011-11-15 12:08:30 +0000921 if (state() != RFBSTATE_NORMAL)
922 return;
923 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000924 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000925
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000926 // Check that we actually have some space on the link and retry in a
927 // bit if things are congested.
928 if (isCongested()) {
929 updateTimer.start(50);
930 return;
931 }
932
Pierre Ossman36dadf82011-11-15 12:11:32 +0000933 // In continuous mode, we will be outputting at least three distinct
934 // messages. We need to aggregate these in order to not clog up TCP's
935 // congestion window.
936 network::TcpSocket::cork(sock->getFd(), true);
937
Pierre Ossmane9962f72009-04-23 12:31:42 +0000938 // First take care of any updates that cannot contain framebuffer data
939 // changes.
940 if (writer()->needNoDataUpdate()) {
941 writer()->writeNoDataUpdate();
942 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000943 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000944 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000945 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000946
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000947 updates.enable_copyrect(cp.useCopyRect);
948
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000949 // Fetch updates from server object, and see if we are allowed to send
950 // anything right now (the framebuffer might have changed in ways we
951 // haven't yet been informed of).
952 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000953 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000954
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000955 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000956 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000957 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000958
Pierre Ossman1b478e52011-11-15 12:08:30 +0000959 if (continuousUpdates)
960 req = cuRegion.union_(requested);
961 else
962 req = requested;
963
964 updates.getUpdateInfo(&ui, req);
965 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000966
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000967 // If the previous position of the rendered cursor overlaps the source of the
968 // copy, then when the copy happens the corresponding rectangle in the
969 // destination will be wrong, so add it to the changed region.
970
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000971 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
972 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000973 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000974 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000975 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000976 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000977 }
978 }
979
980 // If we need to remove the old rendered cursor, just add the rectangle to
981 // the changed region.
982
983 if (removeRenderedCursor) {
984 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000985 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000986 renderedCursorRect.clear();
987 removeRenderedCursor = false;
988 }
989
990 // Return if there is nothing to send the client.
991
992 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000993 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000994
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000995 // The `updates' object could change, make sure we have valid update info.
996
997 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000998 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000999
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001000 // If the client needs a server-side rendered cursor, work out the cursor
1001 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1002 // with the update region, we need to draw the rendered cursor regardless of
1003 // whether it has changed.
1004
1005 if (needRenderedCursor()) {
1006 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001007 = server->renderedCursor.getEffectiveRect()
1008 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001009
1010 if (renderedCursorRect.is_empty()) {
1011 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001012 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013 .intersect(renderedCursorRect).is_empty()) {
1014 drawRenderedCursor = true;
1015 }
1016
1017 // We could remove the new cursor rect from updates here. It's not clear
1018 // whether this is worth it. If we do remove it, then we won't draw over
1019 // the same bit of screen twice, but we have the overhead of a more complex
1020 // region.
1021
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001022 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001023 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001024 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001025 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001026 }
1027
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001028 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanc0397262014-03-14 15:59:46 +01001029 RenderedCursor *cursor;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001030
Pierre Ossmanc0397262014-03-14 15:59:46 +01001031 cursor = NULL;
1032 if (drawRenderedCursor)
1033 cursor = &server->renderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +00001034
1035 writeRTTPing();
1036
Pierre Ossmanc0397262014-03-14 15:59:46 +01001037 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
1038
1039 writeRTTPing();
1040
1041 drawRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001042 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001043 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001044 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001045
1046out:
1047 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001048}
1049
1050
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001051void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1052{
1053 if (!authenticated())
1054 return;
1055
1056 cp.screenLayout = server->screenLayout;
1057
1058 if (state() != RFBSTATE_NORMAL)
1059 return;
1060
1061 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1062 cp.screenLayout);
1063 writeFramebufferUpdate();
1064}
1065
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001066
1067// setCursor() is called whenever the cursor has changed shape or pixel format.
1068// If the client supports local cursor then it will arrange for the cursor to
1069// be sent to the client.
1070
1071void VNCSConnectionST::setCursor()
1072{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001073 if (state() != RFBSTATE_NORMAL)
1074 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001075
Pierre Ossman126e5642014-02-13 14:40:25 +01001076 cp.setCursor(server->cursor);
1077
1078 if (!writer()->writeSetCursor()) {
1079 if (!writer()->writeSetXCursor()) {
1080 // No client support
1081 return;
1082 }
1083 }
1084
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001085 writeFramebufferUpdate();
1086}
1087
1088void VNCSConnectionST::setDesktopName(const char *name)
1089{
1090 cp.setName(name);
1091
1092 if (state() != RFBSTATE_NORMAL)
1093 return;
1094
1095 if (!writer()->writeSetDesktopName()) {
1096 fprintf(stderr, "Client does not support desktop rename\n");
1097 return;
1098 }
1099
1100 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001101}
1102
1103void VNCSConnectionST::setSocketTimeouts()
1104{
1105 int timeoutms = rfb::Server::clientWaitTimeMillis;
1106 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1107 if (timeoutms == 0)
1108 timeoutms = -1;
1109 sock->inStream().setTimeout(timeoutms);
1110 sock->outStream().setTimeout(timeoutms);
1111}
1112
1113char* VNCSConnectionST::getStartTime()
1114{
1115 char* result = ctime(&startTime);
1116 result[24] = '\0';
1117 return result;
1118}
1119
1120void VNCSConnectionST::setStatus(int status)
1121{
1122 switch (status) {
1123 case 0:
1124 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1125 break;
1126 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001127 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001128 break;
1129 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001130 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001131 break;
1132 }
1133 framebufferUpdateRequest(server->pb->getRect(), false);
1134}
1135int VNCSConnectionST::getStatus()
1136{
1137 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1138 return 0;
1139 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1140 return 1;
1141 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1142 return 2;
1143 return 4;
1144}
1145