comparison src/myVncProxy/MyRfbProto.java @ 105:e166c3cad2b8

new Defleter is working with fixed TightVNC clients
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 04 Aug 2011 19:34:58 +0900
parents b649584c9712
children 7a7baebdd3cf
comparison
equal deleted inserted replaced
104:b649584c9712 105:e166c3cad2b8
65 65
66 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); 66 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
67 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); 67 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
68 private int clients = 0; 68 private int clients = 0;
69 private Inflater inflater = new Inflater(); 69 private Inflater inflater = new Inflater();
70 private Deflater deflater = new Deflater();
70 71
71 public 72 public
72 MyRfbProto() throws IOException { 73 MyRfbProto() throws IOException {
73 } 74 }
74 75
505 int len = 0; 506 int len = 0;
506 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE); 507 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
507 while(inputIndex < inputs.size() ) { 508 while(inputIndex < inputs.size() ) {
508 ByteBuffer b1 = inputs.get(inputIndex++); 509 ByteBuffer b1 = inputs.get(inputIndex++);
509 deflater.setInput(b1.array(),b1.position(),b1.remaining()); 510 deflater.setInput(b1.array(),b1.position(),b1.remaining());
510 if (inputIndex==inputs.size()) deflater.finish(); 511 if (inputIndex==inputs.size())
512 deflater.finish();
511 do { 513 do {
512 int len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); 514 int len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
513 if (len1>0) { 515 if (len1>0) {
514 len += len1; 516 len += len1;
515 c1.position(c1.position()+len1); 517 c1.position(c1.position()+len1);
535 * @param outputs 537 * @param outputs
536 * byte data[] 538 * byte data[]
537 *@return number of total bytes 539 *@return number of total bytes
538 * @throws IOException 540 * @throws IOException
539 */ 541 */
540 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) 542 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize)
541 throws DataFormatException { 543 throws DataFormatException {
542 int len=0; 544 int len=0;
543 ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE); 545 ByteBuffer buf = ByteBuffer.allocate(bufSize);
544 while (inputIndex < inputs.size()) { 546 while (inputIndex < inputs.size()) {
545 ByteBuffer input = inputs.get(inputIndex++); 547 ByteBuffer input = inputs.get(inputIndex++);
546 inflater.setInput(input.array(),input.position(),input.limit()); 548 inflater.setInput(input.array(),input.position(),input.limit());
549 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, we need this
550 // inflater.end(); but this won't work
547 do { 551 do {
548 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); 552 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining());
549 if (len0>0) { 553 if (len0>0) {
550 buf.position(buf.position()+len0); 554 buf.position(buf.position()+len0);
551 len += len0; 555 len += len0;
552 if (buf.remaining()==0) { 556 if (buf.remaining()==0) {
553 buf.flip(); 557 buf.flip();
554 outputs.addLast(buf); 558 outputs.addLast(buf);
555 buf = ByteBuffer.allocate(INFLATE_BUFSIZE); 559 buf = ByteBuffer.allocate(bufSize);
556 } 560 }
557 } 561 }
558 } while (!inflater.needsInput()); 562 } while (!inflater.needsInput());
559 } 563 }
560 if (buf.position()!=0) { 564 if (buf.position()!=0) {
576 readFully(len.array(),0,4); len.limit(4); 580 readFully(len.array(),0,4); len.limit(4);
577 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); 581 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
578 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); 582 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
579 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); 583 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
580 inputs.add(inputData); 584 inputs.add(inputData);
585 // int length = rectW * rectH * (bitsPerPixel/8);
581 if (clicomp) { 586 if (clicomp) {
582 unzip(inflater, inputs, 0, bufs); 587 unzip(inflater, inputs, 0, bufs, INFLATE_BUFSIZE);
583 } else { 588 } else {
584 Deflater nDeflater = new Deflater(); 589 Deflater nDeflater = false ? deflater : new Deflater();
585 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); 590 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
586 unzip(inflater, inputs, 0 , out); 591 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE);
592 dump32(inputs);
587 int len2 = zip(nDeflater, out, 0, bufs); 593 int len2 = zip(nDeflater, out, 0, bufs);
588 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); 594 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
589 bufs.addFirst(blen); 595 bufs.addFirst(blen);
590 } 596 }
591 bufs.addFirst(header); 597 bufs.addFirst(header);
633 } 639 }
634 } 640 }
635 } 641 }
636 }; 642 };
637 Runnable sender = new Runnable() { 643 Runnable sender = new Runnable() {
638
639
640 public void run() { 644 public void run() {
641
642 Deflater deflater = new Deflater(); 645 Deflater deflater = new Deflater();
643 try { 646 try {
644 /** 647 /**
645 * initial connection of RFB protocol 648 * initial connection of RFB protocol
646 */ 649 */
671 inputIndex = 0; 674 inputIndex = 0;
672 } else { 675 } else {
673 outs = bufs; 676 outs = bufs;
674 inputIndex = 0; 677 inputIndex = 0;
675 } 678 }
676 while(inputIndex < outs.size()) { 679 writeToClient(os,bufs,inputIndex);
677 ByteBuffer out= outs.get(inputIndex++); 680 continue;
678 os.write(out.array(),out.position(),out.limit());
679 }
680 } 681 }
681 os.flush();
682 continue;
683 } 682 }
684 os.write(header.array(),header.position(),header.limit()); 683 os.write(header.array(),header.position(),header.limit());
685 while(inputIndex < bufs.size()) { 684 writeToClient(os, bufs, inputIndex);
686 ByteBuffer b = bufs.get(inputIndex++);
687 os.write(b.array(), b.position(), b.limit());
688 }
689 os.flush();
690 } 685 }
691 } catch (IOException e) { 686 } catch (IOException e) {
692 try { 687 try {
693 os.close(); 688 os.close();
694 } catch (IOException e1) { 689 } catch (IOException e1) {
695 } 690 }
696 /* if socket closed cliList.remove(newCli); */ 691 /* if socket closed cliList.remove(newCli); */
697 } 692 }
698 } 693 }
694
695 public void writeToClient(final OutputStream os,
696 LinkedList<ByteBuffer> bufs, int inputIndex)
697 throws IOException {
698 while(inputIndex < bufs.size()) {
699 ByteBuffer b = bufs.get(inputIndex++);
700 os.write(b.array(), b.position(), b.limit());
701 }
702 os.flush();
703 }
699 }; 704 };
700 clients++; 705 clients++;
701 new Thread(sender).start(); 706 new Thread(sender).start();
702 707
703 } 708 }
704 709
705 710
711 public void dump32(LinkedList<ByteBuffer>bufs) {
712 int len =0;
713 for(ByteBuffer b: bufs) len += b.remaining();
714 ByteBuffer top = bufs.getFirst();
715 ByteBuffer end = bufs.getLast();
716 System.err.println("length: "+len);
717 System.err.print("head 0: ");
718 for(int i = 0; i<16 && i < top.remaining(); i++) {
719 System.err.print(" "+ top.get(i));
720 }
721 System.err.print("tail 0: ");
722 for(int i = 0; i<16 && i < end.remaining(); i++) {
723 System.err.print(" "+end.get(i));
724 }
725 System.err.println();
726 }
706 727
707 @Test 728 @Test
708 public void test1() { 729 public void test1() {
709 try { 730 try {
710 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); 731 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
719 LinkedList<ByteBuffer> in1 = clone(in); 740 LinkedList<ByteBuffer> in1 = clone(in);
720 741
721 Deflater deflater = new Deflater(); 742 Deflater deflater = new Deflater();
722 zip(deflater,in,0,out); 743 zip(deflater,in,0,out);
723 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state 744 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state
724 unzip(inflater, out, 0,out2); 745 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE);
725 // inflater.reset(); 746 inflater.reset();
726 equalByteBuffers(in1, out2); 747 equalByteBuffers(in1, out2);
727 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); 748 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
728 deflater = new Deflater(); 749 deflater = new Deflater();
729 zip(deflater,out2,0,out4); 750 zip(deflater,out2,0,out4);
730 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); 751 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
731 unzip(inflater,out4,0, out5); 752 unzip(inflater,out4,0, out5, INFLATE_BUFSIZE);
732 int len = equalByteBuffers(in1,out5); 753 int len = equalByteBuffers(in1,out5);
733 754
734 System.out.println("Test Ok. "+len); 755 System.out.println("Test Ok. "+len);
735 } catch (Exception e) { 756 } catch (Exception e) {
736 assertEquals(0,1); 757 assertEquals(0,1);