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