Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 94:b551b7af0a8a
Unzip worked?
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 03 Aug 2011 13:08:39 +0900 (2011-08-03) |
parents | 40c22e507655 |
children | 285dd4d6dacf |
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; |
29 import java.util.concurrent.Executors; | |
80 | 30 import java.util.zip.DataFormatException; |
31 import java.util.zip.Deflater; | |
32 import java.util.zip.Inflater; | |
40 | 33 import java.io.OutputStream; |
10 | 34 |
88 | 35 public |
93 | 36 class MyRfbProto<ByteBuffersIterator> extends RfbProto { |
43 | 37 final static String versionMsg_3_998 = "RFB 003.998\n"; |
65 | 38 /** |
39 * CheckMillis is one of new msgType for RFB 3.998. | |
40 */ | |
90 | 41 final static byte SpeedCheckMillis = 4; |
83 | 42 private static final int INFLATE_BUFSIZE = 1024*100; |
65 | 43 boolean printStatusFlag = false; |
44 long startCheckTime; | |
54 | 45 |
18 | 46 private int messageType; |
47 private int rectangles; | |
23 | 48 private int rectX; |
49 private int rectY; | |
50 private int rectW; | |
51 private int rectH; | |
18 | 52 private int encoding; |
27 | 53 private int zLen; |
18 | 54 |
23 | 55 private ServerSocket servSock; |
56 private int acceptPort; | |
10 | 57 private byte initData[]; |
54 | 58 private LinkedList<Socket> cliListTmp; |
59 private LinkedList<Socket> cliList; | |
60 private LinkedList<Thread> sendThreads; | |
27 | 61 boolean createBimgFlag; |
54 | 62 |
40 | 63 ExecutorService executor; |
54 | 64 |
25 | 65 byte[] pngBytes; |
54 | 66 |
90 | 67 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); |
80 | 68 private int clients = 0; |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
69 private Inflater inflater = new Inflater(); |
54 | 70 |
88 | 71 public |
72 MyRfbProto() throws IOException { | |
73 } | |
74 | |
54 | 75 MyRfbProto(String h, int p, VncViewer v) throws IOException { |
10 | 76 super(h, p, v); |
54 | 77 cliList = new LinkedList<Socket>(); |
78 cliListTmp = new LinkedList<Socket>(); | |
27 | 79 createBimgFlag = false; |
61 | 80 // sendThreads = new LinkedList<Thread>(); |
54 | 81 // executor = Executors.newCachedThreadPool(); |
82 // executor = Executors.newSingleThreadExecutor(); | |
10 | 83 } |
84 | |
13 | 85 MyRfbProto(String h, int p) throws IOException { |
86 super(h, p); | |
54 | 87 cliList = new LinkedList<Socket>(); |
88 cliListTmp = new LinkedList<Socket>(); | |
27 | 89 createBimgFlag = false; |
61 | 90 // sendThreads = new LinkedList<Thread>(); |
54 | 91 // executor = Executors.newCachedThreadPool(); |
92 // executor = Executors.newSingleThreadExecutor(); | |
13 | 93 } |
24 | 94 |
44 | 95 // over write |
43 | 96 void writeVersionMsg() throws IOException { |
97 clientMajor = 3; | |
98 if (serverMinor >= 9) { | |
54 | 99 clientMinor = 9; |
100 os.write(versionMsg_3_998.getBytes()); | |
43 | 101 } else if (serverMajor > 3 || serverMinor >= 8) { |
102 clientMinor = 8; | |
103 os.write(versionMsg_3_8.getBytes()); | |
104 } else if (serverMinor >= 9) { | |
105 clientMinor = 9; | |
106 os.write(versionMsg_3_998.getBytes()); | |
107 } else if (serverMinor >= 7) { | |
108 clientMinor = 7; | |
109 os.write(versionMsg_3_7.getBytes()); | |
110 } else { | |
111 clientMinor = 3; | |
112 os.write(versionMsg_3_3.getBytes()); | |
113 } | |
114 protocolTightVNC = false; | |
115 initCapabilities(); | |
116 } | |
117 | |
54 | 118 void initServSock(int port) throws IOException { |
10 | 119 servSock = new ServerSocket(port); |
23 | 120 acceptPort = port; |
10 | 121 } |
54 | 122 |
123 // 5550を開けるが、開いてないなら+1のポートを開ける。 | |
80 | 124 void selectPort(int p) { |
125 int port = p; | |
54 | 126 while (true) { |
127 try { | |
80 | 128 initServSock(port); |
23 | 129 break; |
54 | 130 } catch (BindException e) { |
80 | 131 port++; |
23 | 132 continue; |
54 | 133 } catch (IOException e) { |
10 | 134 |
23 | 135 } |
136 } | |
80 | 137 System.out.println("accept port = " + port); |
23 | 138 } |
54 | 139 |
140 int getAcceptPort() { | |
23 | 141 return acceptPort; |
142 } | |
54 | 143 |
10 | 144 void setSoTimeout(int num) throws IOException { |
145 servSock.setSoTimeout(num); | |
146 } | |
54 | 147 |
10 | 148 Socket accept() throws IOException { |
149 return servSock.accept(); | |
150 } | |
151 | |
54 | 152 void addSock(Socket sock) { |
10 | 153 cliList.add(sock); |
154 } | |
54 | 155 |
156 void addSockTmp(Socket sock) { | |
157 System.out.println("connected " + sock.getInetAddress()); | |
27 | 158 cliListTmp.add(sock); |
159 } | |
54 | 160 |
10 | 161 boolean markSupported() { |
162 return is.markSupported(); | |
163 } | |
54 | 164 |
10 | 165 void readServerInit() throws IOException { |
54 | 166 |
78 | 167 is.mark(255); |
10 | 168 skipBytes(20); |
169 int nlen = readU32(); | |
54 | 170 int blen = 20 + 4 + nlen; |
10 | 171 initData = new byte[blen]; |
78 | 172 is.reset(); |
10 | 173 |
78 | 174 is.mark(blen); |
10 | 175 readFully(initData); |
78 | 176 is.reset(); |
54 | 177 |
10 | 178 framebufferWidth = readU16(); |
179 framebufferHeight = readU16(); | |
180 bitsPerPixel = readU8(); | |
181 depth = readU8(); | |
182 bigEndian = (readU8() != 0); | |
183 trueColour = (readU8() != 0); | |
184 redMax = readU16(); | |
185 greenMax = readU16(); | |
186 blueMax = readU16(); | |
187 redShift = readU8(); | |
188 greenShift = readU8(); | |
189 blueShift = readU8(); | |
190 byte[] pad = new byte[3]; | |
191 readFully(pad); | |
192 int nameLength = readU32(); | |
193 byte[] name = new byte[nameLength]; | |
194 readFully(name); | |
195 desktopName = new String(name); | |
196 | |
197 // Read interaction capabilities (TightVNC protocol extensions) | |
198 if (protocolTightVNC) { | |
199 int nServerMessageTypes = readU16(); | |
200 int nClientMessageTypes = readU16(); | |
201 int nEncodingTypes = readU16(); | |
202 readU16(); | |
203 readCapabilityList(serverMsgCaps, nServerMessageTypes); | |
204 readCapabilityList(clientMsgCaps, nClientMessageTypes); | |
205 readCapabilityList(encodingCaps, nEncodingTypes); | |
206 } | |
207 | |
208 inNormalProtocol = true; | |
209 } | |
210 | |
54 | 211 void sendRfbVersion(OutputStream os) throws IOException { |
212 os.write(versionMsg_3_998.getBytes()); | |
43 | 213 } |
54 | 214 |
45 | 215 void readVersionMsg(InputStream is) throws IOException { |
216 | |
217 byte[] b = new byte[12]; | |
218 | |
219 is.read(b); | |
220 | |
221 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
222 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
223 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
224 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
225 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
226 throw new IOException("Host " + host + " port " + port | |
227 + " is not an RFB server"); | |
228 } | |
229 | |
230 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
231 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
232 | |
233 if (serverMajor < 3) { | |
234 throw new IOException( | |
235 "RFB server does not support protocol version 3"); | |
54 | 236 } |
237 | |
238 } | |
239 | |
43 | 240 void sendSecurityType(OutputStream os) throws IOException { |
241 // number-of-security-types | |
242 os.write(1); | |
54 | 243 // security-types |
43 | 244 // 1:None |
245 os.write(1); | |
246 } | |
54 | 247 |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
248 void readSecType(InputStream is) throws IOException { |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
249 byte[] b = new byte[1]; |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
250 is.read(b); |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
251 |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
252 } |
54 | 253 |
47
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
254 void sendSecResult(OutputStream os) throws IOException { |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
255 byte[] b = castIntByte(0); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
256 os.write(b); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
257 } |
54 | 258 |
43 | 259 void readClientInit(InputStream in) throws IOException { |
260 byte[] b = new byte[0]; | |
261 in.read(b); | |
262 } | |
54 | 263 |
264 void sendInitData(OutputStream os) throws IOException { | |
265 os.write(initData); | |
10 | 266 } |
267 | |
54 | 268 |
269 void sendPngImage() { | |
270 try { | |
271 for (Socket cli : cliListTmp) { | |
272 try { | |
27 | 273 sendPngData(cli); |
274 addSock(cli); | |
54 | 275 } catch (IOException e) { |
27 | 276 // if socket closed |
277 cliListTmp.remove(cli); | |
278 } | |
279 } | |
54 | 280 // System.out.println("cliSize="+cliSize()); |
281 } catch (Exception e) { | |
27 | 282 } |
283 cliListTmp.clear(); | |
284 } | |
285 | |
15 | 286 boolean ready() throws IOException { |
287 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
288 return br.ready(); | |
54 | 289 } |
10 | 290 |
54 | 291 int cliSize() { |
10 | 292 return cliList.size(); |
54 | 293 } |
294 | |
295 void printNumBytesRead() { | |
296 System.out.println("numBytesRead=" + numBytesRead); | |
297 } | |
298 | |
299 | |
61 | 300 |
54 | 301 void regiFramebufferUpdate() throws IOException { |
78 | 302 is.mark(20); |
80 | 303 messageType = readU8(); // 0 |
304 skipBytes(1); // 1 | |
305 rectangles = readU16(); // 2 | |
306 rectX = readU16(); // 4 | |
307 rectY = readU16(); // 6 | |
308 rectW = readU16(); // 8 | |
309 rectH = readU16(); // 10 | |
310 encoding = readU32(); // 12 | |
78 | 311 System.out.println("encoding = "+encoding); |
80 | 312 if (encoding == EncodingZRLE) |
27 | 313 zLen = readU32(); |
80 | 314 else |
315 zLen = 0; | |
78 | 316 is.reset(); |
67 | 317 /* |
318 int dataLen; | |
319 switch (encoding) { | |
320 case RfbProto.EncodingRaw: | |
321 dataLen = rectW * rectH * 4 + 16; | |
322 mark(dataLen); | |
323 break; | |
324 case RfbProto.EncodingCopyRect: | |
325 dataLen = 16 + 4; | |
326 mark(dataLen); | |
327 break; | |
328 case RfbProto.EncodingRRE: | |
329 case RfbProto.EncodingCoRRE: | |
330 case RfbProto.EncodingHextile: | |
331 | |
332 case RfbProto.EncodingZlib: | |
333 case RfbProto.EncodingTight: | |
334 case RfbProto.EncodingZRLE: | |
335 dataLen = zLen + 20; | |
336 mark(dataLen); | |
337 break; | |
338 default: | |
339 dataLen = 1000000; | |
340 mark(dataLen); | |
341 } | |
342 | |
343 */ | |
344 | |
15 | 345 } |
54 | 346 |
61 | 347 int checkAndMark() throws IOException { |
348 int dataLen; | |
54 | 349 switch (encoding) { |
23 | 350 case RfbProto.EncodingRaw: |
39 | 351 dataLen = rectW * rectH * 4 + 16; |
78 | 352 is.mark(dataLen); |
23 | 353 break; |
67 | 354 case RfbProto.EncodingCopyRect: |
355 dataLen = 16 + 4; | |
78 | 356 is.mark(dataLen); |
67 | 357 break; |
358 case RfbProto.EncodingRRE: | |
359 case RfbProto.EncodingCoRRE: | |
360 case RfbProto.EncodingHextile: | |
80 | 361 case RfbProto.EncodingTight: |
362 dataLen = zLen + 20; | |
363 is.mark(dataLen); | |
364 break; | |
67 | 365 case RfbProto.EncodingZlib: |
27 | 366 case RfbProto.EncodingZRLE: |
54 | 367 dataLen = zLen + 20; |
78 | 368 is.mark(dataLen); |
369 break; | |
370 case RfbProto.EncodingXCursor: | |
371 case RfbProto.EncodingRichCursor: | |
372 int pixArray = rectW * rectH * 4; | |
373 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
374 dataLen = pixArray + u8Array; | |
375 printFramebufferUpdate(); | |
376 is.mark(dataLen); | |
27 | 377 break; |
23 | 378 default: |
61 | 379 dataLen = 1000000; |
78 | 380 is.mark(dataLen); |
54 | 381 } |
61 | 382 return dataLen; |
383 } | |
65 | 384 |
385 | |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
386 void sendDataToClient() throws Exception { |
61 | 387 regiFramebufferUpdate(); |
388 int dataLen = checkAndMark(); | |
389 readSendData(dataLen); | |
23 | 390 } |
54 | 391 |
392 BufferedImage createBufferedImage(Image img) { | |
393 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
394 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 395 |
25 | 396 Graphics g = bimg.getGraphics(); |
397 g.drawImage(img, 0, 0, null); | |
398 g.dispose(); | |
399 return bimg; | |
400 } | |
401 | |
54 | 402 void createPngBytes(BufferedImage bimg) throws IOException { |
403 pngBytes = getImageBytes(bimg, "png"); | |
25 | 404 } |
54 | 405 |
406 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 407 byte[] b = getImageBytes(img, "png"); |
408 return b; | |
409 } | |
54 | 410 |
411 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
412 throws IOException { | |
25 | 413 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
414 BufferedOutputStream os = new BufferedOutputStream(bos); | |
415 image.flush(); | |
416 ImageIO.write(image, imageFormat, os); | |
417 os.flush(); | |
418 os.close(); | |
419 return bos.toByteArray(); | |
420 } | |
421 | |
54 | 422 void sendPngData(Socket sock) throws IOException { |
26 | 423 byte[] dataLength = castIntByte(pngBytes.length); |
424 sock.getOutputStream().write(dataLength); | |
25 | 425 sock.getOutputStream().write(pngBytes); |
426 } | |
54 | 427 |
428 byte[] castIntByte(int len) { | |
26 | 429 byte[] b = new byte[4]; |
54 | 430 b[0] = (byte) ((len >>> 24) & 0xFF); |
431 b[1] = (byte) ((len >>> 16) & 0xFF); | |
432 b[2] = (byte) ((len >>> 8) & 0xFF); | |
433 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 434 return b; |
435 } | |
54 | 436 |
437 BufferedImage createBimg() throws IOException { | |
25 | 438 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
439 return bimg; | |
440 } | |
65 | 441 /* |
54 | 442 void readPngData() throws IOException { |
25 | 443 pngBytes = new byte[is.available()]; |
444 readFully(pngBytes); | |
445 } | |
65 | 446 */ |
54 | 447 void printFramebufferUpdate() { |
448 | |
18 | 449 System.out.println("messageType=" + messageType); |
54 | 450 System.out.println("rectangles=" + rectangles); |
18 | 451 System.out.println("encoding=" + encoding); |
78 | 452 System.out.println("rectX = "+rectX+": rectY = "+rectY); |
453 System.out.println("rectW = "+rectW+": rectH = "+rectH); | |
54 | 454 switch (encoding) { |
23 | 455 case RfbProto.EncodingRaw: |
54 | 456 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
457 + 16); | |
23 | 458 break; |
459 default: | |
460 } | |
18 | 461 } |
65 | 462 |
77 | 463 void readSpeedCheck() throws IOException { |
464 byte[] b = new byte[1]; | |
65 | 465 readFully(b); |
466 } | |
467 | |
77 | 468 void startSpeedCheck() { |
90 | 469 ByteBuffer b = ByteBuffer.allocate(10); |
470 b.put((byte)SpeedCheckMillis); | |
471 b.flip(); | |
66 | 472 startCheckTime = System.currentTimeMillis(); |
473 System.out.println("startChckTime = "+ startCheckTime); | |
90 | 474 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
84 | 475 bufs.add(b); |
476 multicastqueue.put(bufs); | |
65 | 477 } |
478 | |
77 | 479 void endSpeedCheck() { |
65 | 480 long accTime = System.currentTimeMillis(); |
481 long time = accTime - startCheckTime; | |
482 System.out.println("checkMillis: " + time); | |
483 } | |
484 | |
485 void printStatus() { | |
486 System.out.println(); | |
487 } | |
488 | |
489 synchronized void changeStatusFlag() { | |
490 printStatusFlag = true; | |
491 } | |
492 | |
493 void printMills() { | |
494 if(printStatusFlag) { | |
495 | |
496 changeStatusFlag(); | |
497 } else { | |
498 changeStatusFlag(); | |
499 } | |
500 } | |
87 | 501 |
502 void speedCheckMillis() { | |
503 Runnable stdin = new Runnable() { | |
504 public void run() { | |
505 int c; | |
506 try { | |
507 while( (c = System.in.read()) != -1 ) { | |
508 switch(c) { | |
509 case 's': | |
510 break; | |
511 default: | |
512 startSpeedCheck(); | |
513 break; | |
514 } | |
515 } | |
516 }catch(IOException e){ | |
517 System.out.println(e); | |
518 } | |
519 } | |
520 }; | |
521 | |
522 new Thread(stdin).start(); | |
523 } | |
86 | 524 |
87 | 525 /** |
526 * gzip byte arrays | |
527 * @param deflater | |
528 * @param inputs | |
529 * byte data[] | |
530 * @param outputs | |
531 * byte data[] | |
532 * @return byte length in last byte array | |
533 * @throws IOException | |
534 */ | |
90 | 535 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs) throws IOException { |
536 int len1=0,len = 0; | |
86 | 537 deflater.reset(); |
92 | 538 while(inputs.size()>0) { |
90 | 539 ByteBuffer b1 = inputs.poll(); |
540 deflater.setInput(b1.array(),b1.position(),b1.limit()); | |
86 | 541 if (inputs.size()==0) { |
542 deflater.finish(); | |
90 | 543 } |
86 | 544 do { |
90 | 545 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); |
546 len1 = deflater.deflate(c1.array(),c1.position(),c1.capacity()); | |
547 c1.limit(len1); | |
86 | 548 if (len1>0) { |
549 outputs.addLast(c1); | |
550 len += len1; | |
551 } | |
91 | 552 } while (len1==INFLATE_BUFSIZE); |
92 | 553 } |
90 | 554 return len; |
86 | 555 } |
87 | 556 |
557 /** | |
558 * gunzip byte arrays | |
559 * @param inflater | |
560 * @param inputs | |
561 * byte data[] | |
562 * @param outputs | |
563 * byte data[] | |
91 | 564 *@return number of total bytes |
87 | 565 * @throws IOException |
566 */ | |
90 | 567 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs) |
88 | 568 throws DataFormatException { |
86 | 569 int len=0,len0; |
94 | 570 // inflater.reset(); |
90 | 571 do { |
572 ByteBuffer input = inputs.poll(); | |
573 inflater.setInput(input.array(),0,input.limit()); | |
88 | 574 do { |
90 | 575 ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE); |
576 len0 = inflater.inflate(buf.array(),0,buf.capacity()); | |
577 buf.limit(len0); | |
88 | 578 len += len0; |
91 | 579 if (len0>0) { |
580 outputs.addLast(buf); | |
581 } | |
582 } while (len0>0); | |
90 | 583 } while (!inputs.isEmpty()); |
584 return len; | |
86 | 585 } |
65 | 586 |
86 | 587 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 588 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
589 ByteBuffer header = ByteBuffer.allocate(16); | |
590 readFully(header.array(),0,16); | |
591 header.limit(16); | |
592 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
593 int encoding = header.getInt(12); | |
86 | 594 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
90 | 595 ByteBuffer len = ByteBuffer.allocate(4); |
596 readFully(len.array(),0,4); len.limit(4); | |
597 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
598 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); | |
599 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); | |
88 | 600 inputs.add(inputData); |
601 unzip(inflater, inputs, bufs); | |
87 | 602 bufs.addFirst(header); |
86 | 603 multicastqueue.put(bufs); |
604 is.reset(); | |
605 return ; | |
606 } | |
607 } | |
87 | 608 bufs.add(header); |
609 if (dataLen>16) { | |
90 | 610 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
611 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); | |
87 | 612 bufs.add(b); |
613 } | |
86 | 614 multicastqueue.put(bufs); |
615 is.reset(); | |
616 | |
617 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
618 // but it may generate too many large data. It is better to do it in each client. | |
619 // But we have do inflation for all input data, so we have to do it here. | |
620 } | |
43 | 621 |
71 | 622 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 623 final OutputStream os, final InputStream is) throws IOException { |
624 // createBimgFlag = true; | |
625 // rfb.addSockTmp(newCli); | |
626 // addSock(newCli); | |
90 | 627 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); |
54 | 628 Runnable sender = new Runnable() { |
629 public void run() { | |
80 | 630 |
631 Deflater deflater = new Deflater(); | |
54 | 632 try { |
65 | 633 /** |
634 * initial connection of RFB protocol | |
635 */ | |
54 | 636 sendRfbVersion(os); |
637 readVersionMsg(is); | |
638 sendSecurityType(os); | |
639 readSecType(is); | |
640 sendSecResult(os); | |
641 readClientInit(is); | |
642 sendInitData(os); | |
43 | 643 |
54 | 644 for (;;) { |
90 | 645 LinkedList<ByteBuffer> bufs = c.poll(); |
646 ByteBuffer header = bufs.poll(); | |
647 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
648 int encoding = header.getInt(12); | |
80 | 649 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
90 | 650 LinkedList<ByteBuffer> outs = new LinkedList<ByteBuffer>(); |
87 | 651 int len2 = zip(deflater, bufs, outs); |
90 | 652 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); |
653 outs.addFirst(blen); | |
87 | 654 outs.addFirst(header); |
86 | 655 while(!outs.isEmpty()) { |
90 | 656 ByteBuffer out= outs.poll(); |
657 os.write(out.array(),out.position(),out.limit()); | |
84 | 658 } |
80 | 659 } |
87 | 660 os.flush(); |
661 return; | |
662 } | |
90 | 663 os.write(header.array(),header.position(),header.limit()); |
664 for(ByteBuffer b : bufs) { | |
665 os.write(b.array(), b.position(), b.limit()); | |
84 | 666 } |
85 | 667 os.flush(); |
54 | 668 } |
669 } catch (IOException e) { | |
87 | 670 /* if socket closed cliList.remove(newCli); */ |
54 | 671 } |
672 } | |
673 }; | |
80 | 674 clients++; |
54 | 675 new Thread(sender).start(); |
676 | |
677 } | |
66 | 678 |
679 | |
88 | 680 |
681 @Test | |
682 public void test1() { | |
683 try { | |
90 | 684 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
685 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
686 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
88 | 687 for(int i=0;i<10;i++) { |
90 | 688 in.add(ByteBuffer.wrap("test1".getBytes())); |
689 in.add(ByteBuffer.wrap("test2".getBytes())); | |
690 in.add(ByteBuffer.wrap("test3".getBytes())); | |
691 in.add(ByteBuffer.wrap("test4".getBytes())); | |
88 | 692 } |
92 | 693 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 694 |
88 | 695 Deflater deflater = new Deflater(); |
696 zip(deflater, in,out); | |
92 | 697 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state |
88 | 698 unzip(inflater, out, out2); |
92 | 699 equalByteBuffers(in1, out2); |
700 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
701 zip(deflater,out2,out4); | |
702 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); | |
703 unzip(inflater,out4,out5); | |
704 equalByteBuffers(in1,out5); | |
705 | |
88 | 706 System.out.println("Test Ok."); |
707 } catch (Exception e) { | |
708 assertEquals(0,1); | |
709 } | |
710 } | |
711 | |
92 | 712 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
713 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
714 for(ByteBuffer b: in) { | |
715 ByteBuffer c = b.duplicate(); | |
716 copy.add(c); | |
717 } | |
718 return copy; | |
719 } | |
720 | |
93 | 721 |
722 | |
92 | 723 public void equalByteBuffers(LinkedList<ByteBuffer> in, |
724 LinkedList<ByteBuffer> out2) { | |
93 | 725 Iterable<Byte> i = byteBufferIterator(in); |
726 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
727 | |
728 for(int b: i) { | |
729 if (o.hasNext()) { | |
730 int c = o.next(); | |
731 assertEquals(b,c); | |
732 } else | |
733 assertEquals(0,1); | |
734 } | |
735 if (o.hasNext()) assertEquals(0,1); | |
736 // System.out.println(); | |
737 } | |
738 | |
739 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
740 return new Iterable<Byte>() { | |
741 public Iterator<Byte> iterator() { | |
742 return new Iterator<Byte>() { | |
743 int bytes = 0; | |
744 int buffers = 0; | |
745 public boolean hasNext() { | |
746 if (buffers>=in.size()) return false; | |
747 ByteBuffer b = in.getFirst(); | |
748 return bytes<b.remaining(); | |
92 | 749 } |
93 | 750 public Byte next() { |
751 ByteBuffer bf =in.get(buffers); | |
752 byte b = bf.get(bytes++); | |
753 if (bf.remaining()<=bytes) { | |
754 buffers++; | |
755 bytes = 0; | |
756 } | |
757 // System.out.print(b); | |
758 return b; | |
759 } | |
760 public void remove() { | |
761 } | |
762 }; | |
92 | 763 } |
93 | 764 }; |
92 | 765 } |
766 | |
54 | 767 } |
66 | 768 |
769 |