Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 2 | * Copyright 2009-2016 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 | // |
| 21 | // VNCSConnectionST is our derived class of SConnection for VNCServerST - there |
| 22 | // is one for each connected client. We think of VNCSConnectionST as part of |
| 23 | // the VNCServerST implementation, so its methods are allowed full access to |
| 24 | // members of VNCServerST. |
| 25 | // |
| 26 | |
| 27 | #ifndef __RFB_VNCSCONNECTIONST_H__ |
| 28 | #define __RFB_VNCSCONNECTIONST_H__ |
| 29 | |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 30 | #include <map> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | |
Pierre Ossman | c09e558 | 2015-12-11 20:23:17 +0100 | [diff] [blame] | 32 | #include <rfb/Congestion.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 33 | #include <rfb/EncodeManager.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 34 | #include <rfb/SConnection.h> |
| 35 | #include <rfb/Timer.h> |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 36 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | namespace rfb { |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 38 | class VNCServerST; |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 39 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 40 | class VNCSConnectionST : public SConnection, |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 41 | public Timer::Callback { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | public: |
| 43 | VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse); |
| 44 | virtual ~VNCSConnectionST(); |
| 45 | |
| 46 | // Methods called from VNCServerST. None of these methods ever knowingly |
| 47 | // throw an exception. |
| 48 | |
| 49 | // Unless otherwise stated, the SConnectionST may not be valid after any of |
| 50 | // these methods are called, since they catch exceptions and may have |
| 51 | // called close() which deletes the object. |
| 52 | |
| 53 | // init() must be called to initialise the protocol. If it fails it |
| 54 | // returns false, and close() will have been called. |
| 55 | bool init(); |
| 56 | |
| 57 | // close() shuts down the socket to the client and deletes the |
| 58 | // SConnectionST object. |
| 59 | void close(const char* reason); |
| 60 | |
| 61 | // processMessages() processes incoming messages from the client, invoking |
| 62 | // various callbacks as a result. It continues to process messages until |
| 63 | // reading might block. shutdown() will be called on the connection's |
| 64 | // Socket if an error occurs, via the close() call. |
| 65 | void processMessages(); |
| 66 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 67 | // flushSocket() pushes any unwritten data on to the network. |
| 68 | void flushSocket(); |
| 69 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 70 | // Called when the underlying pixelbuffer is resized or replaced. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 71 | void pixelBufferChange(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 72 | |
| 73 | // Wrappers to make these methods "safe" for VNCServerST. |
| 74 | void writeFramebufferUpdateOrClose(); |
| 75 | void screenLayoutChangeOrClose(rdr::U16 reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | void setCursorOrClose(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 77 | void bellOrClose(); |
| 78 | void serverCutTextOrClose(const char *str, int len); |
| 79 | void setDesktopNameOrClose(const char *name); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 80 | void setLEDStateOrClose(unsigned int state); |
Pierre Ossman | e6aab24 | 2018-10-05 16:59:22 +0200 | [diff] [blame^] | 81 | void approveConnectionOrClose(bool accept, const char* reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | |
| 83 | // checkIdleTimeout() returns the number of milliseconds left until the |
| 84 | // idle timeout expires. If it has expired, the connection is closed and |
| 85 | // zero is returned. Zero is also returned if there is no idle timeout. |
| 86 | int checkIdleTimeout(); |
| 87 | |
| 88 | // The following methods never throw exceptions nor do they ever delete the |
| 89 | // SConnectionST object. |
| 90 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 91 | // getComparerState() returns if this client would like the framebuffer |
| 92 | // comparer to be enabled. |
| 93 | bool getComparerState(); |
| 94 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 95 | // renderedCursorChange() is called whenever the server-side rendered |
| 96 | // cursor changes shape or position. It ensures that the next update will |
| 97 | // clean up the old rendered cursor and if necessary draw the new rendered |
| 98 | // cursor. |
| 99 | void renderedCursorChange(); |
| 100 | |
| 101 | // needRenderedCursor() returns true if this client needs the server-side |
| 102 | // rendered cursor. This may be because it does not support local cursor |
| 103 | // or because the current cursor position has not been set by this client. |
| 104 | bool needRenderedCursor(); |
| 105 | |
| 106 | network::Socket* getSock() { return sock; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | void add_changed(const Region& region) { updates.add_changed(region); } |
| 108 | void add_copied(const Region& dest, const Point& delta) { |
| 109 | updates.add_copied(dest, delta); |
| 110 | } |
| 111 | |
| 112 | const char* getPeerEndpoint() const {return peerEndpoint.buf;} |
| 113 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | char* getStartTime(); |
| 115 | |
| 116 | void setStatus(int status); |
| 117 | int getStatus(); |
| 118 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | private: |
| 120 | // SConnection callbacks |
| 121 | |
| 122 | // These methods are invoked as callbacks from processMsg(). Note that |
| 123 | // none of these methods should call any of the above methods which may |
| 124 | // delete the SConnectionST object. |
| 125 | |
| 126 | virtual void authSuccess(); |
| 127 | virtual void queryConnection(const char* userName); |
| 128 | virtual void clientInit(bool shared); |
| 129 | virtual void setPixelFormat(const PixelFormat& pf); |
| 130 | virtual void pointerEvent(const Point& pos, int buttonMask); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 131 | virtual void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 132 | virtual void clientCutText(const char* str, int len); |
| 133 | virtual void framebufferUpdateRequest(const Rect& r, bool incremental); |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 134 | virtual void setDesktopSize(int fb_width, int fb_height, |
| 135 | const ScreenSet& layout); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 136 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 137 | virtual void enableContinuousUpdates(bool enable, |
| 138 | int x, int y, int w, int h); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | virtual void supportsLocalCursor(); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 140 | virtual void supportsFence(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 141 | virtual void supportsContinuousUpdates(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 142 | virtual void supportsLEDState(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 143 | |
| 144 | // setAccessRights() allows a security package to limit the access rights |
| 145 | // of a VNCSConnectioST to the server. These access rights are applied |
| 146 | // such that the actual rights granted are the minimum of the server's |
| 147 | // default access settings and the connection's access settings. |
| 148 | virtual void setAccessRights(AccessRights ar) {accessRights=ar;} |
| 149 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 150 | // Timer callbacks |
| 151 | virtual bool handleTimeout(Timer* t); |
| 152 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 153 | // Internal methods |
| 154 | |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame] | 155 | bool isShiftPressed(); |
| 156 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 157 | // Congestion control |
| 158 | void writeRTTPing(); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 159 | bool isCongested(); |
| 160 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | // writeFramebufferUpdate() attempts to write a framebuffer update to the |
| 162 | // client. |
| 163 | |
| 164 | void writeFramebufferUpdate(); |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 165 | void writeNoDataUpdate(); |
| 166 | void writeDataUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 167 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 168 | void screenLayoutChange(rdr::U16 reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | void setCursor(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 170 | void setDesktopName(const char *name); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 171 | void setLEDState(unsigned int state); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 172 | void setSocketTimeouts(); |
| 173 | |
| 174 | network::Socket* sock; |
| 175 | CharArray peerEndpoint; |
Pierre Ossman | 7069bdd | 2015-02-06 14:41:58 +0100 | [diff] [blame] | 176 | bool reverseConnection; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 177 | |
| 178 | bool inProcessMessages; |
| 179 | |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 180 | bool pendingSyncFence, syncFence; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 181 | rdr::U32 fenceFlags; |
| 182 | unsigned fenceDataLen; |
| 183 | char *fenceData; |
| 184 | |
Pierre Ossman | c09e558 | 2015-12-11 20:23:17 +0100 | [diff] [blame] | 185 | Congestion congestion; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 186 | Timer congestionTimer; |
Peter Åstrand (astrand) | 7a368c9 | 2018-09-19 12:45:17 +0200 | [diff] [blame] | 187 | Timer losslessTimer; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 188 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | VNCServerST* server; |
| 190 | SimpleUpdateTracker updates; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 191 | Region requested; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 192 | bool updateRenderedCursor, removeRenderedCursor; |
| 193 | Region damagedCursorRegion; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 194 | bool continuousUpdates; |
| 195 | Region cuRegion; |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 196 | EncodeManager encodeManager; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 197 | |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 198 | std::map<rdr::U32, rdr::U32> pressedKeys; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | |
| 200 | time_t lastEventTime; |
| 201 | time_t pointerEventTime; |
| 202 | Point pointerEventPos; |
Pierre Ossman | 25db44a | 2017-11-16 16:40:44 +0100 | [diff] [blame] | 203 | bool clientHasCursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 204 | |
| 205 | AccessRights accessRights; |
| 206 | |
| 207 | CharArray closeReason; |
| 208 | time_t startTime; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 209 | }; |
| 210 | } |
| 211 | #endif |