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