Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 102:2ce6077bdb09
needsInput
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 04 Aug 2011 10:51:29 +0900 |
parents | ae4df9b07805 |
children | d1dc2bb0200d |
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; |
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 |
93 | 35 class MyRfbProto<ByteBuffersIterator> 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; | |
27 | 59 boolean createBimgFlag; |
54 | 60 |
40 | 61 ExecutorService executor; |
54 | 62 |
25 | 63 byte[] pngBytes; |
54 | 64 |
102 | 65 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); |
66 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); | |
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[] | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
529 * @param inputIndex |
87 | 530 * @param outputs |
531 * byte data[] | |
532 * @return byte length in last byte array | |
533 * @throws IOException | |
534 */ | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
535 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { |
102 | 536 int len = 0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
537 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
|
538 while(inputIndex < inputs.size() ) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
539 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
|
540 deflater.setInput(b1.array(),b1.position(),b1.remaining()); |
102 | 541 if (inputIndex==inputs.size()) deflater.finish(); |
86 | 542 do { |
102 | 543 int len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); |
544 if (len1>0) { | |
545 len += len1; | |
546 c1.position(c1.position()+len1); | |
547 if (c1.remaining()==0) { | |
548 c1.flip(); outputs.addLast(c1); | |
549 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
550 } | |
86 | 551 } |
102 | 552 } while (!deflater.needsInput()&&!deflater.finished()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
553 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
554 if (c1.position()!=0) { |
102 | 555 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
|
556 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
557 deflater.reset(); |
90 | 558 return len; |
86 | 559 } |
87 | 560 |
561 /** | |
562 * gunzip byte arrays | |
563 * @param inflater | |
564 * @param inputs | |
565 * byte data[] | |
566 * @param outputs | |
567 * byte data[] | |
91 | 568 *@return number of total bytes |
87 | 569 * @throws IOException |
570 */ | |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
571 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) |
88 | 572 throws DataFormatException { |
102 | 573 int len=0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
574 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
|
575 while (inputIndex < inputs.size()) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
576 ByteBuffer input = inputs.get(inputIndex++); |
102 | 577 inflater.setInput(input.array(),input.position(),input.limit()); |
88 | 578 do { |
102 | 579 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); |
580 if (len0>0) { | |
581 buf.position(buf.position()+len0); | |
582 len += len0; | |
583 if (buf.remaining()==0) { | |
584 buf.flip(); | |
585 outputs.addLast(buf); | |
586 buf = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
587 } | |
91 | 588 } |
102 | 589 } while (!inflater.needsInput()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
590 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
591 if (buf.position()!=0) { |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
592 buf.flip(); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
593 outputs.addLast(buf); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
594 } |
90 | 595 return len; |
86 | 596 } |
65 | 597 |
86 | 598 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 599 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
600 ByteBuffer header = ByteBuffer.allocate(16); | |
601 readFully(header.array(),0,16); | |
602 header.limit(16); | |
603 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
604 int encoding = header.getInt(12); | |
86 | 605 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
90 | 606 ByteBuffer len = ByteBuffer.allocate(4); |
607 readFully(len.array(),0,4); len.limit(4); | |
608 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
609 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); | |
610 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); | |
88 | 611 inputs.add(inputData); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
612 unzip(inflater, inputs, 0, bufs); |
87 | 613 bufs.addFirst(header); |
86 | 614 multicastqueue.put(bufs); |
615 is.reset(); | |
616 return ; | |
617 } | |
618 } | |
87 | 619 bufs.add(header); |
620 if (dataLen>16) { | |
90 | 621 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
622 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); | |
87 | 623 bufs.add(b); |
624 } | |
86 | 625 multicastqueue.put(bufs); |
626 is.reset(); | |
627 | |
628 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
629 // but it may generate too many large data. It is better to do it in each client. | |
630 // But we have do inflation for all input data, so we have to do it here. | |
631 } | |
43 | 632 |
71 | 633 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 634 final OutputStream os, final InputStream is) throws IOException { |
635 // createBimgFlag = true; | |
636 // rfb.addSockTmp(newCli); | |
637 // addSock(newCli); | |
90 | 638 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); |
102 | 639 |
640 final Runnable reader = new Runnable() { | |
641 public void run() { | |
642 byte b[] = new byte[4096]; | |
643 for(;;) { | |
644 try { | |
645 int c = is.read(b); | |
646 if (c<=0) throw new IOException(); | |
647 System.out.println("client read "+c); | |
648 } catch (IOException e) { | |
649 try { | |
650 os.close(); | |
651 is.close(); | |
652 } catch (IOException e1) { | |
653 } | |
654 return; | |
655 } | |
656 } | |
657 } | |
658 }; | |
54 | 659 Runnable sender = new Runnable() { |
660 public void run() { | |
80 | 661 |
100
ae4df9b07805
Test passed. VNC partial draw on ZRE.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
99
diff
changeset
|
662 Deflater deflater = new Deflater(); |
54 | 663 try { |
65 | 664 /** |
665 * initial connection of RFB protocol | |
666 */ | |
54 | 667 sendRfbVersion(os); |
668 readVersionMsg(is); | |
669 sendSecurityType(os); | |
670 readSecType(is); | |
671 sendSecResult(os); | |
672 readClientInit(is); | |
673 sendInitData(os); | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
674 new Thread(reader).start(); |
54 | 675 for (;;) { |
90 | 676 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
677 int inputIndex = 0; |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
678 ByteBuffer header = bufs.get(inputIndex++); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
679 if (header==null) continue; |
90 | 680 if (header.get(0)==RfbProto.FramebufferUpdate) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
681 System.out.println("client "+ clients); |
90 | 682 int encoding = header.getInt(12); |
80 | 683 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
90 | 684 LinkedList<ByteBuffer> outs = new LinkedList<ByteBuffer>(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
685 int len2 = zip(deflater, bufs, inputIndex, outs); |
90 | 686 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); |
687 outs.addFirst(blen); | |
87 | 688 outs.addFirst(header); |
86 | 689 while(!outs.isEmpty()) { |
90 | 690 ByteBuffer out= outs.poll(); |
691 os.write(out.array(),out.position(),out.limit()); | |
84 | 692 } |
80 | 693 } |
87 | 694 os.flush(); |
95
285dd4d6dacf
gziped partial drawing.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
94
diff
changeset
|
695 continue; |
87 | 696 } |
90 | 697 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
|
698 while(inputIndex < bufs.size()) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
699 ByteBuffer b = bufs.get(inputIndex++); |
90 | 700 os.write(b.array(), b.position(), b.limit()); |
84 | 701 } |
85 | 702 os.flush(); |
54 | 703 } |
704 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
705 try { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
706 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
707 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
708 } |
87 | 709 /* if socket closed cliList.remove(newCli); */ |
54 | 710 } |
711 } | |
712 }; | |
80 | 713 clients++; |
54 | 714 new Thread(sender).start(); |
715 | |
716 } | |
66 | 717 |
718 | |
88 | 719 |
720 @Test | |
721 public void test1() { | |
722 try { | |
90 | 723 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
724 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
725 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
88 | 726 for(int i=0;i<10;i++) { |
90 | 727 in.add(ByteBuffer.wrap("test1".getBytes())); |
728 in.add(ByteBuffer.wrap("test2".getBytes())); | |
729 in.add(ByteBuffer.wrap("test3".getBytes())); | |
730 in.add(ByteBuffer.wrap("test4".getBytes())); | |
88 | 731 } |
92 | 732 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 733 |
88 | 734 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
735 zip(deflater,in,0,out); |
92 | 736 // 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
|
737 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
|
738 inflater.reset(); |
92 | 739 equalByteBuffers(in1, out2); |
740 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
741 zip(deflater,out2,0,out4); |
92 | 742 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
|
743 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
|
744 int len = equalByteBuffers(in1,out5); |
92 | 745 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
746 System.out.println("Test Ok. "+len); |
88 | 747 } catch (Exception e) { |
748 assertEquals(0,1); | |
749 } | |
750 } | |
751 | |
92 | 752 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
753 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
754 for(ByteBuffer b: in) { | |
755 ByteBuffer c = b.duplicate(); | |
756 copy.add(c); | |
757 } | |
758 return copy; | |
759 } | |
760 | |
93 | 761 |
762 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
763 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 764 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
765 int len = 0; |
93 | 766 Iterable<Byte> i = byteBufferIterator(in); |
767 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
768 | |
769 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
|
770 len ++; |
93 | 771 if (o.hasNext()) { |
772 int c = o.next(); | |
773 assertEquals(b,c); | |
774 } else | |
775 assertEquals(0,1); | |
776 } | |
777 if (o.hasNext()) assertEquals(0,1); | |
778 // System.out.println(); | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
779 return len; |
93 | 780 } |
781 | |
782 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
783 return new Iterable<Byte>() { | |
784 public Iterator<Byte> iterator() { | |
785 return new Iterator<Byte>() { | |
786 int bytes = 0; | |
787 int buffers = 0; | |
788 public boolean hasNext() { | |
789 if (buffers>=in.size()) return false; | |
790 ByteBuffer b = in.getFirst(); | |
791 return bytes<b.remaining(); | |
92 | 792 } |
93 | 793 public Byte next() { |
794 ByteBuffer bf =in.get(buffers); | |
795 byte b = bf.get(bytes++); | |
796 if (bf.remaining()<=bytes) { | |
797 buffers++; | |
798 bytes = 0; | |
799 } | |
800 // System.out.print(b); | |
801 return b; | |
802 } | |
803 public void remove() { | |
804 } | |
805 }; | |
92 | 806 } |
93 | 807 }; |
92 | 808 } |
809 | |
54 | 810 } |
66 | 811 |
812 |