Mercurial > hg > Applications > TightVNC_orginal
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java @ 38:c2f0b6907448
add doc directory
author | one |
---|---|
date | Thu, 27 Sep 2012 19:32:25 +0900 |
parents | b7d4d0349f99 |
children | fe38611c4b2b |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.client; |
3 | 2 |
3 import java.io.DataInputStream; | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 import java.io.OutputStream; | |
4 | 7 import java.net.BindException; |
8 import java.net.ServerSocket; | |
3 | 9 import java.net.Socket; |
10 import java.nio.ByteBuffer; | |
4 | 11 import java.util.LinkedList; |
12 import java.util.zip.DataFormatException; | |
13 import java.util.zip.Deflater; | |
14 import java.util.zip.Inflater; | |
3 | 15 |
32 | 16 import jp.ac.u_ryukyu.treevnc.MyRfbProto; |
12 | 17 |
3 | 18 import com.glavsoft.exceptions.TransportException; |
4 | 19 import com.glavsoft.rfb.encoding.EncodingType; |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
20 import com.glavsoft.rfb.protocol.ProtocolContext; |
3 | 21 import com.glavsoft.transport.Reader; |
22 | |
28 | 23 public class MyRfbProtoClient extends MyRfbProto { |
4 | 24 final static int FramebufferUpdate = 0; |
25 final static int CheckDelay = 11; | |
26 final static String versionMsg_3_855 = "RFB 003.855\n"; | |
29 | 27 private static final int INFLATE_BUFSIZE = 1024 * 100; |
3 | 28 private Reader reader; |
36 | 29 private String host; |
30 private int port; | |
29 | 31 Socket clientSocket, sock; |
3 | 32 DataInputStream is; |
33 OutputStream os; | |
4 | 34 private ServerSocket servSock; |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
35 private ProtocolContext context; |
4 | 36 boolean proxyFlag = false; |
37 int serverMajor, serverMinor; | |
38 int clientMajor, clientMinor; | |
29 | 39 |
4 | 40 private Inflater inflater = new Inflater(); |
41 private Deflater deflater = new Deflater(); | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
42 |
31 | 43 public MyRfbProtoClient() { |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
44 |
31 | 45 } |
29 | 46 |
47 public MyRfbProtoClient(Reader reader, String host, String port) { | |
3 | 48 this.reader = reader; |
49 } | |
29 | 50 |
51 | |
3 | 52 public boolean readProxyFlag() throws TransportException { |
53 int flag = reader.readUInt8(); | |
29 | 54 if (flag == 1) |
3 | 55 return true; |
56 else | |
57 return false; | |
58 } | |
29 | 59 |
3 | 60 public byte[] readEchoPort() throws Exception { |
61 byte[] b = new byte[4]; | |
62 reader.readBytes(b, 0, b.length); | |
63 return b; | |
64 } | |
29 | 65 |
66 | |
3 | 67 int castByteInt(byte[] b) { |
68 ByteBuffer bb = ByteBuffer.wrap(b); | |
69 int value = bb.getInt(); | |
70 return value; | |
71 } | |
29 | 72 |
3 | 73 Socket changeParent(String host, int port) throws IOException { |
74 sock = new Socket(host, port); | |
75 return sock; | |
76 } | |
29 | 77 |
31 | 78 @Override |
4 | 79 public Socket accept() throws IOException { |
80 return servSock.accept(); | |
81 } | |
29 | 82 |
4 | 83 void initServSock(int port) throws IOException { |
84 servSock = new ServerSocket(port); | |
36 | 85 // acceptPort = port; |
4 | 86 } |
29 | 87 |
31 | 88 @Override |
38 | 89 public int selectPort(int p) { |
4 | 90 int port = p; |
91 while (true) { | |
92 try { | |
93 initServSock(port); | |
94 break; | |
95 } catch (BindException e) { | |
96 port++; | |
97 continue; | |
98 } catch (IOException e) { | |
99 | |
100 } | |
101 } | |
102 System.out.println("accept port = " + port); | |
38 | 103 return port; |
4 | 104 } |
29 | 105 |
4 | 106 void sendRfbVersion(OutputStream os) throws IOException { |
107 os.write(versionMsg_3_855.getBytes()); | |
29 | 108 // os.write(versionMsg_3_8.getBytes()); |
4 | 109 } |
29 | 110 |
4 | 111 int readVersionMsg(InputStream is, OutputStream os) throws IOException { |
112 | |
113 byte[] b = new byte[12]; | |
114 | |
115 is.read(b); | |
116 | |
117 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
118 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
119 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
120 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
121 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
122 throw new IOException("Host " + host + " port " + port | |
123 + " is not an RFB server"); | |
124 } | |
125 | |
126 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
127 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
128 | |
129 if (rfbMajor < 3) { | |
130 throw new IOException( | |
29 | 131 "RFB server does not support protocol version 3"); |
4 | 132 } |
133 | |
134 if (rfbMinor == 855) { | |
135 sendProxyFlag(os); | |
29 | 136 // if(proxyFlag)sendPortNumber(os); |
4 | 137 } |
138 return rfbMinor; | |
29 | 139 |
140 } | |
141 | |
142 void readVersionMsg(InputStream is) throws IOException { | |
4 | 143 |
144 byte[] b = new byte[12]; | |
145 | |
146 is.read(b); | |
147 | |
148 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
149 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
150 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
151 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
152 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
153 throw new IOException("Host " + host + " port " + port | |
154 + " is not an RFB server"); | |
155 } | |
156 | |
157 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
158 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
159 | |
160 if (serverMajor < 3) { | |
161 throw new IOException( | |
162 "RFB server does not support protocol version 3"); | |
163 } | |
164 | |
165 } | |
29 | 166 |
4 | 167 void sendSecurityType(OutputStream os) throws IOException { |
168 // number-of-security-types | |
169 os.write(1); | |
170 // security-types | |
171 // 1:None | |
172 os.write(1); | |
173 } | |
29 | 174 |
4 | 175 void readSecType(InputStream is) throws IOException { |
176 byte[] b = new byte[1]; | |
177 is.read(b); | |
178 | |
179 } | |
29 | 180 |
4 | 181 void sendSecResult(OutputStream os) throws IOException { |
182 byte[] b = castIntByte(0); | |
183 os.write(b); | |
184 } | |
29 | 185 |
4 | 186 void readClientInit(InputStream in) throws IOException { |
187 byte[] b = new byte[0]; | |
188 in.read(b); | |
189 } | |
29 | 190 |
4 | 191 void sendInitData(OutputStream os) throws IOException { |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
192 os.write(context.getInitData()); |
4 | 193 } |
29 | 194 |
4 | 195 void sendProxyFlag(OutputStream os) throws IOException { |
29 | 196 if (proxyFlag) |
197 os.write(1); | |
198 else | |
199 os.write(0); | |
4 | 200 } |
29 | 201 |
4 | 202 byte[] castIntByte(int len) { |
203 byte[] b = new byte[4]; | |
204 b[0] = (byte) ((len >>> 24) & 0xFF); | |
205 b[1] = (byte) ((len >>> 16) & 0xFF); | |
206 b[2] = (byte) ((len >>> 8) & 0xFF); | |
207 b[3] = (byte) ((len >>> 0) & 0xFF); | |
208 return b; | |
209 } | |
29 | 210 |
4 | 211 /** |
212 * gzip byte arrays | |
29 | 213 * |
4 | 214 * @param deflater |
215 * @param inputs | |
216 * byte data[] | |
29 | 217 * @param inputIndex |
4 | 218 * @param outputs |
219 * byte data[] | |
29 | 220 * @return byte length in last byte array |
4 | 221 * @throws IOException |
222 */ | |
29 | 223 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs, |
224 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { | |
4 | 225 int len = 0; |
29 | 226 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); |
227 while (inputIndex < inputs.size()) { | |
4 | 228 ByteBuffer b1 = inputs.get(inputIndex++); |
29 | 229 deflater.setInput(b1.array(), b1.position(), b1.remaining()); |
4 | 230 /** |
29 | 231 * If we finish() stream and reset() it, Deflater start new gzip |
232 * stream, this makes continuous zlib reader unhappy. if we remove | |
233 * finish(), Deflater.deflate() never flushes its output. The | |
234 * original zlib deflate has flush flag. I'm pretty sure this a kind | |
235 * of bug of Java library. | |
4 | 236 */ |
29 | 237 if (inputIndex == inputs.size()) |
4 | 238 deflater.finish(); |
239 int len1 = 0; | |
240 do { | |
29 | 241 len1 = deflater.deflate(c1.array(), c1.position(), |
242 c1.remaining()); | |
243 if (len1 > 0) { | |
4 | 244 len += len1; |
29 | 245 c1.position(c1.position() + len1); |
246 if (c1.remaining() == 0) { | |
247 c1.flip(); | |
248 outputs.addLast(c1); | |
4 | 249 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); |
250 } | |
251 } | |
29 | 252 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished()); |
4 | 253 } |
29 | 254 if (c1.position() != 0) { |
255 c1.flip(); | |
256 outputs.addLast(c1); | |
4 | 257 } |
258 deflater.reset(); | |
259 return len; | |
260 } | |
29 | 261 |
4 | 262 /** |
263 * gunzip byte arrays | |
29 | 264 * |
4 | 265 * @param inflater |
266 * @param inputs | |
267 * byte data[] | |
268 * @param outputs | |
269 * byte data[] | |
29 | 270 * @return number of total bytes |
4 | 271 * @throws IOException |
272 */ | |
273 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, | |
274 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize) | |
275 throws DataFormatException { | |
276 int len = 0; | |
277 ByteBuffer buf = ByteBuffer.allocate(bufSize); | |
278 while (inputIndex < inputs.size()) { | |
279 ByteBuffer input = inputs.get(inputIndex++); | |
280 inflater.setInput(input.array(), input.position(), input.limit()); | |
281 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, | |
282 // we need this | |
283 // inflater.end(); but this won't work | |
284 do { | |
285 int len0 = inflater.inflate(buf.array(), buf.position(), | |
286 buf.remaining()); | |
287 if (len0 > 0) { | |
288 buf.position(buf.position() + len0); | |
289 len += len0; | |
290 if (buf.remaining() == 0) { | |
291 buf.flip(); | |
292 outputs.addLast(buf); | |
293 buf = ByteBuffer.allocate(bufSize); | |
294 } | |
295 } | |
296 } while (!inflater.needsInput()); | |
297 } | |
298 if (buf.position() != 0) { | |
299 buf.flip(); | |
300 outputs.addLast(buf); | |
301 } | |
302 return len; | |
303 } | |
29 | 304 |
305 public void readSendData(int dataLen, Reader reader) | |
306 throws TransportException { | |
307 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); | |
4 | 308 ByteBuffer header = ByteBuffer.allocate(16); |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
309 reader.mark(dataLen); |
29 | 310 reader.readBytes(header.array(), 0, 16); |
4 | 311 header.limit(16); |
29 | 312 if (header.get(0) == FramebufferUpdate) { |
4 | 313 int encoding = header.getInt(12); |
29 | 314 if (encoding == EncodingType.ZRLE.getId() |
315 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is | |
316 // already | |
317 // recompressed | |
4 | 318 ByteBuffer len = ByteBuffer.allocate(4); |
29 | 319 reader.readBytes(len.array(), 0, 4); |
320 len.limit(4); | |
321 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20); | |
322 reader.readBytes(inputData.array(), 0, inputData.capacity()); | |
323 inputData.limit(dataLen - 20); | |
324 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>(); | |
4 | 325 inputs.add(inputData); |
326 | |
29 | 327 header.putInt(12, EncodingType.ZRLEE.getId()); // means |
328 // recompress | |
329 // every time | |
330 // using new Deflecter every time is incompatible with the | |
331 // protocol, clients have to be modified. | |
4 | 332 Deflater nDeflater = deflater; // new Deflater(); |
333 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
29 | 334 try { |
335 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE); | |
336 // dump32(inputs); | |
337 int len2 = zip(nDeflater, out, 0, bufs); | |
338 ByteBuffer blen = ByteBuffer.allocate(4); | |
339 blen.putInt(len2); | |
340 blen.flip(); | |
341 bufs.addFirst(blen); | |
4 | 342 |
29 | 343 bufs.addFirst(header); |
344 multicastqueue.put(bufs); | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
345 reader.reset(); |
29 | 346 } catch (DataFormatException e) { |
347 throw new TransportException(e); | |
348 } catch (IOException e) { | |
349 throw new TransportException(e); | |
350 } | |
351 return; | |
4 | 352 } |
29 | 353 } |
4 | 354 bufs.add(header); |
29 | 355 if (dataLen > 16) { |
356 ByteBuffer b = ByteBuffer.allocate(dataLen - 16); | |
357 reader.readBytes(b.array(), 0, dataLen - 16); | |
358 b.limit(dataLen - 16); | |
4 | 359 bufs.add(b); |
360 } | |
361 multicastqueue.put(bufs); | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
362 reader.reset(); |
29 | 363 // It may be compressed. We can inflate here to avoid repeating clients |
364 // decompressing here, | |
365 // but it may generate too many large data. It is better to do it in | |
366 // each client. | |
367 // But we have do inflation for all input data, so we have to do it | |
368 // here. | |
4 | 369 } |
370 | |
3 | 371 } |