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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // 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. // Methods to be overridden in a derived class
  91. // authSuccess() is called when authentication has succeeded.
  92. virtual void authSuccess();
  93. // initDone() is called when the connection is fully established
  94. // and standard messages can be sent. This is called before the
  95. // initial FramebufferUpdateRequest giving a derived class the
  96. // chance to modify pixel format and settings. The derived class
  97. // must also make sure it has provided a valid framebuffer before
  98. // returning.
  99. virtual void initDone() = 0;
  100. // resizeFramebuffer() is called whenever the framebuffer
  101. // dimensions or the screen layout changes. A subclass must make
  102. // sure the pixel buffer has been updated once this call returns.
  103. virtual void resizeFramebuffer();
  104. // Other methods
  105. // refreshFramebuffer() forces a complete refresh of the entire
  106. // framebuffer
  107. void refreshFramebuffer();
  108. // setPreferredEncoding()/getPreferredEncoding() adjusts which
  109. // encoding is listed first as a hint to the server that it is the
  110. // preferred one
  111. void setPreferredEncoding(int encoding);
  112. int getPreferredEncoding();
  113. // setCompressLevel()/setQualityLevel() controls the encoding hints
  114. // sent to the server
  115. void setCompressLevel(int level);
  116. void setQualityLevel(int level);
  117. // setPF() controls the pixel format requested from the server.
  118. // server.pf() will automatically be adjusted once the new format
  119. // is active.
  120. void setPF(const PixelFormat& pf);
  121. CMsgReader* reader() { return reader_; }
  122. CMsgWriter* writer() { return writer_; }
  123. rdr::InStream* getInStream() { return is; }
  124. rdr::OutStream* getOutStream() { return os; }
  125. // Access method used by SSecurity implementations that can verify servers'
  126. // Identities, to determine the unique(ish) name of the server.
  127. const char* getServerName() const { return serverName.buf; }
  128. bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
  129. enum stateEnum {
  130. RFBSTATE_UNINITIALISED,
  131. RFBSTATE_PROTOCOL_VERSION,
  132. RFBSTATE_SECURITY_TYPES,
  133. RFBSTATE_SECURITY,
  134. RFBSTATE_SECURITY_RESULT,
  135. RFBSTATE_INITIALISATION,
  136. RFBSTATE_NORMAL,
  137. RFBSTATE_INVALID
  138. };
  139. stateEnum state() { return state_; }
  140. CSecurity *csecurity;
  141. SecurityClient security;
  142. protected:
  143. void setState(stateEnum s) { state_ = s; }
  144. void setReader(CMsgReader *r) { reader_ = r; }
  145. void setWriter(CMsgWriter *w) { writer_ = w; }
  146. ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
  147. protected:
  148. // Optional capabilities that a subclass is expected to set to true
  149. // if supported
  150. bool supportsLocalCursor;
  151. bool supportsDesktopResize;
  152. bool supportsLEDState;
  153. private:
  154. // This is a default implementation of fences that automatically
  155. // responds to requests, stating no support for synchronisation.
  156. // When overriding, call CMsgHandler::fence() directly in order to
  157. // state correct support for fence flags.
  158. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  159. private:
  160. void processVersionMsg();
  161. void processSecurityTypesMsg();
  162. void processSecurityMsg();
  163. void processSecurityResultMsg();
  164. void processInitMsg();
  165. void throwAuthFailureException();
  166. void throwConnFailedException();
  167. void securityCompleted();
  168. void requestNewUpdate();
  169. void updateEncodings();
  170. rdr::InStream* is;
  171. rdr::OutStream* os;
  172. CMsgReader* reader_;
  173. CMsgWriter* writer_;
  174. bool deleteStreamsWhenDone;
  175. bool shared;
  176. stateEnum state_;
  177. CharArray serverName;
  178. bool pendingPFChange;
  179. rfb::PixelFormat pendingPF;
  180. int preferredEncoding;
  181. int compressLevel;
  182. int qualityLevel;
  183. bool formatChange;
  184. rfb::PixelFormat nextPF;
  185. bool encodingChange;
  186. bool firstUpdate;
  187. bool pendingUpdate;
  188. bool continuousUpdates;
  189. bool forceNonincremental;
  190. ModifiablePixelBuffer* framebuffer;
  191. DecodeManager decoder;
  192. };
  193. }
  194. #endif