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