blob: aa9ade0f906f1493250469d7c1f04cbb26a8d435 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- VNCServerST.h
20
21// Single-threaded VNCServer implementation
22
23#ifndef __RFB_VNCSERVERST_H__
24#define __RFB_VNCSERVERST_H__
25
26#include <list>
27
28#include <rfb/SDesktop.h>
29#include <rfb/VNCServer.h>
30#include <rfb/Configuration.h>
31#include <rfb/LogWriter.h>
32#include <rfb/Blacklist.h>
33#include <rfb/Cursor.h>
34#include <network/Socket.h>
35#include <rfb/ListConnInfo.h>
Pierre Ossman34e62f32009-03-20 21:46:12 +000036#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037
38namespace rfb {
39
40 class VNCSConnectionST;
41 class ComparingUpdateTracker;
42 class PixelBuffer;
43 class KeyRemapper;
44
45 class VNCServerST : public VNCServer, public network::SocketServer {
46 public:
47 // -=- Constructors
48
49 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000050 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 virtual ~VNCServerST();
52
53
54 // Methods overridden from SocketServer
55
56 // addSocket
57 // Causes the server to allocate an RFB-protocol management
58 // structure for the socket & initialise it.
59 virtual void addSocket(network::Socket* sock, bool outgoing=false);
60
61 // removeSocket
62 // Clean up any resources associated with the Socket
63 virtual void removeSocket(network::Socket* sock);
64
65 // processSocketEvent
66 // Read more RFB data from the Socket. If an error occurs during
67 // processing then shutdown() is called on the Socket, causing
68 // removeSocket() to be called by the caller at a later time.
69 virtual void processSocketEvent(network::Socket* sock);
70
71 // checkTimeouts
72 // Returns the number of milliseconds left until the next idle timeout
73 // expires. If any have already expired, the corresponding connections
74 // are closed. Zero is returned if there is no idle timeout.
75 virtual int checkTimeouts();
76
77
78 // Methods overridden from VNCServer
79
Pierre Ossman04e62db2009-03-23 16:57:07 +000080 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000082 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000083 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
85 virtual void serverCutText(const char* str, int len);
86 virtual void add_changed(const Region &region);
87 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 virtual bool clientsReadyForUpdate();
89 virtual void tryUpdate();
90 virtual void setCursor(int width, int height, const Point& hotspot,
91 void* cursorData, void* mask);
92 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093
94 virtual void bell();
95
96 // - Close all currently-connected clients, by calling
97 // their close() method with the supplied reason.
98 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
99
100 // VNCServerST-only methods
101
102 // closeClients() closes all RFB sessions, except the specified one (if
103 // any), and logs the specified reason for closure.
104 void closeClients(const char* reason, network::Socket* sock);
105
106 // getSockets() gets a list of sockets. This can be used to generate an
107 // fd_set for calling select().
108
109 void getSockets(std::list<network::Socket*>* sockets);
110
111 // getSConnection() gets the SConnection for a particular Socket. If
112 // the Socket is not recognised then null is returned.
113
114 SConnection* getSConnection(network::Socket* sock);
115
116 // getDesktopSize() returns the size of the SDesktop exported by this
117 // server.
118 Point getDesktopSize() const {return desktop->getFbSize();}
119
120 // getName() returns the name of this VNC Server. NB: The value returned
121 // is the server's internal buffer which may change after any other methods
122 // are called - take a copy if necessary.
123 const char* getName() const {return name.buf;}
124
125 // setName() specifies the desktop name that the server should provide to
126 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000127 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128
129 // A QueryConnectionHandler, if supplied, is passed details of incoming
130 // connections to approve, reject, or query the user about.
131 //
132 // queryConnection() is called when a connection has been
133 // successfully authenticated. The sock and userName arguments identify
134 // the socket and the name of the authenticated user, if any. It should
135 // return ACCEPT if the connection should be accepted, REJECT if it should
136 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
137 // is returned, *reason can be set to a string describing the reason - this
138 // will be delete[]ed when it is finished with. If PENDING is returned,
139 // approveConnection() must be called some time later to accept or reject
140 // the connection.
141 enum queryResult { ACCEPT, REJECT, PENDING };
142 struct QueryConnectionHandler {
143 virtual ~QueryConnectionHandler() {}
144 virtual queryResult queryConnection(network::Socket* sock,
145 const char* userName,
146 char** reason) = 0;
147 };
148 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
149 queryConnectionHandler = qch;
150 }
151
152 // queryConnection is called as described above, and either passes the
153 // request on to the registered handler, or accepts the connection if
154 // no handler has been specified.
155 virtual queryResult queryConnection(network::Socket* sock,
156 const char* userName,
157 char** reason) {
158 return queryConnectionHandler
159 ? queryConnectionHandler->queryConnection(sock, userName, reason)
160 : ACCEPT;
161 }
162
163 // approveConnection() is called by the active QueryConnectionHandler,
164 // some time after queryConnection() has returned with PENDING, to accept
165 // or reject the connection. The accept argument should be true for
166 // acceptance, or false for rejection, in which case a string reason may
167 // also be given.
168 void approveConnection(network::Socket* sock, bool accept,
169 const char* reason);
170
171 // setBlacklist() is called to replace the VNCServerST's internal
172 // Blacklist instance with another instance. This allows a single
173 // Blacklist to be shared by multiple VNCServerST instances.
174 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
175
176 // setEconomicTranslate() determines (for new connections) whether pixels
177 // should be translated for <=16bpp clients using a large lookup table
178 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
179 // small tables are used, to save memory.
180 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
181
182 // setKeyRemapper() replaces the VNCServerST's default key remapper.
183 // NB: A null pointer is valid here.
184 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
185
186 void getConnInfo(ListConnInfo * listConn);
187 void setConnStatus(ListConnInfo* listConn);
188
189 bool getDisable() { return disableclients;};
190 void setDisable(bool disable) { disableclients = disable;};
191
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192 protected:
193
194 friend class VNCSConnectionST;
195
196 void startDesktop();
197
198 static LogWriter connectionsLog;
199 Blacklist blacklist;
200 Blacklist* blHosts;
201
202 SDesktop* desktop;
203 bool desktopStarted;
204 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000205 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207 CharArray name;
208
209 std::list<VNCSConnectionST*> clients;
210 VNCSConnectionST* pointerClient;
211 std::list<network::Socket*> closingSockets;
212
213 ComparingUpdateTracker* comparer;
214
215 Point cursorPos;
216 Cursor cursor;
217 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
218 Point renderedCursorTL;
219 ManagedPixelBuffer renderedCursor;
220 bool renderedCursorInvalid;
221
222 // - Check how many of the clients are authenticated.
223 int authClientCount();
224
225 bool needRenderedCursor();
226 void checkUpdate();
227
Pierre Ossman04e62db2009-03-23 16:57:07 +0000228 void notifyScreenLayoutChange(VNCSConnectionST *requester);
229
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230 QueryConnectionHandler* queryConnectionHandler;
231 KeyRemapper* keyRemapper;
232 bool useEconomicTranslate;
233
234 time_t lastUserInputTime;
235 time_t lastDisconnectTime;
236 time_t lastConnectionTime;
237
238 bool disableclients;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 };
240
241};
242
243#endif
244