comparison src/myVncProxy/MyRfbProto.java @ 107:a8b2712de4c5

ZRLEE extension
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 05 Aug 2011 08:57:00 +0900
parents 7a7baebdd3cf
children 3f73ebf918bd
comparison
equal deleted inserted replaced
106:7a7baebdd3cf 107:a8b2712de4c5
30 import java.util.zip.Deflater; 30 import java.util.zip.Deflater;
31 import java.util.zip.Inflater; 31 import java.util.zip.Inflater;
32 import java.io.OutputStream; 32 import java.io.OutputStream;
33 33
34 public 34 public
35 class MyRfbProto<ByteBuffersIterator> extends RfbProto { 35 class MyRfbProto extends RfbProto {
36 final static String versionMsg_3_998 = "RFB 003.998\n"; 36 final static String versionMsg_3_998 = "RFB 003.998\n";
37 /** 37 /**
38 * CheckMillis is one of new msgType for RFB 3.998. 38 * CheckMillis is one of new msgType for RFB 3.998.
39 */ 39 */
40 final static byte SpeedCheckMillis = 4; 40 final static byte SpeedCheckMillis = 4;
308 rectY = readU16(); // 6 308 rectY = readU16(); // 6
309 rectW = readU16(); // 8 309 rectW = readU16(); // 8
310 rectH = readU16(); // 10 310 rectH = readU16(); // 10
311 encoding = readU32(); // 12 311 encoding = readU32(); // 12
312 System.out.println("encoding = "+encoding); 312 System.out.println("encoding = "+encoding);
313 if (encoding == EncodingZRLE) 313 if (encoding == EncodingZRLE|| encoding==EncodingZRLEE||encoding==EncodingZlib)
314 zLen = readU32(); 314 zLen = readU32();
315 else 315 else
316 zLen = 0; 316 zLen = 0;
317 is.reset(); 317 is.reset();
318 318
336 dataLen = zLen + 20; 336 dataLen = zLen + 20;
337 is.mark(dataLen); 337 is.mark(dataLen);
338 break; 338 break;
339 case RfbProto.EncodingZlib: 339 case RfbProto.EncodingZlib:
340 case RfbProto.EncodingZRLE: 340 case RfbProto.EncodingZRLE:
341 case RfbProto.EncodingZRLEE:
341 dataLen = zLen + 20; 342 dataLen = zLen + 20;
342 is.mark(dataLen); 343 is.mark(dataLen);
343 break; 344 break;
344 case RfbProto.EncodingXCursor: 345 case RfbProto.EncodingXCursor:
345 case RfbProto.EncodingRichCursor: 346 case RfbProto.EncodingRichCursor:
579 ByteBuffer header = ByteBuffer.allocate(16); 580 ByteBuffer header = ByteBuffer.allocate(16);
580 readFully(header.array(),0,16); 581 readFully(header.array(),0,16);
581 header.limit(16); 582 header.limit(16);
582 if (header.get(0)==RfbProto.FramebufferUpdate) { 583 if (header.get(0)==RfbProto.FramebufferUpdate) {
583 int encoding = header.getInt(12); 584 int encoding = header.getInt(12);
584 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { 585 if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed
585 ByteBuffer len = ByteBuffer.allocate(4); 586 ByteBuffer len = ByteBuffer.allocate(4);
586 readFully(len.array(),0,4); len.limit(4); 587 readFully(len.array(),0,4); len.limit(4);
587 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); 588 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
588 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); 589 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
589 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); 590 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
590 inputs.add(inputData); 591 inputs.add(inputData);
591 // int length = rectW * rectH * (bitsPerPixel/8); 592
592 if (clicomp) { 593 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time
593 unzip(inflater, inputs, 0, bufs, INFLATE_BUFSIZE); 594 // using new Deflecter every time is incompatible with the protocol, clients have to be modified.
594 } else { 595 Deflater nDeflater = deflater; // new Deflater();
595 // using new Deflecter every time is incompatible with the protocol, clients have to be modified. 596 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
596 Deflater nDeflater = deflater; // new Deflater(); 597 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE);
597 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); 598 // dump32(inputs);
598 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); 599 int len2 = zip(nDeflater, out, 0, bufs);
599 // dump32(inputs); 600 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
600 int len2 = zip(nDeflater, out, 0, bufs); 601 bufs.addFirst(blen);
601 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); 602
602 bufs.addFirst(blen);
603 }
604 bufs.addFirst(header); 603 bufs.addFirst(header);
605 multicastqueue.put(bufs); 604 multicastqueue.put(bufs);
606 is.reset(); 605 is.reset();
607 return ; 606 return ;
608 } 607 }
645 return; 644 return;
646 } 645 }
647 } 646 }
648 } 647 }
649 }; 648 };
649 final int myId = clients;
650 Runnable sender = new Runnable() { 650 Runnable sender = new Runnable() {
651 public void run() { 651 public void run() {
652 Deflater deflater = new Deflater();
653 try { 652 try {
654 /** 653 /**
655 * initial connection of RFB protocol 654 * initial connection of RFB protocol
656 */ 655 */
657 sendRfbVersion(os); 656 sendRfbVersion(os);
666 LinkedList<ByteBuffer> bufs = c.poll(); 665 LinkedList<ByteBuffer> bufs = c.poll();
667 int inputIndex = 0; 666 int inputIndex = 0;
668 ByteBuffer header = bufs.get(inputIndex++); 667 ByteBuffer header = bufs.get(inputIndex++);
669 if (header==null) continue; 668 if (header==null) continue;
670 if (header.get(0)==RfbProto.FramebufferUpdate) { 669 if (header.get(0)==RfbProto.FramebufferUpdate) {
671 System.out.println("client "+ clients); 670 System.out.println("client "+ myId);
672 int encoding = header.getInt(12);
673 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
674 LinkedList<ByteBuffer> outs;
675 if (clicomp) {
676 outs = new LinkedList<ByteBuffer>();
677 int len2 = zip(deflater, bufs, inputIndex, outs);
678 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
679 outs.addFirst(blen);
680 outs.addFirst(header);
681 inputIndex = 0;
682 } else {
683 outs = bufs;
684 inputIndex = 0;
685 }
686 writeToClient(os,bufs,inputIndex);
687 continue;
688 }
689 } 671 }
690 os.write(header.array(),header.position(),header.limit()); 672 os.write(header.array(),header.position(),header.limit());
691 writeToClient(os, bufs, inputIndex); 673 writeToClient(os, bufs, inputIndex);
692 } 674 }
693 } catch (IOException e) { 675 } catch (IOException e) {
736 public void test1() { 718 public void test1() {
737 try { 719 try {
738 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); 720 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
739 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); 721 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
740 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); 722 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
741 if (false) { 723 // if (false) {
742 for(int i=0;i<10;i++) { 724 // for(int i=0;i<10;i++) {
743 in.add(ByteBuffer.wrap("test1".getBytes())); 725 // in.add(ByteBuffer.wrap("test1".getBytes()));
744 in.add(ByteBuffer.wrap("test2".getBytes())); 726 // in.add(ByteBuffer.wrap("test2".getBytes()));
745 in.add(ByteBuffer.wrap("test3".getBytes())); 727 // in.add(ByteBuffer.wrap("test3".getBytes()));
746 in.add(ByteBuffer.wrap("test44".getBytes())); 728 // in.add(ByteBuffer.wrap("test44".getBytes()));
747 } 729 // }
748 } else { 730 // } else
731 {
749 String t = ""; 732 String t = "";
750 for(int i=0;i<10;i++) { 733 for(int i=0;i<10;i++) {
751 t += "test1"; 734 t += "test1";
752 t += "test2"; 735 t += "test2";
753 t += "test3"; 736 t += "test3";