# HG changeset patch # User Shinji KONO # Date 1312422689 -32400 # Node ID 2ce6077bdb099ace74e862d8a1f8351d07f9e15e # Parent ae4df9b0780592e580f3646dcea46863093c4d9e needsInput diff -r ae4df9b07805 -r 2ce6077bdb09 src/myVncProxy/MyRfbProto.java --- 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 cliListTmp; private LinkedList cliList; - private LinkedList sendThreads; boolean createBimgFlag; ExecutorService executor; byte[] pngBytes; - private MulticastQueue> multicastqueue = new MostRecentMultiCast>(10); - // private MulticastQueue> multicastqueue = new MulticastQueue>(); + // private MulticastQueue> multicastqueue = new MostRecentMultiCast>(10); + private MulticastQueue> multicastqueue = new MulticastQueue>(); private int clients = 0; private Inflater inflater = new Inflater(); @@ -535,29 +533,26 @@ * @throws IOException */ public int zip(Deflater deflater,LinkedList inputs, int inputIndex, LinkedList 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 inputs, int inputIndex, LinkedList 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 > 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 bufs = c.poll(); int inputIndex = 0;