blob: 66a71a2439f499915ff9af0c8cdf54f33b29b680 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana4c0aac2017-02-19 15:50:29 +01002 * Copyright 2011-2017 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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
24#ifndef __RFB_CCONNECTION_H__
25#define __RFB_CCONNECTION_H__
26
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#include <rfb/CMsgHandler.h>
Pierre Ossman9f273e92015-11-09 16:34:54 +010028#include <rfb/DecodeManager.h>
Michal Srbdccb5f72017-03-27 13:55:46 +030029#include <rfb/SecurityClient.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/util.h>
31
32namespace rfb {
33
34 class CMsgReader;
35 class CMsgWriter;
36 class CSecurity;
37 class IdentityVerifier;
38
39 class CConnection : public CMsgHandler {
40 public:
41
42 CConnection();
43 virtual ~CConnection();
44
45 // Methods to initialise the connection
46
47 // setServerName() is used to provide a unique(ish) name for the server to
48 // which we are connected. This might be the result of getPeerEndpoint on
49 // a TcpSocket, for example, or a host specified by DNS name & port.
50 // The serverName is used when verifying the Identity of a host (see RA2).
Adam Tkacd36b6262009-09-04 10:57:20 +000051 void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052
53 // setStreams() sets the streams to be used for the connection. These must
54 // be set before initialiseProtocol() and processMsg() are called. The
55 // CSecurity object may call setStreams() again to provide alternative
56 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
57 // streams). Ownership of the streams remains with the caller
58 // (i.e. SConnection will not delete them).
59 void setStreams(rdr::InStream* is, rdr::OutStream* os);
60
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061 // setShared sets the value of the shared flag which will be sent to the
62 // server upon initialisation.
63 void setShared(bool s) { shared = s; }
64
Pierre Ossman9f273e92015-11-09 16:34:54 +010065 // setFramebuffer configures the PixelBuffer that the CConnection
66 // should render all pixel data in to. Note that the CConnection
67 // takes ownership of the PixelBuffer and it must not be deleted by
68 // anyone else. Call setFramebuffer again with NULL or a different
69 // PixelBuffer to delete the previous one.
70 void setFramebuffer(ModifiablePixelBuffer* fb);
71
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072 // initialiseProtocol() should be called once the streams and security
73 // types are set. Subsequently, processMsg() should be called whenever
74 // there is data to read on the InStream.
75 void initialiseProtocol();
76
77 // processMsg() should be called whenever there is either:
78 // - data available on the underlying network stream
79 // In this case, processMsg may return without processing an RFB message,
80 // if the available data does not result in an RFB message being ready
81 // to handle. e.g. if data is encrypted.
82 // NB: This makes it safe to call processMsg() in response to select()
83 // - data available on the CConnection's current InStream
84 // In this case, processMsg should always process the available RFB
85 // message before returning.
86 // NB: In either case, you must have called initialiseProtocol() first.
87 void processMsg();
88
89
Pierre Ossman9f273e92015-11-09 16:34:54 +010090 // Methods overridden from CMsgHandler
91
Pierre Ossman3da238d2015-11-12 12:20:05 +010092 // Note: These must be called by any deriving classes
93
94 virtual void setDesktopSize(int w, int h);
95 virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
96 int w, int h,
97 const ScreenSet& layout);
98
Pierre Ossmanef6881b2018-06-20 11:26:18 +020099 virtual void endOfContinuousUpdates();
100
Pierre Ossmandd45b442018-10-31 17:08:59 +0100101 virtual void serverInit(int width, int height,
102 const PixelFormat& pf,
103 const char* name);
Pierre Ossman2affd772018-06-20 07:03:10 +0200104
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100105 virtual void readAndDecodeRect(const Rect& r, int encoding,
106 ModifiablePixelBuffer* pb);
107
Pierre Ossman3da238d2015-11-12 12:20:05 +0100108 virtual void framebufferUpdateStart();
109 virtual void framebufferUpdateEnd();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100110 virtual void dataRect(const Rect& r, int encoding);
111
112
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 // Methods to be overridden in a derived class
114
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 // authSuccess() is called when authentication has succeeded.
116 virtual void authSuccess();
117
Pierre Ossman2affd772018-06-20 07:03:10 +0200118 // initDone() is called when the connection is fully established
119 // and standard messages can be sent. This is called before the
120 // initial FramebufferUpdateRequest giving a derived class the
Pierre Ossmandd45b442018-10-31 17:08:59 +0100121 // chance to modify pixel format and settings. The derived class
122 // must also make sure it has provided a valid framebuffer before
123 // returning.
124 virtual void initDone() = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125
Pierre Ossman6ea58ba2018-06-20 15:47:49 +0200126 // resizeFramebuffer() is called whenever the framebuffer
127 // dimensions or the screen layout changes. A subclass must make
128 // sure the pixel buffer has been updated once this call returns.
129 virtual void resizeFramebuffer();
130
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131
132 // Other methods
133
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200134 // refreshFramebuffer() forces a complete refresh of the entire
135 // framebuffer
136 void refreshFramebuffer();
137
138 // setPreferredEncoding()/getPreferredEncoding() adjusts which
139 // encoding is listed first as a hint to the server that it is the
140 // preferred one
141 void setPreferredEncoding(int encoding);
142 int getPreferredEncoding();
143 // setCompressLevel()/setQualityLevel() controls the encoding hints
144 // sent to the server
145 void setCompressLevel(int level);
146 void setQualityLevel(int level);
147 // setPF() controls the pixel format requested from the server.
148 // server.pf() will automatically be adjusted once the new format
149 // is active.
150 void setPF(const PixelFormat& pf);
151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 CMsgReader* reader() { return reader_; }
153 CMsgWriter* writer() { return writer_; }
154
155 rdr::InStream* getInStream() { return is; }
156 rdr::OutStream* getOutStream() { return os; }
157
158 // Access method used by SSecurity implementations that can verify servers'
159 // Identities, to determine the unique(ish) name of the server.
160 const char* getServerName() const { return serverName.buf; }
161
Pierre Ossmandaf3d882017-09-01 11:14:35 +0200162 bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 enum stateEnum {
165 RFBSTATE_UNINITIALISED,
166 RFBSTATE_PROTOCOL_VERSION,
167 RFBSTATE_SECURITY_TYPES,
168 RFBSTATE_SECURITY,
169 RFBSTATE_SECURITY_RESULT,
170 RFBSTATE_INITIALISATION,
171 RFBSTATE_NORMAL,
172 RFBSTATE_INVALID
173 };
174
175 stateEnum state() { return state_; }
176
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000177 CSecurity *csecurity;
Michal Srbdccb5f72017-03-27 13:55:46 +0300178 SecurityClient security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179 protected:
180 void setState(stateEnum s) { state_ = s; }
181
Pierre Ossman0144c532015-02-04 14:10:43 +0100182 void setReader(CMsgReader *r) { reader_ = r; }
183 void setWriter(CMsgWriter *w) { writer_ = w; }
184
Pierre Ossman9f273e92015-11-09 16:34:54 +0100185 ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
186
Pierre Ossmanb03512c2018-06-20 16:03:23 +0200187 protected:
188 // Optional capabilities that a subclass is expected to set to true
189 // if supported
190 bool supportsLocalCursor;
191 bool supportsDesktopResize;
192 bool supportsLEDState;
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000195 // This is a default implementation of fences that automatically
196 // responds to requests, stating no support for synchronisation.
197 // When overriding, call CMsgHandler::fence() directly in order to
198 // state correct support for fence flags.
199 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
200
201 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 void processVersionMsg();
203 void processSecurityTypesMsg();
204 void processSecurityMsg();
205 void processSecurityResultMsg();
206 void processInitMsg();
207 void throwAuthFailureException();
208 void throwConnFailedException();
209 void securityCompleted();
210
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200211 void requestNewUpdate();
Pierre Ossman96728352018-06-20 11:35:05 +0200212 void updateEncodings();
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200213
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 rdr::InStream* is;
215 rdr::OutStream* os;
216 CMsgReader* reader_;
217 CMsgWriter* writer_;
218 bool deleteStreamsWhenDone;
219 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 stateEnum state_;
221
222 CharArray serverName;
223
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200224 bool pendingPFChange;
225 rfb::PixelFormat pendingPF;
226
227 int preferredEncoding;
Pierre Ossmanb03512c2018-06-20 16:03:23 +0200228 int compressLevel;
229 int qualityLevel;
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200230
231 bool formatChange;
232 rfb::PixelFormat nextPF;
233 bool encodingChange;
234
235 bool firstUpdate;
236 bool pendingUpdate;
237 bool continuousUpdates;
238
239 bool forceNonincremental;
240
Pierre Ossman9f273e92015-11-09 16:34:54 +0100241 ModifiablePixelBuffer* framebuffer;
242 DecodeManager decoder;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 };
244}
245#endif