annotate src/MyRfbProto.java @ 23:b51bb7bc0766

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