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