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