blob: ed8561a898762cb19da2be9c0e5d689095b97647 [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/Blacklist.h>
32#include <rfb/Cursor.h>
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000033#include <rfb/Timer.h>
Pierre Ossman34e62f32009-03-20 21:46:12 +000034#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035
36namespace rfb {
37
38 class VNCSConnectionST;
39 class ComparingUpdateTracker;
Pierre Ossman707fa122015-12-11 20:21:20 +010040 class ListConnInfo;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041 class PixelBuffer;
42 class KeyRemapper;
43
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000044 class VNCServerST : public VNCServer,
Pierre Ossmancd7931d2018-10-05 17:48:58 +020045 public Timer::Callback {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046 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
Pierre Ossman574dc642016-10-05 13:39:11 +020065 // getSockets() gets a list of sockets. This can be used to generate an
66 // fd_set for calling select().
67 virtual void getSockets(std::list<network::Socket*>* sockets);
68
Pierre Ossmand408ca52016-04-29 14:26:05 +020069 // processSocketReadEvent
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070 // Read more RFB data from the Socket. If an error occurs during
71 // processing then shutdown() is called on the Socket, causing
72 // removeSocket() to be called by the caller at a later time.
Pierre Ossmand408ca52016-04-29 14:26:05 +020073 virtual void processSocketReadEvent(network::Socket* sock);
74
75 // processSocketWriteEvent
76 // Flush pending data from the Socket on to the network.
77 virtual void processSocketWriteEvent(network::Socket* sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078
79 // checkTimeouts
80 // Returns the number of milliseconds left until the next idle timeout
81 // expires. If any have already expired, the corresponding connections
82 // are closed. Zero is returned if there is no idle timeout.
83 virtual int checkTimeouts();
84
85
86 // Methods overridden from VNCServer
87
Pierre Ossman559a2e82012-01-23 15:54:11 +000088 virtual void blockUpdates();
89 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000090 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000092 virtual void setScreenLayout(const ScreenSet& layout);
Pierre Ossman6094ced2018-10-05 17:24:51 +020093 virtual const PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 virtual void serverCutText(const char* str, int len);
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +020095
96 virtual void approveConnection(network::Socket* sock, bool accept,
97 const char* reason);
98 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
Pierre Ossman7d64b332018-10-08 15:59:02 +020099 virtual SConnection* getConnection(network::Socket* sock);
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 virtual void add_changed(const Region &region);
102 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100104 const rdr::U8* data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 virtual void setCursorPos(const Point& p);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200106 virtual void setName(const char* name_);
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100107 virtual void setLEDState(unsigned state);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108
109 virtual void bell();
110
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111 // VNCServerST-only methods
112
Pierre Ossman6094ced2018-10-05 17:24:51 +0200113 // Methods to get the currently set server state
114
115 const ScreenSet& getScreenLayout() const { return screenLayout; }
116 const Cursor* getCursor() const { return cursor; }
117 const Point& getCursorPos() const { return cursorPos; }
118 const char* getName() const { return name.buf; }
119 unsigned getLEDState() const { return ledState; }
120
Pierre Ossmanb6843412018-10-05 17:30:52 +0200121 // Event handlers
122 void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down);
123 void pointerEvent(VNCSConnectionST* client, const Point& pos, int buttonMask);
124 void clientCutText(const char* str, int len);
125
Pierre Ossman07e44cc2018-10-05 17:32:57 +0200126 unsigned int setDesktopSize(VNCSConnectionST* requester,
127 int fb_width, int fb_height,
128 const ScreenSet& layout);
129
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 // closeClients() closes all RFB sessions, except the specified one (if
131 // any), and logs the specified reason for closure.
132 void closeClients(const char* reason, network::Socket* sock);
133
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200134 // queryConnection() does some basic checks and then passes on the
135 // request to the desktop.
136 void queryConnection(VNCSConnectionST* client, const char* userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137
138 // setBlacklist() is called to replace the VNCServerST's internal
139 // Blacklist instance with another instance. This allows a single
140 // Blacklist to be shared by multiple VNCServerST instances.
141 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
142
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 // setKeyRemapper() replaces the VNCServerST's default key remapper.
144 // NB: A null pointer is valid here.
145 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
146
147 void getConnInfo(ListConnInfo * listConn);
148 void setConnStatus(ListConnInfo* listConn);
149
150 bool getDisable() { return disableclients;};
151 void setDisable(bool disable) { disableclients = disable;};
152
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200153 // clientReady() is called by a VNCSConnectionST instance when the
154 // client has completed the handshake and is ready for normal
155 // communication.
156 void clientReady(VNCSConnectionST* client, bool shared);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200158 // Estimated time until the next time new updates will be pushed
159 // to clients
160 int msToNextUpdate();
161
162 // Part of the framebuffer that has been modified but is not yet
163 // ready to be sent to clients
164 Region getPendingRegion();
165
166 const RenderedCursor* getRenderedCursor();
167
168 protected:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000170 // Timer callbacks
171 virtual bool handleTimeout(Timer* t);
172
173 // - Internal methods
174
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 void startDesktop();
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100176 void stopDesktop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 Blacklist blacklist;
179 Blacklist* blHosts;
180
181 SDesktop* desktop;
182 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000183 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000185 ScreenSet screenLayout;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100186 unsigned int ledState;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 CharArray name;
189
190 std::list<VNCSConnectionST*> clients;
191 VNCSConnectionST* pointerClient;
192 std::list<network::Socket*> closingSockets;
193
194 ComparingUpdateTracker* comparer;
195
196 Point cursorPos;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100197 Cursor* cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100198 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000199 bool renderedCursorInvalid;
200
201 // - Check how many of the clients are authenticated.
202 int authClientCount();
203
204 bool needRenderedCursor();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200205 void startFrameClock();
206 void stopFrameClock();
207 void writeUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000209 bool getComparerState();
210
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200212
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213 time_t lastUserInputTime;
214 time_t lastDisconnectTime;
215 time_t lastConnectionTime;
216
217 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000218
Pierre Ossman6e49e952016-10-07 15:59:38 +0200219 Timer frameTimer;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 };
221
222};
223
224#endif
225