Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 2 | * Copyright 2009-2019 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 27 | #include <sys/time.h> |
| 28 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | #include <rfb/SDesktop.h> |
| 30 | #include <rfb/VNCServer.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | #include <rfb/Blacklist.h> |
| 32 | #include <rfb/Cursor.h> |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 33 | #include <rfb/Timer.h> |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 34 | #include <rfb/ScreenSet.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | |
| 36 | namespace rfb { |
| 37 | |
| 38 | class VNCSConnectionST; |
| 39 | class ComparingUpdateTracker; |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 40 | class ListConnInfo; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | class PixelBuffer; |
| 42 | class KeyRemapper; |
| 43 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 44 | class VNCServerST : public VNCServer, |
Pierre Ossman | cd7931d | 2018-10-05 17:48:58 +0200 | [diff] [blame] | 45 | public Timer::Callback { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | public: |
| 47 | // -=- Constructors |
| 48 | |
| 49 | // Create a server exporting the supplied desktop. |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 50 | VNCServerST(const char* name_, SDesktop* desktop_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | 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 Ossman | 574dc64 | 2016-10-05 13:39:11 +0200 | [diff] [blame] | 65 | // 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 Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 69 | // processSocketReadEvent |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | // 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 Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 73 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 78 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 79 | |
| 80 | // Methods overridden from VNCServer |
| 81 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 82 | virtual void blockUpdates(); |
| 83 | virtual void unblockUpdates(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 84 | virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | virtual void setPixelBuffer(PixelBuffer* pb); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 86 | virtual void setScreenLayout(const ScreenSet& layout); |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 87 | virtual const PixelBuffer* getPixelBuffer() const { return pb; } |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 88 | |
| 89 | virtual void requestClipboard(); |
| 90 | virtual void announceClipboard(bool available); |
| 91 | virtual void sendClipboardData(const char* data); |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 92 | |
| 93 | virtual void approveConnection(network::Socket* sock, bool accept, |
| 94 | const char* reason); |
| 95 | virtual void closeClients(const char* reason) {closeClients(reason, 0);} |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 96 | virtual SConnection* getConnection(network::Socket* sock); |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 97 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | virtual void add_changed(const Region ®ion); |
| 99 | virtual void add_copied(const Region &dest, const Point &delta); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 100 | virtual void setCursor(int width, int height, const Point& hotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 101 | const rdr::U8* data); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | virtual void setCursorPos(const Point& p); |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 103 | virtual void setName(const char* name_); |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 104 | virtual void setLEDState(unsigned state); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 105 | |
| 106 | virtual void bell(); |
| 107 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 108 | // VNCServerST-only methods |
| 109 | |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 110 | // Methods to get the currently set server state |
| 111 | |
| 112 | const ScreenSet& getScreenLayout() const { return screenLayout; } |
| 113 | const Cursor* getCursor() const { return cursor; } |
| 114 | const Point& getCursorPos() const { return cursorPos; } |
| 115 | const char* getName() const { return name.buf; } |
| 116 | unsigned getLEDState() const { return ledState; } |
| 117 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 118 | // Event handlers |
| 119 | void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down); |
| 120 | void pointerEvent(VNCSConnectionST* client, const Point& pos, int buttonMask); |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 121 | |
| 122 | void handleClipboardRequest(VNCSConnectionST* client); |
| 123 | void handleClipboardAnnounce(VNCSConnectionST* client, bool available); |
| 124 | void handleClipboardData(VNCSConnectionST* client, const char* data); |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 125 | |
Pierre Ossman | 07e44cc | 2018-10-05 17:32:57 +0200 | [diff] [blame] | 126 | unsigned int setDesktopSize(VNCSConnectionST* requester, |
| 127 | int fb_width, int fb_height, |
| 128 | const ScreenSet& layout); |
| 129 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 130 | // 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 Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 134 | // queryConnection() does some basic checks and then passes on the |
| 135 | // request to the desktop. |
| 136 | void queryConnection(VNCSConnectionST* client, const char* userName); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 137 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 138 | // clientReady() is called by a VNCSConnectionST instance when the |
| 139 | // client has completed the handshake and is ready for normal |
| 140 | // communication. |
| 141 | void clientReady(VNCSConnectionST* client, bool shared); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 142 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 143 | // Estimated time until the next time new updates will be pushed |
| 144 | // to clients |
| 145 | int msToNextUpdate(); |
| 146 | |
| 147 | // Part of the framebuffer that has been modified but is not yet |
| 148 | // ready to be sent to clients |
| 149 | Region getPendingRegion(); |
| 150 | |
Pierre Ossman | 7e1093e | 2018-10-11 08:08:19 +0200 | [diff] [blame] | 151 | // getRenderedCursor() returns an up to date version of the server |
| 152 | // side rendered cursor buffer |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 153 | const RenderedCursor* getRenderedCursor(); |
| 154 | |
| 155 | protected: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 156 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 157 | // Timer callbacks |
| 158 | virtual bool handleTimeout(Timer* t); |
| 159 | |
| 160 | // - Internal methods |
| 161 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | void startDesktop(); |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 163 | void stopDesktop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 164 | |
Pierre Ossman | 7e1093e | 2018-10-11 08:08:19 +0200 | [diff] [blame] | 165 | // - Check how many of the clients are authenticated. |
| 166 | int authClientCount(); |
| 167 | |
| 168 | bool needRenderedCursor(); |
| 169 | void startFrameClock(); |
| 170 | void stopFrameClock(); |
| 171 | void writeUpdate(); |
| 172 | |
| 173 | bool getComparerState(); |
| 174 | |
| 175 | protected: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | Blacklist blacklist; |
| 177 | Blacklist* blHosts; |
| 178 | |
| 179 | SDesktop* desktop; |
| 180 | bool desktopStarted; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 181 | int blockCounter; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 182 | PixelBuffer* pb; |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 183 | ScreenSet screenLayout; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 184 | unsigned int ledState; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | CharArray name; |
| 187 | |
| 188 | std::list<VNCSConnectionST*> clients; |
| 189 | VNCSConnectionST* pointerClient; |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 190 | VNCSConnectionST* clipboardClient; |
| 191 | std::list<VNCSConnectionST*> clipboardRequestors; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | std::list<network::Socket*> closingSockets; |
| 193 | |
| 194 | ComparingUpdateTracker* comparer; |
| 195 | |
| 196 | Point cursorPos; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 197 | Cursor* cursor; |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 198 | RenderedCursor renderedCursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | bool renderedCursorInvalid; |
| 200 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 201 | KeyRemapper* keyRemapper; |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 202 | |
Pierre Ossman | f43137c | 2018-10-26 15:34:03 +0200 | [diff] [blame] | 203 | Timer idleTimer; |
| 204 | Timer disconnectTimer; |
| 205 | Timer connectTimer; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 206 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 207 | Timer frameTimer; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | }; |
| 211 | |
| 212 | #endif |
| 213 | |