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