0
|
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
|
|
2 // All rights reserved.
|
|
3 //
|
|
4 //-------------------------------------------------------------------------
|
|
5 // This file is part of the TightVNC software. Please visit our Web site:
|
|
6 //
|
|
7 // http://www.tightvnc.com/
|
|
8 //
|
|
9 // This program is free software; you can redistribute it and/or modify
|
|
10 // it under the terms of the GNU General Public License as published by
|
|
11 // the Free Software Foundation; either version 2 of the License, or
|
|
12 // (at your option) any later version.
|
|
13 //
|
|
14 // This program is distributed in the hope that it will be useful,
|
|
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 // GNU General Public License for more details.
|
|
18 //
|
|
19 // You should have received a copy of the GNU General Public License along
|
|
20 // with this program; if not, write to the Free Software Foundation, Inc.,
|
|
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
22 //-------------------------------------------------------------------------
|
|
23 //
|
|
24
|
|
25 package com.glavsoft.rfb.protocol;
|
|
26
|
|
27 import com.glavsoft.core.SettingsChangedEvent;
|
|
28 import com.glavsoft.exceptions.*;
|
|
29 import com.glavsoft.rfb.*;
|
|
30 import com.glavsoft.rfb.client.ClientToServerMessage;
|
|
31 import com.glavsoft.rfb.client.FramebufferUpdateRequestMessage;
|
|
32 import com.glavsoft.rfb.client.SetEncodingsMessage;
|
|
33 import com.glavsoft.rfb.client.SetPixelFormatMessage;
|
|
34 import com.glavsoft.rfb.encoding.PixelFormat;
|
|
35 import com.glavsoft.rfb.encoding.decoder.DecodersContainer;
|
|
36 import com.glavsoft.rfb.protocol.state.HandshakeState;
|
|
37 import com.glavsoft.rfb.protocol.state.ProtocolState;
|
|
38 import com.glavsoft.transport.Reader;
|
|
39 import com.glavsoft.transport.Writer;
|
|
40
|
|
41 import java.util.logging.Logger;
|
|
42
|
|
43 public class Protocol implements ProtocolContext, IChangeSettingsListener {
|
|
44 private ProtocolState state;
|
|
45 private final Logger logger;
|
|
46 private final IPasswordRetriever passwordRetriever;
|
|
47 private final ProtocolSettings settings;
|
|
48 private int fbWidth;
|
|
49 private int fbHeight;
|
|
50 private PixelFormat pixelFormat;
|
|
51 private final Reader reader;
|
|
52 private final Writer writer;
|
|
53 private String remoteDesktopName;
|
|
54 private MessageQueue messageQueue;
|
|
55 private final DecodersContainer decoders;
|
|
56 private SenderTask senderTask;
|
|
57 private ReceiverTask receiverTask;
|
|
58 private IRfbSessionListener rfbSessionListener;
|
|
59 private IRepaintController repaintController;
|
|
60 private PixelFormat serverPixelFormat;
|
|
61 private Thread senderThread;
|
|
62 private Thread receiverThread;
|
|
63 private boolean isTight;
|
|
64 private String protocolVersion;
|
|
65
|
|
66 public Protocol(Reader reader, Writer writer,
|
|
67 IPasswordRetriever passwordRetriever, ProtocolSettings settings) {
|
|
68 this.reader = reader;
|
|
69 this.writer = writer;
|
|
70 this.passwordRetriever = passwordRetriever;
|
|
71 this.settings = settings;
|
|
72 decoders = new DecodersContainer();
|
|
73 decoders.instantiateDecodersWhenNeeded(settings.encodings);
|
|
74 state = new HandshakeState(this);
|
|
75 logger = Logger.getLogger(getClass().getName());
|
|
76 }
|
|
77
|
|
78 @Override
|
|
79 public void changeStateTo(ProtocolState state) {
|
|
80 this.state = state;
|
|
81 }
|
|
82
|
|
83 public void handshake() throws UnsupportedProtocolVersionException, UnsupportedSecurityTypeException,
|
|
84 AuthenticationFailedException, TransportException, FatalException {
|
|
85 while (state.next()) {
|
|
86 // continue;
|
|
87 }
|
|
88 this.messageQueue = new MessageQueue();
|
|
89 }
|
|
90
|
|
91 @Override
|
|
92 public PixelFormat getPixelFormat() {
|
|
93 return pixelFormat;
|
|
94 }
|
|
95
|
|
96 @Override
|
|
97 public void setPixelFormat(PixelFormat pixelFormat) {
|
|
98 this.pixelFormat = pixelFormat;
|
|
99 if (repaintController != null) {
|
|
100 repaintController.setPixelFormat(pixelFormat);
|
|
101 }
|
|
102 }
|
|
103
|
|
104 @Override
|
|
105 public String getRemoteDesktopName() {
|
|
106 return remoteDesktopName;
|
|
107 }
|
|
108
|
|
109 @Override
|
|
110 public void setRemoteDesktopName(String name) {
|
|
111 remoteDesktopName = name;
|
|
112 }
|
|
113
|
|
114 @Override
|
|
115 public int getFbWidth() {
|
|
116 return fbWidth;
|
|
117 }
|
|
118
|
|
119 @Override
|
|
120 public void setFbWidth(int fbWidth) {
|
|
121 this.fbWidth = fbWidth;
|
|
122 }
|
|
123
|
|
124 @Override
|
|
125 public int getFbHeight() {
|
|
126 return fbHeight;
|
|
127 }
|
|
128
|
|
129 @Override
|
|
130 public void setFbHeight(int fbHeight) {
|
|
131 this.fbHeight = fbHeight;
|
|
132 }
|
|
133
|
|
134 @Override
|
|
135 public IPasswordRetriever getPasswordRetriever() {
|
|
136 return passwordRetriever;
|
|
137 }
|
|
138
|
|
139 @Override
|
|
140 public ProtocolSettings getSettings() {
|
|
141 return settings;
|
|
142 }
|
|
143
|
|
144 @Override
|
|
145 public Writer getWriter() {
|
|
146 return writer;
|
|
147 }
|
|
148
|
|
149 @Override
|
|
150 public Reader getReader() {
|
|
151 return reader;
|
|
152 }
|
|
153
|
|
154 /**
|
|
155 * Following the server initialisation message it's up to the client to send
|
|
156 * whichever protocol messages it wants. Typically it will send a
|
|
157 * SetPixelFormat message and a SetEncodings message, followed by a
|
|
158 * FramebufferUpdateRequest. From then on the server will send
|
|
159 * FramebufferUpdate messages in response to the client's
|
|
160 * FramebufferUpdateRequest messages. The client should send
|
|
161 * FramebufferUpdateRequest messages with incremental set to true when it has
|
|
162 * finished processing one FramebufferUpdate and is ready to process another.
|
|
163 * With a fast client, the rate at which FramebufferUpdateRequests are sent
|
|
164 * should be regulated to avoid hogging the network.
|
|
165 */
|
|
166 public void startNormalHandling(IRfbSessionListener rfbSessionListener,
|
|
167 IRepaintController repaintController, ClipboardController clipboardController) {
|
|
168 this.rfbSessionListener = rfbSessionListener;
|
|
169 this.repaintController = repaintController;
|
|
170 // if (settings.getColorDepth() == 0) {
|
|
171 // settings.setColorDepth(pixelFormat.depth); // the same the server sent when not initialized yet
|
|
172 // }
|
|
173 serverPixelFormat = pixelFormat;
|
|
174 correctServerPixelFormat();
|
|
175 setPixelFormat(createPixelFormat(settings));
|
|
176 sendMessage(new SetPixelFormatMessage(pixelFormat));
|
|
177 logger.fine("sent: " + pixelFormat);
|
|
178
|
|
179 sendSupportedEncodingsMessage(settings);
|
|
180 settings.addListener(this); // to support pixel format (color depth), and encodings changes
|
|
181 settings.addListener(repaintController);
|
|
182
|
|
183 sendRefreshMessage();
|
|
184 senderTask = new SenderTask(messageQueue, writer, this);
|
|
185 senderThread = new Thread(senderTask, "RfbSenderTask");
|
|
186 senderThread.start();
|
|
187 decoders.resetDecoders();
|
|
188 receiverTask = new ReceiverTask(
|
|
189 reader, repaintController,
|
|
190 clipboardController,
|
|
191 decoders, this);
|
|
192 receiverThread = new Thread(receiverTask, "RfbReceiverTask");
|
|
193 receiverThread.start();
|
|
194 }
|
|
195
|
|
196 private void correctServerPixelFormat() {
|
|
197 // correct true color flag - we don't support color maps, so always set it up
|
|
198 serverPixelFormat.trueColourFlag = 1;
|
|
199 // correct .depth to use actual depth 24 instead of incorrect 32, used by ex. UltraVNC server, that cause
|
|
200 // protocol incompatibility in ZRLE encoding
|
|
201 final long significant = serverPixelFormat.redMax << serverPixelFormat.redShift |
|
|
202 serverPixelFormat.greenMax << serverPixelFormat.greenShift |
|
|
203 serverPixelFormat.blueMax << serverPixelFormat.blueShift;
|
|
204 if (32 == serverPixelFormat.bitsPerPixel &&
|
|
205 ((significant & 0x00ff000000L) == 0 || (significant & 0x000000ffL) == 0) &&
|
|
206 32 == serverPixelFormat.depth) {
|
|
207 serverPixelFormat.depth = 24;
|
|
208 }
|
|
209 }
|
|
210
|
|
211 @Override
|
|
212 public void sendMessage(ClientToServerMessage message) {
|
|
213 messageQueue.put(message);
|
|
214 }
|
|
215
|
|
216 private void sendSupportedEncodingsMessage(ProtocolSettings settings) {
|
|
217 decoders.instantiateDecodersWhenNeeded(settings.encodings);
|
|
218 SetEncodingsMessage encodingsMessage = new SetEncodingsMessage(settings.encodings);
|
|
219 sendMessage(encodingsMessage);
|
|
220 logger.fine("sent: " + encodingsMessage.toString());
|
|
221 }
|
|
222
|
|
223 /**
|
|
224 * create pixel format by bpp
|
|
225 */
|
|
226 private PixelFormat createPixelFormat(ProtocolSettings settings) {
|
|
227 int serverBigEndianFlag = serverPixelFormat.bigEndianFlag;
|
|
228 switch (settings.getColorDepth()) {
|
|
229 case ProtocolSettings.COLOR_DEPTH_24:
|
|
230 return PixelFormat.create24bitColorDepthPixelFormat(serverBigEndianFlag);
|
|
231 case ProtocolSettings.COLOR_DEPTH_16:
|
|
232 return PixelFormat.create16bitColorDepthPixelFormat(serverBigEndianFlag);
|
|
233 case ProtocolSettings.COLOR_DEPTH_8:
|
|
234 return PixelFormat.create8bitColorDepthBGRPixelFormat(serverBigEndianFlag);
|
|
235 case ProtocolSettings.COLOR_DEPTH_6:
|
|
236 return PixelFormat.create6bitColorDepthPixelFormat(serverBigEndianFlag);
|
|
237 case ProtocolSettings.COLOR_DEPTH_3:
|
|
238 return PixelFormat.create3bppPixelFormat(serverBigEndianFlag);
|
|
239 case ProtocolSettings.COLOR_DEPTH_SERVER_SETTINGS:
|
|
240 return serverPixelFormat;
|
|
241 default:
|
|
242 // unsupported bpp, use default
|
|
243 return PixelFormat.create24bitColorDepthPixelFormat(serverBigEndianFlag);
|
|
244 }
|
|
245 }
|
|
246
|
|
247 @Override
|
|
248 public void settingsChanged(SettingsChangedEvent e) {
|
|
249 ProtocolSettings settings = (ProtocolSettings) e.getSource();
|
|
250 if (settings.isChangedEncodings()) {
|
|
251 sendSupportedEncodingsMessage(settings);
|
|
252 }
|
|
253 if (settings.isChangedColorDepth() && receiverTask != null) {
|
|
254 receiverTask.queueUpdatePixelFormat(createPixelFormat(settings));
|
|
255 }
|
|
256 }
|
|
257
|
|
258 @Override
|
|
259 public void sendRefreshMessage() {
|
|
260 sendMessage(new FramebufferUpdateRequestMessage(0, 0, fbWidth, fbHeight, false));
|
|
261 logger.fine("sent: full FB Refresh");
|
|
262 }
|
|
263
|
|
264 @Override
|
|
265 public void cleanUpSession(String message) {
|
|
266 cleanUpSession();
|
|
267 rfbSessionListener.rfbSessionStopped(message);
|
|
268 }
|
|
269
|
|
270 public synchronized void cleanUpSession() {
|
|
271 if (senderTask != null) { senderTask.stopTask(); }
|
|
272 if (receiverTask != null) { receiverTask.stopTask(); }
|
|
273 if (senderTask != null) {
|
|
274 try {
|
|
275 senderThread.join(1000);
|
|
276 } catch (InterruptedException e) {
|
|
277 // nop
|
|
278 }
|
|
279 senderTask = null;
|
|
280 }
|
|
281 if (receiverTask != null) {
|
|
282 try {
|
|
283 receiverThread.join(1000);
|
|
284 } catch (InterruptedException e) {
|
|
285 // nop
|
|
286 }
|
|
287 receiverTask = null;
|
|
288 }
|
|
289 }
|
|
290
|
|
291 @Override
|
|
292 public void setTight(boolean isTight) {
|
|
293 this.isTight = isTight;
|
|
294 }
|
|
295
|
|
296 @Override
|
|
297 public boolean isTight() {
|
|
298 return isTight;
|
|
299 }
|
|
300
|
|
301 @Override
|
|
302 public void setProtocolVersion(String protocolVersion) {
|
|
303 this.protocolVersion = protocolVersion;
|
|
304 }
|
|
305
|
|
306 @Override
|
|
307 public String getProtocolVersion() {
|
|
308 return protocolVersion;
|
|
309 }
|
|
310
|
7
|
311 public void setMessageQueue(MessageQueue queue) { // add
|
|
312 messageQueue = queue;
|
3
|
313
|
|
314 }
|
|
315
|
0
|
316 }
|