changeset 84:704e01d2390c

fix
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Aug 2011 06:44:59 +0900
parents d4236fd2efe1
children b384db76c28a
files src/myVncProxy/MyRfbProto.java
diffstat 1 files changed, 68 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Wed Aug 03 04:47:10 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Wed Aug 03 06:44:59 2011 +0900
@@ -57,7 +57,7 @@
 
 	byte[] pngBytes;
 
-	private MulticastQueue<byte[]> multicastqueue = new MostRecentMultiCast<byte[]>(10);
+	private MulticastQueue<LinkedList<byte[]>> multicastqueue = new MostRecentMultiCast<LinkedList<byte[]>>(10);
 	private int clients = 0;
 	private Inflater inflater = new Inflater();
 
@@ -374,27 +374,30 @@
 	void readSendData(int dataLen) throws IOException, DataFormatException {
 		byte b[] = new byte[dataLen];
 		readFully(b);
-
+		LinkedList<byte[]>bufs = new LinkedList<byte[]>();
 		if (b[0]==RfbProto.FramebufferUpdate) {
-			int encoding = ((b[12]<<8+b[13])<<8+b[14])<<8+b[15];
+			int encoding = u32(b,12);
 			if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-				int len;
+				int len=0,len0;
 				inflater.setInput(b, 20, b.length-20);
 				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);
+					byte buf[] = new byte[INFLATE_BUFSIZE];
+					len0 = inflater.inflate(buf);
+					len += len0;
+					bufs.addLast(buf);
+				} while (len0 ==INFLATE_BUFSIZE);
+				byte [] blen = castIntByte(len0);
+				bufs.addFirst(blen);
+				byte inf[] = new byte[16];
+				for(int i = 0;i<16;i++) inf[i] = b[i];
+				bufs.addFirst(inf);
+				multicastqueue.put(bufs);
 				is.reset();
-				return;
+				return ;
 			}
 		} 
-		multicastqueue.put(b);
+		bufs.add(b);
+		multicastqueue.put(bufs);
 		is.reset();
 
 		// It may be compressed. We can inflate here to avoid repeating clients decompressing here,
@@ -503,7 +506,9 @@
 		b[1] = (byte) 0;
 		startCheckTime = System.currentTimeMillis();
 		System.out.println("startChckTime = "+ startCheckTime);
-		multicastqueue.put(b);
+		LinkedList<byte[]>bufs = new LinkedList<byte[]>();
+		bufs.add(b);
+		multicastqueue.put(bufs);
 	}
 
 	void endSpeedCheck() {
@@ -535,7 +540,7 @@
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
 		//		addSock(newCli);
-		final Client<byte[]> c = multicastqueue.newClient();
+		final Client <LinkedList<byte[]>> c = multicastqueue.newClient();
 		Runnable sender = new Runnable() {
 			public void run() {
 
@@ -553,26 +558,48 @@
 					sendInitData(os);
 
 					for (;;) {
-						byte[] b = c.poll();
+						LinkedList<byte[]> bufs = c.poll();
+						byte[] b = bufs.poll();
 						if (b[0]==RfbProto.FramebufferUpdate) {
-							int encoding = ((b[12]<<8+b[13])<<8+b[14])<<8+b[15];
+							int encoding = u32(b,12);
+							int clen = u32(bufs.poll(),0);
 							if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-								byte[] c1 = new byte[INFLATE_BUFSIZE];
-								int len=0,len1,clen;
-								do {
-									clen = ((b[16]<<8+b[17])<<8+b[18])<<8+b[19];
-									deflater.setInput(b,20,clen);
-									len1 = deflater.deflate(c1);
-									if (clen==INFLATE_BUFSIZE) b = c.poll();
-									len += len1;
-								} while(clen== INFLATE_BUFSIZE);
+								LinkedList<byte[]> outs = new LinkedList<byte[]>();
+								int len = 0, count = 0;
+								int len2=0;
+								int bufSize = bufs.size();
+								int bufCount = 0;
+								for( byte[] b1 : bufs) {
+									if (++bufCount > bufSize) 
+										deflater.setInput(b1,0,clen);
+									else
+										deflater.setInput(b1);
+									int len1=0; 
+									do {
+										byte[] c1 = new byte[INFLATE_BUFSIZE];
+										len2 = len1;
+										len1 = deflater.deflate(c1);
+										if (len1>0) {
+											outs.addLast(c1);
+											count ++;
+											len += len1;
+										}
+									} while (len1 > 0);
+								}
 								byte[] blen = castIntByte(len);
-								os.write(b,0,16);
-								os.write(blen,0,4);
-								os.write(c1,0,len);
+								outs.addFirst(blen);
+								outs.addFirst(b);
+								int i = 0;
+								for(byte [] out: outs) {
+									if (i++ <count+2-1)
+										os.write(out);
+									else
+										os.write(out,0,len2);
+								}
 							}
-						} else
+						} else {
 							os.write(b, 0, b.length);
+						}
 					}
 				} catch (IOException e) {
 					/**
@@ -583,12 +610,21 @@
 
 			}
 
+
 		};
 		clients++;
 		new Thread(sender).start();
 
 	}
 
+	private int u32(byte[] b, int i) {
+		int ret = 0;
+		for(int j = 0;j<4;j++) {
+			ret *= 256;
+			ret += b[i+j] & 0xff;
+		}
+		return ret;
+	}
 	void speedCheckMillis() {
 		
 		Runnable stdin = new Runnable() {