Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 2 | * Copyright 2011-2017 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | */ |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 19 | #include <assert.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 22 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | #include <rfb/Exception.h> |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 24 | #include <rfb/fenceTypes.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 25 | #include <rfb/CMsgReader.h> |
| 26 | #include <rfb/CMsgWriter.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #include <rfb/CSecurity.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 28 | #include <rfb/Security.h> |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 29 | #include <rfb/SecurityClient.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #include <rfb/CConnection.h> |
| 31 | #include <rfb/util.h> |
| 32 | |
| 33 | #include <rfb/LogWriter.h> |
| 34 | |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 35 | #include <rdr/InStream.h> |
| 36 | #include <rdr/OutStream.h> |
| 37 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 38 | using namespace rfb; |
| 39 | |
| 40 | static LogWriter vlog("CConnection"); |
| 41 | |
| 42 | CConnection::CConnection() |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 43 | : csecurity(0), is(0), os(0), reader_(0), writer_(0), |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 44 | shared(false), |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 45 | state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false), |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 46 | pendingPFChange(false), preferredEncoding(encodingTight), |
| 47 | formatChange(false), encodingChange(false), |
| 48 | firstUpdate(true), pendingUpdate(false), continuousUpdates(false), |
| 49 | forceNonincremental(true), |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 50 | framebuffer(NULL), decoder(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | CConnection::~CConnection() |
| 55 | { |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 56 | setFramebuffer(NULL); |
Pierre Ossman | 82d22e6 | 2018-09-21 15:26:37 +0200 | [diff] [blame] | 57 | if (csecurity) |
| 58 | delete csecurity; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 59 | delete reader_; |
| 60 | reader_ = 0; |
| 61 | delete writer_; |
| 62 | writer_ = 0; |
| 63 | } |
| 64 | |
| 65 | void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 66 | { |
| 67 | is = is_; |
| 68 | os = os_; |
| 69 | } |
| 70 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 71 | void CConnection::setFramebuffer(ModifiablePixelBuffer* fb) |
| 72 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 73 | decoder.flush(); |
| 74 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 75 | if ((framebuffer != NULL) && (fb != NULL)) { |
| 76 | Rect rect; |
| 77 | |
| 78 | const rdr::U8* data; |
| 79 | int stride; |
| 80 | |
| 81 | const rdr::U8 black[4] = { 0, 0, 0, 0 }; |
| 82 | |
| 83 | // Copy still valid area |
| 84 | |
| 85 | rect.setXYWH(0, 0, |
| 86 | __rfbmin(fb->width(), framebuffer->width()), |
| 87 | __rfbmin(fb->height(), framebuffer->height())); |
| 88 | data = framebuffer->getBuffer(framebuffer->getRect(), &stride); |
| 89 | fb->imageRect(rect, data, stride); |
| 90 | |
| 91 | // Black out any new areas |
| 92 | |
| 93 | if (fb->width() > framebuffer->width()) { |
| 94 | rect.setXYWH(framebuffer->width(), 0, |
Brian P. Hinz | 5d66305 | 2016-09-05 09:15:50 -0400 | [diff] [blame] | 95 | fb->width() - framebuffer->width(), |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 96 | fb->height()); |
| 97 | fb->fillRect(rect, black); |
| 98 | } |
| 99 | |
| 100 | if (fb->height() > framebuffer->height()) { |
| 101 | rect.setXYWH(0, framebuffer->height(), |
| 102 | fb->width(), |
| 103 | fb->height() - framebuffer->height()); |
| 104 | fb->fillRect(rect, black); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | delete framebuffer; |
| 109 | framebuffer = fb; |
| 110 | } |
| 111 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | void CConnection::initialiseProtocol() |
| 113 | { |
| 114 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 115 | } |
| 116 | |
| 117 | void CConnection::processMsg() |
| 118 | { |
| 119 | switch (state_) { |
| 120 | |
| 121 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 122 | case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break; |
| 123 | case RFBSTATE_SECURITY: processSecurityMsg(); break; |
| 124 | case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break; |
| 125 | case RFBSTATE_INITIALISATION: processInitMsg(); break; |
| 126 | case RFBSTATE_NORMAL: reader_->readMsg(); break; |
| 127 | case RFBSTATE_UNINITIALISED: |
| 128 | throw Exception("CConnection::processMsg: not initialised yet?"); |
| 129 | default: |
| 130 | throw Exception("CConnection::processMsg: invalid state"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void CConnection::processVersionMsg() |
| 135 | { |
Pierre Ossman | ea7ede9 | 2018-06-18 16:51:53 +0200 | [diff] [blame] | 136 | char verStr[13]; |
| 137 | int majorVersion; |
| 138 | int minorVersion; |
| 139 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 140 | vlog.debug("reading protocol version"); |
Pierre Ossman | ea7ede9 | 2018-06-18 16:51:53 +0200 | [diff] [blame] | 141 | |
| 142 | if (!is->checkNoWait(12)) |
| 143 | return; |
| 144 | |
| 145 | is->readBytes(verStr, 12); |
| 146 | verStr[12] = '\0'; |
| 147 | |
| 148 | if (sscanf(verStr, "RFB %03d.%03d\n", |
| 149 | &majorVersion, &minorVersion) != 2) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 150 | state_ = RFBSTATE_INVALID; |
| 151 | throw Exception("reading version failed: not an RFB server?"); |
| 152 | } |
Pierre Ossman | ea7ede9 | 2018-06-18 16:51:53 +0200 | [diff] [blame] | 153 | |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 154 | server.setVersion(majorVersion, minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 155 | |
| 156 | vlog.info("Server supports RFB protocol version %d.%d", |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 157 | server.majorVersion, server.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 158 | |
| 159 | // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8 |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 160 | if (server.beforeVersion(3,3)) { |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 161 | vlog.error("Server gave unsupported RFB protocol version %d.%d", |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 162 | server.majorVersion, server.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 163 | state_ = RFBSTATE_INVALID; |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 164 | throw Exception("Server gave unsupported RFB protocol version %d.%d", |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 165 | server.majorVersion, server.minorVersion); |
| 166 | } else if (useProtocol3_3 || server.beforeVersion(3,7)) { |
| 167 | server.setVersion(3,3); |
| 168 | } else if (server.afterVersion(3,8)) { |
| 169 | server.setVersion(3,8); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 172 | sprintf(verStr, "RFB %03d.%03d\n", |
| 173 | server.majorVersion, server.minorVersion); |
Pierre Ossman | ea7ede9 | 2018-06-18 16:51:53 +0200 | [diff] [blame] | 174 | os->writeBytes(verStr, 12); |
| 175 | os->flush(); |
| 176 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 177 | state_ = RFBSTATE_SECURITY_TYPES; |
| 178 | |
| 179 | vlog.info("Using RFB protocol version %d.%d", |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 180 | server.majorVersion, server.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | void CConnection::processSecurityTypesMsg() |
| 185 | { |
| 186 | vlog.debug("processing security types message"); |
| 187 | |
| 188 | int secType = secTypeInvalid; |
| 189 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 190 | std::list<rdr::U8> secTypes; |
Michal Srb | dccb5f7 | 2017-03-27 13:55:46 +0300 | [diff] [blame] | 191 | secTypes = security.GetEnabledSecTypes(); |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 192 | |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 193 | if (server.isVersion(3,3)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 194 | |
| 195 | // legacy 3.3 server may only offer "vnc authentication" or "none" |
| 196 | |
| 197 | secType = is->readU32(); |
| 198 | if (secType == secTypeInvalid) { |
| 199 | throwConnFailedException(); |
| 200 | |
| 201 | } else if (secType == secTypeNone || secType == secTypeVncAuth) { |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 202 | std::list<rdr::U8>::iterator i; |
| 203 | for (i = secTypes.begin(); i != secTypes.end(); i++) |
| 204 | if (*i == secType) { |
| 205 | secType = *i; |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (i == secTypes.end()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | secType = secTypeInvalid; |
| 211 | } else { |
| 212 | vlog.error("Unknown 3.3 security type %d", secType); |
| 213 | throw Exception("Unknown 3.3 security type"); |
| 214 | } |
| 215 | |
| 216 | } else { |
| 217 | |
| 218 | // >=3.7 server will offer us a list |
| 219 | |
| 220 | int nServerSecTypes = is->readU8(); |
| 221 | if (nServerSecTypes == 0) |
| 222 | throwConnFailedException(); |
| 223 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 224 | std::list<rdr::U8>::iterator j; |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 225 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 226 | for (int i = 0; i < nServerSecTypes; i++) { |
| 227 | rdr::U8 serverSecType = is->readU8(); |
| 228 | vlog.debug("Server offers security type %s(%d)", |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 229 | secTypeName(serverSecType), serverSecType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 231 | /* |
| 232 | * Use the first type sent by server which matches client's type. |
| 233 | * It means server's order specifies priority. |
| 234 | */ |
| 235 | if (secType == secTypeInvalid) { |
| 236 | for (j = secTypes.begin(); j != secTypes.end(); j++) |
| 237 | if (*j == serverSecType) { |
| 238 | secType = *j; |
| 239 | break; |
| 240 | } |
| 241 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Inform the server of our decision |
| 245 | if (secType != secTypeInvalid) { |
| 246 | os->writeU8(secType); |
| 247 | os->flush(); |
Pierre Ossman | 71d6666 | 2014-11-11 13:42:51 +0100 | [diff] [blame] | 248 | vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | if (secType == secTypeInvalid) { |
| 253 | state_ = RFBSTATE_INVALID; |
| 254 | vlog.error("No matching security types"); |
| 255 | throw Exception("No matching security types"); |
| 256 | } |
| 257 | |
| 258 | state_ = RFBSTATE_SECURITY; |
Pierre Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 259 | csecurity = security.GetCSecurity(this, secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | processSecurityMsg(); |
| 261 | } |
| 262 | |
| 263 | void CConnection::processSecurityMsg() |
| 264 | { |
| 265 | vlog.debug("processing security message"); |
Pierre Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 266 | if (csecurity->processMsg()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 267 | state_ = RFBSTATE_SECURITY_RESULT; |
| 268 | processSecurityResultMsg(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void CConnection::processSecurityResultMsg() |
| 273 | { |
| 274 | vlog.debug("processing security result message"); |
| 275 | int result; |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 276 | if (server.beforeVersion(3,8) && csecurity->getType() == secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 277 | result = secResultOK; |
| 278 | } else { |
| 279 | if (!is->checkNoWait(1)) return; |
| 280 | result = is->readU32(); |
| 281 | } |
| 282 | switch (result) { |
| 283 | case secResultOK: |
| 284 | securityCompleted(); |
| 285 | return; |
| 286 | case secResultFailed: |
| 287 | vlog.debug("auth failed"); |
| 288 | break; |
| 289 | case secResultTooMany: |
| 290 | vlog.debug("auth failed - too many tries"); |
| 291 | break; |
| 292 | default: |
| 293 | throw Exception("Unknown security result from server"); |
| 294 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 295 | state_ = RFBSTATE_INVALID; |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 296 | if (server.beforeVersion(3,8)) |
Pierre Ossman | 1922550 | 2017-10-12 15:05:07 +0200 | [diff] [blame] | 297 | throw AuthFailureException(); |
| 298 | CharArray reason(is->readString()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 299 | throw AuthFailureException(reason.buf); |
| 300 | } |
| 301 | |
| 302 | void CConnection::processInitMsg() |
| 303 | { |
| 304 | vlog.debug("reading server initialisation"); |
| 305 | reader_->readServerInit(); |
| 306 | } |
| 307 | |
| 308 | void CConnection::throwConnFailedException() |
| 309 | { |
| 310 | state_ = RFBSTATE_INVALID; |
| 311 | CharArray reason; |
| 312 | reason.buf = is->readString(); |
| 313 | throw ConnFailedException(reason.buf); |
| 314 | } |
| 315 | |
| 316 | void CConnection::securityCompleted() |
| 317 | { |
| 318 | state_ = RFBSTATE_INITIALISATION; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 319 | reader_ = new CMsgReader(this, is); |
Pierre Ossman | b14a6bc | 2018-06-18 15:44:26 +0200 | [diff] [blame] | 320 | writer_ = new CMsgWriter(&server, os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 321 | vlog.debug("Authentication success!"); |
| 322 | authSuccess(); |
| 323 | writer_->writeClientInit(shared); |
| 324 | } |
| 325 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 326 | void CConnection::setDesktopSize(int w, int h) |
| 327 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 328 | decoder.flush(); |
| 329 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 330 | CMsgHandler::setDesktopSize(w,h); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 331 | |
| 332 | if (continuousUpdates) |
| 333 | writer()->writeEnableContinuousUpdates(true, 0, 0, |
| 334 | server.width(), |
| 335 | server.height()); |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void CConnection::setExtendedDesktopSize(unsigned reason, |
| 339 | unsigned result, |
| 340 | int w, int h, |
| 341 | const ScreenSet& layout) |
| 342 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 343 | decoder.flush(); |
| 344 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 345 | CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 346 | |
| 347 | if (continuousUpdates) |
| 348 | writer()->writeEnableContinuousUpdates(true, 0, 0, |
| 349 | server.width(), |
| 350 | server.height()); |
| 351 | } |
| 352 | |
| 353 | void CConnection::endOfContinuousUpdates() |
| 354 | { |
| 355 | CMsgHandler::endOfContinuousUpdates(); |
| 356 | |
| 357 | // We've gotten the marker for a format change, so make the pending |
| 358 | // one active |
| 359 | if (pendingPFChange) { |
| 360 | server.setPF(pendingPF); |
| 361 | pendingPFChange = false; |
| 362 | |
| 363 | // We might have another change pending |
| 364 | if (formatChange) |
| 365 | requestNewUpdate(); |
| 366 | } |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 369 | void CConnection::serverInit(int width, int height, |
| 370 | const PixelFormat& pf, |
| 371 | const char* name) |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 372 | { |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 373 | CMsgHandler::serverInit(width, height, pf, name); |
| 374 | |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 375 | state_ = RFBSTATE_NORMAL; |
| 376 | vlog.debug("initialisation done"); |
| 377 | |
| 378 | initDone(); |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 379 | assert(framebuffer != NULL); |
| 380 | assert(framebuffer->width() == server.width()); |
| 381 | assert(framebuffer->height() == server.height()); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 382 | |
| 383 | // We want to make sure we call SetEncodings at least once |
| 384 | encodingChange = true; |
| 385 | |
| 386 | requestNewUpdate(); |
| 387 | |
| 388 | // This initial update request is a bit of a corner case, so we need |
| 389 | // to help out setting the correct format here. |
| 390 | if (pendingPFChange) { |
| 391 | server.setPF(pendingPF); |
| 392 | pendingPFChange = false; |
| 393 | } |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 396 | void CConnection::readAndDecodeRect(const Rect& r, int encoding, |
| 397 | ModifiablePixelBuffer* pb) |
| 398 | { |
| 399 | decoder.decodeRect(r, encoding, pb); |
| 400 | decoder.flush(); |
| 401 | } |
| 402 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 403 | void CConnection::framebufferUpdateStart() |
| 404 | { |
| 405 | CMsgHandler::framebufferUpdateStart(); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 406 | |
| 407 | assert(framebuffer != NULL); |
| 408 | |
| 409 | // Note: This might not be true if continuous updates are supported |
| 410 | pendingUpdate = false; |
| 411 | |
| 412 | requestNewUpdate(); |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void CConnection::framebufferUpdateEnd() |
| 416 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 417 | decoder.flush(); |
| 418 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 419 | CMsgHandler::framebufferUpdateEnd(); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 420 | |
| 421 | // A format change has been scheduled and we are now past the update |
| 422 | // with the old format. Time to active the new one. |
| 423 | if (pendingPFChange && !continuousUpdates) { |
| 424 | server.setPF(pendingPF); |
| 425 | pendingPFChange = false; |
| 426 | } |
| 427 | |
| 428 | if (firstUpdate) { |
| 429 | if (server.supportsContinuousUpdates) { |
| 430 | vlog.info("Enabling continuous updates"); |
| 431 | continuousUpdates = true; |
| 432 | writer()->writeEnableContinuousUpdates(true, 0, 0, |
| 433 | server.width(), |
| 434 | server.height()); |
| 435 | } |
| 436 | |
| 437 | firstUpdate = false; |
| 438 | } |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 441 | void CConnection::dataRect(const Rect& r, int encoding) |
| 442 | { |
| 443 | decoder.decodeRect(r, encoding, framebuffer); |
| 444 | } |
| 445 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 446 | void CConnection::authSuccess() |
| 447 | { |
| 448 | } |
| 449 | |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 450 | void CConnection::initDone() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 451 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 452 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 453 | |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 454 | void CConnection::refreshFramebuffer() |
| 455 | { |
| 456 | forceNonincremental = true; |
| 457 | |
| 458 | // Without continuous updates we have to make sure we only have a |
| 459 | // single update in flight, so we'll have to wait to do the refresh |
| 460 | if (continuousUpdates) |
| 461 | requestNewUpdate(); |
| 462 | } |
| 463 | |
| 464 | void CConnection::setPreferredEncoding(int encoding) |
| 465 | { |
| 466 | if (preferredEncoding == encoding) |
| 467 | return; |
| 468 | |
| 469 | preferredEncoding = encoding; |
| 470 | encodingChange = true; |
| 471 | } |
| 472 | |
| 473 | int CConnection::getPreferredEncoding() |
| 474 | { |
| 475 | return preferredEncoding; |
| 476 | } |
| 477 | |
| 478 | void CConnection::setCompressLevel(int level) |
| 479 | { |
| 480 | if (server.compressLevel == level) |
| 481 | return; |
| 482 | |
| 483 | server.compressLevel = level; |
| 484 | encodingChange = true; |
| 485 | } |
| 486 | |
| 487 | void CConnection::setQualityLevel(int level) |
| 488 | { |
| 489 | if (server.qualityLevel == level) |
| 490 | return; |
| 491 | |
| 492 | server.qualityLevel = level; |
| 493 | encodingChange = true; |
| 494 | } |
| 495 | |
| 496 | void CConnection::setPF(const PixelFormat& pf) |
| 497 | { |
| 498 | if (server.pf().equal(pf) && !formatChange) |
| 499 | return; |
| 500 | |
| 501 | nextPF = pf; |
| 502 | formatChange = true; |
| 503 | } |
| 504 | |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 505 | void CConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 506 | { |
| 507 | CMsgHandler::fence(flags, len, data); |
| 508 | |
| 509 | if (!(flags & fenceFlagRequest)) |
| 510 | return; |
| 511 | |
| 512 | // We cannot guarantee any synchronisation at this level |
| 513 | flags = 0; |
| 514 | |
| 515 | writer()->writeFence(flags, len, data); |
| 516 | } |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame^] | 517 | |
| 518 | // requestNewUpdate() requests an update from the server, having set the |
| 519 | // format and encoding appropriately. |
| 520 | void CConnection::requestNewUpdate() |
| 521 | { |
| 522 | if (formatChange && !pendingPFChange) { |
| 523 | /* Catch incorrect requestNewUpdate calls */ |
| 524 | assert(!pendingUpdate || continuousUpdates); |
| 525 | |
| 526 | // We have to make sure we switch the internal format at a safe |
| 527 | // time. For continuous updates we temporarily disable updates and |
| 528 | // look for a EndOfContinuousUpdates message to see when to switch. |
| 529 | // For classical updates we just got a new update right before this |
| 530 | // function was called, so we need to make sure we finish that |
| 531 | // update before we can switch. |
| 532 | |
| 533 | pendingPFChange = true; |
| 534 | pendingPF = nextPF; |
| 535 | |
| 536 | if (continuousUpdates) |
| 537 | writer()->writeEnableContinuousUpdates(false, 0, 0, 0, 0); |
| 538 | |
| 539 | writer()->writeSetPixelFormat(pendingPF); |
| 540 | |
| 541 | if (continuousUpdates) |
| 542 | writer()->writeEnableContinuousUpdates(true, 0, 0, |
| 543 | server.width(), |
| 544 | server.height()); |
| 545 | |
| 546 | formatChange = false; |
| 547 | } |
| 548 | |
| 549 | if (encodingChange) { |
| 550 | writer()->writeSetEncodings(preferredEncoding, true); |
| 551 | encodingChange = false; |
| 552 | } |
| 553 | |
| 554 | if (forceNonincremental || !continuousUpdates) { |
| 555 | pendingUpdate = true; |
| 556 | writer()->writeFramebufferUpdateRequest(Rect(0, 0, |
| 557 | server.width(), |
| 558 | server.height()), |
| 559 | !forceNonincremental); |
| 560 | } |
| 561 | |
| 562 | forceNonincremental = false; |
| 563 | } |