changeset 86:b7225991184b

refactor for tests
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Aug 2011 08:05:51 +0900
parents b384db76c28a
children a8c33757ac99
files src/myVncProxy/MyRfbProto.java
diffstat 1 files changed, 93 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Wed Aug 03 07:39:43 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Wed Aug 03 08:05:51 2011 +0900
@@ -371,53 +371,7 @@
 		return dataLen;
 	}
 	
-	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 = u32(b,12);
-			if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-				int len=0,len0;
-				inflater.setInput(b, 20, b.length-20);
-				do {
-					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 ;
-			}
-		} 
-		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 sendDataToClient() throws Exception {
 		regiFramebufferUpdate();
 		int dataLen = checkAndMark();
@@ -533,7 +487,93 @@
 			changeStatusFlag();
 		}
 	}
+
+	public int zip(Deflater deflater,LinkedList<byte[]> inputs, byte[] header,	LinkedList<byte[]> 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
+				deflater.setInput(b1);
+			int len1=0; 
+			do {
+				byte[] c1 = new byte[INFLATE_BUFSIZE];
+				len2 = len1;
+				len1 = deflater.deflate(c1);
+				if (len1>0) {
+					outputs.addLast(c1);
+					count ++;
+					len += len1;
+				}
+			} while (len1 > 0);
+		} while(inputs.size()>0);
+		byte[] blen = castIntByte(len);
+		outputs.addFirst(blen);
+		outputs.addFirst(header);
+		return len2;
+
+	}
+
+	public void unzip(Inflater inflater, byte[] input, LinkedList<byte[]> outputs)
+			throws DataFormatException {
+		int len=0,len0;
+		inflater.setInput(input, 20, input.length-20);
+		do {
+			byte buf[] = new byte[INFLATE_BUFSIZE];
+			len0 = inflater.inflate(buf);
+			len += len0;
+			outputs.addLast(buf);
+		} while (len0 ==INFLATE_BUFSIZE);
+		byte [] blen = castIntByte(len0);
+		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);
+		LinkedList<byte[]>bufs = new LinkedList<byte[]>();
+		if (b[0]==RfbProto.FramebufferUpdate) {
+			int encoding = u32(b,12);
+			if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
+				unzip(inflater, b, bufs);
+				multicastqueue.put(bufs);
+				is.reset();
+				return ;
+			}
+		} 
+		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,
 			final OutputStream os, final InputStream is) throws IOException {
@@ -563,41 +603,14 @@
 						if (b[0]==RfbProto.FramebufferUpdate) {
 							int encoding = u32(b,12);
 							if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-								int clen = u32(bufs.poll(),0);
 								LinkedList<byte[]> outs = new LinkedList<byte[]>();
-								int len = 0, count = 0;
-								int len2=0;
-								//int bufSize = bufs.size();
-								//int bufCount = 0;
-								deflater.reset();
-								do {
-									byte[] b1 = bufs.poll();
-									if (bufs.size()==0) {
-										deflater.setInput(b1,0,clen);
-										deflater.finish();
-									} 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);
-								} while(bufs.size()>0);
-								byte[] blen = castIntByte(len);
-								outs.addFirst(blen);
-								outs.addFirst(b);
-								int i = 0;
-								for(byte [] out: outs) {
-									if (i++ <count+2-1)
+								int len2 = zip(deflater, bufs, b, outs);
+								while(!outs.isEmpty()) {
+								   byte [] out=  outs.poll();
+									if (outs.isEmpty()) 
+										os.write(out,0,len2);
+									else
 										os.write(out);
-									else
-										os.write(out,0,len2);
 								}
 							}
 						} else {
@@ -615,6 +628,7 @@
 			}
 
 
+
 		};
 		clients++;
 		new Thread(sender).start();