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