0
|
1 package treeVnc;
|
|
2
|
|
3 import static org.junit.Assert.*;
|
|
4
|
|
5 import java.awt.Graphics;
|
|
6 import java.awt.Image;
|
|
7 import java.awt.image.BufferedImage;
|
|
8 import java.io.BufferedOutputStream;
|
|
9 import java.io.BufferedReader;
|
|
10 import java.io.ByteArrayInputStream;
|
|
11 import java.io.ByteArrayOutputStream;
|
|
12 import java.io.IOException;
|
|
13 import java.io.InputStream;
|
|
14 import java.io.InputStreamReader;
|
|
15 import java.net.BindException;
|
1
|
16 import java.net.DatagramPacket;
|
|
17 import java.net.InetAddress;
|
|
18 import java.net.MulticastSocket;
|
0
|
19 import java.net.ServerSocket;
|
|
20 import java.net.Socket;
|
|
21 import java.nio.ByteBuffer;
|
|
22 import java.util.Iterator;
|
|
23 import java.util.LinkedList;
|
|
24
|
|
25 import javax.imageio.ImageIO;
|
|
26
|
|
27 import org.junit.Test;
|
|
28
|
|
29 //import myVncProxy.MulticastQueue.Client;
|
|
30
|
|
31 import java.util.concurrent.ExecutorService;
|
|
32 import java.util.concurrent.atomic.AtomicInteger;
|
|
33 import java.util.zip.DataFormatException;
|
|
34 import java.util.zip.Deflater;
|
|
35 import java.util.zip.Inflater;
|
|
36 import java.io.OutputStream;
|
|
37
|
|
38 public class MyRfbProtoProxy extends RfbProto implements MyRfbProto {
|
|
39 final static String versionMsg_3_855 = "RFB 003.855\n";
|
|
40 /**
|
|
41 * CheckMillis is one of new msgType for RFB 3.855.
|
|
42 */
|
|
43 final static byte SpeedCheckMillis = 4;
|
|
44
|
|
45 // Secyrity type of OS X
|
|
46 final static int SecTypeReqAccess = 32;
|
|
47
|
|
48 // Supported authentication types
|
|
49 final static int AuthAccess = 32;
|
|
50
|
|
51 private static final int INFLATE_BUFSIZE = 1024 * 100;
|
|
52 boolean printStatusFlag = false;
|
|
53 long startCheckTime;
|
|
54 private int messageType;
|
|
55 private int rectangles;
|
|
56 private int rectX;
|
|
57 private int rectY;
|
|
58 private int rectW;
|
|
59 private int rectH;
|
|
60 private int encoding;
|
|
61 private int zLen;
|
|
62 private boolean clicomp = false;
|
|
63
|
|
64 private ServerSocket servSock;
|
|
65 protected int acceptPort;
|
|
66 // private byte initData[];
|
|
67 byte initData[];
|
|
68 private LinkedList<Socket> cliListTmp;
|
|
69 private LinkedList<Socket> cliList;
|
|
70 boolean createBimgFlag;
|
|
71 boolean proxyFlag = true;
|
|
72
|
|
73 ExecutorService executor;
|
|
74
|
|
75 byte[] pngBytes;
|
|
76
|
|
77 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new
|
|
78 // MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
|
|
79 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
|
|
80 private int clients = 0;
|
|
81 private Inflater inflater = new Inflater();
|
|
82 private Deflater deflater = new Deflater();
|
|
83 private CreateThread geth;
|
|
84 // private Thread requestThread;
|
|
85 private RequestScreenThread rThread;
|
|
86 private Thread requestThread;
|
|
87
|
|
88 public MyRfbProtoProxy() throws IOException {
|
|
89 }
|
|
90
|
|
91 MyRfbProtoProxy(String h, int p, VncViewer v) throws IOException {
|
|
92 super(h, p, v);
|
|
93
|
|
94 rThread = new RequestScreenThread(this);
|
|
95 requestThread = new Thread(rThread);
|
|
96 // requestThread = new Thread(new RequestScreenThread(this));
|
|
97 }
|
|
98
|
|
99 MyRfbProtoProxy(String h, int p, CreateThread geth) throws IOException {
|
|
100 super(h, p);
|
|
101 this.geth = geth;
|
|
102 proxyFlag = true;
|
|
103
|
|
104 rThread = new RequestScreenThread(this);
|
|
105 requestThread = new Thread(rThread);
|
|
106 // requestThread = new Thread(new RequestScreenThread(this));
|
|
107 }
|
|
108
|
|
109 MyRfbProtoProxy(String h, int p) throws IOException {
|
|
110 super(h, p);
|
|
111
|
|
112 rThread = new RequestScreenThread(this);
|
|
113 requestThread = new Thread(rThread);
|
|
114 // requestThread = new Thread(new RequestScreenThread(this));
|
|
115 }
|
|
116
|
|
117 // over write
|
|
118 void writeVersionMsg() throws IOException {
|
|
119 clientMajor = 3;
|
|
120 if (serverMinor == 855) {
|
|
121 clientMinor = 855;
|
|
122 os.write(versionMsg_3_855.getBytes());
|
|
123 } else if (serverMajor > 3 || serverMinor >= 8) {
|
|
124 clientMinor = 8;
|
|
125 os.write(versionMsg_3_8.getBytes());
|
|
126 } else if (serverMinor >= 7) {
|
|
127 clientMinor = 7;
|
|
128 os.write(versionMsg_3_7.getBytes());
|
|
129 } else {
|
|
130 clientMinor = 3;
|
|
131 os.write(versionMsg_3_3.getBytes());
|
|
132 }
|
|
133 protocolTightVNC = false;
|
|
134 initCapabilities();
|
|
135 }
|
|
136
|
|
137 void initServSock(int port) throws IOException {
|
|
138 servSock = new ServerSocket(port);
|
|
139 acceptPort = port;
|
|
140 }
|
|
141
|
|
142 void authenticationRequestAccess() throws IOException {
|
|
143
|
|
144 byte[] headBuf = new byte[2];
|
|
145 is.read(headBuf);
|
|
146 if (headBuf[1] == 2) {
|
|
147 byte[] b = new byte[258];
|
|
148 is.read(b);
|
|
149
|
|
150 byte[] outBuf = new byte[256];
|
|
151 os.write(outBuf);
|
|
152 os.flush();
|
|
153 } else if (headBuf[1] == 23) {
|
|
154 byte[] b = new byte[130];
|
|
155 is.read(b);
|
|
156 byte[] outBuf = new byte[192];
|
|
157 os.write(outBuf);
|
|
158 os.flush();
|
|
159 }
|
|
160
|
|
161 int result = readU32();
|
|
162 if (result != 0) {
|
|
163 System.out.println("faild authentication ");
|
|
164 throw new IOException();
|
|
165 }
|
|
166
|
|
167 }
|
|
168
|
|
169 /*
|
|
170 * default port number is 5999.
|
|
171 */
|
|
172 public void selectPort(int p) {
|
|
173 if (servSock != null)
|
|
174 return;
|
|
175 int port = p;
|
|
176 while (true) {
|
|
177 try {
|
|
178 initServSock(port);
|
|
179 break;
|
|
180 } catch (BindException e) {
|
|
181 port++;
|
|
182 continue;
|
|
183 } catch (IOException e) {
|
|
184
|
|
185 }
|
|
186 }
|
|
187 System.out.println("accept port = " + port);
|
|
188 }
|
|
189
|
|
190 int getAcceptPort() {
|
|
191 return acceptPort;
|
|
192 }
|
|
193
|
|
194 void setSoTimeout(int num) throws IOException {
|
|
195 servSock.setSoTimeout(num);
|
|
196 }
|
|
197
|
|
198 public Socket accept() throws IOException {
|
|
199 return servSock.accept();
|
|
200 }
|
|
201
|
|
202 void addSock(Socket sock) {
|
|
203 cliList.add(sock);
|
|
204 }
|
|
205
|
|
206 void addSockTmp(Socket sock) {
|
|
207 System.out.println("connected " + sock.getInetAddress());
|
|
208 cliListTmp.add(sock);
|
|
209 }
|
|
210
|
|
211 boolean markSupported() {
|
|
212 return is.markSupported();
|
|
213 }
|
|
214
|
|
215 void readServerInit() throws IOException {
|
|
216
|
|
217 is.mark(255);
|
|
218 skipBytes(20);
|
|
219 int nlen = readU32();
|
|
220 int blen = 20 + 4 + nlen;
|
|
221 initData = new byte[blen];
|
|
222 is.reset();
|
|
223
|
|
224 is.mark(blen);
|
|
225 readFully(initData);
|
|
226 is.reset();
|
|
227
|
|
228 framebufferWidth = readU16();
|
|
229 framebufferHeight = readU16();
|
|
230 bitsPerPixel = readU8();
|
|
231 depth = readU8();
|
|
232 bigEndian = (readU8() != 0);
|
|
233 trueColour = (readU8() != 0);
|
|
234 redMax = readU16();
|
|
235 greenMax = readU16();
|
|
236 blueMax = readU16();
|
|
237 redShift = readU8();
|
|
238 greenShift = readU8();
|
|
239 blueShift = readU8();
|
|
240 byte[] pad = new byte[3];
|
|
241 readFully(pad);
|
|
242 int nameLength = readU32();
|
|
243 byte[] name = new byte[nameLength];
|
|
244 readFully(name);
|
|
245 desktopName = new String(name);
|
|
246
|
|
247 // Read interaction capabilities (TightVNC protocol extensions)
|
|
248 if (protocolTightVNC) {
|
|
249 int nServerMessageTypes = readU16();
|
|
250 int nClientMessageTypes = readU16();
|
|
251 int nEncodingTypes = readU16();
|
|
252 readU16();
|
|
253 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
254 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
255 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
256 }
|
|
257
|
|
258 inNormalProtocol = true;
|
|
259 }
|
|
260
|
|
261 void sendRfbVersion(OutputStream os) throws IOException {
|
|
262 // os.write(versionMsg_3_8.getBytes());
|
|
263 os.write(versionMsg_3_855.getBytes());
|
|
264 }
|
|
265
|
|
266 int readVersionMsg(InputStream is, OutputStream os) throws IOException {
|
|
267
|
|
268 byte[] b = new byte[12];
|
|
269
|
|
270 is.read(b);
|
|
271
|
|
272 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
273 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
274 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
275 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
276 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
277 throw new IOException("Host " + host + " port " + port
|
|
278 + " is not an RFB server");
|
|
279 }
|
|
280
|
|
281 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
282 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
283
|
|
284 if (rfbMajor < 3) {
|
|
285 throw new IOException(
|
|
286 "RFB server does not support protocol version 3");
|
|
287 }
|
|
288
|
|
289 if (rfbMinor == 855) {
|
|
290 sendProxyFlag(os);
|
|
291 if (proxyFlag)
|
|
292 sendPortNumber(os);
|
|
293 }
|
|
294 return rfbMinor;
|
|
295 }
|
|
296
|
|
297 void sendProxyFlag(OutputStream os) throws IOException {
|
|
298 if (proxyFlag)
|
|
299 os.write(1);
|
|
300 else
|
|
301 os.write(0);
|
|
302 }
|
|
303
|
|
304 boolean readProxyFlag() throws IOException {
|
|
305 int flag = readU8();
|
|
306 if (flag == 1)
|
|
307 return true;
|
|
308 else
|
|
309 return false;
|
|
310 }
|
|
311
|
|
312 void sendPortNumber(OutputStream os) throws IOException {
|
|
313 byte[] b = new byte[4];
|
|
314 b = castIntByte(geth.port);
|
|
315 os.write(b);
|
|
316 }
|
|
317
|
|
318 void sendSecurityType(OutputStream os) throws IOException {
|
|
319 // number-of-security-types
|
|
320 os.write(1);
|
|
321 // security-types
|
|
322 // 1:None
|
|
323 os.write(1);
|
|
324
|
|
325 /*
|
|
326 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35);
|
|
327 * os.flush();
|
|
328 */
|
|
329 }
|
|
330
|
|
331 void readSecType(InputStream is) throws IOException {
|
|
332 byte[] b = new byte[1];
|
|
333 is.read(b);
|
|
334 }
|
|
335
|
|
336 void readSecType(InputStream is, OutputStream os) throws IOException {
|
|
337 byte[] b = new byte[1];
|
|
338 is.read(b);
|
|
339
|
|
340 int count = 260;
|
|
341 int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15,
|
|
342 -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28,
|
|
343 -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59,
|
|
344 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25,
|
|
345 -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79,
|
|
346 -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94,
|
|
347 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74,
|
|
348 -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82,
|
|
349 -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83,
|
|
350 -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57,
|
|
351 -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105,
|
|
352 -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117,
|
|
353 -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112,
|
|
354 -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120,
|
|
355 -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51,
|
|
356 -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102,
|
|
357 -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3,
|
|
358 -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93,
|
|
359 -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67,
|
|
360 -98, -56, -3, 85, -67, -126, 77 };
|
|
361 for (int i = 0; i < count; i++) {
|
|
362 os.write((byte) data[i]);
|
|
363 os.flush();
|
|
364 }
|
|
365
|
|
366 byte[] c = new byte[256];
|
|
367 is.read(c);
|
|
368
|
|
369 System.out.println(new String(c));
|
|
370
|
|
371 }
|
|
372
|
|
373 void sendSecResult(OutputStream os) throws IOException {
|
|
374 byte[] b = castIntByte(0);
|
|
375 os.write(b);
|
|
376 }
|
|
377
|
|
378 void readClientInit(InputStream in) throws IOException {
|
|
379 byte[] b = new byte[0];
|
|
380 in.read(b);
|
|
381 }
|
|
382
|
|
383 void sendInitData(OutputStream os) throws IOException {
|
|
384 os.write(initData);
|
|
385 }
|
|
386
|
|
387 void sendPngImage() {
|
|
388 try {
|
|
389 for (Socket cli : cliListTmp) {
|
|
390 try {
|
|
391 sendPngData(cli);
|
|
392 addSock(cli);
|
|
393 } catch (IOException e) {
|
|
394 // if socket closed
|
|
395 cliListTmp.remove(cli);
|
|
396 }
|
|
397 }
|
|
398 // System.out.println("cliSize="+cliSize());
|
|
399 } catch (Exception e) {
|
|
400 }
|
|
401 cliListTmp.clear();
|
|
402 }
|
|
403
|
|
404 boolean ready() throws IOException {
|
|
405 BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
406 return br.ready();
|
|
407 }
|
|
408
|
|
409 int cliSize() {
|
|
410 return cliList.size();
|
|
411 }
|
|
412
|
|
413 void printNumBytesRead() {
|
|
414 System.out.println("numBytesRead=" + numBytesRead);
|
|
415 }
|
|
416
|
|
417 void regiFramebufferUpdate() throws IOException {
|
|
418 is.mark(20);
|
|
419 messageType = readU8(); // 0
|
|
420 skipBytes(1); // 1
|
|
421 rectangles = readU16(); // 2
|
|
422 rectX = readU16(); // 4
|
|
423 rectY = readU16(); // 6
|
|
424 rectW = readU16(); // 8
|
|
425 rectH = readU16(); // 10
|
|
426 encoding = readU32(); // 12
|
|
427 // System.out.println("encoding = "+encoding);
|
|
428 if (encoding == EncodingZRLE || encoding == EncodingZRLEE
|
|
429 || encoding == EncodingZlib)
|
|
430 zLen = readU32();
|
|
431 else
|
|
432 zLen = 0;
|
|
433 //System.out.println(zLen);
|
|
434 is.reset();
|
|
435
|
|
436 }
|
|
437
|
|
438 int checkAndMark() throws IOException {
|
|
439 int dataLen;
|
|
440 switch (encoding) {
|
|
441 case RfbProto.EncodingRaw:
|
|
442 dataLen = rectW * rectH * 4 + 16;
|
|
443 // is.mark(dataLen);
|
|
444 break;
|
|
445 case RfbProto.EncodingCopyRect:
|
|
446 dataLen = 16 + 4;
|
|
447 // is.mark(dataLen);
|
|
448 break;
|
|
449 case RfbProto.EncodingRRE:
|
|
450 case RfbProto.EncodingCoRRE:
|
|
451 case RfbProto.EncodingHextile:
|
|
452 case RfbProto.EncodingTight:
|
|
453 dataLen = zLen + 20;
|
|
454 // is.mark(dataLen);
|
|
455 break;
|
|
456 case RfbProto.EncodingZlib:
|
|
457 case RfbProto.EncodingZRLE:
|
|
458 case RfbProto.EncodingZRLEE:
|
|
459 dataLen = zLen + 20;
|
|
460 // is.mark(dataLen);
|
|
461 break;
|
|
462 case RfbProto.EncodingXCursor:
|
|
463 case RfbProto.EncodingRichCursor:
|
|
464 int pixArray = rectW * rectH * 4;
|
|
465 int u8Array = (int) Math.floor((rectW + 7) / 8) * rectH;
|
|
466 dataLen = pixArray + u8Array;
|
|
467 printFramebufferUpdate();
|
|
468 // is.mark(dataLen);
|
|
469 break;
|
|
470 default:
|
|
471 dataLen = 1000000;
|
|
472 // is.mark(dataLen);
|
|
473 }
|
|
474 return dataLen;
|
|
475 }
|
|
476
|
|
477 void sendDataToClient() throws Exception {
|
|
478 regiFramebufferUpdate();
|
|
479 printFramebufferUpdate();
|
|
480 int dataLen = checkAndMark();
|
|
481 readSendData(dataLen);
|
|
482 }
|
|
483
|
|
484 BufferedImage createBufferedImage(Image img) {
|
|
485 BufferedImage bimg = new BufferedImage(img.getWidth(null),
|
|
486 img.getHeight(null), BufferedImage.TYPE_INT_RGB);
|
|
487
|
|
488 Graphics g = bimg.getGraphics();
|
|
489 g.drawImage(img, 0, 0, null);
|
|
490 g.dispose();
|
|
491 return bimg;
|
|
492 }
|
|
493
|
|
494 void createPngBytes(BufferedImage bimg) throws IOException {
|
|
495 pngBytes = getImageBytes(bimg, "png");
|
|
496 }
|
|
497
|
|
498 byte[] getBytes(BufferedImage img) throws IOException {
|
|
499 byte[] b = getImageBytes(img, "png");
|
|
500 return b;
|
|
501 }
|
|
502
|
|
503 byte[] getImageBytes(BufferedImage image, String imageFormat)
|
|
504 throws IOException {
|
|
505 ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
506 BufferedOutputStream os = new BufferedOutputStream(bos);
|
|
507 image.flush();
|
|
508 ImageIO.write(image, imageFormat, os);
|
|
509 os.flush();
|
|
510 os.close();
|
|
511 return bos.toByteArray();
|
|
512 }
|
|
513
|
|
514 void sendPngData(Socket sock) throws IOException {
|
|
515 byte[] dataLength = castIntByte(pngBytes.length);
|
|
516 sock.getOutputStream().write(dataLength);
|
|
517 sock.getOutputStream().write(pngBytes);
|
|
518 }
|
|
519
|
|
520 byte[] castIntByte(int len) {
|
|
521 byte[] b = new byte[4];
|
|
522 b[0] = (byte) ((len >>> 24) & 0xFF);
|
|
523 b[1] = (byte) ((len >>> 16) & 0xFF);
|
|
524 b[2] = (byte) ((len >>> 8) & 0xFF);
|
|
525 b[3] = (byte) ((len >>> 0) & 0xFF);
|
|
526 return b;
|
|
527 }
|
|
528
|
|
529 BufferedImage createBimg() throws IOException {
|
|
530 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
|
|
531 return bimg;
|
|
532 }
|
|
533
|
|
534 void printFramebufferUpdate() {
|
|
535 /*
|
|
536 * System.out.println("messageType=" + messageType);
|
|
537 * System.out.println("rectangles=" + rectangles);
|
|
538 * System.out.println("encoding=" + encoding);
|
|
539 * System.out.println("rectX = "+rectX+": rectY = "+rectY);
|
|
540 * System.out.println("rectW = "+rectW+": rectH = "+rectH);
|
|
541 */
|
|
542 switch (encoding) {
|
|
543 case RfbProto.EncodingRaw:
|
|
544 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4
|
|
545 + 16);
|
|
546 break;
|
|
547 default:
|
|
548 }
|
|
549 }
|
|
550
|
|
551 int returnMsgtype() {
|
|
552 return messageType;
|
|
553 }
|
|
554
|
|
555 void readSpeedCheck() throws IOException {
|
|
556 byte[] b = new byte[1];
|
|
557 readFully(b);
|
|
558 }
|
|
559
|
|
560 void startSpeedCheck() {
|
|
561 ByteBuffer b = ByteBuffer.allocate(10);
|
|
562 b.put((byte) SpeedCheckMillis);
|
|
563 b.flip();
|
|
564 startCheckTime = System.currentTimeMillis();
|
|
565 System.out.println("startChckTime = " + startCheckTime);
|
|
566 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
|
|
567 bufs.add(b);
|
|
568 multicastqueue.put(bufs);
|
|
569 }
|
|
570
|
|
571 void endSpeedCheck() {
|
|
572 long accTime = System.currentTimeMillis();
|
|
573 long time = accTime - startCheckTime;
|
|
574 System.out.println("checkMillis: " + time);
|
|
575 }
|
|
576
|
|
577 synchronized void changeStatusFlag() {
|
|
578 printStatusFlag = true;
|
|
579 }
|
|
580
|
|
581 void printMills() {
|
|
582 if (printStatusFlag) {
|
|
583
|
|
584 changeStatusFlag();
|
|
585 } else {
|
|
586 changeStatusFlag();
|
|
587 }
|
|
588 }
|
|
589
|
|
590 void speedCheckMillis() {
|
|
591 Runnable stdin = new Runnable() {
|
|
592 public void run() {
|
|
593 int c;
|
|
594 try {
|
|
595 while ((c = System.in.read()) != -1) {
|
|
596 switch (c) {
|
|
597 case 's':
|
|
598 break;
|
|
599 default:
|
|
600 startSpeedCheck();
|
|
601 break;
|
|
602 }
|
|
603 }
|
|
604 } catch (IOException e) {
|
|
605 System.out.println(e);
|
|
606 }
|
|
607 }
|
|
608 };
|
|
609
|
|
610 new Thread(stdin).start();
|
|
611 }
|
|
612
|
|
613 void requestThreadStart() {
|
|
614 requestThread.start();
|
|
615 }
|
|
616
|
|
617 public synchronized void requestThreadNotify() {
|
|
618 rThread.reStart();
|
|
619 }
|
|
620
|
|
621 /**
|
|
622 * gzip byte arrays
|
|
623 *
|
|
624 * @param deflater
|
|
625 * @param inputs
|
|
626 * byte data[]
|
|
627 * @param inputIndex
|
|
628 * @param outputs
|
|
629 * byte data[]
|
|
630 * @return byte length in last byte array
|
|
631 * @throws IOException
|
|
632 */
|
|
633 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs,
|
|
634 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
|
|
635 int len = 0;
|
|
636 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
637 while (inputIndex < inputs.size()) {
|
|
638 ByteBuffer b1 = inputs.get(inputIndex++);
|
|
639 deflater.setInput(b1.array(), b1.position(), b1.remaining());
|
|
640 /**
|
|
641 * If we finish() stream and reset() it, Deflater start new gzip
|
|
642 * stream, this makes continuous zlib reader unhappy. if we remove
|
|
643 * finish(), Deflater.deflate() never flushes its output. The
|
|
644 * original zlib deflate has flush flag. I'm pretty sure this a kind
|
|
645 * of bug of Java library.
|
|
646 */
|
|
647 if (inputIndex == inputs.size())
|
|
648 deflater.finish();
|
|
649 int len1 = 0;
|
|
650 do {
|
|
651 len1 = deflater.deflate(c1.array(), c1.position(),
|
|
652 c1.remaining());
|
|
653 if (len1 > 0) {
|
|
654 len += len1;
|
|
655 c1.position(c1.position() + len1);
|
|
656 if (c1.remaining() == 0) {
|
|
657 c1.flip();
|
|
658 outputs.addLast(c1);
|
|
659 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
660 }
|
|
661 }
|
|
662 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished());
|
|
663 }
|
|
664 if (c1.position() != 0) {
|
|
665 c1.flip();
|
|
666 outputs.addLast(c1);
|
|
667 }
|
|
668 deflater.reset();
|
|
669 return len;
|
|
670 }
|
|
671
|
|
672 /**
|
|
673 * gunzip byte arrays
|
|
674 *
|
|
675 * @param inflater
|
|
676 * @param inputs
|
|
677 * byte data[]
|
|
678 * @param outputs
|
|
679 * byte data[]
|
|
680 * @return number of total bytes
|
|
681 * @throws IOException
|
|
682 */
|
|
683 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
|
|
684 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize)
|
|
685 throws DataFormatException {
|
|
686 int len = 0;
|
|
687 ByteBuffer buf = ByteBuffer.allocate(bufSize);
|
|
688 while (inputIndex < inputs.size()) {
|
|
689 ByteBuffer input = inputs.get(inputIndex++);
|
|
690 inflater.setInput(input.array(), input.position(), input.limit());
|
|
691 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry,
|
|
692 // we need this
|
|
693 // inflater.end(); but this won't work
|
|
694 do {
|
|
695 int len0 = inflater.inflate(buf.array(), buf.position(),
|
|
696 buf.remaining());
|
|
697 if (len0 > 0) {
|
|
698 buf.position(buf.position() + len0);
|
|
699 len += len0;
|
|
700 if (buf.remaining() == 0) {
|
|
701 buf.flip();
|
|
702 outputs.addLast(buf);
|
|
703 buf = ByteBuffer.allocate(bufSize);
|
|
704 }
|
|
705 }
|
|
706 } while (!inflater.needsInput());
|
|
707 }
|
|
708 if (buf.position() != 0) {
|
|
709 buf.flip();
|
|
710 outputs.addLast(buf);
|
|
711 }
|
|
712 return len;
|
|
713 }
|
|
714
|
|
715 float maxMag = 1;
|
|
716
|
|
717 /**
|
|
718 * send data to clients
|
|
719 *
|
|
720 * @param dataLen
|
|
721 * @throws IOException
|
|
722 * @throws DataFormatException
|
|
723 *
|
|
724 * Zlibed packet is compressed in context dependent way, that
|
|
725 * is, it have to send from the beginning. But this is
|
|
726 * impossible. So we have to compress it again for each clients.
|
|
727 * Separate deflater for each clients is necessary.
|
|
728 *
|
|
729 * Java's deflater does not support flush. This means to get the
|
|
730 * result, we have to finish the compression. Reseting start new
|
|
731 * compression, but it is not accepted well in zlib continuous
|
|
732 * reading. So we need new Encoding ZRLEE which reset decoder
|
|
733 * for each packet. ZRLEE can be invisible from user, but it
|
|
734 * have to be implemented in the clients. ZRLEE compression is
|
|
735 * not context dependent, so no recompression is necessary.
|
|
736 */
|
|
737 void sendDataCheckDelay() {
|
|
738 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
|
|
739 ByteBuffer b = ByteBuffer.allocate(1);
|
|
740 b.put((byte) CheckDelay);
|
|
741 b.position(0);
|
|
742 bufs.add(b);
|
|
743 multicastqueue.put(bufs);
|
|
744 }
|
|
745
|
|
746 void readSendData(int dataLen) throws IOException, DataFormatException {
|
|
747 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
|
|
748 ByteBuffer header = ByteBuffer.allocate(16);
|
|
749 readFully(header.array(), 0, 16);
|
|
750 header.limit(16);
|
|
751 if (header.get(0) == RfbProto.FramebufferUpdate) {
|
|
752 int encoding = header.getInt(12);
|
|
753 if (encoding == RfbProto.EncodingZRLE
|
|
754 || encoding == RfbProto.EncodingZlib) { // ZRLEE is already
|
|
755 // recompressed
|
|
756 ByteBuffer len = ByteBuffer.allocate(4);
|
|
757 readFully(len.array(), 0, 4);
|
|
758 len.limit(4);
|
|
759 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
|
|
760
|
|
761 startTiming();
|
|
762 readFully(inputData.array(), 0, inputData.capacity());
|
1
|
763 //System.out.println(dataLen);
|
0
|
764 inputData.limit(dataLen - 20);
|
|
765 stopTiming();
|
|
766
|
|
767 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
|
|
768 inputs.add(inputData);
|
|
769
|
|
770 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress
|
|
771 // every time
|
|
772 // using new Deflecter every time is incompatible with the
|
|
773 // protocol, clients have to be modified.
|
|
774 Deflater nDeflater = deflater; // new Deflater();
|
|
775 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
776 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
|
|
777 // dump32(inputs);
|
|
778 int len2 = zip(nDeflater, out, 0, bufs);
|
|
779 ByteBuffer blen = ByteBuffer.allocate(4);
|
|
780 blen.putInt(len2);
|
|
781 blen.flip();
|
|
782 bufs.addFirst(blen);
|
|
783
|
|
784 bufs.addFirst(header);
|
|
785 if(dataLen<=64000)
|
|
786 multicastqueue.put(bufs);
|
|
787 // is.reset();
|
|
788
|
|
789 /*
|
|
790 * System.out.println("ZRLE = "+dataLen);
|
|
791 * System.out.println("ZRLEE = "+(len2+20)); float mag =
|
|
792 * (float)dataLen / (float)(len2 + 20);
|
|
793 * System.out.println("ZRLE / ZRLEE = "+ mag); if(mag > maxMag)
|
|
794 * maxMag = mag; System.out.println("maxMag = "+maxMag);
|
|
795 */
|
|
796 return;
|
|
797 }
|
|
798 bufs.add(header);
|
|
799 if (dataLen > 16) {
|
|
800 ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
|
|
801 startTiming();
|
|
802 readFully(b.array(), 0, dataLen - 16);
|
|
803 b.limit(dataLen - 16);
|
|
804 stopTiming();
|
|
805 bufs.add(b);
|
|
806 }
|
|
807 multicastqueue.put(bufs);
|
|
808 // is.reset();
|
|
809 return;
|
|
810 }
|
|
811 is.reset();
|
|
812
|
|
813 // It may be compressed. We can inflate here to avoid repeating clients
|
|
814 // decompressing here,
|
|
815 // but it may generate too many large data. It is better to do it in
|
|
816 // each client.
|
|
817 // But we have do inflation for all input data, so we have to do it
|
|
818 // here.
|
|
819 }
|
|
820
|
|
821 public void newClient(AcceptThread acceptThread, final Socket newCli,
|
|
822 final OutputStream os, final InputStream is) throws IOException {
|
|
823 // createBimgFlag = true;
|
|
824 // rfb.addSockTmp(newCli);
|
|
825 // addSock(newCli);
|
|
826 final int myId = clients;
|
|
827 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue
|
|
828 .newClient();
|
|
829 final AtomicInteger writerRunning = new AtomicInteger();
|
|
830 writerRunning.set(1);
|
|
831 /**
|
|
832 * Timeout thread. If a client is suspended, it has top of queue
|
|
833 * indefinitely, which caused memory overflow. After the timeout, we
|
|
834 * poll the queue and discard it. Start long wait if writer is running.
|
|
835 */
|
|
836 final Runnable timer = new Runnable() {
|
|
837 public void run() {
|
|
838 int count = 0;
|
|
839 for (;;) {
|
|
840 long timeout = 50000 / 8;
|
|
841 try {
|
|
842 synchronized (this) {
|
|
843 int state, flag;
|
|
844 writerRunning.set(0);
|
|
845 wait(timeout);
|
|
846 flag = 0;
|
|
847 while ((state = writerRunning.get()) == 0) {
|
|
848 c.poll(); // discard, should be timeout
|
|
849 count++;
|
|
850 if (flag == 0) {
|
|
851 System.out.println("Discarding " + myId
|
|
852 + " count=" + count);
|
|
853 flag = 1;
|
|
854 }
|
|
855 wait(10); // if this is too short, writer cannot
|
|
856 // take the poll, if this is too
|
|
857 // long, memory will overflow...
|
|
858 }
|
|
859 if (flag == 1)
|
|
860 System.out.println("Resuming " + myId
|
|
861 + " count=" + count);
|
|
862 if (state != 1) {
|
|
863 System.out.println("Client died " + myId);
|
|
864 break;
|
|
865 }
|
|
866 }
|
|
867 } catch (InterruptedException e) {
|
|
868 }
|
|
869 }
|
|
870 }
|
|
871 };
|
|
872 new Thread(timer).start();
|
|
873 /**
|
|
874 * discard all incoming from clients
|
|
875 */
|
|
876 final Runnable reader = new Runnable() {
|
|
877 public void run() {
|
|
878 byte b[] = new byte[4096];
|
|
879 for (;;) {
|
|
880 try {
|
|
881 int c = is.read(b);
|
|
882 if (c <= 0)
|
|
883 throw new IOException();
|
|
884 // System.out.println("client read "+c);
|
|
885 } catch (IOException e) {
|
|
886 try {
|
|
887 writerRunning.set(2);
|
|
888 os.close();
|
|
889 is.close();
|
|
890 } catch (IOException e1) {
|
|
891 }
|
|
892 return;
|
|
893 }
|
|
894 }
|
|
895 }
|
|
896 };
|
|
897 /**
|
|
898 * send packets to a client
|
|
899 */
|
|
900 Runnable sender = new Runnable() {
|
|
901 public void run() {
|
|
902 writerRunning.set(1);
|
|
903 try {
|
|
904 requestThreadNotify();
|
|
905 // rThread.checkDelay();
|
|
906
|
|
907 /**
|
|
908 * initial connection of RFB protocol
|
|
909 */
|
|
910 sendRfbVersion(os);
|
|
911 // readVersionMsg(is);
|
|
912 int rfbMinor = readVersionMsg(is, os);
|
|
913 sendSecurityType(os);
|
|
914 readSecType(is);
|
|
915 sendSecResult(os);
|
|
916 readClientInit(is);
|
|
917 sendInitData(os);
|
|
918 new Thread(reader).start(); // discard incoming packet here
|
|
919 // after.
|
|
920 // writeFramebufferUpdateRequest(0,0, framebufferWidth,
|
|
921 // framebufferHeight, false );
|
|
922 int i = 0;
|
|
923 for (;;) {
|
|
924 LinkedList<ByteBuffer> bufs = c.poll();
|
|
925 int inputIndex = 0;
|
|
926 ByteBuffer header = bufs.get(inputIndex);
|
|
927 if (header == null)
|
|
928 continue;
|
|
929 else if (header.get(0) == RfbProto.CheckDelay) {
|
|
930 writeToClient(os, bufs, inputIndex);
|
|
931 continue;
|
|
932 } else if (header.get(0) == RfbProto.FramebufferUpdate) {
|
|
933 // System.out.println("client "+ myId);
|
|
934 }
|
|
935 /*
|
|
936 if(i%20==0){
|
|
937 sendDataCheckDelay();
|
|
938 }
|
|
939 i++;
|
|
940 */
|
|
941 writeToClient(os, bufs, inputIndex);
|
|
942 writerRunning.set(1); // yes my client is awaking.
|
|
943 }
|
|
944 } catch (IOException e) {
|
|
945 try {
|
|
946 writerRunning.set(2);
|
|
947 os.close();
|
|
948 } catch (IOException e1) {
|
|
949 }
|
|
950 /* if socket closed cliList.remove(newCli); */
|
|
951 }
|
|
952 }
|
1
|
953
|
|
954 void broadCastCommunication(byte[] buf,int len) {
|
|
955 String mcastAddr = "224.0.0.1";
|
|
956 int port = 8192;
|
|
957
|
|
958 try {
|
|
959 InetAddress mAddr = InetAddress.getByName(mcastAddr);
|
|
960 MulticastSocket soc = new MulticastSocket();
|
|
961 DatagramPacket sendPacket =null;
|
|
962 soc.setTimeToLive(100);
|
|
963
|
|
964 // System.out.println("Multicast = " + mcastAddr);
|
|
965 //while(buf != null) {
|
|
966 // int len = System.in.read(buf);
|
|
967 sendPacket = new DatagramPacket(buf,len,mAddr,port);
|
|
968 soc.send(sendPacket);
|
|
969 // }
|
|
970 soc.close();
|
|
971 } catch(IOException e) {
|
|
972 e.printStackTrace();
|
|
973 }
|
|
974
|
|
975 }
|
0
|
976
|
|
977 public void writeToClient(final OutputStream os,
|
|
978 LinkedList<ByteBuffer> bufs, int inputIndex)
|
|
979 throws IOException {
|
|
980 while (inputIndex < bufs.size()) {
|
|
981 ByteBuffer b = bufs.get(inputIndex++);
|
1
|
982 broadCastCommunication(b.array(),b.limit()-b.position());
|
|
983 // os.write(b.array(), b.position(), b.limit());
|
0
|
984 }
|
|
985 os.flush();
|
|
986 }
|
|
987 };
|
|
988 clients++;
|
|
989 new Thread(sender).start();
|
|
990
|
|
991 }
|
|
992
|
|
993 public void dump32(LinkedList<ByteBuffer> bufs) {
|
|
994 int len = 0;
|
|
995 for (ByteBuffer b : bufs)
|
|
996 len += b.remaining();
|
|
997 ByteBuffer top = bufs.getFirst();
|
|
998 ByteBuffer end = bufs.getLast();
|
|
999 System.err.println("length: " + len);
|
|
1000 System.err.print("head 0: ");
|
|
1001 for (int i = 0; i < 16 && i < top.remaining(); i++) {
|
|
1002 System.err.print(" " + top.get(i));
|
|
1003 }
|
|
1004 System.err.print("tail 0: ");
|
|
1005 for (int i = 0; i < 16 && i < end.remaining(); i++) {
|
|
1006 System.err.print(" " + end.get(i));
|
|
1007 }
|
|
1008 System.err.println();
|
|
1009 }
|
|
1010
|
|
1011 @Test
|
|
1012 public void test1() {
|
|
1013 try {
|
|
1014 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
|
|
1015 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
1016 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
|
|
1017 // if (false) {
|
|
1018 // for(int i=0;i<10;i++) {
|
|
1019 // in.add(ByteBuffer.wrap("test1".getBytes()));
|
|
1020 // in.add(ByteBuffer.wrap("test2".getBytes()));
|
|
1021 // in.add(ByteBuffer.wrap("test3".getBytes()));
|
|
1022 // in.add(ByteBuffer.wrap("test44".getBytes()));
|
|
1023 // }
|
|
1024 // } else
|
|
1025 {
|
|
1026 String t = "";
|
|
1027 for (int i = 0; i < 10; i++) {
|
|
1028 t += "test1";
|
|
1029 t += "test2";
|
|
1030 t += "test3";
|
|
1031 t += "test44";
|
|
1032 }
|
|
1033 in.add(ByteBuffer.wrap(t.getBytes()));
|
|
1034 }
|
|
1035
|
|
1036 LinkedList<ByteBuffer> in1 = clone(in);
|
|
1037
|
|
1038 Deflater deflater = new Deflater();
|
|
1039 zip(deflater, in, 0, out);
|
|
1040 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend
|
|
1041 // on deflator's state
|
|
1042 unzip(inflater, out, 0, out2, INFLATE_BUFSIZE);
|
|
1043 // inflater.reset();
|
|
1044 equalByteBuffers(in1, out2);
|
|
1045 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
|
|
1046 deflater = new Deflater();
|
|
1047 zip(deflater, out2, 0, out4);
|
|
1048 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
|
|
1049 unzip(inflater, out4, 0, out5, INFLATE_BUFSIZE);
|
|
1050 int len = equalByteBuffers(in1, out5);
|
|
1051
|
|
1052 System.out.println("Test Ok. " + len);
|
|
1053 } catch (Exception e) {
|
|
1054 assertEquals(0, 1);
|
|
1055 }
|
|
1056 }
|
|
1057
|
|
1058 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) {
|
|
1059 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>();
|
|
1060 for (ByteBuffer b : in) {
|
|
1061 ByteBuffer c = b.duplicate();
|
|
1062 copy.add(c);
|
|
1063 }
|
|
1064 return copy;
|
|
1065 }
|
|
1066
|
|
1067 public int equalByteBuffers(LinkedList<ByteBuffer> in,
|
|
1068 LinkedList<ByteBuffer> out2) {
|
|
1069 int len = 0;
|
|
1070 Iterable<Byte> i = byteBufferIterator(in);
|
|
1071 Iterator<Byte> o = byteBufferIterator(out2).iterator();
|
|
1072
|
|
1073 for (int b : i) {
|
|
1074 len++;
|
|
1075 if (o.hasNext()) {
|
|
1076 int c = o.next();
|
|
1077 assertEquals(b, c);
|
|
1078 } else
|
|
1079 assertEquals(0, 1);
|
|
1080 }
|
|
1081 if (o.hasNext())
|
|
1082 assertEquals(0, 1);
|
|
1083 // System.out.println();
|
|
1084 return len;
|
|
1085 }
|
|
1086
|
|
1087 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) {
|
|
1088 return new Iterable<Byte>() {
|
|
1089 public Iterator<Byte> iterator() {
|
|
1090 return new Iterator<Byte>() {
|
|
1091 int bytes = 0;
|
|
1092 int buffers = 0;
|
|
1093
|
|
1094 public boolean hasNext() {
|
|
1095 for (;;) {
|
|
1096 if (buffers >= in.size())
|
|
1097 return false;
|
|
1098 ByteBuffer b = in.get(buffers);
|
|
1099 if (!(bytes < b.remaining())) {
|
|
1100 buffers++;
|
|
1101 bytes = 0;
|
|
1102 } else
|
|
1103 return true;
|
|
1104 }
|
|
1105 }
|
|
1106
|
|
1107 public Byte next() {
|
|
1108 ByteBuffer bf = in.get(buffers);
|
|
1109 byte b = bf.get(bytes++);
|
|
1110 if (bf.remaining() <= bytes) {
|
|
1111 buffers++;
|
|
1112 bytes = 0;
|
|
1113 }
|
|
1114 // System.out.print(b);
|
|
1115 return b;
|
|
1116 }
|
|
1117
|
|
1118 public void remove() {
|
|
1119 }
|
|
1120 };
|
|
1121 }
|
|
1122 };
|
|
1123 }
|
|
1124
|
|
1125 }
|