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