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