Mercurial > hg > Members > nobuyasu > tightVNCProxy
changeset 82:0cbe556e2c54
remove item to reduce memory
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 03 Aug 2011 04:26:58 +0900 |
parents | 9109273b96dc |
children | d4236fd2efe1 |
files | src/myVncProxy/MostRecentMultiCast.java src/myVncProxy/MulticastQueue.java src/myVncProxy/MyRfbProto.java |
diffstat | 3 files changed, 70 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncProxy/MostRecentMultiCast.java Wed Aug 03 04:26:58 2011 +0900 @@ -0,0 +1,27 @@ +package myVncProxy; + +import java.util.LinkedList; + + +public class MostRecentMultiCast<T> extends MulticastQueue<T> { + + LinkedList<Node<T>> alive; + int count = 0; + MostRecentMultiCast(int limit) { + count = limit; + this.alive = new LinkedList<Node<T>>(); + } + + @Override + public synchronized void put(T item) + { + Node<T> next = new Node<T>(item); + tail.set(next); + tail = next; + alive.addLast(next); + if (alive.size()>count) { + Node<T> old = alive.getFirst(); + old.clear(); + } + } +}
--- a/src/myVncProxy/MulticastQueue.java Wed Aug 03 03:20:39 2011 +0900 +++ b/src/myVncProxy/MulticastQueue.java Wed Aug 03 04:26:58 2011 +0900 @@ -36,18 +36,21 @@ public T poll() { Node<T> next = null; - - try { - next = node.next(); - }catch(InterruptedException _e){ - _e.printStackTrace(); - } - node = next; - return next.item; + T item; + do { + try { + next = node.next(); + }catch(InterruptedException _e){ + _e.printStackTrace(); + } + item = node.getItem(); + node = next; + } while ( item == null); + return item; } } - private static class Node<T> + static class Node<T> { private T item; private Node<T> next; @@ -60,6 +63,10 @@ latch = new CountDownLatch(1); } + synchronized public T getItem() { + return item; + } + public void set(Node<T> next) { this.next = next; @@ -71,5 +78,9 @@ latch.await(); return next; } + + synchronized public void clear() { + item = null; + } } }
--- a/src/myVncProxy/MyRfbProto.java Wed Aug 03 03:20:39 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Wed Aug 03 04:26:58 2011 +0900 @@ -32,7 +32,7 @@ * CheckMillis is one of new msgType for RFB 3.998. */ final static int SpeedCheckMillis = 4; - private static final int INFLATE_BUFSIZE = 1024*1024*16; + private static final int INFLATE_BUFSIZE = 1024*1024; boolean printStatusFlag = false; long startCheckTime; @@ -57,7 +57,7 @@ byte[] pngBytes; - private MulticastQueue<byte[]> multicastqueue = new MulticastQueue<byte[]>(); + private MulticastQueue<byte[]> multicastqueue = new MostRecentMultiCast<byte[]>(10); private int clients = 0; private Inflater inflater = new Inflater(); @@ -378,16 +378,18 @@ if (b[0]==RfbProto.FramebufferUpdate) { int encoding = ((b[12]*256+b[13])*256+b[14])*256+b[15]; if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { - byte inf[] = new byte[INFLATE_BUFSIZE]; + int len; inflater.setInput(b, 20, b.length-20); - int len = inflater.inflate(inf,20,inf.length-20); - if (len==INFLATE_BUFSIZE) throw new DataFormatException(); // too large - for(int i = 0;i<20;i++) inf[i] = b[i]; - inf[16+0] = (byte) ((len >>> 24) & 0xFF); - inf[16+1] = (byte) ((len >>> 16) & 0xFF); - inf[16+2] = (byte) ((len >>> 8) & 0xFF); - inf[16+3] = (byte) ((len >>> 0) & 0xFF); - multicastqueue.put(inf); + do { + byte inf[] = new byte[INFLATE_BUFSIZE+20]; + len = inflater.inflate(inf,20,inf.length-20); + for(int i = 0;i<20;i++) inf[i] = b[i]; + inf[16+0] = (byte) ((len >>> 24) & 0xFF); + inf[16+1] = (byte) ((len >>> 16) & 0xFF); + inf[16+2] = (byte) ((len >>> 8) & 0xFF); + inf[16+3] = (byte) ((len >>> 0) & 0xFF); + multicastqueue.put(inf); + } while (len ==INFLATE_BUFSIZE); is.reset(); return; } @@ -555,14 +557,19 @@ if (b[0]==RfbProto.FramebufferUpdate) { int encoding = ((b[12]*256+b[13])*256+b[14])*256+b[15]; if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { - byte[] c = new byte[INFLATE_BUFSIZE]; - int clen = ((b[16]*256+b[17])*256+b[18])*256+b[19]; - deflater.setInput(b,20,clen); - int len = deflater.deflate(c); + byte[] c1 = new byte[INFLATE_BUFSIZE]; + int len=0,len1,clen; + do { + clen = ((b[16]*256+b[17])*256+b[18])*256+b[19]; + deflater.setInput(b,20,clen); + len1 = deflater.deflate(c1); + if (clen==INFLATE_BUFSIZE) b = c.poll(); + len += len1; + } while(clen== INFLATE_BUFSIZE); byte[] blen = castIntByte(len); os.write(b,0,16); os.write(blen,0,4); - os.write(c,0,len); + os.write(c1,0,len); } } else os.write(b, 0, b.length);