blob: ca5c4f015b8902d9aafbd6e09e0851a0bbd2e08e [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00002 * Copyright 2009-2011 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
30#include <set>
31#include <rfb/SConnection.h>
32#include <rfb/SMsgWriter.h>
33#include <rfb/TransImageGetter.h>
34#include <rfb/VNCServerST.h>
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000035#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 Ossmanfdba3fe2014-01-31 13:12:18 +010040 class Encoder;
41
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 class VNCSConnectionST : public SConnection,
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000043 public WriteSetCursorCallback,
44 public Timer::Callback {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000045 public:
46 VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse);
47 virtual ~VNCSConnectionST();
48
49 // Methods called from VNCServerST. None of these methods ever knowingly
50 // throw an exception.
51
52 // Unless otherwise stated, the SConnectionST may not be valid after any of
53 // these methods are called, since they catch exceptions and may have
54 // called close() which deletes the object.
55
56 // init() must be called to initialise the protocol. If it fails it
57 // returns false, and close() will have been called.
58 bool init();
59
60 // close() shuts down the socket to the client and deletes the
61 // SConnectionST object.
62 void close(const char* reason);
63
64 // processMessages() processes incoming messages from the client, invoking
65 // various callbacks as a result. It continues to process messages until
66 // reading might block. shutdown() will be called on the connection's
67 // Socket if an error occurs, via the close() call.
68 void processMessages();
69
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000070 // Called when the underlying pixelbuffer is resized or replaced.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071 void pixelBufferChange();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000072
73 // Wrappers to make these methods "safe" for VNCServerST.
74 void writeFramebufferUpdateOrClose();
75 void screenLayoutChangeOrClose(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 void setCursorOrClose();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000077 void bellOrClose();
78 void serverCutTextOrClose(const char *str, int len);
79 void setDesktopNameOrClose(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080
81 // checkIdleTimeout() returns the number of milliseconds left until the
82 // idle timeout expires. If it has expired, the connection is closed and
83 // zero is returned. Zero is also returned if there is no idle timeout.
84 int checkIdleTimeout();
85
86 // The following methods never throw exceptions nor do they ever delete the
87 // SConnectionST object.
88
Pierre Ossmanb114cec2011-11-20 15:36:11 +000089 // getComparerState() returns if this client would like the framebuffer
90 // comparer to be enabled.
91 bool getComparerState();
92
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 // renderedCursorChange() is called whenever the server-side rendered
94 // cursor changes shape or position. It ensures that the next update will
95 // clean up the old rendered cursor and if necessary draw the new rendered
96 // cursor.
97 void renderedCursorChange();
98
99 // needRenderedCursor() returns true if this client needs the server-side
100 // rendered cursor. This may be because it does not support local cursor
101 // or because the current cursor position has not been set by this client.
102 bool needRenderedCursor();
103
104 network::Socket* getSock() { return sock; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 void add_changed(const Region& region) { updates.add_changed(region); }
106 void add_copied(const Region& dest, const Point& delta) {
107 updates.add_copied(dest, delta);
108 }
109
110 const char* getPeerEndpoint() const {return peerEndpoint.buf;}
111
112 // approveConnectionOrClose() is called some time after
113 // VNCServerST::queryConnection() has returned with PENDING to accept or
114 // reject the connection. The accept argument should be true for
115 // acceptance, or false for rejection, in which case a string reason may
116 // also be given.
117
118 void approveConnectionOrClose(bool accept, const char* reason);
119
120 char* getStartTime();
121
122 void setStatus(int status);
123 int getStatus();
124
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125 private:
126 // SConnection callbacks
127
128 // These methods are invoked as callbacks from processMsg(). Note that
129 // none of these methods should call any of the above methods which may
130 // delete the SConnectionST object.
131
132 virtual void authSuccess();
133 virtual void queryConnection(const char* userName);
134 virtual void clientInit(bool shared);
135 virtual void setPixelFormat(const PixelFormat& pf);
136 virtual void pointerEvent(const Point& pos, int buttonMask);
137 virtual void keyEvent(rdr::U32 key, bool down);
138 virtual void clientCutText(const char* str, int len);
139 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
Pierre Ossman34bb0612009-03-21 21:16:14 +0000140 virtual void setDesktopSize(int fb_width, int fb_height,
141 const ScreenSet& layout);
Pierre Ossman2c764942011-11-14 15:54:30 +0000142 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000143 virtual void enableContinuousUpdates(bool enable,
144 int x, int y, int w, int h);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 virtual void supportsLocalCursor();
Pierre Ossman2c764942011-11-14 15:54:30 +0000146 virtual void supportsFence();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000147 virtual void supportsContinuousUpdates();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000148
149 // setAccessRights() allows a security package to limit the access rights
150 // of a VNCSConnectioST to the server. These access rights are applied
151 // such that the actual rights granted are the minimum of the server's
152 // default access settings and the connection's access settings.
153 virtual void setAccessRights(AccessRights ar) {accessRights=ar;}
154
155 // WriteSetCursorCallback
156 virtual void writeSetCursorCallback();
157
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000158 // Timer callbacks
159 virtual bool handleTimeout(Timer* t);
160
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 // Internal methods
162
Pierre Ossman1b478e52011-11-15 12:08:30 +0000163 // Congestion control
164 void writeRTTPing();
165 void handleRTTPong(const struct RTTInfo &rttInfo);
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000166 bool isCongested();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000167 void updateCongestion();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000168
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 // writeFramebufferUpdate() attempts to write a framebuffer update to the
170 // client.
171
172 void writeFramebufferUpdate();
173
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000174 void screenLayoutChange(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 void setCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000176 void setDesktopName(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 void setSocketTimeouts();
178
179 network::Socket* sock;
180 CharArray peerEndpoint;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000181
182 bool inProcessMessages;
183
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000184 bool pendingSyncFence, syncFence;
Pierre Ossman2c764942011-11-14 15:54:30 +0000185 rdr::U32 fenceFlags;
186 unsigned fenceDataLen;
187 char *fenceData;
188
Pierre Ossman1b478e52011-11-15 12:08:30 +0000189 unsigned baseRTT;
190 unsigned congWindow;
191 int ackedOffset, sentOffset;
192
193 unsigned minRTT;
194 bool seenCongestion;
195 unsigned pingCounter;
196 Timer congestionTimer;
197
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198 VNCServerST* server;
199 SimpleUpdateTracker updates;
200 TransImageGetter image_getter;
201 Region requested;
202 bool drawRenderedCursor, removeRenderedCursor;
203 Rect renderedCursorRect;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000204 bool continuousUpdates;
205 Region cuRegion;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100206 Encoder* encoders[encodingMax+1];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000208 Timer updateTimer;
209
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210 std::set<rdr::U32> pressedKeys;
211
212 time_t lastEventTime;
213 time_t pointerEventTime;
214 Point pointerEventPos;
215
216 AccessRights accessRights;
217
218 CharArray closeReason;
219 time_t startTime;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 };
221}
222#endif