Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 127:97bb1436e34c
change encode to Hextile from ZRLE
author | e085711 |
---|---|
date | Tue, 02 Aug 2011 13:17:23 +0900 |
parents | 762d2b7f1db2 |
children | fa2122e5c807 |
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; |
126 | 42 private int bytesPixel; |
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() { | |
126 | 108 int i = 5955;// 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(); |
127 | 298 byte buffer[] = new byte[size]; |
15 | 299 readFully(buffer); |
300 sendData(buffer); | |
301 } | |
54 | 302 |
61 | 303 |
54 | 304 void regiFramebufferUpdate() throws IOException { |
126 | 305 is.mark(30); |
18 | 306 messageType = readU8(); |
307 skipBytes(1); | |
308 rectangles = readU16(); | |
23 | 309 rectX = readU16(); |
310 rectY = readU16(); | |
311 rectW = readU16(); | |
312 rectH = readU16(); | |
27 | 313 encoding = readU32(); |
127 | 314 |
67 | 315 int dataLen; |
316 switch (encoding) { | |
317 case RfbProto.EncodingRaw: | |
318 dataLen = rectW * rectH * 4 + 16; | |
319 break; | |
127 | 320 case RfbProto.EncodingZRLE: |
321 int zLen = readU32(); | |
322 dataLen = zLen + 20; | |
67 | 323 break; |
324 case RfbProto.EncodingRRE: | |
325 case RfbProto.EncodingCoRRE: | |
326 case RfbProto.EncodingHextile: | |
327 case RfbProto.EncodingZlib: | |
328 case RfbProto.EncodingTight: | |
127 | 329 // dataLen <= EncodingRaw |
67 | 330 default: |
127 | 331 dataLen = rectW * rectH * 4 + 16; |
332 break; | |
67 | 333 } |
127 | 334 System.out.println("dataLen = "+dataLen); |
335 is.reset(); | |
336 is.mark(dataLen); | |
337 | |
15 | 338 } |
54 | 339 |
127 | 340 |
61 | 341 int checkAndMark() throws IOException { |
127 | 342 /* |
61 | 343 int dataLen; |
54 | 344 switch (encoding) { |
23 | 345 case RfbProto.EncodingRaw: |
39 | 346 dataLen = rectW * rectH * 4 + 16; |
78 | 347 is.mark(dataLen); |
23 | 348 break; |
67 | 349 case RfbProto.EncodingCopyRect: |
350 dataLen = 16 + 4; | |
78 | 351 is.mark(dataLen); |
67 | 352 break; |
353 case RfbProto.EncodingRRE: | |
354 case RfbProto.EncodingCoRRE: | |
355 case RfbProto.EncodingHextile: | |
356 case RfbProto.EncodingZlib: | |
357 case RfbProto.EncodingTight: | |
126 | 358 dataLen = zLen + 20; |
359 is.mark(dataLen); | |
360 break; | |
27 | 361 case RfbProto.EncodingZRLE: |
54 | 362 dataLen = zLen + 20; |
78 | 363 is.mark(dataLen); |
364 break; | |
365 case RfbProto.EncodingXCursor: | |
366 case RfbProto.EncodingRichCursor: | |
367 int pixArray = rectW * rectH * 4; | |
368 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
369 dataLen = pixArray + u8Array; | |
370 printFramebufferUpdate(); | |
371 is.mark(dataLen); | |
27 | 372 break; |
23 | 373 default: |
61 | 374 dataLen = 1000000; |
78 | 375 is.mark(dataLen); |
54 | 376 } |
61 | 377 return dataLen; |
127 | 378 */ |
379 return 0; | |
61 | 380 } |
65 | 381 |
61 | 382 void readSendData(int dataLen) throws IOException { |
383 byte buffer[] = new byte[dataLen]; | |
384 readFully(buffer); | |
385 multicastqueue.put(buffer); | |
78 | 386 is.reset(); |
65 | 387 |
61 | 388 /* |
389 for (Socket cli : cliList) { | |
390 try { | |
391 OutputStream out = cli.getOutputStream(); | |
392 executor.execute(new SendThread(out, buffer)); | |
393 } catch (IOException e) { | |
394 // if client socket closed | |
395 cliListTmp.remove(cli); | |
396 } catch (Exception e) { | |
397 | |
398 } | |
399 | |
400 } | |
401 */ | |
402 } | |
403 void sendDataToClient() throws IOException { | |
404 regiFramebufferUpdate(); | |
405 int dataLen = checkAndMark(); | |
406 readSendData(dataLen); | |
23 | 407 } |
54 | 408 |
126 | 409 void sendDataToClient(int num) throws IOException { |
410 bytesPixel = num; | |
411 regiFramebufferUpdate(); | |
412 int dataLen = checkAndMark(); | |
413 readSendData(dataLen); | |
414 } | |
54 | 415 BufferedImage createBufferedImage(Image img) { |
416 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
417 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 418 |
25 | 419 Graphics g = bimg.getGraphics(); |
420 g.drawImage(img, 0, 0, null); | |
421 g.dispose(); | |
422 return bimg; | |
423 } | |
424 | |
54 | 425 void createPngBytes(BufferedImage bimg) throws IOException { |
426 pngBytes = getImageBytes(bimg, "png"); | |
25 | 427 } |
54 | 428 |
429 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 430 byte[] b = getImageBytes(img, "png"); |
431 return b; | |
432 } | |
54 | 433 |
434 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
435 throws IOException { | |
25 | 436 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
437 BufferedOutputStream os = new BufferedOutputStream(bos); | |
438 image.flush(); | |
439 ImageIO.write(image, imageFormat, os); | |
440 os.flush(); | |
441 os.close(); | |
442 return bos.toByteArray(); | |
443 } | |
444 | |
54 | 445 void sendPngData(Socket sock) throws IOException { |
26 | 446 byte[] dataLength = castIntByte(pngBytes.length); |
447 sock.getOutputStream().write(dataLength); | |
25 | 448 sock.getOutputStream().write(pngBytes); |
449 } | |
54 | 450 |
451 byte[] castIntByte(int len) { | |
26 | 452 byte[] b = new byte[4]; |
54 | 453 b[0] = (byte) ((len >>> 24) & 0xFF); |
454 b[1] = (byte) ((len >>> 16) & 0xFF); | |
455 b[2] = (byte) ((len >>> 8) & 0xFF); | |
456 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 457 return b; |
458 } | |
54 | 459 |
460 BufferedImage createBimg() throws IOException { | |
25 | 461 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
462 return bimg; | |
463 } | |
65 | 464 /* |
54 | 465 void readPngData() throws IOException { |
25 | 466 pngBytes = new byte[is.available()]; |
467 readFully(pngBytes); | |
468 } | |
65 | 469 */ |
54 | 470 void printFramebufferUpdate() { |
471 | |
18 | 472 System.out.println("messageType=" + messageType); |
54 | 473 System.out.println("rectangles=" + rectangles); |
18 | 474 System.out.println("encoding=" + encoding); |
127 | 475 System.out.println("rectX = "+ rectX +": rectY = "+rectY); |
476 System.out.println("rectW = "+ rectW +": rectH = "+rectH); | |
477 System.out.println("rectW * rectH = " + rectW * rectH); | |
54 | 478 switch (encoding) { |
23 | 479 case RfbProto.EncodingRaw: |
54 | 480 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
481 + 16); | |
23 | 482 break; |
483 default: | |
484 } | |
18 | 485 } |
65 | 486 |
77 | 487 void readSpeedCheck() throws IOException { |
488 byte[] b = new byte[1]; | |
65 | 489 readFully(b); |
490 } | |
491 | |
77 | 492 void startSpeedCheck() { |
66 | 493 byte[] b = new byte[2]; |
77 | 494 b[0] = (byte) SpeedCheckMillis; |
66 | 495 b[1] = (byte) 0; |
496 startCheckTime = System.currentTimeMillis(); | |
497 System.out.println("startChckTime = "+ startCheckTime); | |
65 | 498 multicastqueue.put(b); |
499 } | |
500 | |
77 | 501 void endSpeedCheck() { |
65 | 502 long accTime = System.currentTimeMillis(); |
503 long time = accTime - startCheckTime; | |
504 System.out.println("checkMillis: " + time); | |
505 } | |
506 | |
507 void printStatus() { | |
508 System.out.println(); | |
509 } | |
510 | |
511 synchronized void changeStatusFlag() { | |
512 printStatusFlag = true; | |
513 } | |
514 | |
515 void printMills() { | |
516 if(printStatusFlag) { | |
517 | |
518 changeStatusFlag(); | |
519 } else { | |
520 changeStatusFlag(); | |
521 } | |
522 } | |
523 | |
43 | 524 |
71 | 525 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 526 final OutputStream os, final InputStream is) throws IOException { |
527 // createBimgFlag = true; | |
528 // rfb.addSockTmp(newCli); | |
529 // addSock(newCli); | |
530 final Client<byte[]> c = multicastqueue.newClient(); | |
531 Runnable sender = new Runnable() { | |
532 public void run() { | |
533 try { | |
65 | 534 /** |
535 * initial connection of RFB protocol | |
536 */ | |
54 | 537 sendRfbVersion(os); |
538 readVersionMsg(is); | |
539 sendSecurityType(os); | |
540 readSecType(is); | |
541 sendSecResult(os); | |
542 readClientInit(is); | |
543 sendInitData(os); | |
43 | 544 |
54 | 545 for (;;) { |
546 byte[] b = c.poll(); | |
547 os.write(b, 0, b.length); | |
548 } | |
549 } catch (IOException e) { | |
65 | 550 /** |
551 * if socket closed | |
552 */ | |
54 | 553 // cliList.remove(newCli); |
554 } | |
555 | |
556 } | |
557 | |
558 }; | |
559 new Thread(sender).start(); | |
560 | |
561 } | |
66 | 562 |
77 | 563 void speedCheckMillis() { |
66 | 564 |
565 Runnable stdin = new Runnable() { | |
566 public void run() { | |
567 int c; | |
568 try { | |
569 while( (c = System.in.read()) != -1 ) { | |
570 switch(c) { | |
571 case 's': | |
572 break; | |
573 default: | |
77 | 574 startSpeedCheck(); |
66 | 575 break; |
576 } | |
577 } | |
578 }catch(IOException e){ | |
579 System.out.println(e); | |
580 } | |
581 } | |
582 }; | |
583 | |
584 new Thread(stdin).start(); | |
585 } | |
586 | |
54 | 587 } |
66 | 588 |
589 |