blob: 274c496b8d43e5a785469e2858de2a98fa0c9f0a [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;
448 if (rfb::Server::neverShared) shared = false;
449 if (!shared) {
450 if (rfb::Server::disconnectClients) {
451 // - Close all the other connected clients
452 vlog.debug("non-shared connection - closing clients");
453 server->closeClients("Non-shared connection requested", getSock());
454 } else {
455 // - Refuse this connection if there are existing clients, in addition to
456 // this one
457 if (server->authClientCount() > 1) {
458 close("Server is already in use");
459 return;
460 }
461 }
462 }
463 SConnection::clientInit(shared);
464}
465
466void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
467{
468 SConnection::setPixelFormat(pf);
469 char buffer[256];
470 pf.print(buffer, 256);
471 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000472 setCursor();
473}
474
475void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
476{
477 pointerEventTime = lastEventTime = time(0);
478 server->lastUserInputTime = lastEventTime;
479 if (!(accessRights & AccessPtrEvents)) return;
480 if (!rfb::Server::acceptPointerEvents) return;
481 if (!server->pointerClient || server->pointerClient == this) {
482 pointerEventPos = pos;
483 if (buttonMask)
484 server->pointerClient = this;
485 else
486 server->pointerClient = 0;
487 server->desktop->pointerEvent(pointerEventPos, buttonMask);
488 }
489}
490
491
492class VNCSConnectionSTShiftPresser {
493public:
494 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
495 : desktop(desktop_), pressed(false) {}
496 ~VNCSConnectionSTShiftPresser() {
497 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
498 }
499 void press() {
500 desktop->keyEvent(XK_Shift_L, true);
501 pressed = true;
502 }
503 SDesktop* desktop;
504 bool pressed;
505};
506
507// keyEvent() - record in the pressedKeys which keys were pressed. Allow
508// multiple down events (for autorepeat), but only allow a single up event.
509void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
510 lastEventTime = time(0);
511 server->lastUserInputTime = lastEventTime;
512 if (!(accessRights & AccessKeyEvents)) return;
513 if (!rfb::Server::acceptKeyEvents) return;
514
515 // Remap the key if required
516 if (server->keyRemapper)
517 key = server->keyRemapper->remapKey(key);
518
519 // Turn ISO_Left_Tab into shifted Tab.
520 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
521 if (key == XK_ISO_Left_Tab) {
522 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
523 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
524 shiftPresser.press();
525 key = XK_Tab;
526 }
527
528 if (down) {
529 pressedKeys.insert(key);
530 } else {
531 if (!pressedKeys.erase(key)) return;
532 }
533 server->desktop->keyEvent(key, down);
534}
535
536void VNCSConnectionST::clientCutText(const char* str, int len)
537{
538 if (!(accessRights & AccessCutText)) return;
539 if (!rfb::Server::acceptCutText) return;
540 server->desktop->clientCutText(str, len);
541}
542
543void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
544{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000545 Rect safeRect;
546
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000547 if (!(accessRights & AccessView)) return;
548
549 SConnection::framebufferUpdateRequest(r, incremental);
550
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000551 // Check that the client isn't sending crappy requests
552 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
553 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
554 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000555 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
556 } else {
557 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000558 }
559
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000560 // Just update the requested region.
561 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000562 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000563 if (!incremental || !continuousUpdates)
564 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000565
566 if (!incremental) {
567 // Non-incremental update - treat as if area requested has changed
568 updates.add_changed(reqRgn);
569 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000570
571 // And send the screen layout to the client (which, unlike the
572 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000573 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000574
575 // We do not send a DesktopSize since it only contains the
576 // framebuffer size (which the client already should know) and
577 // because some clients don't handle extra DesktopSize events
578 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000579 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000580}
581
Pierre Ossman34bb0612009-03-21 21:16:14 +0000582void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
583 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000584{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000585 unsigned int result;
586
Michal Srbb318b8f2014-11-24 13:18:28 +0200587 if (!(accessRights & AccessSetDesktopSize)) return;
588 if (!rfb::Server::acceptSetDesktopSize) return;
589
Pierre Ossman04e62db2009-03-23 16:57:07 +0000590 // Don't bother the desktop with an invalid configuration
591 if (!layout.validate(fb_width, fb_height)) {
592 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
593 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000594 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000595 return;
596 }
597
598 // FIXME: the desktop will call back to VNCServerST and an extra set
599 // of ExtendedDesktopSize messages will be sent. This is okay
600 // protocol-wise, but unnecessary.
601 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
602
Pierre Ossman04e62db2009-03-23 16:57:07 +0000603 writer()->writeExtendedDesktopSize(reasonClient, result,
604 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000605
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000606 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000607 if (result == resultSuccess) {
608 if (server->screenLayout != layout)
609 throw Exception("Desktop configured a different screen layout than requested");
610 server->notifyScreenLayoutChange(this);
611 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000612
613 // but always send back a reply to the requesting client
614 // (do this last as it might throw an exception on socket errors)
615 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000616}
617
Pierre Ossman2c764942011-11-14 15:54:30 +0000618void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
619{
620 if (flags & fenceFlagRequest) {
621 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000622 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000623
624 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
625 fenceDataLen = len;
626 delete [] fenceData;
627 if (len > 0) {
628 fenceData = new char[len];
629 memcpy(fenceData, data, len);
630 }
631
632 return;
633 }
634
635 // We handle everything synchronously so we trivially honor these modes
636 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
637
638 writer()->writeFence(flags, len, data);
639 return;
640 }
641
Pierre Ossman1b478e52011-11-15 12:08:30 +0000642 struct RTTInfo rttInfo;
643
Pierre Ossman2c764942011-11-14 15:54:30 +0000644 switch (len) {
645 case 0:
646 // Initial dummy fence;
647 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000648 case sizeof(struct RTTInfo):
649 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
650 handleRTTPong(rttInfo);
651 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000652 default:
653 vlog.error("Fence response of unexpected size received");
654 }
655}
656
Pierre Ossman1b478e52011-11-15 12:08:30 +0000657void VNCSConnectionST::enableContinuousUpdates(bool enable,
658 int x, int y, int w, int h)
659{
660 Rect rect;
661
662 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
663 throw Exception("Client tried to enable continuous updates when not allowed");
664
665 continuousUpdates = enable;
666
667 rect.setXYWH(x, y, w, h);
668 cuRegion.reset(rect);
669
670 if (enable) {
671 requested.clear();
672 writeFramebufferUpdate();
673 } else {
674 writer()->writeEndOfContinuousUpdates();
675 }
676}
677
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000678// supportsLocalCursor() is called whenever the status of
679// cp.supportsLocalCursor has changed. If the client does now support local
680// cursor, we make sure that the old server-side rendered cursor is cleaned up
681// and the cursor is sent to the client.
682
683void VNCSConnectionST::supportsLocalCursor()
684{
685 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000686 if (!renderedCursorRect.is_empty())
687 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000688 drawRenderedCursor = false;
689 setCursor();
690 }
691}
692
Pierre Ossman2c764942011-11-14 15:54:30 +0000693void VNCSConnectionST::supportsFence()
694{
695 writer()->writeFence(fenceFlagRequest, 0, NULL);
696}
697
Pierre Ossman1b478e52011-11-15 12:08:30 +0000698void VNCSConnectionST::supportsContinuousUpdates()
699{
700 // We refuse to use continuous updates if we cannot monitor the buffer
701 // usage using fences.
702 if (!cp.supportsFence)
703 return;
704
705 writer()->writeEndOfContinuousUpdates();
706}
707
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000708
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000709bool VNCSConnectionST::handleTimeout(Timer* t)
710{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000711 try {
712 if (t == &updateTimer)
713 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000714 else if (t == &congestionTimer)
715 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000716 } catch (rdr::Exception& e) {
717 close(e.str());
718 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000719
720 return false;
721}
722
723
Pierre Ossman1b478e52011-11-15 12:08:30 +0000724void VNCSConnectionST::writeRTTPing()
725{
726 struct RTTInfo rttInfo;
727
728 if (!cp.supportsFence)
729 return;
730
731 memset(&rttInfo, 0, sizeof(struct RTTInfo));
732
733 gettimeofday(&rttInfo.tv, NULL);
734 rttInfo.offset = sock->outStream().length();
735 rttInfo.inFlight = rttInfo.offset - ackedOffset;
736
737 // We need to make sure any old update are already processed by the
738 // time we get the response back. This allows us to reliably throttle
739 // back on client overload, as well as network overload.
740 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
741 sizeof(struct RTTInfo), (const char*)&rttInfo);
742
743 pingCounter++;
744
745 sentOffset = rttInfo.offset;
746
747 // Let some data flow before we adjust the settings
748 if (!congestionTimer.isStarted())
749 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
750}
751
752void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
753{
754 unsigned rtt, delay;
755 int bdp;
756
757 pingCounter--;
758
759 rtt = msSince(&rttInfo.tv);
760 if (rtt < 1)
761 rtt = 1;
762
763 ackedOffset = rttInfo.offset;
764
765 // Try to estimate wire latency by tracking lowest seen latency
766 if (rtt < baseRTT)
767 baseRTT = rtt;
768
769 if (rttInfo.inFlight > congWindow) {
770 seenCongestion = true;
771
772 // Estimate added delay because of overtaxed buffers
773 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
774
775 if (delay < rtt)
776 rtt -= delay;
777 else
778 rtt = 1;
779
780 // If we underestimate the congestion window, then we'll get a latency
781 // that's less than the wire latency, which will confuse other portions
782 // of the code.
783 if (rtt < baseRTT)
784 rtt = baseRTT;
785 }
786
787 // We only keep track of the minimum latency seen (for a given interval)
788 // on the basis that we want to avoid continous buffer issue, but don't
789 // mind (or even approve of) bursts.
790 if (rtt < minRTT)
791 minRTT = rtt;
792}
793
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000794bool VNCSConnectionST::isCongested()
795{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000796 int offset;
797
798 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000799 if (sock->outStream().bufferUsage() > 0)
800 return true;
801
Pierre Ossman1b478e52011-11-15 12:08:30 +0000802 if (!cp.supportsFence)
803 return false;
804
805 // Idle for too long? (and no data on the wire)
806 //
807 // FIXME: This should really just be one baseRTT, but we're getting
808 // problems with triggering the idle timeout on each update.
809 // Maybe we need to use a moving average for the wire latency
810 // instead of baseRTT.
811 if ((sentOffset == ackedOffset) &&
812 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
813
814#ifdef CONGESTION_DEBUG
815 if (congWindow > INITIAL_WINDOW)
816 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
817 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
818#endif
819
820 // Close congestion window and allow a transfer
821 // FIXME: Reset baseRTT like Linux Vegas?
822 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
823
824 return false;
825 }
826
827 offset = sock->outStream().length();
828
829 // FIXME: Should we compensate for non-update data?
830 // (i.e. use sentOffset instead of offset)
831 if ((offset - ackedOffset) < congWindow)
832 return false;
833
834 // If we just have one outstanding "ping", that means the client has
835 // started receiving our update. In order to not regress compared to
836 // before we had congestion avoidance, we allow another update here.
837 // This could further clog up the tubes, but congestion control isn't
838 // really working properly right now anyway as the wire would otherwise
839 // be idle for at least RTT/2.
840 if (pingCounter == 1)
841 return false;
842
843 return true;
844}
845
846
847void VNCSConnectionST::updateCongestion()
848{
849 unsigned diff;
850
851 if (!seenCongestion)
852 return;
853
854 diff = minRTT - baseRTT;
855
856 if (diff > __rfbmin(100, baseRTT)) {
857 // Way too fast
858 congWindow = congWindow * baseRTT / minRTT;
859 } else if (diff > __rfbmin(50, baseRTT/2)) {
860 // Slightly too fast
861 congWindow -= 4096;
862 } else if (diff < 5) {
863 // Way too slow
864 congWindow += 8192;
865 } else if (diff < 25) {
866 // Too slow
867 congWindow += 4096;
868 }
869
870 if (congWindow < MINIMUM_WINDOW)
871 congWindow = MINIMUM_WINDOW;
872 if (congWindow > MAXIMUM_WINDOW)
873 congWindow = MAXIMUM_WINDOW;
874
875#ifdef CONGESTION_DEBUG
876 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
877 minRTT, baseRTT, congWindow / 1024,
878 congWindow * 8.0 / baseRTT / 1000.0);
879
880#ifdef TCP_INFO
881 struct tcp_info tcp_info;
882 socklen_t tcp_info_length;
883
884 tcp_info_length = sizeof(tcp_info);
885 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
886 (void *)&tcp_info, &tcp_info_length) == 0) {
887 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
888 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
889 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
890 }
891#endif
892
893#endif
894
895 minRTT = -1;
896 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000897}
898
899
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000900void VNCSConnectionST::writeFramebufferUpdate()
901{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000902 Region req;
903 UpdateInfo ui;
904 bool needNewUpdateInfo;
905
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000906 updateTimer.stop();
907
Pierre Ossman2c764942011-11-14 15:54:30 +0000908 // We're in the middle of processing a command that's supposed to be
909 // synchronised. Allowing an update to slip out right now might violate
910 // that synchronisation.
911 if (syncFence)
912 return;
913
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000914 // We try to aggregate responses, so don't send out anything whilst we
915 // still have incoming messages. processMessages() will give us another
916 // chance to run once things are idle.
917 if (inProcessMessages)
918 return;
919
Pierre Ossman1b478e52011-11-15 12:08:30 +0000920 if (state() != RFBSTATE_NORMAL)
921 return;
922 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000923 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000924
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000925 // Check that we actually have some space on the link and retry in a
926 // bit if things are congested.
927 if (isCongested()) {
928 updateTimer.start(50);
929 return;
930 }
931
Pierre Ossman36dadf82011-11-15 12:11:32 +0000932 // In continuous mode, we will be outputting at least three distinct
933 // messages. We need to aggregate these in order to not clog up TCP's
934 // congestion window.
935 network::TcpSocket::cork(sock->getFd(), true);
936
Pierre Ossmane9962f72009-04-23 12:31:42 +0000937 // First take care of any updates that cannot contain framebuffer data
938 // changes.
939 if (writer()->needNoDataUpdate()) {
940 writer()->writeNoDataUpdate();
941 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000942 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000943 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000944 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000945
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000946 updates.enable_copyrect(cp.useCopyRect);
947
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000948 // Fetch updates from server object, and see if we are allowed to send
949 // anything right now (the framebuffer might have changed in ways we
950 // haven't yet been informed of).
951 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000952 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000953
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000954 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000955 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000956 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000957
Pierre Ossman1b478e52011-11-15 12:08:30 +0000958 if (continuousUpdates)
959 req = cuRegion.union_(requested);
960 else
961 req = requested;
962
963 updates.getUpdateInfo(&ui, req);
964 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000965
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000966 // If the previous position of the rendered cursor overlaps the source of the
967 // copy, then when the copy happens the corresponding rectangle in the
968 // destination will be wrong, so add it to the changed region.
969
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000970 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
971 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000972 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000973 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000974 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000975 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000976 }
977 }
978
979 // If we need to remove the old rendered cursor, just add the rectangle to
980 // the changed region.
981
982 if (removeRenderedCursor) {
983 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000984 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000985 renderedCursorRect.clear();
986 removeRenderedCursor = false;
987 }
988
989 // Return if there is nothing to send the client.
990
991 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000992 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000993
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000994 // The `updates' object could change, make sure we have valid update info.
995
996 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000997 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000998
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000999 // If the client needs a server-side rendered cursor, work out the cursor
1000 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1001 // with the update region, we need to draw the rendered cursor regardless of
1002 // whether it has changed.
1003
1004 if (needRenderedCursor()) {
1005 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001006 = server->renderedCursor.getEffectiveRect()
1007 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001008
1009 if (renderedCursorRect.is_empty()) {
1010 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001011 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001012 .intersect(renderedCursorRect).is_empty()) {
1013 drawRenderedCursor = true;
1014 }
1015
1016 // We could remove the new cursor rect from updates here. It's not clear
1017 // whether this is worth it. If we do remove it, then we won't draw over
1018 // the same bit of screen twice, but we have the overhead of a more complex
1019 // region.
1020
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001021 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001022 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001023 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001024 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001025 }
1026
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001027 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanc0397262014-03-14 15:59:46 +01001028 RenderedCursor *cursor;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001029
Pierre Ossmanc0397262014-03-14 15:59:46 +01001030 cursor = NULL;
1031 if (drawRenderedCursor)
1032 cursor = &server->renderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +00001033
1034 writeRTTPing();
1035
Pierre Ossmanc0397262014-03-14 15:59:46 +01001036 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
1037
1038 writeRTTPing();
1039
1040 drawRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001041 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001042 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001043 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001044
1045out:
1046 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001047}
1048
1049
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001050void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1051{
1052 if (!authenticated())
1053 return;
1054
1055 cp.screenLayout = server->screenLayout;
1056
1057 if (state() != RFBSTATE_NORMAL)
1058 return;
1059
1060 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1061 cp.screenLayout);
1062 writeFramebufferUpdate();
1063}
1064
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001065
1066// setCursor() is called whenever the cursor has changed shape or pixel format.
1067// If the client supports local cursor then it will arrange for the cursor to
1068// be sent to the client.
1069
1070void VNCSConnectionST::setCursor()
1071{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001072 if (state() != RFBSTATE_NORMAL)
1073 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001074
Pierre Ossman126e5642014-02-13 14:40:25 +01001075 cp.setCursor(server->cursor);
1076
1077 if (!writer()->writeSetCursor()) {
1078 if (!writer()->writeSetXCursor()) {
1079 // No client support
1080 return;
1081 }
1082 }
1083
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001084 writeFramebufferUpdate();
1085}
1086
1087void VNCSConnectionST::setDesktopName(const char *name)
1088{
1089 cp.setName(name);
1090
1091 if (state() != RFBSTATE_NORMAL)
1092 return;
1093
1094 if (!writer()->writeSetDesktopName()) {
1095 fprintf(stderr, "Client does not support desktop rename\n");
1096 return;
1097 }
1098
1099 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001100}
1101
1102void VNCSConnectionST::setSocketTimeouts()
1103{
1104 int timeoutms = rfb::Server::clientWaitTimeMillis;
1105 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1106 if (timeoutms == 0)
1107 timeoutms = -1;
1108 sock->inStream().setTimeout(timeoutms);
1109 sock->outStream().setTimeout(timeoutms);
1110}
1111
1112char* VNCSConnectionST::getStartTime()
1113{
1114 char* result = ctime(&startTime);
1115 result[24] = '\0';
1116 return result;
1117}
1118
1119void VNCSConnectionST::setStatus(int status)
1120{
1121 switch (status) {
1122 case 0:
1123 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1124 break;
1125 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001126 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001127 break;
1128 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001129 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001130 break;
1131 }
1132 framebufferUpdateRequest(server->pb->getRect(), false);
1133}
1134int VNCSConnectionST::getStatus()
1135{
1136 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1137 return 0;
1138 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1139 return 1;
1140 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1141 return 2;
1142 return 4;
1143}
1144