changeset 25:49eed04b9b3b

add SendThread, modify MyRfbProto.java
author e085711
date Wed, 15 Jun 2011 15:30:33 +0900
parents 1d3baf14cafb
children 072306e78a95
files src/myVncClient/MyRfbProto.java src/myVncClient/SendThread.java src/myVncClient/VncCanvas.java
diffstat 3 files changed, 78 insertions(+), 16 deletions(-) [+]
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);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myVncClient/SendThread.java	Wed Jun 15 15:30:33 2011 +0900
@@ -0,0 +1,30 @@
+package myVncClient;
+import java.net.Socket;
+import java.io.IOException;
+import java.io.OutputStream;
+
+
+public class SendThread implements Runnable {
+
+	Socket sock;
+	OutputStream out;
+	byte b[];
+
+	SendThread(Socket _sock, byte _b[]){
+		sock = _sock;
+		b = _b;
+	}
+	SendThread(OutputStream _out, byte _b[]){
+		out = _out;
+		b = _b;
+	}
+	
+	public void run() {
+		try{
+			out.write(b, 0, b.length);	
+//			sock.getOutputStream().write(b, 0, b.length);
+		}catch(IOException e){
+			
+		}
+	}
+}
--- a/src/myVncClient/VncCanvas.java	Wed May 18 11:39:26 2011 +0900
+++ b/src/myVncClient/VncCanvas.java	Wed Jun 15 15:30:33 2011 +0900
@@ -393,7 +393,7 @@
 			
 			rfb.regiFramebufferUpdate();
 			rfb.checkAndMark();
-//			rfb.printFramebufferUpdate();
+//			rfb.readSendData();
 			
 			int bufSize = (int)rfb.getNumBytesRead();
 			
@@ -551,6 +551,7 @@
 //			System.out.println("bufSize="+bufSize);
 			rfb.bufResetSend(bufSize);
 
+			
 			if(rfb.createBimgFlag){
 //				bimg = createBufferedImage(rawPixelsImage);
 				bimg = createBufferedImage(memImage);
@@ -559,10 +560,6 @@
 				rfb.sendPngImage();	
 				rfb.createBimgFlag = false;
 			}
-			
-			
-			
-			
 		}
 	}