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