blob: 8742fa6765ba67b810548675116dee9c6e20545d [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 {
40 class VNCSConnectionST : public SConnection,
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000041 public WriteSetCursorCallback,
42 public Timer::Callback {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 public:
44 VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse);
45 virtual ~VNCSConnectionST();
46
47 // Methods called from VNCServerST. None of these methods ever knowingly
48 // throw an exception.
49
50 // Unless otherwise stated, the SConnectionST may not be valid after any of
51 // these methods are called, since they catch exceptions and may have
52 // called close() which deletes the object.
53
54 // 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
58 // close() shuts down the socket to the client and deletes the
59 // SConnectionST object.
60 void close(const char* reason);
61
62 // processMessages() processes incoming messages from the client, invoking
63 // various callbacks as a result. It continues to process messages until
64 // reading might block. shutdown() will be called on the connection's
65 // Socket if an error occurs, via the close() call.
66 void processMessages();
67
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000068 // Called when the underlying pixelbuffer is resized or replaced.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069 void pixelBufferChange();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000070
71 // Wrappers to make these methods "safe" for VNCServerST.
72 void writeFramebufferUpdateOrClose();
73 void screenLayoutChangeOrClose(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 void setColourMapEntriesOrClose(int firstColour, int nColours);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 void setCursorOrClose();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000076 void bellOrClose();
77 void serverCutTextOrClose(const char *str, int len);
78 void setDesktopNameOrClose(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079
80 // checkIdleTimeout() returns the number of milliseconds left until the
81 // idle timeout expires. If it has expired, the connection is closed and
82 // zero is returned. Zero is also returned if there is no idle timeout.
83 int checkIdleTimeout();
84
85 // The following methods never throw exceptions nor do they ever delete the
86 // SConnectionST object.
87
88 // renderedCursorChange() is called whenever the server-side rendered
89 // cursor changes shape or position. It ensures that the next update will
90 // clean up the old rendered cursor and if necessary draw the new rendered
91 // cursor.
92 void renderedCursorChange();
93
94 // needRenderedCursor() returns true if this client needs the server-side
95 // rendered cursor. This may be because it does not support local cursor
96 // or because the current cursor position has not been set by this client.
97 bool needRenderedCursor();
98
99 network::Socket* getSock() { return sock; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 void add_changed(const Region& region) { updates.add_changed(region); }
101 void add_copied(const Region& dest, const Point& delta) {
102 updates.add_copied(dest, delta);
103 }
104
105 const char* getPeerEndpoint() const {return peerEndpoint.buf;}
106
107 // approveConnectionOrClose() is called some time after
108 // VNCServerST::queryConnection() has returned with PENDING to accept or
109 // reject the connection. The accept argument should be true for
110 // acceptance, or false for rejection, in which case a string reason may
111 // also be given.
112
113 void approveConnectionOrClose(bool accept, const char* reason);
114
115 char* getStartTime();
116
117 void setStatus(int status);
118 int getStatus();
119
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000120 private:
121 // SConnection callbacks
122
123 // These methods are invoked as callbacks from processMsg(). Note that
124 // none of these methods should call any of the above methods which may
125 // delete the SConnectionST object.
126
127 virtual void authSuccess();
128 virtual void queryConnection(const char* userName);
129 virtual void clientInit(bool shared);
130 virtual void setPixelFormat(const PixelFormat& pf);
131 virtual void pointerEvent(const Point& pos, int buttonMask);
132 virtual void keyEvent(rdr::U32 key, bool down);
133 virtual void clientCutText(const char* str, int len);
134 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
Pierre Ossman34bb0612009-03-21 21:16:14 +0000135 virtual void setDesktopSize(int fb_width, int fb_height,
136 const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 virtual void setInitialColourMap();
Pierre Ossman2c764942011-11-14 15:54:30 +0000138 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000139 virtual void enableContinuousUpdates(bool enable,
140 int x, int y, int w, int h);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000141 virtual void supportsLocalCursor();
Pierre Ossman2c764942011-11-14 15:54:30 +0000142 virtual void supportsFence();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000143 virtual void supportsContinuousUpdates();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000144
145 // setAccessRights() allows a security package to limit the access rights
146 // of a VNCSConnectioST to the server. These access rights are applied
147 // such that the actual rights granted are the minimum of the server's
148 // default access settings and the connection's access settings.
149 virtual void setAccessRights(AccessRights ar) {accessRights=ar;}
150
151 // WriteSetCursorCallback
152 virtual void writeSetCursorCallback();
153
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000154 // Timer callbacks
155 virtual bool handleTimeout(Timer* t);
156
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 // Internal methods
158
Pierre Ossman1b478e52011-11-15 12:08:30 +0000159 // Congestion control
160 void writeRTTPing();
161 void handleRTTPong(const struct RTTInfo &rttInfo);
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000162 bool isCongested();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000163 void updateCongestion();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000164
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 // writeFramebufferUpdate() attempts to write a framebuffer update to the
166 // client.
167
168 void writeFramebufferUpdate();
169
170 void writeRenderedCursorRect();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000171 void screenLayoutChange(rdr::U16 reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000172 void setColourMapEntries(int firstColour, int nColours);
173 void setCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000174 void setDesktopName(const char *name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 void setSocketTimeouts();
176
177 network::Socket* sock;
178 CharArray peerEndpoint;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000179
180 bool inProcessMessages;
181
Pierre Ossman2c764942011-11-14 15:54:30 +0000182 bool syncFence;
183 rdr::U32 fenceFlags;
184 unsigned fenceDataLen;
185 char *fenceData;
186
Pierre Ossman1b478e52011-11-15 12:08:30 +0000187 unsigned baseRTT;
188 unsigned congWindow;
189 int ackedOffset, sentOffset;
190
191 unsigned minRTT;
192 bool seenCongestion;
193 unsigned pingCounter;
194 Timer congestionTimer;
195
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196 VNCServerST* server;
197 SimpleUpdateTracker updates;
198 TransImageGetter image_getter;
199 Region requested;
200 bool drawRenderedCursor, removeRenderedCursor;
201 Rect renderedCursorRect;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000202 bool continuousUpdates;
203 Region cuRegion;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000205 Timer updateTimer;
206
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207 std::set<rdr::U32> pressedKeys;
208
209 time_t lastEventTime;
210 time_t pointerEventTime;
211 Point pointerEventPos;
212
213 AccessRights accessRights;
214
215 CharArray closeReason;
216 time_t startTime;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217 };
218}
219#endif