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