comparison src/myVncProxy/MyRfbProto.java @ 128:fa2122e5c807

modify ProxyVncCanva
author e085711
date Tue, 02 Aug 2011 20:15:01 +0900
parents 97bb1436e34c
children acd88e63854b
comparison
equal deleted inserted replaced
127:97bb1436e34c 128:fa2122e5c807
1 package myVncProxy; 1 package myVncProxy;
2 2
3 import java.awt.Graphics; 3 import java.awt.Graphics;
4 import java.awt.Image; 4 import java.awt.Image;
5 import java.awt.image.BufferedImage; 5 import java.awt.image.BufferedImage;
6 import java.io.BufferedInputStream;
6 import java.io.BufferedOutputStream; 7 import java.io.BufferedOutputStream;
7 import java.io.BufferedReader; 8 import java.io.BufferedReader;
8 import java.io.ByteArrayInputStream; 9 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream; 10 import java.io.ByteArrayOutputStream;
11 import java.io.DataInputStream;
10 import java.io.IOException; 12 import java.io.IOException;
11 import java.io.InputStream; 13 import java.io.InputStream;
12 import java.io.InputStreamReader; 14 import java.io.InputStreamReader;
13 import java.net.BindException; 15 import java.net.BindException;
14 import java.net.ServerSocket; 16 import java.net.ServerSocket;
46 private byte initData[]; 48 private byte initData[];
47 private LinkedList<Socket> cliListTmp; 49 private LinkedList<Socket> cliListTmp;
48 private LinkedList<Socket> cliList; 50 private LinkedList<Socket> cliList;
49 private LinkedList<Thread> sendThreads; 51 private LinkedList<Thread> sendThreads;
50 boolean createBimgFlag; 52 boolean createBimgFlag;
51 53 boolean sendFlag = true;
52 ExecutorService executor; 54 ExecutorService executor;
53 55
56 // override viewer to VncProxyService from VncViewer
57 VncProxyService viewer;
58
54 byte[] pngBytes; 59 byte[] pngBytes;
55 60
56 private MulticastQueue<byte[]> multicastqueue = new MulticastQueue<byte[]>(); 61 private MulticastQueue<byte[]> multicastqueue = new MulticastQueue<byte[]>();
57 62
58 MyRfbProto(String h, int p, VncViewer v) throws IOException { 63 MyRfbProto(String h, int p, VncViewer v) throws IOException {
63 // sendThreads = new LinkedList<Thread>(); 68 // sendThreads = new LinkedList<Thread>();
64 // executor = Executors.newCachedThreadPool(); 69 // executor = Executors.newCachedThreadPool();
65 // executor = Executors.newSingleThreadExecutor(); 70 // executor = Executors.newSingleThreadExecutor();
66 } 71 }
67 72
73 MyRfbProto(String h, int p, VncProxyService v) throws IOException {
74 super(h, p);
75 viewer = v;
76 }
77
68 MyRfbProto(String h, int p) throws IOException { 78 MyRfbProto(String h, int p) throws IOException {
69 super(h, p); 79 super(h, p);
70 cliList = new LinkedList<Socket>(); 80 cliList = new LinkedList<Socket>();
71 cliListTmp = new LinkedList<Socket>(); 81 cliListTmp = new LinkedList<Socket>();
72 createBimgFlag = false; 82 createBimgFlag = false;
73 // sendThreads = new LinkedList<Thread>(); 83 // sendThreads = new LinkedList<Thread>();
74 // executor = Executors.newCachedThreadPool(); 84 // executor = Executors.newCachedThreadPool();
75 // executor = Executors.newSingleThreadExecutor(); 85 // executor = Executors.newSingleThreadExecutor();
76 } 86 }
77 87
88
78 // over write 89 // over write
79 void writeVersionMsg() throws IOException { 90 void writeVersionMsg() throws IOException {
80 clientMajor = 3; 91 clientMajor = 3;
81 if (serverMinor >= 9) { 92 if (serverMinor >= 9) {
82 clientMinor = 9; 93 clientMinor = 9;
101 void initServSock(int port) throws IOException { 112 void initServSock(int port) throws IOException {
102 servSock = new ServerSocket(port); 113 servSock = new ServerSocket(port);
103 acceptPort = port; 114 acceptPort = port;
104 } 115 }
105 116
106 // 5550を開けるが、開いてないなら+1のポートを開ける。 117 // open port 5999 for to accept client.
107 void selectPort() { 118 void selectPort() {
108 int i = 5955;// i = 5550; 119 int i = 5999;// i = 5550;
109 while (true) { 120 while (true) {
110 try { 121 try {
111 initServSock(i); 122 initServSock(i);
112 break; 123 break;
113 } catch (BindException e) { 124 } catch (BindException e) {
315 int dataLen; 326 int dataLen;
316 switch (encoding) { 327 switch (encoding) {
317 case RfbProto.EncodingRaw: 328 case RfbProto.EncodingRaw:
318 dataLen = rectW * rectH * 4 + 16; 329 dataLen = rectW * rectH * 4 + 16;
319 break; 330 break;
331 case RfbProto.EncodingTight:
332 dataLen = 4000000;
333 break;
320 case RfbProto.EncodingZRLE: 334 case RfbProto.EncodingZRLE:
321 int zLen = readU32(); 335 int zLen = readU32();
322 dataLen = zLen + 20; 336 dataLen = zLen + 20;
323 break; 337 break;
324 case RfbProto.EncodingRRE: 338 case RfbProto.EncodingRRE:
325 case RfbProto.EncodingCoRRE: 339 case RfbProto.EncodingCoRRE:
326 case RfbProto.EncodingHextile: 340 case RfbProto.EncodingHextile:
341 dataLen = rectW * rectH * 4 + 16;
342 break;
327 case RfbProto.EncodingZlib: 343 case RfbProto.EncodingZlib:
328 case RfbProto.EncodingTight:
329 // dataLen <= EncodingRaw
330 default: 344 default:
331 dataLen = rectW * rectH * 4 + 16; 345 dataLen = rectW * rectH * 4 + 16;
332 break; 346 break;
333 } 347 }
334 System.out.println("dataLen = "+dataLen); 348 System.out.println("dataLen = "+dataLen);
335 is.reset(); 349 is.reset();
336 is.mark(dataLen); 350 is.mark(dataLen);
337 351
338 } 352 }
339 353
340 354
341 int checkAndMark() throws IOException { 355 int checkAndMark() throws IOException {
342 /* 356 /*
475 System.out.println("rectX = "+ rectX +": rectY = "+rectY); 489 System.out.println("rectX = "+ rectX +": rectY = "+rectY);
476 System.out.println("rectW = "+ rectW +": rectH = "+rectH); 490 System.out.println("rectW = "+ rectW +": rectH = "+rectH);
477 System.out.println("rectW * rectH = " + rectW * rectH); 491 System.out.println("rectW * rectH = " + rectW * rectH);
478 switch (encoding) { 492 switch (encoding) {
479 case RfbProto.EncodingRaw: 493 case RfbProto.EncodingRaw:
480 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 494 int dataLen = rectW * rectH * 4 + 16;
481 + 16); 495 System.out.println("rectW * rectH * 4 + 16 = " + (rectW * rectH * 4 + 16));
482 break; 496 break;
483 default: 497 default:
484 } 498 }
485 } 499 }
486 500
518 changeStatusFlag(); 532 changeStatusFlag();
519 } else { 533 } else {
520 changeStatusFlag(); 534 changeStatusFlag();
521 } 535 }
522 } 536 }
523 537 void require() throws IOException {
524 538 sendFlag = false;
539 System.out.println("setEncodingRaw()");
540 setEncodingRaw();
541 writeFramebufferUpdateRequest(0, 0,
542 16, 16, false);
543 System.out.println("setEncodingZRLE()");
544 setEncodingZRLE();
545 System.out.println("writeFramebufferUpdateRequest");
546 writeFramebufferUpdateRequest(0, 0,
547 framebufferWidth, framebufferHeight, false);
548 }
549
550 void setEncodingRaw() throws IOException{
551 byte[] b = new byte[4 + 4];
552
553 b[0] = (byte) SetEncodings;
554 b[2] = (byte) ((1 >> 8) & 0xff);
555 b[3] = (byte) (1 & 0xff);
556
557 b[4] = (byte)0;
558 b[5] = (byte)0;
559 b[6] = (byte)0;
560 b[7] = (byte)0;
561
562 os.write(b);
563
564 }
565
566 void setEncodingZRLE() throws IOException{
567 byte[] b = new byte[4 + 4];
568
569 b[0] = (byte) SetEncodings;
570 b[2] = (byte) ((1 >> 8) & 0xff);
571 b[3] = (byte) (1 & 0xff);
572
573 b[4] = (byte)0;
574 b[5] = (byte)0;
575 b[6] = (byte)0;
576 b[7] = (byte)16;
577
578 os.write(b);
579
580 }
581
525 void newClient(AcceptThread acceptThread, final Socket newCli, 582 void newClient(AcceptThread acceptThread, final Socket newCli,
526 final OutputStream os, final InputStream is) throws IOException { 583 final OutputStream os, final InputStream is) throws IOException {
527 // createBimgFlag = true; 584 // createBimgFlag = true;
528 // rfb.addSockTmp(newCli); 585 // rfb.addSockTmp(newCli);
529 // addSock(newCli); 586 // addSock(newCli);
530 final Client<byte[]> c = multicastqueue.newClient(); 587 final Client<byte[]> c = multicastqueue.newClient();
588 require();
531 Runnable sender = new Runnable() { 589 Runnable sender = new Runnable() {
532 public void run() { 590 public void run() {
533 try { 591 try {
534 /** 592 /**
535 * initial connection of RFB protocol 593 * initial connection of RFB protocol
569 while( (c = System.in.read()) != -1 ) { 627 while( (c = System.in.read()) != -1 ) {
570 switch(c) { 628 switch(c) {
571 case 's': 629 case 's':
572 break; 630 break;
573 default: 631 default:
574 startSpeedCheck(); 632 // startSpeedCheck();
633 writeFramebufferUpdateRequest(0, 0, framebufferWidth,
634 framebufferHeight, false);
635
575 break; 636 break;
576 } 637 }
577 } 638 }
578 }catch(IOException e){ 639 }catch(IOException e){
579 System.out.println(e); 640 System.out.println(e);
582 }; 643 };
583 644
584 new Thread(stdin).start(); 645 new Thread(stdin).start();
585 } 646 }
586 647
648 void requireFramebuffer() {
649 Runnable stdin = new Runnable() {
650 public void run() {
651 int c;
652 try {
653 while ((c = System.in.read()) != -1) {
654 sendFlag = false;
655 switch (c) {
656 default:
657 System.out.println("writeFramebufferUpdateRequest()");
658 writeFramebufferUpdateRequest(0, 0,
659 framebufferWidth, framebufferHeight, false);
660 break;
661 }
662 }
663 } catch (IOException e) {
664 e.printStackTrace();
665 }
666 }
667 };
668
669 new Thread(stdin).start();
670 }
671
587 } 672 }
588 673
589 674