comparison src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java @ 502:b9e73589202c

fix header
author oshiro
date Mon, 18 Feb 2019 17:08:34 +0900
parents 57ee5c26e8eb
children 0f9160194a04
comparison
equal deleted inserted replaced
501:57ee5c26e8eb 502:b9e73589202c
28 import java.util.zip.Inflater; 28 import java.util.zip.Inflater;
29 29
30 30
31 public class TreeRFBProto { 31 public class TreeRFBProto {
32 32
33 final static int CheckDelay = 11;
34 protected final static int FramebufferUpdate = 0; 33 protected final static int FramebufferUpdate = 0;
35 protected ProtocolContext context; 34 protected ProtocolContext context;
36 private int clients = 0; 35 private int clients = 0;
37 public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); 36 public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
38 public int acceptPort = 0; 37 public int acceptPort = 0;
75 private static int uniqueNodeId = 0; // uniquenodeid in all trees (less than MAX_UNIQUE_NODE_ID) 74 private static int uniqueNodeId = 0; // uniquenodeid in all trees (less than MAX_UNIQUE_NODE_ID)
76 private int deflate_size = 65507; 75 private int deflate_size = 65507;
77 private ByteBuffer header; 76 private ByteBuffer header;
78 private ByteBuffer c1; 77 private ByteBuffer c1;
79 private FramebufferUpdateRectangle c1rect; 78 private FramebufferUpdateRectangle c1rect;
79 private int c1headerPos;
80 80
81 public TreeRFBProto(boolean isTreeManager, ViewerInterface viewer) { 81 public TreeRFBProto(boolean isTreeManager, ViewerInterface viewer) {
82 nets.setMyRfb(this); 82 nets.setMyRfb(this);
83 this.isTreeManager = isTreeManager; 83 this.isTreeManager = isTreeManager;
84 this.viewer = viewer; 84 this.viewer = viewer;
759 759
760 unzip(inflater, inputs, 0, bytes, inflate_size); 760 unzip(inflater, inputs, 0, bytes, inflate_size);
761 this.header = header; 761 this.header = header;
762 // dump32(inputs); 762 // dump32(inputs);
763 c1 = multicastqueue.allocate(deflate_size); 763 c1 = multicastqueue.allocate(deflate_size);
764 if (addSerialNum)
765 c1.putLong(counter++);
766 if (checkDelay)
767 CheckDelay.checkDelay(c1, rect.x, rect.y, rect.width, rect.height, System.currentTimeMillis(), EncodingType.CHECK_DELAY);
768 c1headerPos = c1.position();
769 c1.put(header);
764 c1rect = new FramebufferUpdateRectangle(rect.x, rect.y, 0, 0); 770 c1rect = new FramebufferUpdateRectangle(rect.x, rect.y, 0, 0);
765 return; 771 return;
766 } 772 }
767 773
768 public void multicastPut(FramebufferUpdateRectangle rect, byte[] bytes, int prevoffset, int offset, int tilex, int tiley) { 774 public void multicastPut(FramebufferUpdateRectangle rect, byte[] bytes, int prevoffset, int offset, int tilex, int tiley) {
769 int span = offset - prevoffset; 775 int span = offset - prevoffset;
770 int x = rect.x, y = rect.y, w = 0, h = 0, w0 = 0, h0 = 0;
771 deflater.setInput(bytes,prevoffset,span); 776 deflater.setInput(bytes,prevoffset,span);
772 c1rect.height = tiley; 777 c1rect.height = tiley;
773 if (c1.remaining() < span || c1rect.x + c1rect.width + tilex >= rect.x + rect.width ) { 778 if (c1.remaining() < span || c1rect.x + c1rect.width + tilex >= rect.x + rect.width ) {
774 deflater.deflate(c1, Deflater.FULL_FLUSH); 779 deflater.deflate(c1, Deflater.FULL_FLUSH);
775 c1.flip(); 780 c1.flip();
776 try { 781 try {
777 writeUpdateRectangleWithHeader(c1,header,c1.remaining(), c1rect.x, c1rect.y, c1rect.width, c1rect.height); 782 writeUpdateRectangleWithHeader(c1, c1headerPos, c1.remaining(), c1rect.x, c1rect.y, c1rect.width, c1rect.height);
778 } catch (InterruptedException e) { 783 } catch (InterruptedException e) {
779 e.printStackTrace(); 784 e.printStackTrace();
780 } 785 }
781 c1rect.x += c1rect.width; 786 c1rect.x += c1rect.width;
782 if (c1rect.x >= rect.x + rect.width) { 787 if (c1rect.x >= rect.x + rect.width) {
783 c1rect.x = rect.x; 788 c1rect.x = rect.x;
784 c1rect.y += tiley; 789 c1rect.y += tiley;
785 } 790 }
786 c1rect.width = 0; 791 c1rect.width = 0;
787 c1 = multicastqueue.allocate(deflate_size); 792 c1 = multicastqueue.allocate(deflate_size);
793 if (addSerialNum)
794 c1.putLong(counter++);
795 c1headerPos = c1.position();
796 c1.put(header);
788 } else { 797 } else {
789 deflater.deflate(c1, Deflater.SYNC_FLUSH); 798 deflater.deflate(c1, Deflater.SYNC_FLUSH);
790 } 799 }
791 c1rect.width += tilex; 800 c1rect.width += tilex;
792 } 801 }
793 802
794 private void writeUpdateRectangleWithHeader(ByteBuffer c1, ByteBuffer header, int len2, int x, int y, int w, int h) throws InterruptedException { 803 private void writeUpdateRectangleWithHeader(ByteBuffer c1, int headerPos, int len2, int x, int y, int w, int h) throws InterruptedException {
804 deflater.reset();
805
806 c1.putInt(len2);
807 c1.putShort(headerPos + 4,(short) x);
808 c1.putShort(headerPos + 6,(short) y);
809 c1.putShort(headerPos + 8,(short) w);
810 c1.putShort(headerPos + 10,(short) h);
795 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); 811 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
796 bufs.add(c1); 812 bufs.add(c1);
797 deflater.reset();
798
799 ByteBuffer blen = multicastqueue.allocate(4);
800 blen.putInt(len2);
801 blen.flip();
802 bufs.addFirst(blen);
803 ByteBuffer hdr = multicastqueue.allocate(header.array().length);
804 hdr.put(header.array());
805 hdr.putShort(4,(short) x);
806 hdr.putShort(6,(short) y);
807 hdr.putShort(8,(short) w);
808 hdr.putShort(10,(short) h);
809 hdr.flip();
810 if (checkDelay) {
811 bufs = createCheckDelayHeader(bufs, hdr);
812 } else {
813 bufs.addFirst(hdr);
814 }
815 if (addSerialNum) {
816 addSerialNumber(bufs);
817 }
818 multicastqueue.waitput(bufs); 813 multicastqueue.waitput(bufs);
819 } 814 }
820 815
821 public LinkedList<ByteBuffer> createCheckDelayHeader(LinkedList<ByteBuffer> checkDelay, ByteBuffer header) { 816 public LinkedList<ByteBuffer> createCheckDelayHeader(LinkedList<ByteBuffer> checkDelay, ByteBuffer header) {
822 int x = (int) header.getShort(4); 817 int x = (int) header.getShort(4);