Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 155:0510022e9e92
modify MyRfbProto
author | e085711 |
---|---|
date | Thu, 22 Sep 2011 05:20:51 +0900 |
parents | 6606b71b64a6 |
children | b570efb0d266 |
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; |
130 | 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(); |
153 | 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 } |
65 | 600 |
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
|
601 /** |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
602 * 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
|
603 * @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
|
604 * @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
|
605 * @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
|
606 * |
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 * 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
|
608 * 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
|
609 * |
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 * 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
|
611 * 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
|
612 * 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
|
613 * 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
|
614 */ |
86 | 615 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 616 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
617 ByteBuffer header = ByteBuffer.allocate(16); | |
618 readFully(header.array(),0,16); | |
619 header.limit(16); | |
620 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
621 int encoding = header.getInt(12); | |
107 | 622 if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed |
90 | 623 ByteBuffer len = ByteBuffer.allocate(4); |
624 readFully(len.array(),0,4); len.limit(4); | |
625 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
151 | 626 |
627 startTiming(); | |
90 | 628 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); |
151 | 629 stopTiming(); |
630 | |
90 | 631 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); |
88 | 632 inputs.add(inputData); |
107 | 633 |
634 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time | |
635 // using new Deflecter every time is incompatible with the protocol, clients have to be modified. | |
636 Deflater nDeflater = deflater; // new Deflater(); | |
637 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
638 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); | |
639 // dump32(inputs); | |
640 int len2 = zip(nDeflater, out, 0, bufs); | |
641 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); | |
642 bufs.addFirst(blen); | |
643 | |
87 | 644 bufs.addFirst(header); |
86 | 645 multicastqueue.put(bufs); |
130 | 646 |
647 // is.reset(); | |
86 | 648 return ; |
649 } | |
87 | 650 bufs.add(header); |
651 if (dataLen>16) { | |
90 | 652 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
653 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); | |
87 | 654 bufs.add(b); |
655 } | |
86 | 656 multicastqueue.put(bufs); |
135 | 657 // is.reset(); |
658 return ; | |
130 | 659 } |
660 is.reset(); | |
86 | 661 |
662 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
663 // but it may generate too many large data. It is better to do it in each client. | |
664 // But we have do inflation for all input data, so we have to do it here. | |
665 } | |
43 | 666 |
71 | 667 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 668 final OutputStream os, final InputStream is) throws IOException { |
669 // createBimgFlag = true; | |
670 // rfb.addSockTmp(newCli); | |
671 // addSock(newCli); | |
112 | 672 final int myId = clients; |
90 | 673 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
|
674 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
|
675 writerRunning.set(1); |
112 | 676 /** |
677 * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory | |
678 * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running. | |
679 */ | |
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
|
680 final Runnable timer = new Runnable() { |
128 | 681 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
|
682 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
|
683 for(;;) { |
114 | 684 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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 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
|
691 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
|
692 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
|
693 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
|
694 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
|
695 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
|
696 } |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
697 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
|
698 } |
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 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
|
700 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
|
701 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
|
702 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
|
703 } |
128 | 704 } |
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
|
705 } catch (InterruptedException e) { |
128 | 706 } |
707 } | |
708 } | |
709 }; | |
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
|
710 new Thread(timer).start(); |
112 | 711 /** |
712 * discard all incoming from clients | |
713 */ | |
102 | 714 final Runnable reader = new Runnable() { |
715 public void run() { | |
716 byte b[] = new byte[4096]; | |
717 for(;;) { | |
718 try { | |
719 int c = is.read(b); | |
720 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
|
721 // System.out.println("client read "+c); |
102 | 722 } catch (IOException e) { |
723 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
|
724 writerRunning.set(2); |
102 | 725 os.close(); |
726 is.close(); | |
727 } catch (IOException e1) { | |
728 } | |
729 return; | |
730 } | |
731 } | |
732 } | |
733 }; | |
112 | 734 /** |
735 * send packets to a client | |
736 */ | |
54 | 737 Runnable sender = new Runnable() { |
738 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
|
739 writerRunning.set(1); |
54 | 740 try { |
65 | 741 /** |
742 * initial connection of RFB protocol | |
743 */ | |
54 | 744 sendRfbVersion(os); |
131 | 745 // readVersionMsg(is); |
155 | 746 int rfbMinor = readVersionMsg(is,os); |
54 | 747 sendSecurityType(os); |
748 readSecType(is); | |
749 sendSecResult(os); | |
750 readClientInit(is); | |
751 sendInitData(os); | |
112 | 752 new Thread(reader).start(); // discard incoming packet here after. |
153 | 753 writeFramebufferUpdateRequest(0,0, framebufferWidth, framebufferHeight, false ); |
54 | 754 for (;;) { |
90 | 755 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
756 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
|
757 ByteBuffer header = bufs.get(inputIndex); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
758 if (header==null) continue; |
90 | 759 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
|
760 // System.out.println("client "+ myId); |
87 | 761 } |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
762 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
|
763 writerRunning.set(1); // yes my client is awaking. |
54 | 764 } |
765 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
766 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
|
767 writerRunning.set(2); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
768 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
769 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
770 } |
87 | 771 /* if socket closed cliList.remove(newCli); */ |
54 | 772 } |
773 } | |
128 | 774 |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
775 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
|
776 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
|
777 throws IOException { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
778 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
|
779 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
|
780 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
|
781 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
782 os.flush(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
783 } |
54 | 784 }; |
80 | 785 clients++; |
54 | 786 new Thread(sender).start(); |
787 | |
788 } | |
66 | 789 |
790 | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
791 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
|
792 int len =0; |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
793 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
|
794 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
|
795 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
|
796 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
|
797 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
|
798 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
|
799 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
|
800 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
801 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
|
802 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
|
803 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
|
804 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
805 System.err.println(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
806 } |
88 | 807 |
808 @Test | |
809 public void test1() { | |
810 try { | |
90 | 811 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
812 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
813 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
107 | 814 // if (false) { |
815 // for(int i=0;i<10;i++) { | |
816 // in.add(ByteBuffer.wrap("test1".getBytes())); | |
817 // in.add(ByteBuffer.wrap("test2".getBytes())); | |
818 // in.add(ByteBuffer.wrap("test3".getBytes())); | |
819 // in.add(ByteBuffer.wrap("test44".getBytes())); | |
820 // } | |
821 // } else | |
822 { | |
106 | 823 String t = ""; |
824 for(int i=0;i<10;i++) { | |
825 t += "test1"; | |
826 t += "test2"; | |
827 t += "test3"; | |
828 t += "test44"; | |
829 } | |
830 in.add(ByteBuffer.wrap(t.getBytes())); | |
831 } | |
832 | |
92 | 833 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 834 |
88 | 835 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
836 zip(deflater,in,0,out); |
92 | 837 // 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
|
838 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); |
106 | 839 // inflater.reset(); |
92 | 840 equalByteBuffers(in1, out2); |
841 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
104 | 842 deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
843 zip(deflater,out2,0,out4); |
92 | 844 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
|
845 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
|
846 int len = equalByteBuffers(in1,out5); |
92 | 847 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
848 System.out.println("Test Ok. "+len); |
88 | 849 } catch (Exception e) { |
850 assertEquals(0,1); | |
851 } | |
852 } | |
853 | |
92 | 854 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
855 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
856 for(ByteBuffer b: in) { | |
857 ByteBuffer c = b.duplicate(); | |
858 copy.add(c); | |
859 } | |
860 return copy; | |
861 } | |
862 | |
93 | 863 |
864 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
865 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 866 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
867 int len = 0; |
93 | 868 Iterable<Byte> i = byteBufferIterator(in); |
869 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
870 | |
871 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
|
872 len ++; |
93 | 873 if (o.hasNext()) { |
874 int c = o.next(); | |
875 assertEquals(b,c); | |
876 } else | |
877 assertEquals(0,1); | |
878 } | |
104 | 879 if (o.hasNext()) |
880 assertEquals(0,1); | |
93 | 881 // System.out.println(); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
882 return len; |
93 | 883 } |
884 | |
885 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
886 return new Iterable<Byte>() { | |
887 public Iterator<Byte> iterator() { | |
888 return new Iterator<Byte>() { | |
889 int bytes = 0; | |
890 int buffers = 0; | |
891 public boolean hasNext() { | |
104 | 892 for(;;) { |
893 if (buffers>=in.size()) return false; | |
894 ByteBuffer b = in.get(buffers); | |
895 if (! (bytes<b.remaining())) { | |
896 buffers ++; bytes=0; | |
897 } else return true; | |
898 } | |
92 | 899 } |
93 | 900 public Byte next() { |
104 | 901 ByteBuffer bf =in.get(buffers); |
93 | 902 byte b = bf.get(bytes++); |
903 if (bf.remaining()<=bytes) { | |
904 buffers++; | |
905 bytes = 0; | |
906 } | |
907 // System.out.print(b); | |
908 return b; | |
909 } | |
910 public void remove() { | |
911 } | |
912 }; | |
92 | 913 } |
93 | 914 }; |
128 | 915 } |
916 | |
54 | 917 } |
66 | 918 |
919 |