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