comparison src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java @ 498:8d95d64c33bf

add multicastPut
author oshiro
date Fri, 15 Feb 2019 20:11:47 +0900
parents 62d6e9180a11
children 5455c62a3775
comparison
equal deleted inserted replaced
497:62d6e9180a11 498:8d95d64c33bf
10 import com.glavsoft.transport.Writer; 10 import com.glavsoft.transport.Writer;
11 import com.glavsoft.viewer.ConnectionPresenter; 11 import com.glavsoft.viewer.ConnectionPresenter;
12 import com.glavsoft.viewer.ViewerInterface; 12 import com.glavsoft.viewer.ViewerInterface;
13 import com.glavsoft.viewer.swing.ConnectionParams; 13 import com.glavsoft.viewer.swing.ConnectionParams;
14 14
15 import java.awt.*;
15 import java.io.IOException; 16 import java.io.IOException;
16 import java.io.OutputStream; 17 import java.io.OutputStream;
17 import java.io.UnsupportedEncodingException; 18 import java.io.UnsupportedEncodingException;
18 import java.net.*; 19 import java.net.*;
19 import java.nio.ByteBuffer; 20 import java.nio.ByteBuffer;
71 private DatagramSocket socket = null; 72 private DatagramSocket socket = null;
72 private byte[] originalInitData = null; 73 private byte[] originalInitData = null;
73 private boolean childrenMulticast = true; 74 private boolean childrenMulticast = true;
74 private static int uniqueNodeId = 0; // uniquenodeid in all trees (less than MAX_UNIQUE_NODE_ID) 75 private static int uniqueNodeId = 0; // uniquenodeid in all trees (less than MAX_UNIQUE_NODE_ID)
75 private int deflate_size = 65507; 76 private int deflate_size = 65507;
77 private ByteBuffer header;
76 78
77 public TreeRFBProto(boolean isTreeManager, ViewerInterface viewer) { 79 public TreeRFBProto(boolean isTreeManager, ViewerInterface viewer) {
78 nets.setMyRfb(this); 80 nets.setMyRfb(this);
79 this.isTreeManager = isTreeManager; 81 this.isTreeManager = isTreeManager;
80 this.viewer = viewer; 82 this.viewer = viewer;
619 } while (!inflater.needsInput()); 621 } while (!inflater.needsInput());
620 } 622 }
621 return position; 623 return position;
622 } 624 }
623 625
626
624 /** 627 /**
625 * Multicast framebufferUpdate to children. 628 * Multicast framebufferUpdate to children.
626 * read FrameBuffferUpdate. If it is ZLE, make it ZLEE which is self contained compressed packet. 629 * read FrameBuffferUpdate. If it is ZLE, make it ZLEE which is self contained compressed packet.
627 * put the packet to the multicastqueue. Then normal rendering engine read the same stream using is.reset(). 630 * put the packet to the multicastqueue. Then normal rendering engine read the same stream using is.reset().
628 * 631 *
629 * @param dataLen 632 * @param dataLen
630 * @param reader 633 * @param reader
631 * @throws TransportException 634 * @throws TransportException
632 * @throws UnsupportedEncodingException
633 */ 635 */
634 public void readSendData(int dataLen, Reader reader, byte[] bytes, FramebufferUpdateRectangle rect) 636 public void readSendData(int dataLen, Reader reader, byte[] bytes, FramebufferUpdateRectangle rect)
635 throws TransportException,UnsupportedEncodingException { 637 throws TransportException {
636 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); 638 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
637 int BLOCKSIZE = 64 * 1024;
638 int headerLen = rect.getEncodingType() == EncodingType.CHECK_DELAY ? 24 : 16; 639 int headerLen = rect.getEncodingType() == EncodingType.CHECK_DELAY ? 24 : 16;
639 ByteBuffer header = multicastqueue.allocate(headerLen); 640 ByteBuffer header = multicastqueue.allocate(headerLen);
640 ByteBuffer serial = multicastqueue.allocate(4 + 8); 641 ByteBuffer serial = multicastqueue.allocate(4 + 8);
641 if (!isTreeManager() && addSerialNum) { 642 if (!isTreeManager() && addSerialNum) {
642 reader.readBytes(serial.array(), 0, 4 + 8); 643 reader.readBytes(serial.array(), 0, 4 + 8);
648 if (header.get(0) == FramebufferUpdate) { 649 if (header.get(0) == FramebufferUpdate) {
649 int encoding = header.getInt(12); 650 int encoding = header.getInt(12);
650 651
651 if (encoding == EncodingType.ZRLE.getId() 652 if (encoding == EncodingType.ZRLE.getId()
652 || encoding == EncodingType.ZLIB.getId()) { 653 || encoding == EncodingType.ZLIB.getId()) {
653 if (true) { 654 if (true) {
654 try { 655 try {
655 zrleeBlocking(dataLen, reader, bytes, rect, context.getPixelFormat().bitsPerPixel/8, bufs, header); 656 zrleeBlocking(dataLen, reader, bytes, rect, context.getPixelFormat().bitsPerPixel/8, bufs, header);
656 } catch (DataFormatException e) { 657 } catch (DataFormatException e) {
657 e.printStackTrace(); 658 e.printStackTrace();
658 } 659 }
753 Deflater nDeflater = deflater; // new Deflater(); 754 Deflater nDeflater = deflater; // new Deflater();
754 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); 755 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
755 int inflate_size = rect.width * rect.height * bytePerPixel; 756 int inflate_size = rect.width * rect.height * bytePerPixel;
756 757
757 unzip(inflater, inputs, 0, bytes, inflate_size); 758 unzip(inflater, inputs, 0, bytes, inflate_size);
759 this.header = header;
758 // dump32(inputs); 760 // dump32(inputs);
759 out.add(ByteBuffer.wrap(bytes)); 761 return;
760 int inputIndex = 0; 762 }
763
764 public void multicastPut(Rectangle rect, byte[] bytes, int prevoffset, int offset, int bytePerPixel) {
765 byte subencoding = bytes[0];
766 int inputIndex = 1;
761 ByteBuffer c1 = multicastqueue.allocate(deflate_size); 767 ByteBuffer c1 = multicastqueue.allocate(deflate_size);
762 int x = rect.x, y = rect.y, w = 0, h = 0, w0 = 0, h0 = 0; 768 int x = rect.x, y = rect.y, w = 0, h = 0, w0 = 0, h0 = 0;
763 int span = rect.width; 769 int span = rect.width;
764 while (inputIndex < inflate_size) { 770 while (inputIndex < INFLATE_BUFSIZE) {
765 deflater.setInput(bytes,inputIndex,span * bytePerPixel); 771 byte save = bytes[inputIndex-1];
766 if (c1.remaining() < span * bytePerPixel || inputIndex >= inflate_size - span * bytePerPixel) { 772 bytes[inputIndex-1] = subencoding;
767 deflater.deflate(c1,Deflater.FULL_FLUSH); 773 deflater.setInput(bytes,inputIndex-1,span * bytePerPixel +1);
774 if (c1.remaining() < span * bytePerPixel || inputIndex >= INFLATE_BUFSIZE - span * bytePerPixel) {
775 deflater.deflate(c1, Deflater.FULL_FLUSH);
768 c1.flip(); 776 c1.flip();
769 try { 777 try {
770 writeUpdateRectangleWithHeader(c1,header,c1.remaining(),x+w,y+h,rect.width,h-h0); 778 writeUpdateRectangleWithHeader(c1,header,c1.remaining(),x+w,y+h,rect.width,h-h0);
771 } catch (InterruptedException e) { 779 } catch (InterruptedException e) {
772 e.printStackTrace(); 780 e.printStackTrace();
775 w0 = w; 783 w0 = w;
776 c1 = multicastqueue.allocate(deflate_size); 784 c1 = multicastqueue.allocate(deflate_size);
777 } else { 785 } else {
778 deflater.deflate(c1,Deflater.SYNC_FLUSH); 786 deflater.deflate(c1,Deflater.SYNC_FLUSH);
779 } 787 }
788 bytes[inputIndex-1] = save;
780 w += span; 789 w += span;
781 if (w >= rect.width ) { 790 if (w >= rect.width ) {
782 h++; 791 h++;
783 w = 0; 792 w = 0;
784 w0 = 0; 793 w0 = 0;
785 } 794 }
786 inputIndex += rect.width * bytePerPixel; 795 inputIndex += rect.width * bytePerPixel;
787 } 796 }
788 return;
789 } 797 }
790 798
791 private void writeUpdateRectangleWithHeader(ByteBuffer c1, ByteBuffer header, int len2, int x, int y, int w, int h) throws InterruptedException { 799 private void writeUpdateRectangleWithHeader(ByteBuffer c1, ByteBuffer header, int len2, int x, int y, int w, int h) throws InterruptedException {
792 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); 800 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
793 bufs.add(c1); 801 bufs.add(c1);