blob: 0fa2c87500dd989fe41c36a85beb3010323d0316 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman24684e52016-12-05 16:58:19 +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// -=- VNCServerST.h
21
22// Single-threaded VNCServer implementation
23
24#ifndef __RFB_VNCSERVERST_H__
25#define __RFB_VNCSERVERST_H__
26
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000027#include <sys/time.h>
28
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029#include <rfb/SDesktop.h>
30#include <rfb/VNCServer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031#include <rfb/LogWriter.h>
32#include <rfb/Blacklist.h>
33#include <rfb/Cursor.h>
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000034#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035#include <network/Socket.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;
Pierre Ossman707fa122015-12-11 20:21:20 +010042 class ListConnInfo;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 class PixelBuffer;
44 class KeyRemapper;
45
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000046 class VNCServerST : public VNCServer,
47 public Timer::Callback,
48 public network::SocketServer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049 public:
50 // -=- Constructors
51
52 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000053 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 virtual ~VNCServerST();
55
56
57 // Methods overridden from SocketServer
58
59 // addSocket
60 // Causes the server to allocate an RFB-protocol management
61 // structure for the socket & initialise it.
62 virtual void addSocket(network::Socket* sock, bool outgoing=false);
63
64 // removeSocket
65 // Clean up any resources associated with the Socket
66 virtual void removeSocket(network::Socket* sock);
67
Pierre Ossman574dc642016-10-05 13:39:11 +020068 // getSockets() gets a list of sockets. This can be used to generate an
69 // fd_set for calling select().
70 virtual void getSockets(std::list<network::Socket*>* sockets);
71
Pierre Ossmand408ca52016-04-29 14:26:05 +020072 // processSocketReadEvent
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 // Read more RFB data from the Socket. If an error occurs during
74 // processing then shutdown() is called on the Socket, causing
75 // removeSocket() to be called by the caller at a later time.
Pierre Ossmand408ca52016-04-29 14:26:05 +020076 virtual void processSocketReadEvent(network::Socket* sock);
77
78 // processSocketWriteEvent
79 // Flush pending data from the Socket on to the network.
80 virtual void processSocketWriteEvent(network::Socket* sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081
82 // checkTimeouts
83 // Returns the number of milliseconds left until the next idle timeout
84 // expires. If any have already expired, the corresponding connections
85 // are closed. Zero is returned if there is no idle timeout.
86 virtual int checkTimeouts();
87
88
89 // Methods overridden from VNCServer
90
Pierre Ossman559a2e82012-01-23 15:54:11 +000091 virtual void blockUpdates();
92 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000093 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000095 virtual void setScreenLayout(const ScreenSet& layout);
Pierre Ossman6094ced2018-10-05 17:24:51 +020096 virtual const PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 virtual void serverCutText(const char* str, int len);
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +020098
99 virtual void approveConnection(network::Socket* sock, bool accept,
100 const char* reason);
101 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
Pierre Ossman7d64b332018-10-08 15:59:02 +0200102 virtual SConnection* getConnection(network::Socket* sock);
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200103
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 virtual void add_changed(const Region &region);
105 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100107 const rdr::U8* data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108 virtual void setCursorPos(const Point& p);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200109 virtual void setName(const char* name_);
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100110 virtual void setLEDState(unsigned state);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111
112 virtual void bell();
113
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 // VNCServerST-only methods
115
Pierre Ossman6094ced2018-10-05 17:24:51 +0200116 // Methods to get the currently set server state
117
118 const ScreenSet& getScreenLayout() const { return screenLayout; }
119 const Cursor* getCursor() const { return cursor; }
120 const Point& getCursorPos() const { return cursorPos; }
121 const char* getName() const { return name.buf; }
122 unsigned getLEDState() const { return ledState; }
123
Pierre Ossmanb6843412018-10-05 17:30:52 +0200124 // Event handlers
125 void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down);
126 void pointerEvent(VNCSConnectionST* client, const Point& pos, int buttonMask);
127 void clientCutText(const char* str, int len);
128
Pierre Ossman07e44cc2018-10-05 17:32:57 +0200129 unsigned int setDesktopSize(VNCSConnectionST* requester,
130 int fb_width, int fb_height,
131 const ScreenSet& layout);
132
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 // closeClients() closes all RFB sessions, except the specified one (if
134 // any), and logs the specified reason for closure.
135 void closeClients(const char* reason, network::Socket* sock);
136
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 // queryConnection() is called when a connection has been
138 // successfully authenticated. The sock and userName arguments identify
Pierre Ossmane6aab242018-10-05 16:59:22 +0200139 // the socket and the name of the authenticated user, if any.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140 // approveConnection() must be called some time later to accept or reject
141 // the connection.
Pierre Ossmane6aab242018-10-05 16:59:22 +0200142 virtual void queryConnection(network::Socket* sock,
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200143 const char* userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000144
145 // setBlacklist() is called to replace the VNCServerST's internal
146 // Blacklist instance with another instance. This allows a single
147 // Blacklist to be shared by multiple VNCServerST instances.
148 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
149
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150 // setKeyRemapper() replaces the VNCServerST's default key remapper.
151 // NB: A null pointer is valid here.
152 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
153
154 void getConnInfo(ListConnInfo * listConn);
155 void setConnStatus(ListConnInfo* listConn);
156
157 bool getDisable() { return disableclients;};
158 void setDisable(bool disable) { disableclients = disable;};
159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 protected:
161
162 friend class VNCSConnectionST;
163
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000164 // Timer callbacks
165 virtual bool handleTimeout(Timer* t);
166
167 // - Internal methods
168
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 void startDesktop();
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100170 void stopDesktop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171
172 static LogWriter connectionsLog;
173 Blacklist blacklist;
174 Blacklist* blHosts;
175
176 SDesktop* desktop;
177 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000178 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000180 ScreenSet screenLayout;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100181 unsigned int ledState;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 CharArray name;
184
185 std::list<VNCSConnectionST*> clients;
186 VNCSConnectionST* pointerClient;
187 std::list<network::Socket*> closingSockets;
188
189 ComparingUpdateTracker* comparer;
190
191 Point cursorPos;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100192 Cursor* cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100193 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 bool renderedCursorInvalid;
195
196 // - Check how many of the clients are authenticated.
197 int authClientCount();
198
199 bool needRenderedCursor();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200200 void startFrameClock();
201 void stopFrameClock();
Pierre Ossmana2b80d62018-03-23 09:30:09 +0100202 int msToNextUpdate();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200203 void writeUpdate();
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100204 Region getPendingRegion();
Pierre Ossman24684e52016-12-05 16:58:19 +0100205 const RenderedCursor* getRenderedCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000207 bool getComparerState();
208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200210
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 time_t lastUserInputTime;
212 time_t lastDisconnectTime;
213 time_t lastConnectionTime;
214
215 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000216
Pierre Ossman6e49e952016-10-07 15:59:38 +0200217 Timer frameTimer;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 };
219
220};
221
222#endif
223