You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CConnection.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  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. // CConnection - class on the client side representing a connection to a
  21. // server. A derived class should override methods appropriately.
  22. //
  23. #ifndef __RFB_CCONNECTION_H__
  24. #define __RFB_CCONNECTION_H__
  25. #include <rfb/CMsgHandler.h>
  26. #include <rfb/DecodeManager.h>
  27. #include <rfb/SecurityClient.h>
  28. #include <rfb/util.h>
  29. namespace rfb {
  30. class CMsgReader;
  31. class CMsgWriter;
  32. class CSecurity;
  33. class IdentityVerifier;
  34. class CConnection : public CMsgHandler {
  35. public:
  36. CConnection();
  37. virtual ~CConnection();
  38. // Methods to initialise the connection
  39. // setServerName() is used to provide a unique(ish) name for the server to
  40. // which we are connected. This might be the result of getPeerEndpoint on
  41. // a TcpSocket, for example, or a host specified by DNS name & port.
  42. // The serverName is used when verifying the Identity of a host (see RA2).
  43. void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
  44. // setStreams() sets the streams to be used for the connection. These must
  45. // be set before initialiseProtocol() and processMsg() are called. The
  46. // CSecurity object may call setStreams() again to provide alternative
  47. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  48. // streams). Ownership of the streams remains with the caller
  49. // (i.e. SConnection will not delete them).
  50. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  51. // setShared sets the value of the shared flag which will be sent to the
  52. // server upon initialisation.
  53. void setShared(bool s) { shared = s; }
  54. // setFramebuffer configures the PixelBuffer that the CConnection
  55. // should render all pixel data in to. Note that the CConnection
  56. // takes ownership of the PixelBuffer and it must not be deleted by
  57. // anyone else. Call setFramebuffer again with NULL or a different
  58. // PixelBuffer to delete the previous one.
  59. void setFramebuffer(ModifiablePixelBuffer* fb);
  60. // initialiseProtocol() should be called once the streams and security
  61. // types are set. Subsequently, processMsg() should be called whenever
  62. // there is data to read on the InStream.
  63. void initialiseProtocol();
  64. // processMsg() should be called whenever there is either:
  65. // - data available on the underlying network stream
  66. // In this case, processMsg may return without processing an RFB message,
  67. // if the available data does not result in an RFB message being ready
  68. // to handle. e.g. if data is encrypted.
  69. // NB: This makes it safe to call processMsg() in response to select()
  70. // - data available on the CConnection's current InStream
  71. // In this case, processMsg should always process the available RFB
  72. // message before returning.
  73. // NB: In either case, you must have called initialiseProtocol() first.
  74. void processMsg();
  75. // Methods overridden from CMsgHandler
  76. // Note: These must be called by any deriving classes
  77. virtual void setDesktopSize(int w, int h);
  78. virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
  79. int w, int h,
  80. const ScreenSet& layout);
  81. virtual void endOfContinuousUpdates();
  82. virtual void serverInit(int width, int height,
  83. const PixelFormat& pf,
  84. const char* name);
  85. virtual void readAndDecodeRect(const Rect& r, int encoding,
  86. ModifiablePixelBuffer* pb);
  87. virtual void framebufferUpdateStart();
  88. virtual void framebufferUpdateEnd();
  89. virtual void dataRect(const Rect& r, int encoding);
  90. virtual void serverCutText(const char* str);
  91. virtual void handleClipboardCaps(rdr::U32 flags,
  92. const rdr::U32* lengths);
  93. virtual void handleClipboardRequest(rdr::U32 flags);
  94. virtual void handleClipboardPeek(rdr::U32 flags);
  95. virtual void handleClipboardNotify(rdr::U32 flags);
  96. virtual void handleClipboardProvide(rdr::U32 flags,
  97. const size_t* lengths,
  98. const rdr::U8* const* data);
  99. // Methods to be overridden in a derived class
  100. // authSuccess() is called when authentication has succeeded.
  101. virtual void authSuccess();
  102. // initDone() is called when the connection is fully established
  103. // and standard messages can be sent. This is called before the
  104. // initial FramebufferUpdateRequest giving a derived class the
  105. // chance to modify pixel format and settings. The derived class
  106. // must also make sure it has provided a valid framebuffer before
  107. // returning.
  108. virtual void initDone() = 0;
  109. // resizeFramebuffer() is called whenever the framebuffer
  110. // dimensions or the screen layout changes. A subclass must make
  111. // sure the pixel buffer has been updated once this call returns.
  112. virtual void resizeFramebuffer();
  113. // handleClipboardRequest() is called whenever the server requests
  114. // the client to send over its clipboard data. It will only be
  115. // called after the client has first announced a clipboard change
  116. // via announceClipboard().
  117. virtual void handleClipboardRequest();
  118. // handleClipboardAnnounce() is called to indicate a change in the
  119. // clipboard on the server. Call requestClipboard() to access the
  120. // actual data.
  121. virtual void handleClipboardAnnounce(bool available);
  122. // handleClipboardData() is called when the server has sent over
  123. // the clipboard data as a result of a previous call to
  124. // requestClipboard(). Note that this function might never be
  125. // called if the clipboard data was no longer available when the
  126. // server received the request.
  127. virtual void handleClipboardData(const char* data);
  128. // Other methods
  129. // requestClipboard() will result in a request to the server to
  130. // transfer its clipboard data. A call to handleClipboardData()
  131. // will be made once the data is available.
  132. virtual void requestClipboard();
  133. // announceClipboard() informs the server of changes to the
  134. // clipboard on the client. The server may later request the
  135. // clipboard data via handleClipboardRequest().
  136. virtual void announceClipboard(bool available);
  137. // sendClipboardData() transfers the clipboard data to the server
  138. // and should be called whenever the server has requested the
  139. // clipboard via handleClipboardRequest().
  140. virtual void sendClipboardData(const char* data);
  141. // refreshFramebuffer() forces a complete refresh of the entire
  142. // framebuffer
  143. void refreshFramebuffer();
  144. // setPreferredEncoding()/getPreferredEncoding() adjusts which
  145. // encoding is listed first as a hint to the server that it is the
  146. // preferred one
  147. void setPreferredEncoding(int encoding);
  148. int getPreferredEncoding();
  149. // setCompressLevel()/setQualityLevel() controls the encoding hints
  150. // sent to the server
  151. void setCompressLevel(int level);
  152. void setQualityLevel(int level);
  153. // setPF() controls the pixel format requested from the server.
  154. // server.pf() will automatically be adjusted once the new format
  155. // is active.
  156. void setPF(const PixelFormat& pf);
  157. CMsgReader* reader() { return reader_; }
  158. CMsgWriter* writer() { return writer_; }
  159. rdr::InStream* getInStream() { return is; }
  160. rdr::OutStream* getOutStream() { return os; }
  161. // Access method used by SSecurity implementations that can verify servers'
  162. // Identities, to determine the unique(ish) name of the server.
  163. const char* getServerName() const { return serverName.buf; }
  164. bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
  165. enum stateEnum {
  166. RFBSTATE_UNINITIALISED,
  167. RFBSTATE_PROTOCOL_VERSION,
  168. RFBSTATE_SECURITY_TYPES,
  169. RFBSTATE_SECURITY,
  170. RFBSTATE_SECURITY_RESULT,
  171. RFBSTATE_INITIALISATION,
  172. RFBSTATE_NORMAL,
  173. RFBSTATE_INVALID
  174. };
  175. stateEnum state() { return state_; }
  176. CSecurity *csecurity;
  177. SecurityClient security;
  178. protected:
  179. void setState(stateEnum s) { state_ = s; }
  180. void setReader(CMsgReader *r) { reader_ = r; }
  181. void setWriter(CMsgWriter *w) { writer_ = w; }
  182. ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
  183. protected:
  184. // Optional capabilities that a subclass is expected to set to true
  185. // if supported
  186. bool supportsLocalCursor;
  187. bool supportsDesktopResize;
  188. bool supportsLEDState;
  189. private:
  190. // This is a default implementation of fences that automatically
  191. // responds to requests, stating no support for synchronisation.
  192. // When overriding, call CMsgHandler::fence() directly in order to
  193. // state correct support for fence flags.
  194. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  195. private:
  196. void processVersionMsg();
  197. void processSecurityTypesMsg();
  198. void processSecurityMsg();
  199. void processSecurityResultMsg();
  200. void processInitMsg();
  201. void throwAuthFailureException();
  202. void throwConnFailedException();
  203. void securityCompleted();
  204. void requestNewUpdate();
  205. void updateEncodings();
  206. rdr::InStream* is;
  207. rdr::OutStream* os;
  208. CMsgReader* reader_;
  209. CMsgWriter* writer_;
  210. bool deleteStreamsWhenDone;
  211. bool shared;
  212. stateEnum state_;
  213. CharArray serverName;
  214. bool pendingPFChange;
  215. rfb::PixelFormat pendingPF;
  216. int preferredEncoding;
  217. int compressLevel;
  218. int qualityLevel;
  219. bool formatChange;
  220. rfb::PixelFormat nextPF;
  221. bool encodingChange;
  222. bool firstUpdate;
  223. bool pendingUpdate;
  224. bool continuousUpdates;
  225. bool forceNonincremental;
  226. ModifiablePixelBuffer* framebuffer;
  227. DecodeManager decoder;
  228. char* serverClipboard;
  229. bool hasLocalClipboard;
  230. };
  231. }
  232. #endif