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