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