blob: e261cfbac303f9e78a3badfa5df410938e7d7efc [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanf8e3b342015-01-26 14:37:04 +01002 * Copyright 2009-2015 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 Ossman7069bdd2015-02-06 14:41:58 +010069 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010070 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000071 pendingSyncFence(false), syncFence(false), fenceFlags(0),
72 fenceDataLen(0), fenceData(NULL),
Pierre Ossman1b478e52011-11-15 12:08:30 +000073 baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
74 ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020075 server(server_), updates(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmanc0397262014-03-14 15:59:46 +010077 continuousUpdates(false), encodeManager(this),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000078 updateTimer(this), pointerEventTime(0),
79 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080{
81 setStreams(&sock->inStream(), &sock->outStream());
82 peerEndpoint.buf = sock->getPeerEndpoint();
83 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
84
85 // Configure the socket
86 setSocketTimeouts();
87 lastEventTime = time(0);
88
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 server->clients.push_front(this);
90}
91
92
93VNCSConnectionST::~VNCSConnectionST()
94{
95 // If we reach here then VNCServerST is deleting us!
96 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
97 peerEndpoint.buf,
98 (closeReason.buf) ? closeReason.buf : "");
99
100 // Release any keys the client still had pressed
101 std::set<rdr::U32>::iterator i;
102 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
103 server->desktop->keyEvent(*i, false);
104 if (server->pointerClient == this)
105 server->pointerClient = 0;
106
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 // Remove this client from the server
108 server->clients.remove(this);
109
Pierre Ossman2c764942011-11-14 15:54:30 +0000110 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111}
112
113
114// Methods called from VNCServerST
115
116bool VNCSConnectionST::init()
117{
118 try {
119 initialiseProtocol();
120 } catch (rdr::Exception& e) {
121 close(e.str());
122 return false;
123 }
124 return true;
125}
126
127void VNCSConnectionST::close(const char* reason)
128{
129 // Log the reason for the close
130 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000131 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 else
133 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
134
135 if (authenticated()) {
136 server->lastDisconnectTime = time(0);
137 }
138
139 // Just shutdown the socket and mark our state as closing. Eventually the
140 // calling code will call VNCServerST's removeSocket() method causing us to
141 // be deleted.
142 sock->shutdown();
143 setState(RFBSTATE_CLOSING);
144}
145
146
147void VNCSConnectionST::processMessages()
148{
149 if (state() == RFBSTATE_CLOSING) return;
150 try {
151 // - Now set appropriate socket timeouts and process data
152 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000153
154 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155
Pierre Ossmana830bec2011-11-08 12:12:02 +0000156 // Get the underlying TCP layer to build large packets if we send
157 // multiple small responses.
158 network::TcpSocket::cork(sock->getFd(), true);
159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000161 if (pendingSyncFence) {
162 syncFence = true;
163 pendingSyncFence = false;
164 }
165
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000166 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000167
Pierre Ossman2c764942011-11-14 15:54:30 +0000168 if (syncFence) {
169 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
170 syncFence = false;
171 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000172 }
173
Pierre Ossmana830bec2011-11-08 12:12:02 +0000174 // Flush out everything in case we go idle after this.
175 network::TcpSocket::cork(sock->getFd(), false);
176
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000177 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000178
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000179 // If there were anything requiring an update, try to send it here.
180 // We wait until now with this to aggregate responses and to give
181 // higher priority to user actions such as keyboard and pointer events.
182 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 } catch (rdr::EndOfStream&) {
184 close("Clean disconnection");
185 } catch (rdr::Exception &e) {
186 close(e.str());
187 }
188}
189
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190void VNCSConnectionST::pixelBufferChange()
191{
192 try {
193 if (!authenticated()) return;
194 if (cp.width && cp.height && (server->pb->width() != cp.width ||
195 server->pb->height() != cp.height))
196 {
197 // We need to clip the next update to the new size, but also add any
198 // extra bits if it's bigger. If we wanted to do this exactly, something
199 // like the code below would do it, but at the moment we just update the
200 // entire new size. However, we do need to clip the renderedCursorRect
201 // because that might be added to updates in writeFramebufferUpdate().
202
203 //updates.intersect(server->pb->getRect());
204 //
205 //if (server->pb->width() > cp.width)
206 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
207 // server->pb->height()));
208 //if (server->pb->height() > cp.height)
209 // updates.add_changed(Rect(0, cp.height, cp.width,
210 // server->pb->height()));
211
212 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
213
214 cp.width = server->pb->width();
215 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000216 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000218 // We should only send EDS to client asking for both
219 if (!writer()->writeExtendedDesktopSize()) {
220 if (!writer()->writeSetDesktopSize()) {
221 close("Client does not support desktop resize");
222 return;
223 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224 }
225 }
226 }
227 // Just update the whole screen at the moment because we're too lazy to
228 // work out what's actually changed.
229 updates.clear();
230 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000231 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232 } catch(rdr::Exception &e) {
233 close(e.str());
234 }
235}
236
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000237void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000238{
239 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000240 writeFramebufferUpdate();
241 } catch(rdr::Exception &e) {
242 close(e.str());
243 }
244}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000245
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000246void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
247{
248 try {
249 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000250 } catch(rdr::Exception &e) {
251 close(e.str());
252 }
253}
254
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000255void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256{
257 try {
258 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
259 } catch(rdr::Exception& e) {
260 close(e.str());
261 }
262}
263
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000264void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000265{
266 try {
267 if (!(accessRights & AccessCutText)) return;
268 if (!rfb::Server::sendCutText) return;
269 if (state() == RFBSTATE_NORMAL)
270 writer()->writeServerCutText(str, len);
271 } catch(rdr::Exception& e) {
272 close(e.str());
273 }
274}
275
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000276
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000277void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000278{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000279 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000280 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000281 } catch(rdr::Exception& e) {
282 close(e.str());
283 }
284}
285
286
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000287void VNCSConnectionST::setCursorOrClose()
288{
289 try {
290 setCursor();
291 } catch(rdr::Exception& e) {
292 close(e.str());
293 }
294}
295
296
297int VNCSConnectionST::checkIdleTimeout()
298{
299 int idleTimeout = rfb::Server::idleTimeout;
300 if (idleTimeout == 0) return 0;
301 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
302 idleTimeout = 15; // minimum of 15 seconds while authenticating
303 time_t now = time(0);
304 if (now < lastEventTime) {
305 // Someone must have set the time backwards. Set lastEventTime so that the
306 // idleTimeout will count from now.
307 vlog.info("Time has gone backwards - resetting idle timeout");
308 lastEventTime = now;
309 }
310 int timeLeft = lastEventTime + idleTimeout - now;
311 if (timeLeft < -60) {
312 // Our callback is over a minute late - someone must have set the time
313 // forwards. Set lastEventTime so that the idleTimeout will count from
314 // now.
315 vlog.info("Time has gone forwards - resetting idle timeout");
316 lastEventTime = now;
317 return secsToMillis(idleTimeout);
318 }
319 if (timeLeft <= 0) {
320 close("Idle timeout");
321 return 0;
322 }
323 return secsToMillis(timeLeft);
324}
325
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000326
327bool VNCSConnectionST::getComparerState()
328{
329 // We interpret a low compression level as an indication that the client
330 // wants to prioritise CPU usage over bandwidth, and hence disable the
331 // comparing update tracker.
332 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
333}
334
335
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000336// renderedCursorChange() is called whenever the server-side rendered cursor
337// changes shape or position. It ensures that the next update will clean up
338// the old rendered cursor and if necessary draw the new rendered cursor.
339
340void VNCSConnectionST::renderedCursorChange()
341{
342 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000343 if (!renderedCursorRect.is_empty())
344 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000345 if (needRenderedCursor()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000346 drawRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000347 writeFramebufferUpdateOrClose();
348 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000349}
350
351// needRenderedCursor() returns true if this client needs the server-side
352// rendered cursor. This may be because it does not support local cursor or
353// because the current cursor position has not been set by this client.
354// Unfortunately we can't know for sure when the current cursor position has
355// been set by this client. We guess that this is the case when the current
356// cursor position is the same as the last pointer event from this client, or
357// if it is a very short time since this client's last pointer event (up to a
358// second). [ Ideally we should do finer-grained timing here and make the time
359// configurable, but I don't think it's that important. ]
360
361bool VNCSConnectionST::needRenderedCursor()
362{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000363 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000364 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000365 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000366}
367
368
369void VNCSConnectionST::approveConnectionOrClose(bool accept,
370 const char* reason)
371{
372 try {
373 approveConnection(accept, reason);
374 } catch (rdr::Exception& e) {
375 close(e.str());
376 }
377}
378
379
380
381// -=- Callbacks from SConnection
382
383void VNCSConnectionST::authSuccess()
384{
385 lastEventTime = time(0);
386
387 server->startDesktop();
388
389 // - Set the connection parameters appropriately
390 cp.width = server->pb->width();
391 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000392 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000393 cp.setName(server->getName());
394
395 // - Set the default pixel format
396 cp.setPF(server->pb->getPF());
397 char buffer[256];
398 cp.pf().print(buffer, 256);
399 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000400
401 // - Mark the entire display as "dirty"
402 updates.add_changed(server->pb->getRect());
403 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000404
405 // - Bootstrap the congestion control
406 ackedOffset = sock->outStream().length();
407 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000408}
409
410void VNCSConnectionST::queryConnection(const char* userName)
411{
412 // - Authentication succeeded - clear from blacklist
413 CharArray name; name.buf = sock->getPeerAddress();
414 server->blHosts->clearBlackmark(name.buf);
415
416 // - Special case to provide a more useful error message
417 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
418 server->authClientCount() > 0) {
419 approveConnection(false, "The server is already in use");
420 return;
421 }
422
423 // - Does the client have the right to bypass the query?
424 if (reverseConnection ||
425 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
426 (accessRights & AccessNoQuery))
427 {
428 approveConnection(true);
429 return;
430 }
431
432 // - Get the server to display an Accept/Reject dialog, if required
433 // If a dialog is displayed, the result will be PENDING, and the
434 // server will call approveConnection at a later time
435 CharArray reason;
436 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
437 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100438 if (qr == VNCServerST::PENDING) {
439 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000440 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100441 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000442
443 // - If server returns ACCEPT/REJECT then pass result to SConnection
444 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
445}
446
447void VNCSConnectionST::clientInit(bool shared)
448{
449 lastEventTime = time(0);
450 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100451 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000452 if (rfb::Server::neverShared) shared = false;
453 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100454 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000455 // - Close all the other connected clients
456 vlog.debug("non-shared connection - closing clients");
457 server->closeClients("Non-shared connection requested", getSock());
458 } else {
459 // - Refuse this connection if there are existing clients, in addition to
460 // this one
461 if (server->authClientCount() > 1) {
462 close("Server is already in use");
463 return;
464 }
465 }
466 }
467 SConnection::clientInit(shared);
468}
469
470void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
471{
472 SConnection::setPixelFormat(pf);
473 char buffer[256];
474 pf.print(buffer, 256);
475 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000476 setCursor();
477}
478
479void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
480{
481 pointerEventTime = lastEventTime = time(0);
482 server->lastUserInputTime = lastEventTime;
483 if (!(accessRights & AccessPtrEvents)) return;
484 if (!rfb::Server::acceptPointerEvents) return;
485 if (!server->pointerClient || server->pointerClient == this) {
486 pointerEventPos = pos;
487 if (buttonMask)
488 server->pointerClient = this;
489 else
490 server->pointerClient = 0;
491 server->desktop->pointerEvent(pointerEventPos, buttonMask);
492 }
493}
494
495
496class VNCSConnectionSTShiftPresser {
497public:
498 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
499 : desktop(desktop_), pressed(false) {}
500 ~VNCSConnectionSTShiftPresser() {
501 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
502 }
503 void press() {
504 desktop->keyEvent(XK_Shift_L, true);
505 pressed = true;
506 }
507 SDesktop* desktop;
508 bool pressed;
509};
510
511// keyEvent() - record in the pressedKeys which keys were pressed. Allow
512// multiple down events (for autorepeat), but only allow a single up event.
513void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
514 lastEventTime = time(0);
515 server->lastUserInputTime = lastEventTime;
516 if (!(accessRights & AccessKeyEvents)) return;
517 if (!rfb::Server::acceptKeyEvents) return;
518
519 // Remap the key if required
520 if (server->keyRemapper)
521 key = server->keyRemapper->remapKey(key);
522
523 // Turn ISO_Left_Tab into shifted Tab.
524 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
525 if (key == XK_ISO_Left_Tab) {
526 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
527 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
528 shiftPresser.press();
529 key = XK_Tab;
530 }
531
532 if (down) {
533 pressedKeys.insert(key);
534 } else {
535 if (!pressedKeys.erase(key)) return;
536 }
537 server->desktop->keyEvent(key, down);
538}
539
540void VNCSConnectionST::clientCutText(const char* str, int len)
541{
542 if (!(accessRights & AccessCutText)) return;
543 if (!rfb::Server::acceptCutText) return;
544 server->desktop->clientCutText(str, len);
545}
546
547void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
548{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000549 Rect safeRect;
550
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000551 if (!(accessRights & AccessView)) return;
552
553 SConnection::framebufferUpdateRequest(r, incremental);
554
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000555 // Check that the client isn't sending crappy requests
556 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
557 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
558 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000559 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
560 } else {
561 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000562 }
563
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000564 // Just update the requested region.
565 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000566 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000567 if (!incremental || !continuousUpdates)
568 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000569
570 if (!incremental) {
571 // Non-incremental update - treat as if area requested has changed
572 updates.add_changed(reqRgn);
573 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000574
575 // And send the screen layout to the client (which, unlike the
576 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000577 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000578
579 // We do not send a DesktopSize since it only contains the
580 // framebuffer size (which the client already should know) and
581 // because some clients don't handle extra DesktopSize events
582 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000583 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000584}
585
Pierre Ossman34bb0612009-03-21 21:16:14 +0000586void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
587 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000588{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000589 unsigned int result;
590
Michal Srbb318b8f2014-11-24 13:18:28 +0200591 if (!(accessRights & AccessSetDesktopSize)) return;
592 if (!rfb::Server::acceptSetDesktopSize) return;
593
Pierre Ossman04e62db2009-03-23 16:57:07 +0000594 // Don't bother the desktop with an invalid configuration
595 if (!layout.validate(fb_width, fb_height)) {
596 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
597 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000598 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000599 return;
600 }
601
602 // FIXME: the desktop will call back to VNCServerST and an extra set
603 // of ExtendedDesktopSize messages will be sent. This is okay
604 // protocol-wise, but unnecessary.
605 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
606
Pierre Ossman04e62db2009-03-23 16:57:07 +0000607 writer()->writeExtendedDesktopSize(reasonClient, result,
608 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000609
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000610 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000611 if (result == resultSuccess) {
612 if (server->screenLayout != layout)
613 throw Exception("Desktop configured a different screen layout than requested");
614 server->notifyScreenLayoutChange(this);
615 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000616
617 // but always send back a reply to the requesting client
618 // (do this last as it might throw an exception on socket errors)
619 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000620}
621
Pierre Ossman2c764942011-11-14 15:54:30 +0000622void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
623{
624 if (flags & fenceFlagRequest) {
625 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000626 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000627
628 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
629 fenceDataLen = len;
630 delete [] fenceData;
631 if (len > 0) {
632 fenceData = new char[len];
633 memcpy(fenceData, data, len);
634 }
635
636 return;
637 }
638
639 // We handle everything synchronously so we trivially honor these modes
640 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
641
642 writer()->writeFence(flags, len, data);
643 return;
644 }
645
Pierre Ossman1b478e52011-11-15 12:08:30 +0000646 struct RTTInfo rttInfo;
647
Pierre Ossman2c764942011-11-14 15:54:30 +0000648 switch (len) {
649 case 0:
650 // Initial dummy fence;
651 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000652 case sizeof(struct RTTInfo):
653 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
654 handleRTTPong(rttInfo);
655 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000656 default:
657 vlog.error("Fence response of unexpected size received");
658 }
659}
660
Pierre Ossman1b478e52011-11-15 12:08:30 +0000661void VNCSConnectionST::enableContinuousUpdates(bool enable,
662 int x, int y, int w, int h)
663{
664 Rect rect;
665
666 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
667 throw Exception("Client tried to enable continuous updates when not allowed");
668
669 continuousUpdates = enable;
670
671 rect.setXYWH(x, y, w, h);
672 cuRegion.reset(rect);
673
674 if (enable) {
675 requested.clear();
676 writeFramebufferUpdate();
677 } else {
678 writer()->writeEndOfContinuousUpdates();
679 }
680}
681
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000682// supportsLocalCursor() is called whenever the status of
683// cp.supportsLocalCursor has changed. If the client does now support local
684// cursor, we make sure that the old server-side rendered cursor is cleaned up
685// and the cursor is sent to the client.
686
687void VNCSConnectionST::supportsLocalCursor()
688{
689 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000690 if (!renderedCursorRect.is_empty())
691 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692 drawRenderedCursor = false;
693 setCursor();
694 }
695}
696
Pierre Ossman2c764942011-11-14 15:54:30 +0000697void VNCSConnectionST::supportsFence()
698{
699 writer()->writeFence(fenceFlagRequest, 0, NULL);
700}
701
Pierre Ossman1b478e52011-11-15 12:08:30 +0000702void VNCSConnectionST::supportsContinuousUpdates()
703{
704 // We refuse to use continuous updates if we cannot monitor the buffer
705 // usage using fences.
706 if (!cp.supportsFence)
707 return;
708
709 writer()->writeEndOfContinuousUpdates();
710}
711
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000712
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000713bool VNCSConnectionST::handleTimeout(Timer* t)
714{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000715 try {
716 if (t == &updateTimer)
717 writeFramebufferUpdate();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000718 else if (t == &congestionTimer)
719 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100720 else if (t == &queryConnectTimer) {
721 if (state() == RFBSTATE_QUERYING)
722 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
723 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000724 } catch (rdr::Exception& e) {
725 close(e.str());
726 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000727
728 return false;
729}
730
731
Pierre Ossman1b478e52011-11-15 12:08:30 +0000732void VNCSConnectionST::writeRTTPing()
733{
734 struct RTTInfo rttInfo;
735
736 if (!cp.supportsFence)
737 return;
738
739 memset(&rttInfo, 0, sizeof(struct RTTInfo));
740
741 gettimeofday(&rttInfo.tv, NULL);
742 rttInfo.offset = sock->outStream().length();
743 rttInfo.inFlight = rttInfo.offset - ackedOffset;
744
745 // We need to make sure any old update are already processed by the
746 // time we get the response back. This allows us to reliably throttle
747 // back on client overload, as well as network overload.
748 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
749 sizeof(struct RTTInfo), (const char*)&rttInfo);
750
751 pingCounter++;
752
753 sentOffset = rttInfo.offset;
754
755 // Let some data flow before we adjust the settings
756 if (!congestionTimer.isStarted())
757 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
758}
759
760void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
761{
762 unsigned rtt, delay;
763 int bdp;
764
765 pingCounter--;
766
767 rtt = msSince(&rttInfo.tv);
768 if (rtt < 1)
769 rtt = 1;
770
771 ackedOffset = rttInfo.offset;
772
773 // Try to estimate wire latency by tracking lowest seen latency
774 if (rtt < baseRTT)
775 baseRTT = rtt;
776
777 if (rttInfo.inFlight > congWindow) {
778 seenCongestion = true;
779
780 // Estimate added delay because of overtaxed buffers
781 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
782
783 if (delay < rtt)
784 rtt -= delay;
785 else
786 rtt = 1;
787
788 // If we underestimate the congestion window, then we'll get a latency
789 // that's less than the wire latency, which will confuse other portions
790 // of the code.
791 if (rtt < baseRTT)
792 rtt = baseRTT;
793 }
794
795 // We only keep track of the minimum latency seen (for a given interval)
796 // on the basis that we want to avoid continous buffer issue, but don't
797 // mind (or even approve of) bursts.
798 if (rtt < minRTT)
799 minRTT = rtt;
800}
801
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000802bool VNCSConnectionST::isCongested()
803{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000804 int offset;
805
806 // Stuff still waiting in the send buffer?
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000807 if (sock->outStream().bufferUsage() > 0)
808 return true;
809
Pierre Ossman1b478e52011-11-15 12:08:30 +0000810 if (!cp.supportsFence)
811 return false;
812
813 // Idle for too long? (and no data on the wire)
814 //
815 // FIXME: This should really just be one baseRTT, but we're getting
816 // problems with triggering the idle timeout on each update.
817 // Maybe we need to use a moving average for the wire latency
818 // instead of baseRTT.
819 if ((sentOffset == ackedOffset) &&
820 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
821
822#ifdef CONGESTION_DEBUG
823 if (congWindow > INITIAL_WINDOW)
824 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
825 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
826#endif
827
828 // Close congestion window and allow a transfer
829 // FIXME: Reset baseRTT like Linux Vegas?
830 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
831
832 return false;
833 }
834
835 offset = sock->outStream().length();
836
837 // FIXME: Should we compensate for non-update data?
838 // (i.e. use sentOffset instead of offset)
839 if ((offset - ackedOffset) < congWindow)
840 return false;
841
842 // If we just have one outstanding "ping", that means the client has
843 // started receiving our update. In order to not regress compared to
844 // before we had congestion avoidance, we allow another update here.
845 // This could further clog up the tubes, but congestion control isn't
846 // really working properly right now anyway as the wire would otherwise
847 // be idle for at least RTT/2.
848 if (pingCounter == 1)
849 return false;
850
851 return true;
852}
853
854
855void VNCSConnectionST::updateCongestion()
856{
857 unsigned diff;
858
859 if (!seenCongestion)
860 return;
861
862 diff = minRTT - baseRTT;
863
864 if (diff > __rfbmin(100, baseRTT)) {
865 // Way too fast
866 congWindow = congWindow * baseRTT / minRTT;
867 } else if (diff > __rfbmin(50, baseRTT/2)) {
868 // Slightly too fast
869 congWindow -= 4096;
870 } else if (diff < 5) {
871 // Way too slow
872 congWindow += 8192;
873 } else if (diff < 25) {
874 // Too slow
875 congWindow += 4096;
876 }
877
878 if (congWindow < MINIMUM_WINDOW)
879 congWindow = MINIMUM_WINDOW;
880 if (congWindow > MAXIMUM_WINDOW)
881 congWindow = MAXIMUM_WINDOW;
882
883#ifdef CONGESTION_DEBUG
884 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
885 minRTT, baseRTT, congWindow / 1024,
886 congWindow * 8.0 / baseRTT / 1000.0);
887
888#ifdef TCP_INFO
889 struct tcp_info tcp_info;
890 socklen_t tcp_info_length;
891
892 tcp_info_length = sizeof(tcp_info);
893 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
894 (void *)&tcp_info, &tcp_info_length) == 0) {
895 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
896 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
897 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
898 }
899#endif
900
901#endif
902
903 minRTT = -1;
904 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000905}
906
907
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000908void VNCSConnectionST::writeFramebufferUpdate()
909{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000910 Region req;
911 UpdateInfo ui;
912 bool needNewUpdateInfo;
913
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000914 updateTimer.stop();
915
Pierre Ossman2c764942011-11-14 15:54:30 +0000916 // We're in the middle of processing a command that's supposed to be
917 // synchronised. Allowing an update to slip out right now might violate
918 // that synchronisation.
919 if (syncFence)
920 return;
921
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000922 // We try to aggregate responses, so don't send out anything whilst we
923 // still have incoming messages. processMessages() will give us another
924 // chance to run once things are idle.
925 if (inProcessMessages)
926 return;
927
Pierre Ossman1b478e52011-11-15 12:08:30 +0000928 if (state() != RFBSTATE_NORMAL)
929 return;
930 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000931 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000932
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000933 // Check that we actually have some space on the link and retry in a
934 // bit if things are congested.
935 if (isCongested()) {
936 updateTimer.start(50);
937 return;
938 }
939
Pierre Ossman36dadf82011-11-15 12:11:32 +0000940 // In continuous mode, we will be outputting at least three distinct
941 // messages. We need to aggregate these in order to not clog up TCP's
942 // congestion window.
943 network::TcpSocket::cork(sock->getFd(), true);
944
Pierre Ossmane9962f72009-04-23 12:31:42 +0000945 // First take care of any updates that cannot contain framebuffer data
946 // changes.
947 if (writer()->needNoDataUpdate()) {
948 writer()->writeNoDataUpdate();
949 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000950 if (!continuousUpdates)
Pierre Ossman36dadf82011-11-15 12:11:32 +0000951 goto out;
Pierre Ossmane9962f72009-04-23 12:31:42 +0000952 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000953
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000954 updates.enable_copyrect(cp.useCopyRect);
955
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000956 // Fetch updates from server object, and see if we are allowed to send
957 // anything right now (the framebuffer might have changed in ways we
958 // haven't yet been informed of).
959 if (!server->checkUpdate())
Pierre Ossman36dadf82011-11-15 12:11:32 +0000960 goto out;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000961
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000962 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000963 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000964 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000965
Pierre Ossman1b478e52011-11-15 12:08:30 +0000966 if (continuousUpdates)
967 req = cuRegion.union_(requested);
968 else
969 req = requested;
970
971 updates.getUpdateInfo(&ui, req);
972 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000973
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000974 // If the previous position of the rendered cursor overlaps the source of the
975 // copy, then when the copy happens the corresponding rectangle in the
976 // destination will be wrong, so add it to the changed region.
977
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000978 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
979 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000980 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000981 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000982 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000983 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000984 }
985 }
986
987 // If we need to remove the old rendered cursor, just add the rectangle to
988 // the changed region.
989
990 if (removeRenderedCursor) {
991 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000992 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000993 renderedCursorRect.clear();
994 removeRenderedCursor = false;
995 }
996
997 // Return if there is nothing to send the client.
998
999 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
Pierre Ossman36dadf82011-11-15 12:11:32 +00001000 goto out;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001001
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001002 // The `updates' object could change, make sure we have valid update info.
1003
1004 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001005 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001006
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001007 // If the client needs a server-side rendered cursor, work out the cursor
1008 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1009 // with the update region, we need to draw the rendered cursor regardless of
1010 // whether it has changed.
1011
1012 if (needRenderedCursor()) {
1013 renderedCursorRect
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +01001014 = server->renderedCursor.getEffectiveRect()
1015 .intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001016
1017 if (renderedCursorRect.is_empty()) {
1018 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001019 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001020 .intersect(renderedCursorRect).is_empty()) {
1021 drawRenderedCursor = true;
1022 }
1023
1024 // We could remove the new cursor rect from updates here. It's not clear
1025 // whether this is worth it. If we do remove it, then we won't draw over
1026 // the same bit of screen twice, but we have the overhead of a more complex
1027 // region.
1028
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001029 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001030 // updates.subtract(renderedCursorRect);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001031 // updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001032 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001033 }
1034
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001035 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Pierre Ossmanc0397262014-03-14 15:59:46 +01001036 RenderedCursor *cursor;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001037
Pierre Ossmanc0397262014-03-14 15:59:46 +01001038 cursor = NULL;
1039 if (drawRenderedCursor)
1040 cursor = &server->renderedCursor;
Pierre Ossman1b478e52011-11-15 12:08:30 +00001041
1042 writeRTTPing();
1043
Pierre Ossmanc0397262014-03-14 15:59:46 +01001044 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
1045
1046 writeRTTPing();
1047
1048 drawRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001049 requested.clear();
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001050 updates.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001051 }
Pierre Ossman36dadf82011-11-15 12:11:32 +00001052
1053out:
1054 network::TcpSocket::cork(sock->getFd(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001055}
1056
1057
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001058void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1059{
1060 if (!authenticated())
1061 return;
1062
1063 cp.screenLayout = server->screenLayout;
1064
1065 if (state() != RFBSTATE_NORMAL)
1066 return;
1067
1068 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1069 cp.screenLayout);
1070 writeFramebufferUpdate();
1071}
1072
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001073
1074// setCursor() is called whenever the cursor has changed shape or pixel format.
1075// If the client supports local cursor then it will arrange for the cursor to
1076// be sent to the client.
1077
1078void VNCSConnectionST::setCursor()
1079{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001080 if (state() != RFBSTATE_NORMAL)
1081 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001082
Pierre Ossman126e5642014-02-13 14:40:25 +01001083 cp.setCursor(server->cursor);
1084
1085 if (!writer()->writeSetCursor()) {
1086 if (!writer()->writeSetXCursor()) {
1087 // No client support
1088 return;
1089 }
1090 }
1091
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001092 writeFramebufferUpdate();
1093}
1094
1095void VNCSConnectionST::setDesktopName(const char *name)
1096{
1097 cp.setName(name);
1098
1099 if (state() != RFBSTATE_NORMAL)
1100 return;
1101
1102 if (!writer()->writeSetDesktopName()) {
1103 fprintf(stderr, "Client does not support desktop rename\n");
1104 return;
1105 }
1106
1107 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001108}
1109
1110void VNCSConnectionST::setSocketTimeouts()
1111{
1112 int timeoutms = rfb::Server::clientWaitTimeMillis;
1113 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1114 if (timeoutms == 0)
1115 timeoutms = -1;
1116 sock->inStream().setTimeout(timeoutms);
1117 sock->outStream().setTimeout(timeoutms);
1118}
1119
1120char* VNCSConnectionST::getStartTime()
1121{
1122 char* result = ctime(&startTime);
1123 result[24] = '\0';
1124 return result;
1125}
1126
1127void VNCSConnectionST::setStatus(int status)
1128{
1129 switch (status) {
1130 case 0:
1131 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1132 break;
1133 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +00001134 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001135 break;
1136 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001137 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001138 break;
1139 }
1140 framebufferUpdateRequest(server->pb->getRect(), false);
1141}
1142int VNCSConnectionST::getStatus()
1143{
1144 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1145 return 0;
1146 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1147 return 1;
1148 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1149 return 2;
1150 return 4;
1151}
1152