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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2017 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. // setProtocol3_3 configures whether or not the CConnection should
  55. // only ever support protocol version 3.3
  56. void setProtocol3_3(bool s) {useProtocol3_3 = s;}
  57. // setFramebuffer configures the PixelBuffer that the CConnection
  58. // should render all pixel data in to. Note that the CConnection
  59. // takes ownership of the PixelBuffer and it must not be deleted by
  60. // anyone else. Call setFramebuffer again with NULL or a different
  61. // PixelBuffer to delete the previous one.
  62. void setFramebuffer(ModifiablePixelBuffer* fb);
  63. // initialiseProtocol() should be called once the streams and security
  64. // types are set. Subsequently, processMsg() should be called whenever
  65. // there is data to read on the InStream.
  66. void initialiseProtocol();
  67. // processMsg() should be called whenever there is either:
  68. // - data available on the underlying network stream
  69. // In this case, processMsg may return without processing an RFB message,
  70. // if the available data does not result in an RFB message being ready
  71. // to handle. e.g. if data is encrypted.
  72. // NB: This makes it safe to call processMsg() in response to select()
  73. // - data available on the CConnection's current InStream
  74. // In this case, processMsg should always process the available RFB
  75. // message before returning.
  76. // NB: In either case, you must have called initialiseProtocol() first.
  77. void processMsg();
  78. // Methods overridden from CMsgHandler
  79. // Note: These must be called by any deriving classes
  80. virtual void setDesktopSize(int w, int h);
  81. virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
  82. int w, int h,
  83. const ScreenSet& layout);
  84. virtual void endOfContinuousUpdates();
  85. virtual void serverInit(int width, int height,
  86. const PixelFormat& pf,
  87. const char* name);
  88. virtual void readAndDecodeRect(const Rect& r, int encoding,
  89. ModifiablePixelBuffer* pb);
  90. virtual void framebufferUpdateStart();
  91. virtual void framebufferUpdateEnd();
  92. virtual void dataRect(const Rect& r, int encoding);
  93. // Methods to be overridden in a derived class
  94. // authSuccess() is called when authentication has succeeded.
  95. virtual void authSuccess();
  96. // initDone() is called when the connection is fully established
  97. // and standard messages can be sent. This is called before the
  98. // initial FramebufferUpdateRequest giving a derived class the
  99. // chance to modify pixel format and settings. The derived class
  100. // must also make sure it has provided a valid framebuffer before
  101. // returning.
  102. virtual void initDone() = 0;
  103. // resizeFramebuffer() is called whenever the framebuffer
  104. // dimensions or the screen layout changes. A subclass must make
  105. // sure the pixel buffer has been updated once this call returns.
  106. virtual void resizeFramebuffer();
  107. // Other methods
  108. // refreshFramebuffer() forces a complete refresh of the entire
  109. // framebuffer
  110. void refreshFramebuffer();
  111. // setPreferredEncoding()/getPreferredEncoding() adjusts which
  112. // encoding is listed first as a hint to the server that it is the
  113. // preferred one
  114. void setPreferredEncoding(int encoding);
  115. int getPreferredEncoding();
  116. // setCompressLevel()/setQualityLevel() controls the encoding hints
  117. // sent to the server
  118. void setCompressLevel(int level);
  119. void setQualityLevel(int level);
  120. // setPF() controls the pixel format requested from the server.
  121. // server.pf() will automatically be adjusted once the new format
  122. // is active.
  123. void setPF(const PixelFormat& pf);
  124. CMsgReader* reader() { return reader_; }
  125. CMsgWriter* writer() { return writer_; }
  126. rdr::InStream* getInStream() { return is; }
  127. rdr::OutStream* getOutStream() { return os; }
  128. // Access method used by SSecurity implementations that can verify servers'
  129. // Identities, to determine the unique(ish) name of the server.
  130. const char* getServerName() const { return serverName.buf; }
  131. bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
  132. enum stateEnum {
  133. RFBSTATE_UNINITIALISED,
  134. RFBSTATE_PROTOCOL_VERSION,
  135. RFBSTATE_SECURITY_TYPES,
  136. RFBSTATE_SECURITY,
  137. RFBSTATE_SECURITY_RESULT,
  138. RFBSTATE_INITIALISATION,
  139. RFBSTATE_NORMAL,
  140. RFBSTATE_INVALID
  141. };
  142. stateEnum state() { return state_; }
  143. CSecurity *csecurity;
  144. SecurityClient security;
  145. protected:
  146. void setState(stateEnum s) { state_ = s; }
  147. void setReader(CMsgReader *r) { reader_ = r; }
  148. void setWriter(CMsgWriter *w) { writer_ = w; }
  149. ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
  150. protected:
  151. // Optional capabilities that a subclass is expected to set to true
  152. // if supported
  153. bool supportsLocalCursor;
  154. bool supportsDesktopResize;
  155. bool supportsLEDState;
  156. private:
  157. // This is a default implementation of fences that automatically
  158. // responds to requests, stating no support for synchronisation.
  159. // When overriding, call CMsgHandler::fence() directly in order to
  160. // state correct support for fence flags.
  161. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  162. private:
  163. void processVersionMsg();
  164. void processSecurityTypesMsg();
  165. void processSecurityMsg();
  166. void processSecurityResultMsg();
  167. void processInitMsg();
  168. void throwAuthFailureException();
  169. void throwConnFailedException();
  170. void securityCompleted();
  171. void requestNewUpdate();
  172. void updateEncodings();
  173. rdr::InStream* is;
  174. rdr::OutStream* os;
  175. CMsgReader* reader_;
  176. CMsgWriter* writer_;
  177. bool deleteStreamsWhenDone;
  178. bool shared;
  179. stateEnum state_;
  180. CharArray serverName;
  181. bool useProtocol3_3;
  182. bool pendingPFChange;
  183. rfb::PixelFormat pendingPF;
  184. int preferredEncoding;
  185. int compressLevel;
  186. int qualityLevel;
  187. bool formatChange;
  188. rfb::PixelFormat nextPF;
  189. bool encodingChange;
  190. bool firstUpdate;
  191. bool pendingUpdate;
  192. bool continuousUpdates;
  193. bool forceNonincremental;
  194. ModifiablePixelBuffer* framebuffer;
  195. DecodeManager decoder;
  196. };
  197. }
  198. #endif