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