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