Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 78:5970410efee7
modify VncProxyService. EncodingRAW -> EncodingZlib
author | e085711 |
---|---|
date | Fri, 29 Jul 2011 19:17:31 +0900 |
parents | fe5925bb9a7e |
children | 712a047908df 762d2b7f1db2 |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
54 | 2 |
25 | 3 import java.awt.Graphics; |
4 import java.awt.Image; | |
5 import java.awt.image.BufferedImage; | |
6 import java.io.BufferedOutputStream; | |
15 | 7 import java.io.BufferedReader; |
25 | 8 import java.io.ByteArrayInputStream; |
9 import java.io.ByteArrayOutputStream; | |
10 | 10 import java.io.IOException; |
43 | 11 import java.io.InputStream; |
15 | 12 import java.io.InputStreamReader; |
23 | 13 import java.net.BindException; |
10 | 14 import java.net.ServerSocket; |
15 import java.net.Socket; | |
16 import java.util.LinkedList; | |
17 | |
25 | 18 import javax.imageio.ImageIO; |
19 | |
54 | 20 import myVncProxy.MulticastQueue.Client; |
21 | |
40 | 22 import java.util.concurrent.ExecutorService; |
23 import java.util.concurrent.Executors; | |
24 import java.io.OutputStream; | |
10 | 25 |
26 class MyRfbProto extends RfbProto { | |
43 | 27 final static String versionMsg_3_998 = "RFB 003.998\n"; |
65 | 28 /** |
29 * CheckMillis is one of new msgType for RFB 3.998. | |
30 */ | |
77 | 31 final static int SpeedCheckMillis = 4; |
65 | 32 boolean printStatusFlag = false; |
33 long startCheckTime; | |
54 | 34 |
18 | 35 private int messageType; |
36 private int rectangles; | |
23 | 37 private int rectX; |
38 private int rectY; | |
39 private int rectW; | |
40 private int rectH; | |
18 | 41 private int encoding; |
27 | 42 private int zLen; |
18 | 43 |
23 | 44 private ServerSocket servSock; |
45 private int acceptPort; | |
10 | 46 private byte initData[]; |
54 | 47 private LinkedList<Socket> cliListTmp; |
48 private LinkedList<Socket> cliList; | |
49 private LinkedList<Thread> sendThreads; | |
27 | 50 boolean createBimgFlag; |
54 | 51 |
40 | 52 ExecutorService executor; |
54 | 53 |
25 | 54 byte[] pngBytes; |
54 | 55 |
56 private MulticastQueue<byte[]> multicastqueue = new MulticastQueue<byte[]>(); | |
57 | |
58 MyRfbProto(String h, int p, VncViewer v) throws IOException { | |
10 | 59 super(h, p, v); |
54 | 60 cliList = new LinkedList<Socket>(); |
61 cliListTmp = new LinkedList<Socket>(); | |
27 | 62 createBimgFlag = false; |
61 | 63 // sendThreads = new LinkedList<Thread>(); |
54 | 64 // executor = Executors.newCachedThreadPool(); |
65 // executor = Executors.newSingleThreadExecutor(); | |
10 | 66 } |
67 | |
13 | 68 MyRfbProto(String h, int p) throws IOException { |
69 super(h, p); | |
54 | 70 cliList = new LinkedList<Socket>(); |
71 cliListTmp = new LinkedList<Socket>(); | |
27 | 72 createBimgFlag = false; |
61 | 73 // sendThreads = new LinkedList<Thread>(); |
54 | 74 // executor = Executors.newCachedThreadPool(); |
75 // executor = Executors.newSingleThreadExecutor(); | |
13 | 76 } |
24 | 77 |
44 | 78 // over write |
43 | 79 void writeVersionMsg() throws IOException { |
80 clientMajor = 3; | |
81 if (serverMinor >= 9) { | |
54 | 82 clientMinor = 9; |
83 os.write(versionMsg_3_998.getBytes()); | |
43 | 84 } else if (serverMajor > 3 || serverMinor >= 8) { |
85 clientMinor = 8; | |
86 os.write(versionMsg_3_8.getBytes()); | |
87 } else if (serverMinor >= 9) { | |
88 clientMinor = 9; | |
89 os.write(versionMsg_3_998.getBytes()); | |
90 } else if (serverMinor >= 7) { | |
91 clientMinor = 7; | |
92 os.write(versionMsg_3_7.getBytes()); | |
93 } else { | |
94 clientMinor = 3; | |
95 os.write(versionMsg_3_3.getBytes()); | |
96 } | |
97 protocolTightVNC = false; | |
98 initCapabilities(); | |
99 } | |
100 | |
54 | 101 void initServSock(int port) throws IOException { |
10 | 102 servSock = new ServerSocket(port); |
23 | 103 acceptPort = port; |
10 | 104 } |
54 | 105 |
106 // 5550を開けるが、開いてないなら+1のポートを開ける。 | |
107 void selectPort() { | |
23 | 108 int i = 5550; |
54 | 109 while (true) { |
110 try { | |
23 | 111 initServSock(i); |
112 break; | |
54 | 113 } catch (BindException e) { |
23 | 114 i++; |
115 continue; | |
54 | 116 } catch (IOException e) { |
10 | 117 |
23 | 118 } |
119 } | |
54 | 120 System.out.println("accept port = " + i); |
23 | 121 } |
54 | 122 |
123 int getAcceptPort() { | |
23 | 124 return acceptPort; |
125 } | |
54 | 126 |
10 | 127 void setSoTimeout(int num) throws IOException { |
128 servSock.setSoTimeout(num); | |
129 } | |
54 | 130 |
10 | 131 Socket accept() throws IOException { |
132 return servSock.accept(); | |
133 } | |
134 | |
54 | 135 void addSock(Socket sock) { |
10 | 136 cliList.add(sock); |
137 } | |
54 | 138 |
139 void addSockTmp(Socket sock) { | |
140 System.out.println("connected " + sock.getInetAddress()); | |
27 | 141 cliListTmp.add(sock); |
142 } | |
54 | 143 |
10 | 144 boolean markSupported() { |
145 return is.markSupported(); | |
146 } | |
54 | 147 |
10 | 148 void readServerInit() throws IOException { |
54 | 149 |
78 | 150 is.mark(255); |
10 | 151 skipBytes(20); |
152 int nlen = readU32(); | |
54 | 153 int blen = 20 + 4 + nlen; |
10 | 154 initData = new byte[blen]; |
78 | 155 is.reset(); |
10 | 156 |
78 | 157 is.mark(blen); |
10 | 158 readFully(initData); |
78 | 159 is.reset(); |
54 | 160 |
10 | 161 framebufferWidth = readU16(); |
162 framebufferHeight = readU16(); | |
163 bitsPerPixel = readU8(); | |
164 depth = readU8(); | |
165 bigEndian = (readU8() != 0); | |
166 trueColour = (readU8() != 0); | |
167 redMax = readU16(); | |
168 greenMax = readU16(); | |
169 blueMax = readU16(); | |
170 redShift = readU8(); | |
171 greenShift = readU8(); | |
172 blueShift = readU8(); | |
173 byte[] pad = new byte[3]; | |
174 readFully(pad); | |
175 int nameLength = readU32(); | |
176 byte[] name = new byte[nameLength]; | |
177 readFully(name); | |
178 desktopName = new String(name); | |
179 | |
180 // Read interaction capabilities (TightVNC protocol extensions) | |
181 if (protocolTightVNC) { | |
182 int nServerMessageTypes = readU16(); | |
183 int nClientMessageTypes = readU16(); | |
184 int nEncodingTypes = readU16(); | |
185 readU16(); | |
186 readCapabilityList(serverMsgCaps, nServerMessageTypes); | |
187 readCapabilityList(clientMsgCaps, nClientMessageTypes); | |
188 readCapabilityList(encodingCaps, nEncodingTypes); | |
189 } | |
190 | |
191 inNormalProtocol = true; | |
192 } | |
193 | |
54 | 194 void sendRfbVersion(OutputStream os) throws IOException { |
195 os.write(versionMsg_3_998.getBytes()); | |
43 | 196 } |
54 | 197 |
45 | 198 void readVersionMsg(InputStream is) throws IOException { |
199 | |
200 byte[] b = new byte[12]; | |
201 | |
202 is.read(b); | |
203 | |
204 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
205 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
206 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
207 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
208 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
209 throw new IOException("Host " + host + " port " + port | |
210 + " is not an RFB server"); | |
211 } | |
212 | |
213 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
214 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
215 | |
216 if (serverMajor < 3) { | |
217 throw new IOException( | |
218 "RFB server does not support protocol version 3"); | |
54 | 219 } |
220 | |
221 } | |
222 | |
43 | 223 void sendSecurityType(OutputStream os) throws IOException { |
224 // number-of-security-types | |
225 os.write(1); | |
54 | 226 // security-types |
43 | 227 // 1:None |
228 os.write(1); | |
229 } | |
54 | 230 |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
231 void readSecType(InputStream is) throws IOException { |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
232 byte[] b = new byte[1]; |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
233 is.read(b); |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
234 |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
235 } |
54 | 236 |
47
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
237 void sendSecResult(OutputStream os) throws IOException { |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
238 byte[] b = castIntByte(0); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
239 os.write(b); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
240 } |
54 | 241 |
43 | 242 void readClientInit(InputStream in) throws IOException { |
243 byte[] b = new byte[0]; | |
244 in.read(b); | |
245 } | |
54 | 246 |
247 void sendInitData(OutputStream os) throws IOException { | |
248 os.write(initData); | |
10 | 249 } |
250 | |
54 | 251 void sendData(byte b[]) { |
252 try { | |
253 multicastqueue.put(b); | |
254 | |
255 /* | |
256 * // for(Socket cli : cliList){ // try{ // | |
257 * cli.getOutputStream().write(b, 0, b.length); // | |
258 * }catch(IOException e){ // // if socket closed // | |
259 * cliList.remove(cli); // } // } | |
260 */ | |
261 // System.out.println("cliSize="+cliSize()); | |
262 } catch (Exception e) { | |
17 | 263 } |
27 | 264 } |
54 | 265 |
266 void sendPngImage() { | |
267 try { | |
268 for (Socket cli : cliListTmp) { | |
269 try { | |
27 | 270 sendPngData(cli); |
271 addSock(cli); | |
54 | 272 } catch (IOException e) { |
27 | 273 // if socket closed |
274 cliListTmp.remove(cli); | |
275 } | |
276 } | |
54 | 277 // System.out.println("cliSize="+cliSize()); |
278 } catch (Exception e) { | |
27 | 279 } |
280 cliListTmp.clear(); | |
281 } | |
282 | |
15 | 283 boolean ready() throws IOException { |
284 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
285 return br.ready(); | |
54 | 286 } |
10 | 287 |
54 | 288 int cliSize() { |
10 | 289 return cliList.size(); |
54 | 290 } |
291 | |
292 void printNumBytesRead() { | |
293 System.out.println("numBytesRead=" + numBytesRead); | |
294 } | |
295 | |
15 | 296 void bufResetSend(int size) throws IOException { |
78 | 297 is.reset(); |
15 | 298 int len = size; |
54 | 299 if (available() < size) |
15 | 300 len = available(); |
301 byte buffer[] = new byte[len]; | |
302 readFully(buffer); | |
303 sendData(buffer); | |
304 } | |
54 | 305 |
61 | 306 |
54 | 307 void regiFramebufferUpdate() throws IOException { |
78 | 308 is.mark(20); |
18 | 309 messageType = readU8(); |
310 skipBytes(1); | |
311 rectangles = readU16(); | |
23 | 312 rectX = readU16(); |
313 rectY = readU16(); | |
314 rectW = readU16(); | |
315 rectH = readU16(); | |
27 | 316 encoding = readU32(); |
78 | 317 System.out.println("encoding = "+encoding); |
54 | 318 if (encoding == 16) |
27 | 319 zLen = readU32(); |
78 | 320 is.reset(); |
67 | 321 /* |
322 int dataLen; | |
323 switch (encoding) { | |
324 case RfbProto.EncodingRaw: | |
325 dataLen = rectW * rectH * 4 + 16; | |
326 mark(dataLen); | |
327 break; | |
328 case RfbProto.EncodingCopyRect: | |
329 dataLen = 16 + 4; | |
330 mark(dataLen); | |
331 break; | |
332 case RfbProto.EncodingRRE: | |
333 case RfbProto.EncodingCoRRE: | |
334 case RfbProto.EncodingHextile: | |
335 | |
336 case RfbProto.EncodingZlib: | |
337 case RfbProto.EncodingTight: | |
338 case RfbProto.EncodingZRLE: | |
339 dataLen = zLen + 20; | |
340 mark(dataLen); | |
341 break; | |
342 default: | |
343 dataLen = 1000000; | |
344 mark(dataLen); | |
345 } | |
346 | |
347 */ | |
348 | |
15 | 349 } |
54 | 350 |
61 | 351 int checkAndMark() throws IOException { |
352 int dataLen; | |
54 | 353 switch (encoding) { |
23 | 354 case RfbProto.EncodingRaw: |
39 | 355 dataLen = rectW * rectH * 4 + 16; |
78 | 356 is.mark(dataLen); |
23 | 357 break; |
67 | 358 case RfbProto.EncodingCopyRect: |
359 dataLen = 16 + 4; | |
78 | 360 is.mark(dataLen); |
67 | 361 break; |
362 case RfbProto.EncodingRRE: | |
363 case RfbProto.EncodingCoRRE: | |
364 case RfbProto.EncodingHextile: | |
365 case RfbProto.EncodingZlib: | |
366 case RfbProto.EncodingTight: | |
27 | 367 case RfbProto.EncodingZRLE: |
54 | 368 dataLen = zLen + 20; |
78 | 369 is.mark(dataLen); |
370 break; | |
371 case RfbProto.EncodingXCursor: | |
372 case RfbProto.EncodingRichCursor: | |
373 int pixArray = rectW * rectH * 4; | |
374 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
375 dataLen = pixArray + u8Array; | |
376 printFramebufferUpdate(); | |
377 is.mark(dataLen); | |
27 | 378 break; |
23 | 379 default: |
61 | 380 dataLen = 1000000; |
78 | 381 is.mark(dataLen); |
54 | 382 } |
61 | 383 return dataLen; |
384 } | |
65 | 385 |
61 | 386 void readSendData(int dataLen) throws IOException { |
387 byte buffer[] = new byte[dataLen]; | |
388 readFully(buffer); | |
389 multicastqueue.put(buffer); | |
78 | 390 is.reset(); |
65 | 391 |
61 | 392 /* |
393 for (Socket cli : cliList) { | |
394 try { | |
395 OutputStream out = cli.getOutputStream(); | |
396 executor.execute(new SendThread(out, buffer)); | |
397 } catch (IOException e) { | |
398 // if client socket closed | |
399 cliListTmp.remove(cli); | |
400 } catch (Exception e) { | |
401 | |
402 } | |
403 | |
404 } | |
405 */ | |
406 } | |
407 void sendDataToClient() throws IOException { | |
408 regiFramebufferUpdate(); | |
409 int dataLen = checkAndMark(); | |
410 readSendData(dataLen); | |
23 | 411 } |
54 | 412 |
413 BufferedImage createBufferedImage(Image img) { | |
414 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
415 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 416 |
25 | 417 Graphics g = bimg.getGraphics(); |
418 g.drawImage(img, 0, 0, null); | |
419 g.dispose(); | |
420 return bimg; | |
421 } | |
422 | |
54 | 423 void createPngBytes(BufferedImage bimg) throws IOException { |
424 pngBytes = getImageBytes(bimg, "png"); | |
25 | 425 } |
54 | 426 |
427 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 428 byte[] b = getImageBytes(img, "png"); |
429 return b; | |
430 } | |
54 | 431 |
432 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
433 throws IOException { | |
25 | 434 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
435 BufferedOutputStream os = new BufferedOutputStream(bos); | |
436 image.flush(); | |
437 ImageIO.write(image, imageFormat, os); | |
438 os.flush(); | |
439 os.close(); | |
440 return bos.toByteArray(); | |
441 } | |
442 | |
54 | 443 void sendPngData(Socket sock) throws IOException { |
26 | 444 byte[] dataLength = castIntByte(pngBytes.length); |
445 sock.getOutputStream().write(dataLength); | |
25 | 446 sock.getOutputStream().write(pngBytes); |
447 } | |
54 | 448 |
449 byte[] castIntByte(int len) { | |
26 | 450 byte[] b = new byte[4]; |
54 | 451 b[0] = (byte) ((len >>> 24) & 0xFF); |
452 b[1] = (byte) ((len >>> 16) & 0xFF); | |
453 b[2] = (byte) ((len >>> 8) & 0xFF); | |
454 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 455 return b; |
456 } | |
54 | 457 |
458 BufferedImage createBimg() throws IOException { | |
25 | 459 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
460 return bimg; | |
461 } | |
65 | 462 /* |
54 | 463 void readPngData() throws IOException { |
25 | 464 pngBytes = new byte[is.available()]; |
465 readFully(pngBytes); | |
466 } | |
65 | 467 */ |
54 | 468 void printFramebufferUpdate() { |
469 | |
18 | 470 System.out.println("messageType=" + messageType); |
54 | 471 System.out.println("rectangles=" + rectangles); |
18 | 472 System.out.println("encoding=" + encoding); |
78 | 473 System.out.println("rectX = "+rectX+": rectY = "+rectY); |
474 System.out.println("rectW = "+rectW+": rectH = "+rectH); | |
54 | 475 switch (encoding) { |
23 | 476 case RfbProto.EncodingRaw: |
54 | 477 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
478 + 16); | |
23 | 479 break; |
480 default: | |
481 } | |
18 | 482 } |
65 | 483 |
77 | 484 void readSpeedCheck() throws IOException { |
485 byte[] b = new byte[1]; | |
65 | 486 readFully(b); |
487 } | |
488 | |
77 | 489 void startSpeedCheck() { |
66 | 490 byte[] b = new byte[2]; |
77 | 491 b[0] = (byte) SpeedCheckMillis; |
66 | 492 b[1] = (byte) 0; |
493 startCheckTime = System.currentTimeMillis(); | |
494 System.out.println("startChckTime = "+ startCheckTime); | |
65 | 495 multicastqueue.put(b); |
496 } | |
497 | |
77 | 498 void endSpeedCheck() { |
65 | 499 long accTime = System.currentTimeMillis(); |
500 long time = accTime - startCheckTime; | |
501 System.out.println("checkMillis: " + time); | |
502 } | |
503 | |
504 void printStatus() { | |
505 System.out.println(); | |
506 } | |
507 | |
508 synchronized void changeStatusFlag() { | |
509 printStatusFlag = true; | |
510 } | |
511 | |
512 void printMills() { | |
513 if(printStatusFlag) { | |
514 | |
515 changeStatusFlag(); | |
516 } else { | |
517 changeStatusFlag(); | |
518 } | |
519 } | |
520 | |
43 | 521 |
71 | 522 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 523 final OutputStream os, final InputStream is) throws IOException { |
524 // createBimgFlag = true; | |
525 // rfb.addSockTmp(newCli); | |
526 // addSock(newCli); | |
527 final Client<byte[]> c = multicastqueue.newClient(); | |
528 Runnable sender = new Runnable() { | |
529 public void run() { | |
530 try { | |
65 | 531 /** |
532 * initial connection of RFB protocol | |
533 */ | |
54 | 534 sendRfbVersion(os); |
535 readVersionMsg(is); | |
536 sendSecurityType(os); | |
537 readSecType(is); | |
538 sendSecResult(os); | |
539 readClientInit(is); | |
540 sendInitData(os); | |
43 | 541 |
54 | 542 for (;;) { |
543 byte[] b = c.poll(); | |
544 os.write(b, 0, b.length); | |
545 } | |
546 } catch (IOException e) { | |
65 | 547 /** |
548 * if socket closed | |
549 */ | |
54 | 550 // cliList.remove(newCli); |
551 } | |
552 | |
553 } | |
554 | |
555 }; | |
556 new Thread(sender).start(); | |
557 | |
558 } | |
66 | 559 |
77 | 560 void speedCheckMillis() { |
66 | 561 |
562 Runnable stdin = new Runnable() { | |
563 public void run() { | |
564 int c; | |
565 try { | |
566 while( (c = System.in.read()) != -1 ) { | |
567 switch(c) { | |
568 case 's': | |
569 break; | |
570 default: | |
77 | 571 startSpeedCheck(); |
66 | 572 break; |
573 } | |
574 } | |
575 }catch(IOException e){ | |
576 System.out.println(e); | |
577 } | |
578 } | |
579 }; | |
580 | |
581 new Thread(stdin).start(); | |
582 } | |
583 | |
54 | 584 } |
66 | 585 |
586 |