Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 107:a8b2712de4c5
ZRLEE extension
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 05 Aug 2011 08:57:00 +0900 |
parents | 7a7baebdd3cf |
children | 3f73ebf918bd |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
54 | 2 |
88 | 3 import static org.junit.Assert.*; |
4 | |
25 | 5 import java.awt.Graphics; |
6 import java.awt.Image; | |
7 import java.awt.image.BufferedImage; | |
8 import java.io.BufferedOutputStream; | |
15 | 9 import java.io.BufferedReader; |
25 | 10 import java.io.ByteArrayInputStream; |
11 import java.io.ByteArrayOutputStream; | |
10 | 12 import java.io.IOException; |
43 | 13 import java.io.InputStream; |
15 | 14 import java.io.InputStreamReader; |
23 | 15 import java.net.BindException; |
10 | 16 import java.net.ServerSocket; |
17 import java.net.Socket; | |
90 | 18 import java.nio.ByteBuffer; |
93 | 19 import java.util.Iterator; |
10 | 20 import java.util.LinkedList; |
21 | |
25 | 22 import javax.imageio.ImageIO; |
23 | |
88 | 24 import org.junit.Test; |
25 | |
54 | 26 import myVncProxy.MulticastQueue.Client; |
27 | |
40 | 28 import java.util.concurrent.ExecutorService; |
80 | 29 import java.util.zip.DataFormatException; |
30 import java.util.zip.Deflater; | |
31 import java.util.zip.Inflater; | |
40 | 32 import java.io.OutputStream; |
10 | 33 |
88 | 34 public |
107 | 35 class MyRfbProto extends RfbProto { |
43 | 36 final static String versionMsg_3_998 = "RFB 003.998\n"; |
65 | 37 /** |
38 * CheckMillis is one of new msgType for RFB 3.998. | |
39 */ | |
90 | 40 final static byte SpeedCheckMillis = 4; |
83 | 41 private static final int INFLATE_BUFSIZE = 1024*100; |
65 | 42 boolean printStatusFlag = false; |
43 long startCheckTime; | |
54 | 44 |
18 | 45 private int messageType; |
46 private int rectangles; | |
23 | 47 private int rectX; |
48 private int rectY; | |
49 private int rectW; | |
50 private int rectH; | |
18 | 51 private int encoding; |
27 | 52 private int zLen; |
104 | 53 private boolean clicomp = false; |
18 | 54 |
23 | 55 private ServerSocket servSock; |
56 private int acceptPort; | |
10 | 57 private byte initData[]; |
54 | 58 private LinkedList<Socket> cliListTmp; |
59 private LinkedList<Socket> cliList; | |
27 | 60 boolean createBimgFlag; |
54 | 61 |
40 | 62 ExecutorService executor; |
54 | 63 |
25 | 64 byte[] pngBytes; |
54 | 65 |
102 | 66 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); |
67 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); | |
80 | 68 private int clients = 0; |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
69 private Inflater inflater = new Inflater(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
70 private Deflater deflater = new Deflater(); |
54 | 71 |
88 | 72 public |
73 MyRfbProto() throws IOException { | |
74 } | |
75 | |
54 | 76 MyRfbProto(String h, int p, VncViewer v) throws IOException { |
10 | 77 super(h, p, v); |
54 | 78 cliList = new LinkedList<Socket>(); |
79 cliListTmp = new LinkedList<Socket>(); | |
27 | 80 createBimgFlag = false; |
61 | 81 // sendThreads = new LinkedList<Thread>(); |
54 | 82 // executor = Executors.newCachedThreadPool(); |
83 // executor = Executors.newSingleThreadExecutor(); | |
10 | 84 } |
85 | |
13 | 86 MyRfbProto(String h, int p) throws IOException { |
87 super(h, p); | |
54 | 88 cliList = new LinkedList<Socket>(); |
89 cliListTmp = new LinkedList<Socket>(); | |
27 | 90 createBimgFlag = false; |
61 | 91 // sendThreads = new LinkedList<Thread>(); |
54 | 92 // executor = Executors.newCachedThreadPool(); |
93 // executor = Executors.newSingleThreadExecutor(); | |
13 | 94 } |
24 | 95 |
44 | 96 // over write |
43 | 97 void writeVersionMsg() throws IOException { |
98 clientMajor = 3; | |
99 if (serverMinor >= 9) { | |
54 | 100 clientMinor = 9; |
101 os.write(versionMsg_3_998.getBytes()); | |
43 | 102 } else if (serverMajor > 3 || serverMinor >= 8) { |
103 clientMinor = 8; | |
104 os.write(versionMsg_3_8.getBytes()); | |
105 } else if (serverMinor >= 9) { | |
106 clientMinor = 9; | |
107 os.write(versionMsg_3_998.getBytes()); | |
108 } else if (serverMinor >= 7) { | |
109 clientMinor = 7; | |
110 os.write(versionMsg_3_7.getBytes()); | |
111 } else { | |
112 clientMinor = 3; | |
113 os.write(versionMsg_3_3.getBytes()); | |
114 } | |
115 protocolTightVNC = false; | |
116 initCapabilities(); | |
117 } | |
118 | |
54 | 119 void initServSock(int port) throws IOException { |
10 | 120 servSock = new ServerSocket(port); |
23 | 121 acceptPort = port; |
10 | 122 } |
54 | 123 |
124 // 5550を開けるが、開いてないなら+1のポートを開ける。 | |
80 | 125 void selectPort(int p) { |
126 int port = p; | |
54 | 127 while (true) { |
128 try { | |
80 | 129 initServSock(port); |
23 | 130 break; |
54 | 131 } catch (BindException e) { |
80 | 132 port++; |
23 | 133 continue; |
54 | 134 } catch (IOException e) { |
10 | 135 |
23 | 136 } |
137 } | |
80 | 138 System.out.println("accept port = " + port); |
23 | 139 } |
54 | 140 |
141 int getAcceptPort() { | |
23 | 142 return acceptPort; |
143 } | |
54 | 144 |
10 | 145 void setSoTimeout(int num) throws IOException { |
146 servSock.setSoTimeout(num); | |
147 } | |
54 | 148 |
10 | 149 Socket accept() throws IOException { |
150 return servSock.accept(); | |
151 } | |
152 | |
54 | 153 void addSock(Socket sock) { |
10 | 154 cliList.add(sock); |
155 } | |
54 | 156 |
157 void addSockTmp(Socket sock) { | |
158 System.out.println("connected " + sock.getInetAddress()); | |
27 | 159 cliListTmp.add(sock); |
160 } | |
54 | 161 |
10 | 162 boolean markSupported() { |
163 return is.markSupported(); | |
164 } | |
54 | 165 |
10 | 166 void readServerInit() throws IOException { |
54 | 167 |
78 | 168 is.mark(255); |
10 | 169 skipBytes(20); |
170 int nlen = readU32(); | |
54 | 171 int blen = 20 + 4 + nlen; |
10 | 172 initData = new byte[blen]; |
78 | 173 is.reset(); |
10 | 174 |
78 | 175 is.mark(blen); |
10 | 176 readFully(initData); |
78 | 177 is.reset(); |
54 | 178 |
10 | 179 framebufferWidth = readU16(); |
180 framebufferHeight = readU16(); | |
181 bitsPerPixel = readU8(); | |
182 depth = readU8(); | |
183 bigEndian = (readU8() != 0); | |
184 trueColour = (readU8() != 0); | |
185 redMax = readU16(); | |
186 greenMax = readU16(); | |
187 blueMax = readU16(); | |
188 redShift = readU8(); | |
189 greenShift = readU8(); | |
190 blueShift = readU8(); | |
191 byte[] pad = new byte[3]; | |
192 readFully(pad); | |
193 int nameLength = readU32(); | |
194 byte[] name = new byte[nameLength]; | |
195 readFully(name); | |
196 desktopName = new String(name); | |
197 | |
198 // Read interaction capabilities (TightVNC protocol extensions) | |
199 if (protocolTightVNC) { | |
200 int nServerMessageTypes = readU16(); | |
201 int nClientMessageTypes = readU16(); | |
202 int nEncodingTypes = readU16(); | |
203 readU16(); | |
204 readCapabilityList(serverMsgCaps, nServerMessageTypes); | |
205 readCapabilityList(clientMsgCaps, nClientMessageTypes); | |
206 readCapabilityList(encodingCaps, nEncodingTypes); | |
207 } | |
208 | |
209 inNormalProtocol = true; | |
210 } | |
211 | |
54 | 212 void sendRfbVersion(OutputStream os) throws IOException { |
213 os.write(versionMsg_3_998.getBytes()); | |
43 | 214 } |
54 | 215 |
45 | 216 void readVersionMsg(InputStream is) throws IOException { |
217 | |
218 byte[] b = new byte[12]; | |
219 | |
220 is.read(b); | |
221 | |
222 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
223 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
224 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
225 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
226 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
227 throw new IOException("Host " + host + " port " + port | |
228 + " is not an RFB server"); | |
229 } | |
230 | |
231 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
232 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
233 | |
234 if (serverMajor < 3) { | |
235 throw new IOException( | |
236 "RFB server does not support protocol version 3"); | |
54 | 237 } |
238 | |
239 } | |
240 | |
43 | 241 void sendSecurityType(OutputStream os) throws IOException { |
242 // number-of-security-types | |
243 os.write(1); | |
54 | 244 // security-types |
43 | 245 // 1:None |
246 os.write(1); | |
247 } | |
54 | 248 |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
249 void readSecType(InputStream is) throws IOException { |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
250 byte[] b = new byte[1]; |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
251 is.read(b); |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
252 |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
253 } |
54 | 254 |
47
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
255 void sendSecResult(OutputStream os) throws IOException { |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
256 byte[] b = castIntByte(0); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
257 os.write(b); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
258 } |
54 | 259 |
43 | 260 void readClientInit(InputStream in) throws IOException { |
261 byte[] b = new byte[0]; | |
262 in.read(b); | |
263 } | |
54 | 264 |
265 void sendInitData(OutputStream os) throws IOException { | |
266 os.write(initData); | |
10 | 267 } |
268 | |
54 | 269 |
270 void sendPngImage() { | |
271 try { | |
272 for (Socket cli : cliListTmp) { | |
273 try { | |
27 | 274 sendPngData(cli); |
275 addSock(cli); | |
54 | 276 } catch (IOException e) { |
27 | 277 // if socket closed |
278 cliListTmp.remove(cli); | |
279 } | |
280 } | |
54 | 281 // System.out.println("cliSize="+cliSize()); |
282 } catch (Exception e) { | |
27 | 283 } |
284 cliListTmp.clear(); | |
285 } | |
286 | |
15 | 287 boolean ready() throws IOException { |
288 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
289 return br.ready(); | |
54 | 290 } |
10 | 291 |
54 | 292 int cliSize() { |
10 | 293 return cliList.size(); |
54 | 294 } |
295 | |
296 void printNumBytesRead() { | |
297 System.out.println("numBytesRead=" + numBytesRead); | |
298 } | |
299 | |
300 | |
61 | 301 |
54 | 302 void regiFramebufferUpdate() throws IOException { |
78 | 303 is.mark(20); |
80 | 304 messageType = readU8(); // 0 |
305 skipBytes(1); // 1 | |
306 rectangles = readU16(); // 2 | |
307 rectX = readU16(); // 4 | |
308 rectY = readU16(); // 6 | |
309 rectW = readU16(); // 8 | |
310 rectH = readU16(); // 10 | |
311 encoding = readU32(); // 12 | |
78 | 312 System.out.println("encoding = "+encoding); |
107 | 313 if (encoding == EncodingZRLE|| encoding==EncodingZRLEE||encoding==EncodingZlib) |
27 | 314 zLen = readU32(); |
80 | 315 else |
316 zLen = 0; | |
78 | 317 is.reset(); |
104 | 318 |
15 | 319 } |
54 | 320 |
61 | 321 int checkAndMark() throws IOException { |
322 int dataLen; | |
54 | 323 switch (encoding) { |
23 | 324 case RfbProto.EncodingRaw: |
39 | 325 dataLen = rectW * rectH * 4 + 16; |
78 | 326 is.mark(dataLen); |
23 | 327 break; |
67 | 328 case RfbProto.EncodingCopyRect: |
329 dataLen = 16 + 4; | |
78 | 330 is.mark(dataLen); |
67 | 331 break; |
332 case RfbProto.EncodingRRE: | |
333 case RfbProto.EncodingCoRRE: | |
334 case RfbProto.EncodingHextile: | |
80 | 335 case RfbProto.EncodingTight: |
336 dataLen = zLen + 20; | |
337 is.mark(dataLen); | |
338 break; | |
67 | 339 case RfbProto.EncodingZlib: |
27 | 340 case RfbProto.EncodingZRLE: |
107 | 341 case RfbProto.EncodingZRLEE: |
54 | 342 dataLen = zLen + 20; |
78 | 343 is.mark(dataLen); |
344 break; | |
345 case RfbProto.EncodingXCursor: | |
346 case RfbProto.EncodingRichCursor: | |
347 int pixArray = rectW * rectH * 4; | |
348 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
349 dataLen = pixArray + u8Array; | |
350 printFramebufferUpdate(); | |
351 is.mark(dataLen); | |
27 | 352 break; |
23 | 353 default: |
61 | 354 dataLen = 1000000; |
78 | 355 is.mark(dataLen); |
54 | 356 } |
61 | 357 return dataLen; |
358 } | |
65 | 359 |
360 | |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
361 void sendDataToClient() throws Exception { |
61 | 362 regiFramebufferUpdate(); |
363 int dataLen = checkAndMark(); | |
364 readSendData(dataLen); | |
23 | 365 } |
54 | 366 |
367 BufferedImage createBufferedImage(Image img) { | |
368 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
369 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 370 |
25 | 371 Graphics g = bimg.getGraphics(); |
372 g.drawImage(img, 0, 0, null); | |
373 g.dispose(); | |
374 return bimg; | |
375 } | |
376 | |
54 | 377 void createPngBytes(BufferedImage bimg) throws IOException { |
378 pngBytes = getImageBytes(bimg, "png"); | |
25 | 379 } |
54 | 380 |
381 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 382 byte[] b = getImageBytes(img, "png"); |
383 return b; | |
384 } | |
54 | 385 |
386 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
387 throws IOException { | |
25 | 388 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
389 BufferedOutputStream os = new BufferedOutputStream(bos); | |
390 image.flush(); | |
391 ImageIO.write(image, imageFormat, os); | |
392 os.flush(); | |
393 os.close(); | |
394 return bos.toByteArray(); | |
395 } | |
396 | |
54 | 397 void sendPngData(Socket sock) throws IOException { |
26 | 398 byte[] dataLength = castIntByte(pngBytes.length); |
399 sock.getOutputStream().write(dataLength); | |
25 | 400 sock.getOutputStream().write(pngBytes); |
401 } | |
54 | 402 |
403 byte[] castIntByte(int len) { | |
26 | 404 byte[] b = new byte[4]; |
54 | 405 b[0] = (byte) ((len >>> 24) & 0xFF); |
406 b[1] = (byte) ((len >>> 16) & 0xFF); | |
407 b[2] = (byte) ((len >>> 8) & 0xFF); | |
408 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 409 return b; |
410 } | |
54 | 411 |
412 BufferedImage createBimg() throws IOException { | |
25 | 413 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
414 return bimg; | |
415 } | |
104 | 416 |
54 | 417 void printFramebufferUpdate() { |
418 | |
18 | 419 System.out.println("messageType=" + messageType); |
54 | 420 System.out.println("rectangles=" + rectangles); |
18 | 421 System.out.println("encoding=" + encoding); |
78 | 422 System.out.println("rectX = "+rectX+": rectY = "+rectY); |
423 System.out.println("rectW = "+rectW+": rectH = "+rectH); | |
54 | 424 switch (encoding) { |
23 | 425 case RfbProto.EncodingRaw: |
54 | 426 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
427 + 16); | |
23 | 428 break; |
429 default: | |
430 } | |
18 | 431 } |
65 | 432 |
77 | 433 void readSpeedCheck() throws IOException { |
434 byte[] b = new byte[1]; | |
65 | 435 readFully(b); |
436 } | |
437 | |
77 | 438 void startSpeedCheck() { |
90 | 439 ByteBuffer b = ByteBuffer.allocate(10); |
440 b.put((byte)SpeedCheckMillis); | |
441 b.flip(); | |
66 | 442 startCheckTime = System.currentTimeMillis(); |
443 System.out.println("startChckTime = "+ startCheckTime); | |
90 | 444 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
84 | 445 bufs.add(b); |
446 multicastqueue.put(bufs); | |
65 | 447 } |
448 | |
77 | 449 void endSpeedCheck() { |
65 | 450 long accTime = System.currentTimeMillis(); |
451 long time = accTime - startCheckTime; | |
452 System.out.println("checkMillis: " + time); | |
453 } | |
454 | |
455 void printStatus() { | |
456 System.out.println(); | |
457 } | |
458 | |
459 synchronized void changeStatusFlag() { | |
460 printStatusFlag = true; | |
461 } | |
462 | |
463 void printMills() { | |
464 if(printStatusFlag) { | |
465 | |
466 changeStatusFlag(); | |
467 } else { | |
468 changeStatusFlag(); | |
469 } | |
470 } | |
87 | 471 |
472 void speedCheckMillis() { | |
473 Runnable stdin = new Runnable() { | |
474 public void run() { | |
475 int c; | |
476 try { | |
477 while( (c = System.in.read()) != -1 ) { | |
478 switch(c) { | |
479 case 's': | |
480 break; | |
481 default: | |
482 startSpeedCheck(); | |
483 break; | |
484 } | |
485 } | |
486 }catch(IOException e){ | |
487 System.out.println(e); | |
488 } | |
489 } | |
490 }; | |
491 | |
492 new Thread(stdin).start(); | |
493 } | |
86 | 494 |
87 | 495 /** |
496 * gzip byte arrays | |
497 * @param deflater | |
498 * @param inputs | |
499 * byte data[] | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
500 * @param inputIndex |
87 | 501 * @param outputs |
502 * byte data[] | |
503 * @return byte length in last byte array | |
504 * @throws IOException | |
505 */ | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
506 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { |
102 | 507 int len = 0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
508 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
509 while(inputIndex < inputs.size() ) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
510 ByteBuffer b1 = inputs.get(inputIndex++); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
511 deflater.setInput(b1.array(),b1.position(),b1.remaining()); |
106 | 512 /** |
513 * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy. | |
514 * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty | |
515 * sure this a kind of bug of Java library. | |
516 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
517 if (inputIndex==inputs.size()) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
518 deflater.finish(); |
106 | 519 int len1 = 0; |
86 | 520 do { |
106 | 521 len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); |
102 | 522 if (len1>0) { |
523 len += len1; | |
524 c1.position(c1.position()+len1); | |
525 if (c1.remaining()==0) { | |
526 c1.flip(); outputs.addLast(c1); | |
527 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
528 } | |
86 | 529 } |
106 | 530 } while (len1 >0 || !deflater.needsInput()); // &&!deflater.finished()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
531 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
532 if (c1.position()!=0) { |
102 | 533 c1.flip(); outputs.addLast(c1); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
534 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
535 deflater.reset(); |
90 | 536 return len; |
86 | 537 } |
87 | 538 |
539 /** | |
540 * gunzip byte arrays | |
541 * @param inflater | |
542 * @param inputs | |
543 * byte data[] | |
544 * @param outputs | |
545 * byte data[] | |
91 | 546 *@return number of total bytes |
87 | 547 * @throws IOException |
548 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
549 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize) |
88 | 550 throws DataFormatException { |
102 | 551 int len=0; |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
552 ByteBuffer buf = ByteBuffer.allocate(bufSize); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
553 while (inputIndex < inputs.size()) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
554 ByteBuffer input = inputs.get(inputIndex++); |
102 | 555 inflater.setInput(input.array(),input.position(),input.limit()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
556 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, we need this |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
557 // inflater.end(); but this won't work |
88 | 558 do { |
102 | 559 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); |
560 if (len0>0) { | |
561 buf.position(buf.position()+len0); | |
562 len += len0; | |
563 if (buf.remaining()==0) { | |
564 buf.flip(); | |
565 outputs.addLast(buf); | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
566 buf = ByteBuffer.allocate(bufSize); |
102 | 567 } |
91 | 568 } |
102 | 569 } while (!inflater.needsInput()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
570 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
571 if (buf.position()!=0) { |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
572 buf.flip(); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
573 outputs.addLast(buf); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
574 } |
90 | 575 return len; |
86 | 576 } |
65 | 577 |
86 | 578 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 579 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
580 ByteBuffer header = ByteBuffer.allocate(16); | |
581 readFully(header.array(),0,16); | |
582 header.limit(16); | |
583 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
584 int encoding = header.getInt(12); | |
107 | 585 if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed |
90 | 586 ByteBuffer len = ByteBuffer.allocate(4); |
587 readFully(len.array(),0,4); len.limit(4); | |
588 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
589 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); | |
590 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); | |
88 | 591 inputs.add(inputData); |
107 | 592 |
593 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time | |
594 // using new Deflecter every time is incompatible with the protocol, clients have to be modified. | |
595 Deflater nDeflater = deflater; // new Deflater(); | |
596 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
597 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); | |
598 // dump32(inputs); | |
599 int len2 = zip(nDeflater, out, 0, bufs); | |
600 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); | |
601 bufs.addFirst(blen); | |
602 | |
87 | 603 bufs.addFirst(header); |
86 | 604 multicastqueue.put(bufs); |
605 is.reset(); | |
606 return ; | |
607 } | |
608 } | |
87 | 609 bufs.add(header); |
610 if (dataLen>16) { | |
90 | 611 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
612 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); | |
87 | 613 bufs.add(b); |
614 } | |
86 | 615 multicastqueue.put(bufs); |
616 is.reset(); | |
617 | |
618 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
619 // but it may generate too many large data. It is better to do it in each client. | |
620 // But we have do inflation for all input data, so we have to do it here. | |
621 } | |
43 | 622 |
71 | 623 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 624 final OutputStream os, final InputStream is) throws IOException { |
625 // createBimgFlag = true; | |
626 // rfb.addSockTmp(newCli); | |
627 // addSock(newCli); | |
90 | 628 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); |
102 | 629 |
630 final Runnable reader = new Runnable() { | |
631 public void run() { | |
632 byte b[] = new byte[4096]; | |
633 for(;;) { | |
634 try { | |
635 int c = is.read(b); | |
636 if (c<=0) throw new IOException(); | |
637 System.out.println("client read "+c); | |
638 } catch (IOException e) { | |
639 try { | |
640 os.close(); | |
641 is.close(); | |
642 } catch (IOException e1) { | |
643 } | |
644 return; | |
645 } | |
646 } | |
647 } | |
648 }; | |
107 | 649 final int myId = clients; |
54 | 650 Runnable sender = new Runnable() { |
651 public void run() { | |
652 try { | |
65 | 653 /** |
654 * initial connection of RFB protocol | |
655 */ | |
54 | 656 sendRfbVersion(os); |
657 readVersionMsg(is); | |
658 sendSecurityType(os); | |
659 readSecType(is); | |
660 sendSecResult(os); | |
661 readClientInit(is); | |
662 sendInitData(os); | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
663 new Thread(reader).start(); |
54 | 664 for (;;) { |
90 | 665 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
666 int inputIndex = 0; |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
667 ByteBuffer header = bufs.get(inputIndex++); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
668 if (header==null) continue; |
90 | 669 if (header.get(0)==RfbProto.FramebufferUpdate) { |
107 | 670 System.out.println("client "+ myId); |
87 | 671 } |
90 | 672 os.write(header.array(),header.position(),header.limit()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
673 writeToClient(os, bufs, inputIndex); |
54 | 674 } |
675 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
676 try { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
677 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
678 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
679 } |
87 | 680 /* if socket closed cliList.remove(newCli); */ |
54 | 681 } |
682 } | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
683 |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
684 public void writeToClient(final OutputStream os, |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
685 LinkedList<ByteBuffer> bufs, int inputIndex) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
686 throws IOException { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
687 while(inputIndex < bufs.size()) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
688 ByteBuffer b = bufs.get(inputIndex++); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
689 os.write(b.array(), b.position(), b.limit()); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
690 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
691 os.flush(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
692 } |
54 | 693 }; |
80 | 694 clients++; |
54 | 695 new Thread(sender).start(); |
696 | |
697 } | |
66 | 698 |
699 | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
700 public void dump32(LinkedList<ByteBuffer>bufs) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
701 int len =0; |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
702 for(ByteBuffer b: bufs) len += b.remaining(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
703 ByteBuffer top = bufs.getFirst(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
704 ByteBuffer end = bufs.getLast(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
705 System.err.println("length: "+len); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
706 System.err.print("head 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
707 for(int i = 0; i<16 && i < top.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
708 System.err.print(" "+ top.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
709 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
710 System.err.print("tail 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
711 for(int i = 0; i<16 && i < end.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
712 System.err.print(" "+end.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
713 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
714 System.err.println(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
715 } |
88 | 716 |
717 @Test | |
718 public void test1() { | |
719 try { | |
90 | 720 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
721 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
722 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
107 | 723 // if (false) { |
724 // for(int i=0;i<10;i++) { | |
725 // in.add(ByteBuffer.wrap("test1".getBytes())); | |
726 // in.add(ByteBuffer.wrap("test2".getBytes())); | |
727 // in.add(ByteBuffer.wrap("test3".getBytes())); | |
728 // in.add(ByteBuffer.wrap("test44".getBytes())); | |
729 // } | |
730 // } else | |
731 { | |
106 | 732 String t = ""; |
733 for(int i=0;i<10;i++) { | |
734 t += "test1"; | |
735 t += "test2"; | |
736 t += "test3"; | |
737 t += "test44"; | |
738 } | |
739 in.add(ByteBuffer.wrap(t.getBytes())); | |
740 } | |
741 | |
92 | 742 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 743 |
88 | 744 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
745 zip(deflater,in,0,out); |
92 | 746 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
747 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); |
106 | 748 // inflater.reset(); |
92 | 749 equalByteBuffers(in1, out2); |
750 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
104 | 751 deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
752 zip(deflater,out2,0,out4); |
92 | 753 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
754 unzip(inflater,out4,0, out5, INFLATE_BUFSIZE); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
755 int len = equalByteBuffers(in1,out5); |
92 | 756 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
757 System.out.println("Test Ok. "+len); |
88 | 758 } catch (Exception e) { |
759 assertEquals(0,1); | |
760 } | |
761 } | |
762 | |
92 | 763 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
764 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
765 for(ByteBuffer b: in) { | |
766 ByteBuffer c = b.duplicate(); | |
767 copy.add(c); | |
768 } | |
769 return copy; | |
770 } | |
771 | |
93 | 772 |
773 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
774 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 775 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
776 int len = 0; |
93 | 777 Iterable<Byte> i = byteBufferIterator(in); |
778 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
779 | |
780 for(int b: i) { | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
781 len ++; |
93 | 782 if (o.hasNext()) { |
783 int c = o.next(); | |
784 assertEquals(b,c); | |
785 } else | |
786 assertEquals(0,1); | |
787 } | |
104 | 788 if (o.hasNext()) |
789 assertEquals(0,1); | |
93 | 790 // System.out.println(); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
791 return len; |
93 | 792 } |
793 | |
794 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
795 return new Iterable<Byte>() { | |
796 public Iterator<Byte> iterator() { | |
797 return new Iterator<Byte>() { | |
798 int bytes = 0; | |
799 int buffers = 0; | |
800 public boolean hasNext() { | |
104 | 801 for(;;) { |
802 if (buffers>=in.size()) return false; | |
803 ByteBuffer b = in.get(buffers); | |
804 if (! (bytes<b.remaining())) { | |
805 buffers ++; bytes=0; | |
806 } else return true; | |
807 } | |
92 | 808 } |
93 | 809 public Byte next() { |
104 | 810 ByteBuffer bf =in.get(buffers); |
93 | 811 byte b = bf.get(bytes++); |
812 if (bf.remaining()<=bytes) { | |
813 buffers++; | |
814 bytes = 0; | |
815 } | |
816 // System.out.print(b); | |
817 return b; | |
818 } | |
819 public void remove() { | |
820 } | |
821 }; | |
92 | 822 } |
93 | 823 }; |
92 | 824 } |
825 | |
54 | 826 } |
66 | 827 |
828 |