17
|
1 package myVncClient;
|
19
|
2 import java.awt.Graphics;
|
|
3 import java.awt.Image;
|
18
|
4 import java.awt.image.BufferedImage;
|
19
|
5 import java.io.BufferedOutputStream;
|
15
|
6 import java.io.BufferedReader;
|
18
|
7 import java.io.ByteArrayInputStream;
|
19
|
8 import java.io.ByteArrayOutputStream;
|
7
|
9 import java.io.IOException;
|
27
|
10 import java.io.InputStream;
|
15
|
11 import java.io.InputStreamReader;
|
18
|
12 import java.net.BindException;
|
10
|
13 import java.net.ServerSocket;
|
7
|
14 import java.net.Socket;
|
10
|
15 import java.util.LinkedList;
|
8
|
16
|
18
|
17 import javax.imageio.ImageIO;
|
|
18
|
25
|
19 import java.util.concurrent.ExecutorService;
|
|
20 import java.util.concurrent.Executors;
|
|
21 import java.io.OutputStream;
|
7
|
22
|
|
23 class MyRfbProto extends RfbProto {
|
8
|
24
|
27
|
25 final static String versionMsg_3_998 = "RFB 003.998\n";
|
|
26
|
|
27
|
15
|
28 private int messageType;
|
|
29 private int rectangles;
|
19
|
30 private int rectX;
|
|
31 private int rectY;
|
|
32 private int rectW;
|
|
33 private int rectH;
|
15
|
34 private int encoding;
|
19
|
35 private int zLen;
|
25
|
36 private int dataLen;
|
15
|
37
|
19
|
38 private ServerSocket servSock;
|
18
|
39 private int acceptPort;
|
8
|
40 private byte initData[];
|
19
|
41 private LinkedList <Socket> cliListTmp;
|
10
|
42 private LinkedList <Socket> cliList;
|
25
|
43 private LinkedList <Thread> sendThreads;
|
19
|
44 boolean createBimgFlag;
|
25
|
45
|
34
|
46 echoClient echo;
|
|
47
|
25
|
48 ExecutorService executor;
|
|
49
|
|
50
|
18
|
51 byte[] pngBytes;
|
25
|
52
|
15
|
53 MyRfbProto(String h, int p, VncViewer v ) throws IOException {
|
7
|
54 super(h, p, v);
|
10
|
55 cliList = new LinkedList <Socket>();
|
19
|
56 cliListTmp = new LinkedList <Socket>();
|
|
57 createBimgFlag = false;
|
25
|
58 sendThreads = new LinkedList <Thread>();
|
|
59 // executor = Executors.newCachedThreadPool();
|
|
60 executor = Executors.newSingleThreadExecutor();
|
10
|
61 }
|
|
62
|
15
|
63 MyRfbProto(String h, int p) throws IOException {
|
|
64 super(h, p);
|
|
65 cliList = new LinkedList <Socket>();
|
19
|
66 cliListTmp = new LinkedList <Socket>();
|
|
67 createBimgFlag = false;
|
25
|
68 sendThreads = new LinkedList <Thread>();
|
|
69 // executor = Executors.newCachedThreadPool();
|
|
70 executor = Executors.newSingleThreadExecutor();
|
15
|
71 }
|
19
|
72
|
28
|
73 // over write
|
27
|
74 void writeVersionMsg() throws IOException {
|
|
75 clientMajor = 3;
|
|
76 if (serverMinor >= 9) {
|
|
77 clientMinor = 9;
|
|
78 os.write(versionMsg_3_998.getBytes());
|
|
79 } else if (serverMajor > 3 || serverMinor >= 8) {
|
|
80 clientMinor = 8;
|
|
81 os.write(versionMsg_3_8.getBytes());
|
|
82 } else if (serverMinor >= 9) {
|
|
83 clientMinor = 9;
|
|
84 os.write(versionMsg_3_998.getBytes());
|
|
85 } else if (serverMinor >= 7) {
|
|
86 clientMinor = 7;
|
|
87 os.write(versionMsg_3_7.getBytes());
|
|
88 } else {
|
|
89 clientMinor = 3;
|
|
90 os.write(versionMsg_3_3.getBytes());
|
|
91 }
|
|
92 protocolTightVNC = false;
|
|
93 initCapabilities();
|
|
94 }
|
|
95
|
|
96
|
|
97
|
10
|
98 void initServSock(int port) throws IOException{
|
|
99 servSock = new ServerSocket(port);
|
18
|
100 acceptPort = port;
|
|
101 }
|
|
102 void selectPort(){
|
|
103 int i = 5550;
|
|
104 while(true){
|
|
105 try{
|
|
106 initServSock(i);
|
|
107 break;
|
|
108 }catch(BindException e){
|
|
109 i++;
|
|
110 continue;
|
|
111 }catch(IOException e){
|
19
|
112
|
18
|
113 }
|
|
114 }
|
25
|
115 System.out.println("accept port = "+i);
|
18
|
116 }
|
|
117 int getAcceptPort(){
|
|
118 return acceptPort;
|
7
|
119 }
|
10
|
120 void setSoTimeout(int num) throws IOException {
|
|
121 servSock.setSoTimeout(num);
|
|
122 }
|
|
123
|
|
124 Socket accept() throws IOException {
|
|
125 return servSock.accept();
|
|
126 }
|
|
127
|
|
128 void addSock(Socket sock){
|
|
129 cliList.add(sock);
|
|
130 }
|
19
|
131 void addSockTmp(Socket sock){
|
22
|
132 System.out.println("connected "+sock.getInetAddress());
|
19
|
133 cliListTmp.add(sock);
|
|
134 }
|
10
|
135
|
7
|
136 void mark(int len) throws IOException {
|
|
137 is.mark(len);
|
|
138 }
|
|
139
|
|
140 void reset() throws IOException {
|
|
141 is.reset();
|
15
|
142 }
|
7
|
143
|
8
|
144 boolean markSupported() {
|
7
|
145 return is.markSupported();
|
|
146 }
|
8
|
147
|
|
148 void readServerInit() throws IOException {
|
|
149
|
|
150 mark(255);
|
|
151 skipBytes(20);
|
|
152 int nlen = readU32();
|
10
|
153 int blen = 20+4+nlen;
|
|
154 initData = new byte[blen];
|
|
155 reset();
|
|
156
|
|
157 mark(blen);
|
8
|
158 readFully(initData);
|
|
159 reset();
|
|
160
|
7
|
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();
|
8
|
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);
|
7
|
179
|
8
|
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;
|
7
|
192 }
|
|
193
|
27
|
194 void sendRfbVersion(OutputStream os) throws IOException{
|
|
195 os.write(versionMsg_3_998.getBytes());
|
|
196 }
|
28
|
197 void readVersionMsg(InputStream is) throws IOException {
|
|
198
|
|
199 byte[] b = new byte[12];
|
|
200
|
|
201 is.read(b);
|
|
202
|
|
203 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
204 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
205 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
206 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
207 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
208 throw new IOException("Host " + host + " port " + port
|
|
209 + " is not an RFB server");
|
|
210 }
|
|
211
|
|
212 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
213 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
214
|
|
215 if (serverMajor < 3) {
|
|
216 throw new IOException(
|
|
217 "RFB server does not support protocol version 3");
|
|
218 }
|
|
219
|
|
220 }
|
27
|
221 void sendSecurityType(OutputStream os) throws IOException {
|
|
222 // number-of-security-types
|
|
223 os.write(1);
|
|
224 // security-types
|
|
225 // 1:None
|
|
226 os.write(1);
|
|
227 }
|
28
|
228 void readSecType(InputStream is) throws IOException {
|
|
229 byte[] b = new byte[1];
|
|
230 is.read(b);
|
|
231
|
|
232 }
|
|
233 void sendSecResult(OutputStream os) throws IOException {
|
|
234 byte[] b = castIntByte(0);
|
|
235 os.write(b);
|
|
236 }
|
|
237
|
27
|
238 void readClientInit(InputStream in) throws IOException {
|
|
239 byte[] b = new byte[0];
|
|
240 in.read(b);
|
|
241 }
|
|
242
|
|
243 void sendInitData(OutputStream os) throws IOException{
|
|
244 os.write(initData);
|
8
|
245 }
|
10
|
246
|
15
|
247 void sendData(byte b[]){
|
|
248 try{
|
|
249 for(Socket cli : cliList){
|
|
250 try{
|
|
251 cli.getOutputStream().write(b, 0, b.length);
|
|
252 }catch(IOException e){
|
34
|
253 //insert
|
|
254
|
|
255 System.out.println("lostchild");
|
|
256 if("1".equals(echoClient.value.leaderflag)){
|
|
257 echo = new echoClient();
|
|
258 echo.openport();
|
|
259 echo.lostchild();
|
|
260 }
|
15
|
261 // if socket closed
|
|
262 cliList.remove(cli);
|
|
263 }
|
|
264 }
|
19
|
265 // System.out.println("cliSize="+cliSize());
|
15
|
266 }catch(Exception e){
|
|
267 }
|
19
|
268 }
|
|
269
|
|
270 void sendPngImage(){
|
|
271 try{
|
|
272 for(Socket cli : cliListTmp){
|
|
273 try{
|
|
274 sendPngData(cli);
|
|
275 addSock(cli);
|
|
276 }catch(IOException e){
|
|
277 // if socket closed
|
|
278 cliListTmp.remove(cli);
|
|
279 }
|
|
280 }
|
|
281 // System.out.println("cliSize="+cliSize());
|
|
282 }catch(Exception e){
|
|
283 }
|
|
284 cliListTmp.clear();
|
|
285 }
|
25
|
286
|
|
287
|
19
|
288
|
15
|
289 boolean ready() throws IOException {
|
|
290 BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
291 return br.ready();
|
10
|
292 }
|
|
293
|
|
294 int cliSize(){
|
|
295 return cliList.size();
|
|
296 }
|
15
|
297 void printNumBytesRead(){
|
|
298 System.out.println("numBytesRead="+numBytesRead);
|
|
299 }
|
|
300 void bufResetSend(int size) throws IOException {
|
|
301 reset();
|
|
302 int len = size;
|
|
303 if(available() < size )
|
|
304 len = available();
|
|
305 byte buffer[] = new byte[len];
|
|
306 readFully(buffer);
|
|
307 sendData(buffer);
|
|
308 }
|
25
|
309 void readSendData()throws IOException{
|
|
310 byte buffer[] = new byte[dataLen];
|
|
311 readFully(buffer);
|
|
312 reset();
|
|
313
|
|
314 for(Socket cli : cliList){
|
|
315 try{
|
|
316 OutputStream out = cli.getOutputStream();
|
|
317 executor.execute(new SendThread(out, buffer));
|
|
318 }catch(IOException e){
|
|
319 // if client socket closed
|
|
320 cliListTmp.remove(cli);
|
|
321 }catch(Exception e){
|
|
322
|
|
323 }
|
|
324
|
|
325 }
|
|
326 }
|
15
|
327 void regiFramebufferUpdate()throws IOException{
|
19
|
328 mark(20);
|
15
|
329 messageType = readU8();
|
|
330 skipBytes(1);
|
|
331 rectangles = readU16();
|
19
|
332 rectX = readU16();
|
|
333 rectY = readU16();
|
|
334 rectW = readU16();
|
|
335 rectH = readU16();
|
|
336 encoding = readU32();
|
|
337 if(encoding == 16)
|
|
338 zLen = readU32();
|
|
339 reset();
|
|
340 }
|
|
341 void checkAndMark() throws IOException{
|
|
342 switch(encoding){
|
|
343 case RfbProto.EncodingRaw:
|
25
|
344 dataLen = rectW * rectH * 4 + 16;
|
|
345 mark(dataLen);
|
19
|
346 break;
|
|
347 case RfbProto.EncodingZRLE:
|
25
|
348 dataLen = zLen+20;
|
|
349 mark(dataLen);
|
19
|
350 break;
|
|
351 default:
|
|
352 mark(1000000);
|
|
353 }
|
|
354 }
|
|
355 BufferedImage createBufferedImage(Image img){
|
|
356 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB );
|
|
357
|
|
358 Graphics g = bimg.getGraphics();
|
|
359 g.drawImage(img, 0, 0, null);
|
|
360 g.dispose();
|
|
361 return bimg;
|
|
362 }
|
|
363
|
|
364 void createPngBytes(BufferedImage bimg)throws IOException {
|
|
365 pngBytes = getImageBytes(bimg , "png");
|
|
366 }
|
|
367 byte[] getBytes(BufferedImage img)throws IOException {
|
|
368 byte[] b = getImageBytes(img, "png");
|
|
369 return b;
|
15
|
370 }
|
18
|
371
|
19
|
372 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException {
|
|
373 ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
374 BufferedOutputStream os = new BufferedOutputStream(bos);
|
|
375 image.flush();
|
|
376 ImageIO.write(image, imageFormat, os);
|
|
377 os.flush();
|
|
378 os.close();
|
|
379 return bos.toByteArray();
|
|
380 }
|
|
381
|
|
382 void sendPngData(Socket sock)throws IOException{
|
|
383 byte[] dataLength = castIntByte(pngBytes.length);
|
|
384 sock.getOutputStream().write(dataLength);
|
|
385 sock.getOutputStream().write(pngBytes);
|
|
386 }
|
|
387 byte[] castIntByte(int len){
|
|
388 byte[] b = new byte[4];
|
|
389 b[0] = (byte)((len >>> 24 ) & 0xFF);
|
|
390 b[1] = (byte)((len >>> 16 ) & 0xFF);
|
|
391 b[2] = (byte)((len >>> 8 ) & 0xFF);
|
|
392 b[3] = (byte)((len >>> 0 ) & 0xFF);
|
|
393 return b;
|
|
394 }
|
18
|
395
|
19
|
396 BufferedImage createBimg()throws IOException{
|
|
397 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
|
|
398 return bimg;
|
|
399 }
|
25
|
400 void readPngData()throws IOException{
|
27
|
401 pngBytes = new byte[is.available()];
|
25
|
402 readFully(pngBytes);
|
|
403 }
|
15
|
404 void printFramebufferUpdate(){
|
|
405
|
|
406 System.out.println("messageType=" + messageType);
|
|
407 System.out.println("rectangles="+rectangles);
|
|
408 System.out.println("encoding=" + encoding);
|
19
|
409 switch(encoding){
|
|
410 case RfbProto.EncodingRaw:
|
|
411 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
|
|
412 break;
|
|
413 default:
|
|
414 }
|
15
|
415 }
|
7
|
416 }
|
27
|
417
|
|
418
|