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