diff src/myVncClient/MyRfbProto.java @ 25:49eed04b9b3b

add SendThread, modify MyRfbProto.java
author e085711
date Wed, 15 Jun 2011 15:30:33 +0900
parents ddecfefc7cbe
children 53c831c3a513
line wrap: on
line diff
--- a/src/myVncClient/MyRfbProto.java	Wed May 18 11:39:26 2011 +0900
+++ b/src/myVncClient/MyRfbProto.java	Wed Jun 15 15:30:33 2011 +0900
@@ -15,6 +15,9 @@
 
 import javax.imageio.ImageIO;
 
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.io.OutputStream;
 
 class MyRfbProto extends RfbProto {
 
@@ -26,21 +29,29 @@
 	private int rectH;
 	private int encoding;
 	private int zLen;
+	private int dataLen; 
 
 	private ServerSocket servSock;
 	private int acceptPort;
 	private byte initData[];
 	private LinkedList <Socket> cliListTmp;
 	private LinkedList <Socket> cliList;
+	private LinkedList <Thread> sendThreads;
 	boolean createBimgFlag;
-
+	
+	ExecutorService executor;
+	
+	
 	byte[] pngBytes;
-
+	
 	MyRfbProto(String h, int p, VncViewer v ) throws IOException {
 		super(h, p, v);
 		cliList = new LinkedList <Socket>();
 		cliListTmp = new LinkedList <Socket>();
 		createBimgFlag = false;
+		sendThreads = new LinkedList <Thread>();
+//		executor = Executors.newCachedThreadPool();
+		executor = Executors.newSingleThreadExecutor();
 	}
 
 	MyRfbProto(String h, int p) throws IOException {
@@ -48,6 +59,9 @@
 		cliList = new LinkedList <Socket>();
 		cliListTmp = new LinkedList <Socket>();
 		createBimgFlag = false;
+		sendThreads = new LinkedList <Thread>();
+//		executor = Executors.newCachedThreadPool();
+		executor = Executors.newSingleThreadExecutor();
 	}
 
 	void initServSock(int port) throws IOException{
@@ -67,7 +81,7 @@
 
 			}
 		}
-		System.out.println("acceptport="+i);
+		System.out.println("accept port = "+i);
 	}
 	int getAcceptPort(){
 		return acceptPort;
@@ -145,12 +159,6 @@
 
 		inNormalProtocol = true;
 	}
-	void readPngData()throws IOException{
-		// Dataの大きさを読み込む
-		int length = readU32();
- 		pngBytes = new byte[length];
-		readFully(pngBytes);
-	}
 
 	void sendInitData(Socket sock) throws IOException{
 			sock.getOutputStream().write(initData);
@@ -187,6 +195,8 @@
 		}
 		cliListTmp.clear();
 	}
+	
+	
 
 	boolean ready() throws IOException {
 		BufferedReader br = new BufferedReader(new InputStreamReader(is));
@@ -208,6 +218,24 @@
 		readFully(buffer);
 		sendData(buffer);
 	}
+	void readSendData()throws IOException{
+		byte buffer[] = new byte[dataLen];
+		readFully(buffer);
+		reset();
+
+		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 regiFramebufferUpdate()throws IOException{
 		mark(20);
 		messageType = readU8();
@@ -225,10 +253,12 @@
 	void checkAndMark() throws IOException{
 		switch(encoding){
 		case RfbProto.EncodingRaw:
-			mark(rectW * rectH * 4 + 16);		
+			dataLen = rectW * rectH * 4 + 16;
+			mark(dataLen);
 			break;
 		case RfbProto.EncodingZRLE:
-			mark(zLen);
+			dataLen = zLen+20;
+			mark(dataLen);
 			break;
 		default:
 			mark(1000000);
@@ -279,6 +309,11 @@
 		BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
 		return bimg;
 	}
+	void readPngData()throws IOException{
+		int length = readU32();
+		pngBytes = new byte[length];
+		readFully(pngBytes);
+	}
 	void printFramebufferUpdate(){
 	
 		System.out.println("messageType=" + messageType);