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