annotate src/myVncProxy/MyRfbProto.java @ 24:87b29d6039a6

add package myVncProxy
author e085711
date Sun, 24 Apr 2011 23:03:00 +0900
parents src/MyRfbProto.java@b51bb7bc0766
children cded9fd297ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
87b29d6039a6 add package myVncProxy
e085711
parents: 23
diff changeset
1 package myVncProxy;
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
2 import java.io.BufferedReader;
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
3 import java.io.IOException;
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
4 import java.io.InputStreamReader;
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
5 import java.net.BindException;
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
6 import java.net.ServerSocket;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
7 import java.net.Socket;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
8 import java.util.LinkedList;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
9
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
11 class MyRfbProto extends RfbProto {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
12
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
13 private int messageType;
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
14 private int rectangles;
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
15 private int rectX;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
16 private int rectY;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
17 private int rectW;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
18 private int rectH;
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
19 private int encoding;
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
20
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
21 private ServerSocket servSock;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
22 private int acceptPort;
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
23 private byte initData[];
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
24 private LinkedList <Socket> cliList;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
25
13
a5d73cafc8fe add ProxyVncCanvas and VncProxyService
e085711
parents: 10
diff changeset
26 MyRfbProto(String h, int p, VncViewer v ) throws IOException {
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
27 super(h, p, v);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
28 cliList = new LinkedList <Socket>();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
29 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
30
13
a5d73cafc8fe add ProxyVncCanvas and VncProxyService
e085711
parents: 10
diff changeset
31 MyRfbProto(String h, int p) throws IOException {
a5d73cafc8fe add ProxyVncCanvas and VncProxyService
e085711
parents: 10
diff changeset
32 super(h, p);
a5d73cafc8fe add ProxyVncCanvas and VncProxyService
e085711
parents: 10
diff changeset
33 cliList = new LinkedList <Socket>();
a5d73cafc8fe add ProxyVncCanvas and VncProxyService
e085711
parents: 10
diff changeset
34 }
24
87b29d6039a6 add package myVncProxy
e085711
parents: 23
diff changeset
35
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
36 void initServSock(int port) throws IOException{
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
37 servSock = new ServerSocket(port);
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
38 acceptPort = port;
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
39 }
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
40 void selectPort(){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
41 int i = 5550;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
42 while(true){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
43 try{
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
44 initServSock(i);
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
45 break;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
46 }catch(BindException e){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
47 i++;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
48 continue;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
49 }catch(IOException e){
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
50
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
51 }
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
52 }
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
53 }
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
54 int getAcceptPort(){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
55 return acceptPort;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
56 }
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
57 void setSoTimeout(int num) throws IOException {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
58 servSock.setSoTimeout(num);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
59 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
60
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
61 Socket accept() throws IOException {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
62 return servSock.accept();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
63 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
64
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
65 void addSock(Socket sock){
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
66 cliList.add(sock);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
67 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
68
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
69 void mark(int len) throws IOException {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
70 is.mark(len);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
71 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
72
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
73 void reset() throws IOException {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
74 is.reset();
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
75 }
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
76
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
77 boolean markSupported() {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
78 return is.markSupported();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
79 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
80
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
81 void readServerInit() throws IOException {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
82
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
83 mark(255);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
84 skipBytes(20);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
85 int nlen = readU32();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
86 int blen = 20+4+nlen;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
87 initData = new byte[blen];
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
88 reset();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
89
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
90 mark(blen);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
91 readFully(initData);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
92 reset();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
93
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
94 framebufferWidth = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
95 framebufferHeight = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
96 bitsPerPixel = readU8();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
97 depth = readU8();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
98 bigEndian = (readU8() != 0);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
99 trueColour = (readU8() != 0);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
100 redMax = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
101 greenMax = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
102 blueMax = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
103 redShift = readU8();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
104 greenShift = readU8();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
105 blueShift = readU8();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
106 byte[] pad = new byte[3];
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
107 readFully(pad);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
108 int nameLength = readU32();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
109 byte[] name = new byte[nameLength];
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
110 readFully(name);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
111 desktopName = new String(name);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
112
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
113 // Read interaction capabilities (TightVNC protocol extensions)
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
114 if (protocolTightVNC) {
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
115 int nServerMessageTypes = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
116 int nClientMessageTypes = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
117 int nEncodingTypes = readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
118 readU16();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
119 readCapabilityList(serverMsgCaps, nServerMessageTypes);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
120 readCapabilityList(clientMsgCaps, nClientMessageTypes);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
121 readCapabilityList(encodingCaps, nEncodingTypes);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
122 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
123
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
124 inNormalProtocol = true;
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
125 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
126
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
127 void sendInitData(Socket sock) throws IOException{
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
128 sock.getOutputStream().write(initData);
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
129 }
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
130
17
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
131 // void sendData(byte b[]) throws IOException{
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
132 void sendData(byte b[]){
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
133 try{
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
134 for(Socket cli : cliList){
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
135 try{
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
136 cli.getOutputStream().write(b, 0, b.length);
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
137 }catch(IOException e){
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
138 // if socket closed
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
139 // cliList.remove(cli);
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
140 cliList.remove(cli);
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
141 }
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
142 }
20
361016476783 not print out message version. ProxyVncCanvas
e085711
parents: 18
diff changeset
143 // System.out.println("cliSize="+cliSize());
17
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
144 }catch(Exception e){
20
361016476783 not print out message version. ProxyVncCanvas
e085711
parents: 18
diff changeset
145 // System.out.println("cliSize 0");
17
4eda2dbb7fef ProxyVncCanvas version 0.2
e085711
parents: 15
diff changeset
146 }
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
147 }
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
148 boolean ready() throws IOException {
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
149 BufferedReader br = new BufferedReader(new InputStreamReader(is));
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
150 return br.ready();
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
151 }
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
152
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
153 int cliSize(){
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
154 return cliList.size();
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
155 }
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
156 void printNumBytesRead(){
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
157 System.out.println("numBytesRead="+numBytesRead);
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
158 }
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
159 void bufResetSend(int size) throws IOException {
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
160 reset();
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
161 int len = size;
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
162 if(available() < size )
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
163 len = available();
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
164 byte buffer[] = new byte[len];
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
165 readFully(buffer);
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
166 sendData(buffer);
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
167 }
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
168 void regiFramebufferUpdate()throws IOException{
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
169 mark(16);
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
170 messageType = readU8();
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
171 skipBytes(1);
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
172 rectangles = readU16();
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
173 rectX = readU16();
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
174 rectY = readU16();
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
175 rectW = readU16();
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
176 rectH = readU16();
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
177 encoding = readU32();
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
178 reset();
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
179 }
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
180 void checkAndMark() throws IOException{
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
181 switch(encoding){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
182 case RfbProto.EncodingRaw:
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
183 mark(rectW * rectH * 4 + 16);
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
184 break;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
185 default:
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
186 mark(1000000);
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
187 }
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
188 }
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
189
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
190 void printFramebufferUpdate(){
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
191
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
192 System.out.println("messageType=" + messageType);
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
193 System.out.println("rectangles="+rectangles);
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
194 System.out.println("encoding=" + encoding);
23
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
195 switch(encoding){
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
196 case RfbProto.EncodingRaw:
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
197 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
198 break;
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
199 default:
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
200
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
201
b51bb7bc0766 test update
e085711
parents: 20
diff changeset
202 }
18
ccad9909e0e2 raw only version
e085711
parents: 17
diff changeset
203 }
15
89e1c5f84407 faile ZRLE encode. success raw encode.
e085711
parents: 13
diff changeset
204
10
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
205
2840c7a259f1 add acceptThread
e085711
parents:
diff changeset
206 }