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
|
29
|
27 import alice.daemon.AliceDaemon;
|
9
|
28 import alice.datasegment.DataSegment;
|
18
|
29 import alice.datasegment.ReceiveData;
|
9
|
30
|
0
|
31 import com.glavsoft.drawing.Renderer;
|
|
32 import com.glavsoft.exceptions.CommonException;
|
|
33 import com.glavsoft.exceptions.ProtocolException;
|
|
34 import com.glavsoft.exceptions.TransportException;
|
|
35 import com.glavsoft.rfb.ClipboardController;
|
|
36 import com.glavsoft.rfb.IRepaintController;
|
|
37 import com.glavsoft.rfb.client.FramebufferUpdateRequestMessage;
|
|
38 import com.glavsoft.rfb.client.SetPixelFormatMessage;
|
|
39 import com.glavsoft.rfb.encoding.EncodingType;
|
|
40 import com.glavsoft.rfb.encoding.PixelFormat;
|
|
41 import com.glavsoft.rfb.encoding.decoder.Decoder;
|
|
42 import com.glavsoft.rfb.encoding.decoder.DecodersContainer;
|
|
43 import com.glavsoft.rfb.encoding.decoder.FramebufferUpdateRectangle;
|
|
44 import com.glavsoft.rfb.encoding.decoder.RichCursorDecoder;
|
|
45 import com.glavsoft.transport.Reader;
|
|
46
|
|
47 import java.io.PrintWriter;
|
|
48 import java.io.StringWriter;
|
|
49 import java.util.logging.Logger;
|
|
50
|
26
|
51 import jp.ac.u_ryukyu.ie.cr.DecodeInfomation;
|
|
52
|
0
|
53 public class ReceiverTask implements Runnable {
|
|
54 private static final byte FRAMEBUFFER_UPDATE = 0;
|
|
55 private static final byte SET_COLOR_MAP_ENTRIES = 1;
|
|
56 private static final byte BELL = 2;
|
|
57 private static final byte SERVER_CUT_TEXT = 3;
|
|
58
|
|
59
|
|
60 private static Logger logger = Logger.getLogger("com.glavsoft.rfb.protocol.ReceiverTask");
|
|
61 private final Reader reader;
|
|
62 private volatile boolean isRunning = false;
|
|
63 private Renderer renderer;
|
|
64 private final IRepaintController repaintController;
|
|
65 private final ClipboardController clipboardController;
|
|
66 private final DecodersContainer decoders;
|
|
67 private FramebufferUpdateRequestMessage fullscreenFbUpdateIncrementalRequest;
|
|
68 private final ProtocolContext context;
|
|
69 private PixelFormat pixelFormat;
|
|
70 private boolean needSendPixelFormat;
|
|
71
|
29
|
72 private AliceDaemon aliceDaemon = new AliceDaemon();
|
|
73
|
0
|
74 public ReceiverTask(Reader reader,
|
|
75 IRepaintController repaintController, ClipboardController clipboardController,
|
|
76 DecodersContainer decoders, ProtocolContext context) {
|
|
77 this.reader = reader;
|
|
78 this.repaintController = repaintController;
|
|
79 this.clipboardController = clipboardController;
|
|
80 this.context = context;
|
|
81 this.decoders = decoders;
|
|
82 renderer = repaintController.createRenderer(reader, context.getFbWidth(), context.getFbHeight(),
|
|
83 context.getPixelFormat());
|
|
84 fullscreenFbUpdateIncrementalRequest =
|
|
85 new FramebufferUpdateRequestMessage(0, 0, context.getFbWidth(), context.getFbHeight(), true);
|
|
86 }
|
|
87
|
|
88 @Override
|
|
89 public void run() {
|
|
90 isRunning = true;
|
|
91 while (isRunning) {
|
|
92 try {
|
|
93 byte messageId = reader.readByte();
|
|
94 switch (messageId) {
|
|
95 case FRAMEBUFFER_UPDATE:
|
|
96 // logger.fine("Server message: FramebufferUpdate (0)");
|
|
97 framebufferUpdateMessage();
|
|
98 break;
|
|
99 case SET_COLOR_MAP_ENTRIES:
|
|
100 logger.severe("Server message SetColorMapEntries is not implemented. Skip.");
|
|
101 setColorMapEntries();
|
|
102 break;
|
|
103 case BELL:
|
|
104 logger.fine("Server message: Bell");
|
|
105 System.out.print("\0007");
|
|
106 System.out.flush();
|
|
107 break;
|
|
108 case SERVER_CUT_TEXT:
|
|
109 logger.fine("Server message: CutText (3)");
|
|
110 serverCutText();
|
|
111 break;
|
|
112 default:
|
|
113 logger.severe("Unsupported server message. Id = " + messageId);
|
|
114 }
|
|
115 } catch (TransportException e) {
|
|
116 if (isRunning) {
|
|
117 logger.severe("Close session: " + e.getMessage());
|
|
118 context.cleanUpSession("Connection closed.");
|
|
119 }
|
|
120 stopTask();
|
|
121 } catch (ProtocolException e) {
|
|
122 logger.severe(e.getMessage());
|
|
123 if (isRunning) {
|
|
124 context.cleanUpSession(e.getMessage() + "\nConnection closed.");
|
|
125 }
|
|
126 stopTask();
|
|
127 } catch (CommonException e) {
|
|
128 logger.severe(e.getMessage());
|
|
129 if (isRunning) {
|
|
130 context.cleanUpSession("Connection closed..");
|
|
131 }
|
|
132 stopTask();
|
|
133 } catch (Throwable te) {
|
|
134 StringWriter sw = new StringWriter();
|
|
135 PrintWriter pw = new PrintWriter(sw);
|
|
136 te.printStackTrace(pw);
|
|
137 if (isRunning) {
|
|
138 context.cleanUpSession(te.getMessage() + "\n" + sw.toString());
|
|
139 }
|
|
140 stopTask();
|
|
141 }
|
|
142 }
|
|
143 }
|
|
144
|
|
145 private void setColorMapEntries() throws TransportException {
|
|
146 reader.readByte(); // padding
|
|
147 reader.readUInt16(); // first color index
|
|
148 int length = reader.readUInt16();
|
|
149 while (length-- > 0) {
|
|
150 reader.readUInt16(); // R
|
|
151 reader.readUInt16(); // G
|
|
152 reader.readUInt16(); // B
|
|
153 }
|
|
154 }
|
|
155
|
|
156 private void serverCutText() throws TransportException {
|
|
157 reader.readByte(); // padding
|
|
158 reader.readInt16(); // padding
|
|
159 int length = reader.readInt32() & Integer.MAX_VALUE;
|
|
160 clipboardController.updateSystemClipboard(reader.readBytes(length));
|
|
161 }
|
|
162
|
|
163 public void framebufferUpdateMessage() throws CommonException {
|
|
164 reader.readByte(); // padding
|
|
165 int numberOfRectangles = reader.readUInt16();
|
|
166 while (numberOfRectangles-- > 0) {
|
|
167 FramebufferUpdateRectangle rect = new FramebufferUpdateRectangle();
|
|
168 rect.fill(reader);
|
|
169
|
|
170 Decoder decoder = decoders.getDecoderByType(rect.getEncodingType());
|
|
171 logger.finest(rect.toString() + (0 == numberOfRectangles ? "\n---" : ""));
|
|
172 if (decoder != null) {
|
|
173 decoder.decode(reader, renderer, rect);
|
|
174 repaintController.repaintBitmap(rect);
|
|
175 } else if (rect.getEncodingType() == EncodingType.RICH_CURSOR) {
|
|
176 RichCursorDecoder.getInstance().decode(reader, renderer, rect);
|
|
177 repaintController.repaintCursor();
|
|
178 } else if (rect.getEncodingType() == EncodingType.CURSOR_POS) {
|
|
179 renderer.decodeCursorPosition(rect);
|
|
180 repaintController.repaintCursor();
|
|
181 } else if (rect.getEncodingType() == EncodingType.DESKTOP_SIZE) {
|
|
182 fullscreenFbUpdateIncrementalRequest =
|
|
183 new FramebufferUpdateRequestMessage(0, 0, rect.width, rect.height, true);
|
|
184 synchronized (renderer.getLock()) {
|
|
185 renderer = repaintController.createRenderer(reader, rect.width, rect.height,
|
|
186 context.getPixelFormat());
|
|
187 }
|
26
|
188 DecodeInfomation message = new DecodeInfomation();
|
9
|
189 message.setRectangle(rect);
|
|
190 message.setPixelFormat(context.getPixelFormat());
|
26
|
191
|
29
|
192 ReceiveData rData = new ReceiveData("dummy");
|
|
193 aliceDaemon.dataSegment.getLocal().put("pixelData", rData, false);
|
|
194 rData = new ReceiveData(message);
|
|
195 aliceDaemon.dataSegment.getLocal().put("updateRectangle", rData, false);
|
26
|
196
|
0
|
197 context.sendMessage(new FramebufferUpdateRequestMessage(0, 0, rect.width, rect.height, false));
|
|
198 // repaintController.repaintCursor();
|
|
199 } else
|
|
200 throw new CommonException("Unprocessed encoding: " + rect.toString());
|
|
201 }
|
|
202 synchronized (this) {
|
|
203 if (needSendPixelFormat) {
|
|
204 needSendPixelFormat = false;
|
|
205 context.setPixelFormat(pixelFormat);
|
|
206 context.sendMessage(new SetPixelFormatMessage(pixelFormat));
|
|
207 logger.fine("sent: "+pixelFormat);
|
|
208 context.sendRefreshMessage();
|
|
209 logger.fine("sent: nonincremental fb update");
|
|
210 } else {
|
|
211 context.sendMessage(fullscreenFbUpdateIncrementalRequest);
|
|
212 }
|
|
213 }
|
|
214 }
|
|
215
|
|
216 public synchronized void queueUpdatePixelFormat(PixelFormat pf) {
|
|
217 pixelFormat = pf;
|
|
218 needSendPixelFormat = true;
|
|
219 // context.sendMessage(new FramebufferUpdateRequestMessage(0, 0, 1, 1, false));
|
|
220 }
|
|
221
|
|
222 public void stopTask() {
|
|
223 isRunning = false;
|
|
224 }
|
|
225
|
|
226 }
|