blob: 662d9f34d0c357ace4655da927974d09a26ce561 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01002 * Copyright 2009-2016 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
20//
21// VNCSConnectionST is our derived class of SConnection for VNCServerST - there
22// is one for each connected client. We think of VNCSConnectionST as part of
23// the VNCServerST implementation, so its methods are allowed full access to
24// members of VNCServerST.
25//
26
27#ifndef __RFB_VNCSCONNECTIONST_H__
28#define __RFB_VNCSCONNECTIONST_H__
29
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020030#include <map>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031
Pierre Ossmanc09e5582015-12-11 20:23:17 +010032#include <rfb/Congestion.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033#include <rfb/EncodeManager.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010034#include <rfb/SConnection.h>
35#include <rfb/Timer.h>
Pierre Ossman1b478e52011-11-15 12:08:30 +000036
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037namespace rfb {
Pierre Ossman707fa122015-12-11 20:21:20 +010038 class VNCServerST;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010039
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040 class VNCSConnectionST : public SConnection,
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000041 public Timer::Callback {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 public:
43 VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse);
44 virtual ~VNCSConnectionST();
45
Pierre Ossman7d64b332018-10-08 15:59:02 +020046 // SConnection methods
47
48 virtual bool accessCheck(AccessRights ar) const;
49 virtual void close(const char* reason);
50
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 // Methods called from VNCServerST. None of these methods ever knowingly
52 // throw an exception.
53
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 // init() must be called to initialise the protocol. If it fails it
55 // returns false, and close() will have been called.
56 bool init();
57
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058 // processMessages() processes incoming messages from the client, invoking
59 // various callbacks as a result. It continues to process messages until
60 // reading might block. shutdown() will be called on the connection's
61 // Socket if an error occurs, via the close() call.
62 void processMessages();
63
Pierre Ossmand408ca52016-04-29 14:26:05 +020064 // flushSocket() pushes any unwritten data on to the network.
65 void flushSocket();
66
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000067 // Called when the underlying pixelbuffer is resized or replaced.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068 void pixelBufferChange();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000069
70 // Wrappers to make these methods "safe" for VNCServerST.
71 void writeFramebufferUpdateOrClose();
72 void screenLayoutChangeOrClose(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 void setCursorOrClose();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000074 void bellOrClose();
75 void serverCutTextOrClose(const char *str, int len);
76 void setDesktopNameOrClose(const char *name);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +010077 void setLEDStateOrClose(unsigned int state);
Pierre Ossmane6aab242018-10-05 16:59:22 +020078 void approveConnectionOrClose(bool accept, const char* reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079
Pierre Ossmanf8586822018-10-08 16:03:43 +020080 // The following methods never throw exceptions
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081
Pierre Ossmanb114cec2011-11-20 15:36:11 +000082 // getComparerState() returns if this client would like the framebuffer
83 // comparer to be enabled.
84 bool getComparerState();
85
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 // renderedCursorChange() is called whenever the server-side rendered
87 // cursor changes shape or position. It ensures that the next update will
88 // clean up the old rendered cursor and if necessary draw the new rendered
89 // cursor.
90 void renderedCursorChange();
91
92 // needRenderedCursor() returns true if this client needs the server-side
93 // rendered cursor. This may be because it does not support local cursor
94 // or because the current cursor position has not been set by this client.
95 bool needRenderedCursor();
96
97 network::Socket* getSock() { return sock; }
Pierre Ossman7e1093e2018-10-11 08:08:19 +020098
99 // Change tracking
100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 void add_changed(const Region& region) { updates.add_changed(region); }
102 void add_copied(const Region& dest, const Point& delta) {
103 updates.add_copied(dest, delta);
104 }
105
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 private:
107 // SConnection callbacks
108
Pierre Ossmanf8586822018-10-08 16:03:43 +0200109 // These methods are invoked as callbacks from processMsg()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110
111 virtual void authSuccess();
112 virtual void queryConnection(const char* userName);
113 virtual void clientInit(bool shared);
114 virtual void setPixelFormat(const PixelFormat& pf);
115 virtual void pointerEvent(const Point& pos, int buttonMask);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200116 virtual void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 virtual void clientCutText(const char* str, int len);
118 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
Pierre Ossman34bb0612009-03-21 21:16:14 +0000119 virtual void setDesktopSize(int fb_width, int fb_height,
120 const ScreenSet& layout);
Pierre Ossman2c764942011-11-14 15:54:30 +0000121 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000122 virtual void enableContinuousUpdates(bool enable,
123 int x, int y, int w, int h);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124 virtual void supportsLocalCursor();
Pierre Ossman2c764942011-11-14 15:54:30 +0000125 virtual void supportsFence();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000126 virtual void supportsContinuousUpdates();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100127 virtual void supportsLEDState();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000129 // Timer callbacks
130 virtual bool handleTimeout(Timer* t);
131
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 // Internal methods
133
Pierre Ossman851e6802017-09-12 16:44:44 +0200134 bool isShiftPressed();
135
Pierre Ossman1b478e52011-11-15 12:08:30 +0000136 // Congestion control
137 void writeRTTPing();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000138 bool isCongested();
139
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140 // writeFramebufferUpdate() attempts to write a framebuffer update to the
141 // client.
142
143 void writeFramebufferUpdate();
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100144 void writeNoDataUpdate();
145 void writeDataUpdate();
Pierre Ossmanb65feda2018-11-23 17:45:04 +0100146 void writeLosslessRefresh();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000148 void screenLayoutChange(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149 void setCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000150 void setDesktopName(const char *name);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100151 void setLEDState(unsigned int state);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 void setSocketTimeouts();
153
Pierre Ossman7e1093e2018-10-11 08:08:19 +0200154 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 network::Socket* sock;
156 CharArray peerEndpoint;
Pierre Ossman7069bdd2015-02-06 14:41:58 +0100157 bool reverseConnection;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000158
159 bool inProcessMessages;
160
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000161 bool pendingSyncFence, syncFence;
Pierre Ossman2c764942011-11-14 15:54:30 +0000162 rdr::U32 fenceFlags;
163 unsigned fenceDataLen;
164 char *fenceData;
165
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100166 Congestion congestion;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000167 Timer congestionTimer;
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200168 Timer losslessTimer;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000169
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 VNCServerST* server;
171 SimpleUpdateTracker updates;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000172 Region requested;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200173 bool updateRenderedCursor, removeRenderedCursor;
174 Region damagedCursorRegion;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000175 bool continuousUpdates;
176 Region cuRegion;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100177 EncodeManager encodeManager;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200179 std::map<rdr::U32, rdr::U32> pressedKeys;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200181 Timer idleTimer;
182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 time_t pointerEventTime;
184 Point pointerEventPos;
Pierre Ossman25db44a2017-11-16 16:40:44 +0100185 bool clientHasCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 CharArray closeReason;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 };
189}
190#endif