blob: 7022d8a9c9b70d618df17b7615f425e24c1c0059 [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 Ossmanb2a417c2015-12-11 19:32:21 +010030#include <list>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031#include <set>
Pierre Ossmanb2a417c2015-12-11 19:32:21 +010032
Pierre Ossmanc0397262014-03-14 15:59:46 +010033#include <rfb/EncodeManager.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010034#include <rfb/SConnection.h>
35#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036
Pierre Ossman1b478e52011-11-15 12:08:30 +000037struct RTTInfo;
38
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039namespace rfb {
Pierre Ossman707fa122015-12-11 20:21:20 +010040 class VNCServerST;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010041
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 class VNCSConnectionST : public SConnection,
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000043 public Timer::Callback {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044 public:
45 VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse);
46 virtual ~VNCSConnectionST();
47
48 // Methods called from VNCServerST. None of these methods ever knowingly
49 // throw an exception.
50
51 // Unless otherwise stated, the SConnectionST may not be valid after any of
52 // these methods are called, since they catch exceptions and may have
53 // called close() which deletes the object.
54
55 // init() must be called to initialise the protocol. If it fails it
56 // returns false, and close() will have been called.
57 bool init();
58
59 // close() shuts down the socket to the client and deletes the
60 // SConnectionST object.
61 void close(const char* reason);
62
63 // processMessages() processes incoming messages from the client, invoking
64 // various callbacks as a result. It continues to process messages until
65 // reading might block. shutdown() will be called on the connection's
66 // Socket if an error occurs, via the close() call.
67 void processMessages();
68
Pierre Ossmand408ca52016-04-29 14:26:05 +020069 // flushSocket() pushes any unwritten data on to the network.
70 void flushSocket();
71
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000072 // Called when the underlying pixelbuffer is resized or replaced.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 void pixelBufferChange();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000074
75 // Wrappers to make these methods "safe" for VNCServerST.
76 void writeFramebufferUpdateOrClose();
77 void screenLayoutChangeOrClose(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078 void setCursorOrClose();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000079 void bellOrClose();
80 void serverCutTextOrClose(const char *str, int len);
81 void setDesktopNameOrClose(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082
83 // checkIdleTimeout() returns the number of milliseconds left until the
84 // idle timeout expires. If it has expired, the connection is closed and
85 // zero is returned. Zero is also returned if there is no idle timeout.
86 int checkIdleTimeout();
87
88 // The following methods never throw exceptions nor do they ever delete the
89 // SConnectionST object.
90
Pierre Ossmanb114cec2011-11-20 15:36:11 +000091 // getComparerState() returns if this client would like the framebuffer
92 // comparer to be enabled.
93 bool getComparerState();
94
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 // renderedCursorChange() is called whenever the server-side rendered
96 // cursor changes shape or position. It ensures that the next update will
97 // clean up the old rendered cursor and if necessary draw the new rendered
98 // cursor.
99 void renderedCursorChange();
100
101 // needRenderedCursor() returns true if this client needs the server-side
102 // rendered cursor. This may be because it does not support local cursor
103 // or because the current cursor position has not been set by this client.
104 bool needRenderedCursor();
105
106 network::Socket* getSock() { return sock; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 void add_changed(const Region& region) { updates.add_changed(region); }
108 void add_copied(const Region& dest, const Point& delta) {
109 updates.add_copied(dest, delta);
110 }
111
112 const char* getPeerEndpoint() const {return peerEndpoint.buf;}
113
114 // approveConnectionOrClose() is called some time after
115 // VNCServerST::queryConnection() has returned with PENDING to accept or
116 // reject the connection. The accept argument should be true for
117 // acceptance, or false for rejection, in which case a string reason may
118 // also be given.
119
120 void approveConnectionOrClose(bool accept, const char* reason);
121
122 char* getStartTime();
123
124 void setStatus(int status);
125 int getStatus();
126
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127 private:
128 // SConnection callbacks
129
130 // These methods are invoked as callbacks from processMsg(). Note that
131 // none of these methods should call any of the above methods which may
132 // delete the SConnectionST object.
133
134 virtual void authSuccess();
135 virtual void queryConnection(const char* userName);
136 virtual void clientInit(bool shared);
137 virtual void setPixelFormat(const PixelFormat& pf);
138 virtual void pointerEvent(const Point& pos, int buttonMask);
139 virtual void keyEvent(rdr::U32 key, bool down);
140 virtual void clientCutText(const char* str, int len);
141 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
Pierre Ossman34bb0612009-03-21 21:16:14 +0000142 virtual void setDesktopSize(int fb_width, int fb_height,
143 const ScreenSet& layout);
Pierre Ossman2c764942011-11-14 15:54:30 +0000144 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000145 virtual void enableContinuousUpdates(bool enable,
146 int x, int y, int w, int h);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147 virtual void supportsLocalCursor();
Pierre Ossman2c764942011-11-14 15:54:30 +0000148 virtual void supportsFence();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000149 virtual void supportsContinuousUpdates();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150
151 // setAccessRights() allows a security package to limit the access rights
152 // of a VNCSConnectioST to the server. These access rights are applied
153 // such that the actual rights granted are the minimum of the server's
154 // default access settings and the connection's access settings.
155 virtual void setAccessRights(AccessRights ar) {accessRights=ar;}
156
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000157 // Timer callbacks
158 virtual bool handleTimeout(Timer* t);
159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 // Internal methods
161
Pierre Ossman1b478e52011-11-15 12:08:30 +0000162 // Congestion control
163 void writeRTTPing();
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100164 void handleRTTPong();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000165 bool isCongested();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000166 void updateCongestion();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000167
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 // writeFramebufferUpdate() attempts to write a framebuffer update to the
169 // client.
170
171 void writeFramebufferUpdate();
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100172 void writeNoDataUpdate();
173 void writeDataUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000175 void screenLayoutChange(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176 void setCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000177 void setDesktopName(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 void setSocketTimeouts();
179
180 network::Socket* sock;
181 CharArray peerEndpoint;
Pierre Ossman7069bdd2015-02-06 14:41:58 +0100182 bool reverseConnection;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000183
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100184 Timer queryConnectTimer;
185
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000186 bool inProcessMessages;
187
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000188 bool pendingSyncFence, syncFence;
Pierre Ossman2c764942011-11-14 15:54:30 +0000189 rdr::U32 fenceFlags;
190 unsigned fenceDataLen;
191 char *fenceData;
192
Pierre Ossman1b478e52011-11-15 12:08:30 +0000193 unsigned baseRTT;
194 unsigned congWindow;
Pierre Ossmana9af1f12015-06-17 10:47:28 +0200195 unsigned ackedOffset, sentOffset;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000196
197 unsigned minRTT;
198 bool seenCongestion;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000199 Timer congestionTimer;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100200 std::list<struct RTTInfo> pings;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000201
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 VNCServerST* server;
203 SimpleUpdateTracker updates;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 Region requested;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200205 bool updateRenderedCursor, removeRenderedCursor;
206 Region damagedCursorRegion;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000207 bool continuousUpdates;
208 Region cuRegion;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100209 EncodeManager encodeManager;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 std::set<rdr::U32> pressedKeys;
212
213 time_t lastEventTime;
214 time_t pointerEventTime;
215 Point pointerEventPos;
216
217 AccessRights accessRights;
218
219 CharArray closeReason;
220 time_t startTime;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 };
222}
223#endif