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