Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 2 | * Copyright 2009-2011 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 | |
| 30 | #include <set> |
| 31 | #include <rfb/SConnection.h> |
| 32 | #include <rfb/SMsgWriter.h> |
| 33 | #include <rfb/TransImageGetter.h> |
| 34 | #include <rfb/VNCServerST.h> |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 35 | #include <rfb/Timer.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 37 | struct RTTInfo; |
| 38 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | namespace rfb { |
| 40 | class VNCSConnectionST : public SConnection, |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 41 | public WriteSetCursorCallback, |
| 42 | public Timer::Callback { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | public: |
| 44 | VNCSConnectionST(VNCServerST* server_, network::Socket* s, bool reverse); |
| 45 | virtual ~VNCSConnectionST(); |
| 46 | |
| 47 | // Methods called from VNCServerST. None of these methods ever knowingly |
| 48 | // throw an exception. |
| 49 | |
| 50 | // Unless otherwise stated, the SConnectionST may not be valid after any of |
| 51 | // these methods are called, since they catch exceptions and may have |
| 52 | // called close() which deletes the object. |
| 53 | |
| 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 | |
| 58 | // close() shuts down the socket to the client and deletes the |
| 59 | // SConnectionST object. |
| 60 | void close(const char* reason); |
| 61 | |
| 62 | // processMessages() processes incoming messages from the client, invoking |
| 63 | // various callbacks as a result. It continues to process messages until |
| 64 | // reading might block. shutdown() will be called on the connection's |
| 65 | // Socket if an error occurs, via the close() call. |
| 66 | void processMessages(); |
| 67 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 68 | // Called when the underlying pixelbuffer is resized or replaced. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | void pixelBufferChange(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 70 | |
| 71 | // Wrappers to make these methods "safe" for VNCServerST. |
| 72 | void writeFramebufferUpdateOrClose(); |
| 73 | void screenLayoutChangeOrClose(rdr::U16 reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 74 | void setColourMapEntriesOrClose(int firstColour, int nColours); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | void setCursorOrClose(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 76 | void bellOrClose(); |
| 77 | void serverCutTextOrClose(const char *str, int len); |
| 78 | void setDesktopNameOrClose(const char *name); |
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 | |
| 85 | // The following methods never throw exceptions nor do they ever delete the |
| 86 | // SConnectionST object. |
| 87 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 88 | // getComparerState() returns if this client would like the framebuffer |
| 89 | // comparer to be enabled. |
| 90 | bool getComparerState(); |
| 91 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 92 | // renderedCursorChange() is called whenever the server-side rendered |
| 93 | // cursor changes shape or position. It ensures that the next update will |
| 94 | // clean up the old rendered cursor and if necessary draw the new rendered |
| 95 | // cursor. |
| 96 | void renderedCursorChange(); |
| 97 | |
| 98 | // needRenderedCursor() returns true if this client needs the server-side |
| 99 | // rendered cursor. This may be because it does not support local cursor |
| 100 | // or because the current cursor position has not been set by this client. |
| 101 | bool needRenderedCursor(); |
| 102 | |
| 103 | network::Socket* getSock() { return sock; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | void add_changed(const Region& region) { updates.add_changed(region); } |
| 105 | void add_copied(const Region& dest, const Point& delta) { |
| 106 | updates.add_copied(dest, delta); |
| 107 | } |
| 108 | |
| 109 | const char* getPeerEndpoint() const {return peerEndpoint.buf;} |
| 110 | |
| 111 | // approveConnectionOrClose() is called some time after |
| 112 | // VNCServerST::queryConnection() has returned with PENDING to accept or |
| 113 | // reject the connection. The accept argument should be true for |
| 114 | // acceptance, or false for rejection, in which case a string reason may |
| 115 | // also be given. |
| 116 | |
| 117 | void approveConnectionOrClose(bool accept, const char* reason); |
| 118 | |
| 119 | char* getStartTime(); |
| 120 | |
| 121 | void setStatus(int status); |
| 122 | int getStatus(); |
| 123 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | private: |
| 125 | // SConnection callbacks |
| 126 | |
| 127 | // These methods are invoked as callbacks from processMsg(). Note that |
| 128 | // none of these methods should call any of the above methods which may |
| 129 | // delete the SConnectionST object. |
| 130 | |
| 131 | virtual void authSuccess(); |
| 132 | virtual void queryConnection(const char* userName); |
| 133 | virtual void clientInit(bool shared); |
| 134 | virtual void setPixelFormat(const PixelFormat& pf); |
| 135 | virtual void pointerEvent(const Point& pos, int buttonMask); |
| 136 | virtual void keyEvent(rdr::U32 key, bool down); |
| 137 | virtual void clientCutText(const char* str, int len); |
| 138 | virtual void framebufferUpdateRequest(const Rect& r, bool incremental); |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 139 | virtual void setDesktopSize(int fb_width, int fb_height, |
| 140 | const ScreenSet& layout); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 141 | virtual void setInitialColourMap(); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 142 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 143 | virtual void enableContinuousUpdates(bool enable, |
| 144 | int x, int y, int w, int h); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 145 | virtual void supportsLocalCursor(); |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 146 | virtual void supportsFence(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 147 | virtual void supportsContinuousUpdates(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 148 | |
| 149 | // setAccessRights() allows a security package to limit the access rights |
| 150 | // of a VNCSConnectioST to the server. These access rights are applied |
| 151 | // such that the actual rights granted are the minimum of the server's |
| 152 | // default access settings and the connection's access settings. |
| 153 | virtual void setAccessRights(AccessRights ar) {accessRights=ar;} |
| 154 | |
| 155 | // WriteSetCursorCallback |
| 156 | virtual void writeSetCursorCallback(); |
| 157 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 158 | // Timer callbacks |
| 159 | virtual bool handleTimeout(Timer* t); |
| 160 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | // Internal methods |
| 162 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 163 | // Congestion control |
| 164 | void writeRTTPing(); |
| 165 | void handleRTTPong(const struct RTTInfo &rttInfo); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 166 | bool isCongested(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 167 | void updateCongestion(); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 168 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | // writeFramebufferUpdate() attempts to write a framebuffer update to the |
| 170 | // client. |
| 171 | |
| 172 | void writeFramebufferUpdate(); |
| 173 | |
| 174 | void writeRenderedCursorRect(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 175 | void screenLayoutChange(rdr::U16 reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | void setColourMapEntries(int firstColour, int nColours); |
| 177 | void setCursor(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 178 | void setDesktopName(const char *name); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 179 | void setSocketTimeouts(); |
| 180 | |
| 181 | network::Socket* sock; |
| 182 | CharArray peerEndpoint; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 183 | |
| 184 | bool inProcessMessages; |
| 185 | |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame^] | 186 | bool pendingSyncFence, syncFence; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 187 | rdr::U32 fenceFlags; |
| 188 | unsigned fenceDataLen; |
| 189 | char *fenceData; |
| 190 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 191 | unsigned baseRTT; |
| 192 | unsigned congWindow; |
| 193 | int ackedOffset, sentOffset; |
| 194 | |
| 195 | unsigned minRTT; |
| 196 | bool seenCongestion; |
| 197 | unsigned pingCounter; |
| 198 | Timer congestionTimer; |
| 199 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | VNCServerST* server; |
| 201 | SimpleUpdateTracker updates; |
| 202 | TransImageGetter image_getter; |
| 203 | Region requested; |
| 204 | bool drawRenderedCursor, removeRenderedCursor; |
| 205 | Rect renderedCursorRect; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 206 | bool continuousUpdates; |
| 207 | Region cuRegion; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 209 | Timer updateTimer; |
| 210 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 211 | std::set<rdr::U32> pressedKeys; |
| 212 | |
| 213 | time_t lastEventTime; |
| 214 | time_t pointerEventTime; |
| 215 | Point pointerEventPos; |
| 216 | |
| 217 | AccessRights accessRights; |
| 218 | |
| 219 | CharArray closeReason; |
| 220 | time_t startTime; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 221 | }; |
| 222 | } |
| 223 | #endif |