blob: 5f953ae180da1eb272951d4a0a9b35aa70786964 [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
65 // setProtocol3_3 configures whether or not the CConnection should
66 // only ever support protocol version 3.3
67 void setProtocol3_3(bool s) {useProtocol3_3 = s;}
68
Pierre Ossman9f273e92015-11-09 16:34:54 +010069 // setFramebuffer configures the PixelBuffer that the CConnection
70 // should render all pixel data in to. Note that the CConnection
71 // takes ownership of the PixelBuffer and it must not be deleted by
72 // anyone else. Call setFramebuffer again with NULL or a different
73 // PixelBuffer to delete the previous one.
74 void setFramebuffer(ModifiablePixelBuffer* fb);
75
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 // initialiseProtocol() should be called once the streams and security
77 // types are set. Subsequently, processMsg() should be called whenever
78 // there is data to read on the InStream.
79 void initialiseProtocol();
80
81 // processMsg() should be called whenever there is either:
82 // - data available on the underlying network stream
83 // In this case, processMsg may return without processing an RFB message,
84 // if the available data does not result in an RFB message being ready
85 // to handle. e.g. if data is encrypted.
86 // NB: This makes it safe to call processMsg() in response to select()
87 // - data available on the CConnection's current InStream
88 // In this case, processMsg should always process the available RFB
89 // message before returning.
90 // NB: In either case, you must have called initialiseProtocol() first.
91 void processMsg();
92
93
Pierre Ossman9f273e92015-11-09 16:34:54 +010094 // Methods overridden from CMsgHandler
95
Pierre Ossman3da238d2015-11-12 12:20:05 +010096 // Note: These must be called by any deriving classes
97
98 virtual void setDesktopSize(int w, int h);
99 virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
100 int w, int h,
101 const ScreenSet& layout);
102
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200103 virtual void endOfContinuousUpdates();
104
Pierre Ossmandd45b442018-10-31 17:08:59 +0100105 virtual void serverInit(int width, int height,
106 const PixelFormat& pf,
107 const char* name);
Pierre Ossman2affd772018-06-20 07:03:10 +0200108
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100109 virtual void readAndDecodeRect(const Rect& r, int encoding,
110 ModifiablePixelBuffer* pb);
111
Pierre Ossman3da238d2015-11-12 12:20:05 +0100112 virtual void framebufferUpdateStart();
113 virtual void framebufferUpdateEnd();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100114 virtual void dataRect(const Rect& r, int encoding);
115
116
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 // Methods to be overridden in a derived class
118
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119 // authSuccess() is called when authentication has succeeded.
120 virtual void authSuccess();
121
Pierre Ossman2affd772018-06-20 07:03:10 +0200122 // initDone() is called when the connection is fully established
123 // and standard messages can be sent. This is called before the
124 // initial FramebufferUpdateRequest giving a derived class the
Pierre Ossmandd45b442018-10-31 17:08:59 +0100125 // chance to modify pixel format and settings. The derived class
126 // must also make sure it has provided a valid framebuffer before
127 // returning.
128 virtual void initDone() = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129
130
131 // Other methods
132
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200133 // refreshFramebuffer() forces a complete refresh of the entire
134 // framebuffer
135 void refreshFramebuffer();
136
137 // setPreferredEncoding()/getPreferredEncoding() adjusts which
138 // encoding is listed first as a hint to the server that it is the
139 // preferred one
140 void setPreferredEncoding(int encoding);
141 int getPreferredEncoding();
142 // setCompressLevel()/setQualityLevel() controls the encoding hints
143 // sent to the server
144 void setCompressLevel(int level);
145 void setQualityLevel(int level);
146 // setPF() controls the pixel format requested from the server.
147 // server.pf() will automatically be adjusted once the new format
148 // is active.
149 void setPF(const PixelFormat& pf);
150
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 CMsgReader* reader() { return reader_; }
152 CMsgWriter* writer() { return writer_; }
153
154 rdr::InStream* getInStream() { return is; }
155 rdr::OutStream* getOutStream() { return os; }
156
157 // Access method used by SSecurity implementations that can verify servers'
158 // Identities, to determine the unique(ish) name of the server.
159 const char* getServerName() const { return serverName.buf; }
160
Pierre Ossmandaf3d882017-09-01 11:14:35 +0200161 bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
162
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000163 enum stateEnum {
164 RFBSTATE_UNINITIALISED,
165 RFBSTATE_PROTOCOL_VERSION,
166 RFBSTATE_SECURITY_TYPES,
167 RFBSTATE_SECURITY,
168 RFBSTATE_SECURITY_RESULT,
169 RFBSTATE_INITIALISATION,
170 RFBSTATE_NORMAL,
171 RFBSTATE_INVALID
172 };
173
174 stateEnum state() { return state_; }
175
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000176 CSecurity *csecurity;
Michal Srbdccb5f72017-03-27 13:55:46 +0300177 SecurityClient security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 protected:
179 void setState(stateEnum s) { state_ = s; }
180
Pierre Ossman0144c532015-02-04 14:10:43 +0100181 void setReader(CMsgReader *r) { reader_ = r; }
182 void setWriter(CMsgWriter *w) { writer_ = w; }
183
Pierre Ossman9f273e92015-11-09 16:34:54 +0100184 ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
185
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000187 // This is a default implementation of fences that automatically
188 // responds to requests, stating no support for synchronisation.
189 // When overriding, call CMsgHandler::fence() directly in order to
190 // state correct support for fence flags.
191 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
192
193 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 void processVersionMsg();
195 void processSecurityTypesMsg();
196 void processSecurityMsg();
197 void processSecurityResultMsg();
198 void processInitMsg();
199 void throwAuthFailureException();
200 void throwConnFailedException();
201 void securityCompleted();
202
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200203 void requestNewUpdate();
204
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 rdr::InStream* is;
206 rdr::OutStream* os;
207 CMsgReader* reader_;
208 CMsgWriter* writer_;
209 bool deleteStreamsWhenDone;
210 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 stateEnum state_;
212
213 CharArray serverName;
214
215 bool useProtocol3_3;
Pierre Ossman9f273e92015-11-09 16:34:54 +0100216
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200217 bool pendingPFChange;
218 rfb::PixelFormat pendingPF;
219
220 int preferredEncoding;
221
222 bool formatChange;
223 rfb::PixelFormat nextPF;
224 bool encodingChange;
225
226 bool firstUpdate;
227 bool pendingUpdate;
228 bool continuousUpdates;
229
230 bool forceNonincremental;
231
Pierre Ossman9f273e92015-11-09 16:34:54 +0100232 ModifiablePixelBuffer* framebuffer;
233 DecodeManager decoder;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000234 };
235}
236#endif