blob: 6655a0d6a93d4e225df18c75e12c97a873b8c215 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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
36namespace rfb {
37
38 class VNCSConnectionST;
39 class ComparingUpdateTracker;
40 class PixelBuffer;
41
42 class VNCServerST : public VNCServer, public network::SocketServer {
43 public:
44 // -=- Constructors
45
46 // Create a server exporting the supplied desktop.
47 VNCServerST(const char* name_, SDesktop* desktop_,
48 SSecurityFactory* securityFactory_=0);
49 virtual ~VNCServerST();
50
51
52 // Methods overridden from SocketServer
53
54 // - Run a client connection on the supplied socket
55 // This causes the server to allocate the required structures
56 // to handle a client connection, and to initialise the RFB
57 // protocol.
58 // NB: The server assumes ownership of the Socket object.
59
60 virtual void addClient(network::Socket* sock);
61
62 // - Process an input event on a particular Socket
63 // The platform-specific side of the server implementation calls
64 // this method whenever data arrives on one of the active
65 // network sockets.
66 // The method returns true if the Socket is still in use by the
67 // server, or false if it is no longer required and has been
68 // deleted.
69 // NB: If false is returned then the Socket is deleted and must
70 // not be accessed again!
71
72 virtual bool processSocketEvent(network::Socket* sock);
73
74 // - checkTimeouts() returns the number of milliseconds left until the next
75 // idle timeout expires. If any have already expired, the corresponding
76 // connections are closed. Zero is returned if there is no idle timeout.
77
78 virtual int checkTimeouts();
79
80
81 // Methods overridden from VNCServer
82
83 virtual void setPixelBuffer(PixelBuffer* pb);
84 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);
88 virtual bool clientsReadyForUpdate();
89 virtual void tryUpdate();
90 virtual void setCursor(int width, int height, int hotspotX, int hotspotY,
91 void* cursorData, void* mask);
92 virtual void setCursorPos(int x, int y);
93 virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;}
94
95 virtual void bell();
96
97 // - Close all currently-connected clients, by calling
98 // their close() method with the supplied reason.
99 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
100
101 // VNCServerST-only methods
102
103 // If a particular VNCSConnectionST* is specified then
104 // that connection will NOT be closed.
105 void closeClients(const char* reason, network::Socket* sock);
106
107 // addClient() with an extra flag to say if this is a reverse connection to
108 // a listening client. Reverse connections are not authenticated and are
109 // always shared (unless the NeverShared parameter is set).
110
111 void addClient(network::Socket* sock, bool reverse);
112
113
114 // getSockets() gets a list of sockets. This can be used to generate an
115 // fd_set for calling select().
116
117 void getSockets(std::list<network::Socket*>* sockets);
118
119 // getSConnection() gets the SConnection for a particular Socket. If
120 // the Socket is not recognised then null is returned.
121
122 SConnection* getSConnection(network::Socket* sock);
123
124 // getDesktopSize() returns the size of the SDesktop exported by this
125 // server.
126 Point getDesktopSize() const {return desktop->getFbSize();}
127
128 // getName() returns the name of this VNC Server. NB: The value returned
129 // is the server's internal buffer which may change after any other methods
130 // are called - take a copy if necessary.
131 const char* getName() const {return name.buf;}
132
133 // setName() specifies the desktop name that the server should provide to
134 // clients
135 void setName(const char* name_) {name.replaceBuf(strDup(name_));}
136
137 // A QueryConnectionHandler, if supplied, is passed details of incoming
138 // connections to approve, reject, or query the user about.
139 //
140 // queryConnection() is called when a connection has been
141 // successfully authenticated. The sock and userName arguments identify
142 // the socket and the name of the authenticated user, if any. It should
143 // return ACCEPT if the connection should be accepted, REJECT if it should
144 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
145 // is returned, *reason can be set to a string describing the reason - this
146 // will be delete[]ed when it is finished with. If PENDING is returned,
147 // approveConnection() must be called some time later to accept or reject
148 // the connection.
149 enum queryResult { ACCEPT, REJECT, PENDING };
150 struct QueryConnectionHandler {
151 virtual ~QueryConnectionHandler() {}
152 virtual queryResult queryConnection(network::Socket* sock,
153 const char* userName,
154 char** reason) = 0;
155 };
156 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
157 queryConnectionHandler = qch;
158 }
159
160 // queryConnection is called as described above, and either passes the
161 // request on to the registered handler, or accepts the connection if
162 // no handler has been specified.
163 virtual queryResult queryConnection(network::Socket* sock,
164 const char* userName,
165 char** reason) {
166 return queryConnectionHandler
167 ? queryConnectionHandler->queryConnection(sock, userName, reason)
168 : ACCEPT;
169 }
170
171 // approveConnection() is called by the active QueryConnectionHandler,
172 // some time after queryConnection() has returned with PENDING, to accept
173 // or reject the connection. The accept argument should be true for
174 // acceptance, or false for rejection, in which case a string reason may
175 // also be given.
176 void approveConnection(network::Socket* sock, bool accept,
177 const char* reason);
178
179 // setBlacklist() is called to replace the VNCServerST's internal
180 // Blacklist instance with another instance. This allows a single
181 // Blacklist to be shared by multiple VNCServerST instances.
182 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
183
184 // setEconomicTranslate() determines (for new connections) whether pixels
185 // should be translated for <=16bpp clients using a large lookup table (fast)
186 // or separate, smaller R, G and B tables (slower). If set to true, small tables
187 // are used, to save memory.
188 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
189
190 protected:
191
192 friend class VNCSConnectionST;
193
194 void startDesktop();
195
196 static LogWriter connectionsLog;
197 Blacklist blacklist;
198 Blacklist* blHosts;
199
200 SDesktop* desktop;
201 bool desktopStarted;
202 PixelBuffer* pb;
203
204 CharArray name;
205
206 std::list<VNCSConnectionST*> clients;
207 VNCSConnectionST* pointerClient;
208 std::list<network::Socket*> closingSockets;
209
210 ComparingUpdateTracker* comparer;
211
212 Point cursorPos;
213 Cursor cursor;
214 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
215 Point renderedCursorTL;
216 ManagedPixelBuffer renderedCursor;
217 bool renderedCursorInvalid;
218
219 // - Check how many of the clients are authenticated.
220 int authClientCount();
221
222 bool needRenderedCursor();
223 void checkUpdate();
224
225 SSecurityFactory* securityFactory;
226 QueryConnectionHandler* queryConnectionHandler;
227 bool useEconomicTranslate;
228 };
229
230};
231
232#endif
233