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