# HG changeset patch # User Shinji KONO # Date 1312328035 -32400 # Node ID a8c33757ac99eb52b21065a080045ff16a2cc898 # Parent b7225991184b7883b48a83d70bf6e36af0c8afde refactoring zip/unzip diff -r b7225991184b -r a8c33757ac99 src/myVncProxy/MyRfbProto.java --- a/src/myVncProxy/MyRfbProto.java Wed Aug 03 08:05:51 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Wed Aug 03 08:33:55 2011 +0900 @@ -487,22 +487,56 @@ changeStatusFlag(); } } + + void speedCheckMillis() { + Runnable stdin = new Runnable() { + public void run() { + int c; + try { + while( (c = System.in.read()) != -1 ) { + switch(c) { + case 's': + break; + default: + startSpeedCheck(); + break; + } + } + }catch(IOException e){ + System.out.println(e); + } + } + }; + + new Thread(stdin).start(); + } - public int zip(Deflater deflater,LinkedList inputs, byte[] header, LinkedList outputs) throws IOException { + /** + * gzip byte arrays + * @param deflater + * @param inputs + * byte len[4] total byte length + * byte data[] + * @param outputs + * byte len[4] total byte length + * byte data[] + * @return byte length in last byte array + * @throws IOException + */ + public int zip(Deflater deflater,LinkedList inputs, LinkedList outputs) throws IOException { int clen = u32(inputs.poll(),0); - int len = 0, count = 0; int len2=0; - //int bufSize = bufs.size(); - //int bufCount = 0; deflater.reset(); do { byte[] b1 = inputs.poll(); if (inputs.size()==0) { deflater.setInput(b1,0,clen); deflater.finish(); - } else + } else { deflater.setInput(b1); + clen -= b1.length; + } int len1=0; do { byte[] c1 = new byte[INFLATE_BUFSIZE]; @@ -517,62 +551,64 @@ } while(inputs.size()>0); byte[] blen = castIntByte(len); outputs.addFirst(blen); - outputs.addFirst(header); return len2; - } - + + /** + * gunzip byte arrays + * @param inflater + * @param inputs + * byte len[4] total byte length + * byte data[] + * @param outputs + * byte len[4] total byte length + * byte data[] + * @throws IOException + */ public void unzip(Inflater inflater, byte[] input, LinkedList outputs) throws DataFormatException { int len=0,len0; - inflater.setInput(input, 20, input.length-20); + inflater.setInput(input); do { byte buf[] = new byte[INFLATE_BUFSIZE]; len0 = inflater.inflate(buf); len += len0; outputs.addLast(buf); } while (len0 ==INFLATE_BUFSIZE); - byte [] blen = castIntByte(len0); + byte [] blen = castIntByte(len); outputs.addFirst(blen); - byte inf[] = new byte[16]; - for(int i = 0;i<16;i++) inf[i] = input[i]; - outputs.addFirst(inf); } void readSendData(int dataLen) throws IOException, DataFormatException { - byte b[] = new byte[dataLen]; - readFully(b); LinkedListbufs = new LinkedList(); - if (b[0]==RfbProto.FramebufferUpdate) { - int encoding = u32(b,12); + byte header[] = new byte[16]; + readFully(header,0,16); + if (header[0]==RfbProto.FramebufferUpdate) { + int encoding = u32(header,12); if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { - unzip(inflater, b, bufs); + byte[] len = new byte[4]; + readFully(len,0,4); + byte inputData[] = new byte[dataLen-20]; + readFully(inputData); + unzip(inflater, inputData, bufs); + bufs.addFirst(header); multicastqueue.put(bufs); is.reset(); return ; } } - bufs.add(b); + bufs.add(header); + if (dataLen>16) { + byte b[] = new byte[dataLen-16]; + readFully(b); + bufs.add(b); + } multicastqueue.put(bufs); is.reset(); // It may be compressed. We can inflate here to avoid repeating clients decompressing here, // but it may generate too many large data. It is better to do it in each client. // But we have do inflation for all input data, so we have to do it here. -/* - for (Socket cli : cliList) { - try { - OutputStream out = cli.getOutputStream(); - executor.execute(new SendThread(out, buffer)); - } catch (IOException e) { - // if client socket closed - cliListTmp.remove(cli); - } catch (Exception e) { - - } - - } -*/ } void newClient(AcceptThread acceptThread, final Socket newCli, @@ -599,12 +635,13 @@ for (;;) { LinkedList bufs = c.poll(); - byte[] b = bufs.poll(); - if (b[0]==RfbProto.FramebufferUpdate) { - int encoding = u32(b,12); + byte[] header = bufs.poll(); + if (header[0]==RfbProto.FramebufferUpdate) { + int encoding = u32(header,12); if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { LinkedList outs = new LinkedList(); - int len2 = zip(deflater, bufs, b, outs); + int len2 = zip(deflater, bufs, outs); + outs.addFirst(header); while(!outs.isEmpty()) { byte [] out= outs.poll(); if (outs.isEmpty()) @@ -613,22 +650,19 @@ os.write(out); } } - } else { + os.flush(); + return; + } + os.write(header); + for(byte [] b : bufs) { os.write(b, 0, b.length); } os.flush(); } } catch (IOException e) { - /** - * if socket closed - */ - // cliList.remove(newCli); + /* if socket closed cliList.remove(newCli); */ } - } - - - }; clients++; new Thread(sender).start(); @@ -643,29 +677,6 @@ } return ret; } - void speedCheckMillis() { - - Runnable stdin = new Runnable() { - public void run() { - int c; - try { - while( (c = System.in.read()) != -1 ) { - switch(c) { - case 's': - break; - default: - startSpeedCheck(); - break; - } - } - }catch(IOException e){ - System.out.println(e); - } - } - }; - - new Thread(stdin).start(); - } }