changeset 102:2ce6077bdb09

needsInput
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 04 Aug 2011 10:51:29 +0900
parents ae4df9b07805
children d1dc2bb0200d
files src/myVncProxy/MyRfbProto.java
diffstat 1 files changed, 46 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Wed Aug 03 22:12:41 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Thu Aug 04 10:51:29 2011 +0900
@@ -26,7 +26,6 @@
 import myVncProxy.MulticastQueue.Client;
 
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.zip.DataFormatException;
 import java.util.zip.Deflater;
 import java.util.zip.Inflater;
@@ -57,15 +56,14 @@
 	private byte initData[];
 	private LinkedList<Socket> cliListTmp;
 	private LinkedList<Socket> cliList;
-	private LinkedList<Thread> sendThreads;
 	boolean createBimgFlag;
 
 	ExecutorService executor;
 
 	byte[] pngBytes;
 
-	private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
-	// private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
+	// private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
+	private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
 	private int clients = 0;
 	private Inflater inflater = new Inflater();
 
@@ -535,29 +533,26 @@
 	 * @throws IOException
 	 */
 	public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
-		int len1=0,len = 0;
+		int len = 0;
 		ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
 		while(inputIndex < inputs.size() ) {
 			ByteBuffer b1 = inputs.get(inputIndex++);
 			deflater.setInput(b1.array(),b1.position(),b1.remaining());
-			if (inputIndex==inputs.size()) {
-				deflater.finish();
-			} 
+			if (inputIndex==inputs.size())	deflater.finish();
 			do {
-				len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
-				if (len1<=0) break;   // get next buffer 
-				len += len1;
-				c1.position(c1.position()+len1); 
-				if (c1.remaining()==0) {
-					c1.flip();
-					outputs.addLast(c1);
-					c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
+				int len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
+				if (len1>0) {
+					len += len1;
+					c1.position(c1.position()+len1); 
+					if (c1.remaining()==0) {
+						c1.flip();	outputs.addLast(c1);
+						c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
+					}
 				}
-			} while (!deflater.finished());
+			} while (!deflater.needsInput()&&!deflater.finished());
 		}
 		if (c1.position()!=0) {
-			c1.flip();
-			outputs.addLast(c1);
+			c1.flip();	outputs.addLast(c1);
 		}
 		deflater.reset();
 		return len;
@@ -575,29 +570,28 @@
 	 */
 	public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs)
 																	throws DataFormatException {
-		int len=0,len0;
+		int len=0;
 		ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
-	    // inflater.reset();   // if we uncomment this, test1() will be passed. But it won't work with real connection.
 		while (inputIndex < inputs.size()) {
 			ByteBuffer input = inputs.get(inputIndex++);
-			inflater.setInput(input.array(),0,input.limit());
+			inflater.setInput(input.array(),input.position(),input.limit());
 			do {
-				len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining());
-				if (len0<=0) break;
-				buf.position(buf.position()+len0);
-				len += len0;
-				if (buf.remaining()==0) {
-					buf.flip();
-					outputs.addLast(buf);
-					buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
+				int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining());
+				if (len0>0) {
+					buf.position(buf.position()+len0);
+					len += len0;
+					if (buf.remaining()==0) {
+						buf.flip();
+						outputs.addLast(buf);
+						buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
+					}
 				}
-			} while (!inflater.finished());
+			} while (!inflater.needsInput());
 		} 
 		if (buf.position()!=0) {
 			buf.flip();
 			outputs.addLast(buf);
 		}
-		// inflater.reset(); // this make JUnit happy, but VNC won't happy with this
 		return len;
 	}
 	
@@ -642,6 +636,26 @@
 		// rfb.addSockTmp(newCli);
 		//		addSock(newCli);
 		final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
+
+		final Runnable reader = new Runnable() {
+			public void run() {
+				byte b[] = new byte[4096];
+				for(;;) {
+					try {
+						int c = is.read(b);
+						if (c<=0) throw new IOException();
+						System.out.println("client read "+c);
+					} catch (IOException e) {
+						try {
+							os.close();
+							is.close();
+						} catch (IOException e1) {
+						}
+						return;
+					}
+				}
+			}
+		};
 		Runnable sender = new Runnable() {
 			public void run() {
 
@@ -657,28 +671,7 @@
 					sendSecResult(os);
 					readClientInit(is);
 					sendInitData(os);
-
-					Runnable reader = new Runnable() {
-						public void run() {
-							byte b[] = new byte[4096];
-							for(;;) {
-								try {
-									int c = is.read(b);
-									if (c<=0) throw new IOException();
-									System.out.println("client read "+c);
-								} catch (IOException e) {
-									try {
-										os.close();
-										is.close();
-									} catch (IOException e1) {
-									}
-									return;
-								}
-							}
-						}
-					};
 					new Thread(reader).start();
-
 					for (;;) {
 						LinkedList<ByteBuffer> bufs = c.poll();
 						int inputIndex = 0;