diff src/myVncClient/MyRfbProto.java @ 19:965360af5f0b

merged some file
author e085711
date Tue, 26 Apr 2011 15:26:15 +0900
parents 4881586aead9
children ddecfefc7cbe
line wrap: on
line diff
--- a/src/myVncClient/MyRfbProto.java	Tue Apr 26 09:08:14 2011 +0900
+++ b/src/myVncClient/MyRfbProto.java	Tue Apr 26 15:26:15 2011 +0900
@@ -1,7 +1,11 @@
 package myVncClient;
+import java.awt.Graphics;
+import java.awt.Image;
 import java.awt.image.BufferedImage;
+import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.BindException;
@@ -16,24 +20,36 @@
 
 	private int messageType;
 	private int rectangles;
+	private int rectX;
+	private int rectY;
+	private int rectW;
+	private int rectH;
 	private int encoding;
+	private int zLen;
 
-	private ServerSocket servSock; 	
+	private ServerSocket servSock;
 	private int acceptPort;
 	private byte initData[];
+	private LinkedList <Socket> cliListTmp;
 	private LinkedList <Socket> cliList;
+	boolean createBimgFlag;
+
 	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;
 	}
 
 	MyRfbProto(String h, int p) throws IOException {
 		super(h, p);
 		cliList = new LinkedList <Socket>();
+		cliListTmp = new LinkedList <Socket>();
+		createBimgFlag = false;
 	}
-	
+
 	void initServSock(int port) throws IOException{
 		servSock = new ServerSocket(port);
 		acceptPort = port;
@@ -48,14 +64,14 @@
 				i++;
 				continue;
 			}catch(IOException e){
-				
+
 			}
 		}
+		System.out.println("acceptport="+i);
 	}
 	int getAcceptPort(){
 		return acceptPort;
 	}
-
 	void setSoTimeout(int num) throws IOException {
 		servSock.setSoTimeout(num);
 	}
@@ -67,6 +83,9 @@
 	void addSock(Socket sock){
 		cliList.add(sock);
 	}
+	void addSockTmp(Socket sock){
+		cliListTmp.add(sock);
+	}
 	
 	void mark(int len) throws IOException {
 		is.mark(len);
@@ -129,22 +148,13 @@
 		// Dataの大きさを読み込む
 		int length = readU32();
  		pngBytes = new byte[length];
-//		skipBytes(1);
-//		pngBytes = new byte[is.available()];
- 		System.out.println("is.available()="+is.available());
 		readFully(pngBytes);
 	}
-	
-	BufferedImage createBimg()throws IOException{
-		BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
-		return bimg;
-	}
 
 	void sendInitData(Socket sock) throws IOException{
 			sock.getOutputStream().write(initData);
 	}
 
-//	void sendData(byte b[]) throws IOException{
 	void sendData(byte b[]){
 		try{
 			for(Socket cli : cliList){
@@ -152,15 +162,31 @@
 					cli.getOutputStream().write(b, 0, b.length);
 				}catch(IOException e){
 					// if socket closed
-					//				cliList.remove(cli);
 					cliList.remove(cli);
 				}
 			}
-		System.out.println("cliSize="+cliSize());
+//		System.out.println("cliSize="+cliSize());
 		}catch(Exception e){
-			System.out.println("cliSize 0");
 		}
-	}	
+	}
+	
+	void sendPngImage(){
+		try{
+			for(Socket cli : cliListTmp){
+				try{
+					sendPngData(cli);
+					addSock(cli);
+				}catch(IOException e){
+					// if socket closed
+					cliListTmp.remove(cli);
+				}
+			}
+//		System.out.println("cliSize="+cliSize());
+		}catch(Exception e){
+		}
+		cliListTmp.clear();
+	}
+
 	boolean ready() throws IOException {
 		BufferedReader br = new BufferedReader(new InputStreamReader(is));
 		return br.ready();
@@ -182,24 +208,86 @@
 		sendData(buffer);
 	}
 	void regiFramebufferUpdate()throws IOException{
-		mark(16);
+		mark(20);
 		messageType = readU8();
 		skipBytes(1);
 		rectangles = readU16();
-		skipBytes(8);
-		encoding = readU32();	
-		reset();	
+		rectX = readU16();
+		rectY = readU16();
+		rectW = readU16();
+		rectH = readU16();
+		encoding = readU32();
+		if(encoding == 16)
+			zLen = readU32();
+		reset();
+	}
+	void checkAndMark() throws IOException{
+		switch(encoding){
+		case RfbProto.EncodingRaw:
+			mark(rectW * rectH * 4 + 16);		
+			break;
+		case RfbProto.EncodingZRLE:
+			mark(zLen);
+			break;
+		default:
+			mark(1000000);
+			}
+	}
+	BufferedImage createBufferedImage(Image img){
+		BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB  );
+
+		Graphics g = bimg.getGraphics();
+		g.drawImage(img, 0, 0, null);
+		g.dispose();
+		return bimg;
+	}
+
+	void createPngBytes(BufferedImage bimg)throws IOException {
+		pngBytes = getImageBytes(bimg , "png");
+	}
+	byte[] getBytes(BufferedImage img)throws IOException { 
+		byte[] b = getImageBytes(img, "png");
+		return b;
 	}
 	
-	
+	byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException {
+		ByteArrayOutputStream bos = new ByteArrayOutputStream();
+		BufferedOutputStream os = new BufferedOutputStream(bos);
+		image.flush();
+		ImageIO.write(image, imageFormat, os);
+		os.flush();
+		os.close();
+		return bos.toByteArray();
+	}
+
+	void sendPngData(Socket sock)throws IOException{
+		byte[] dataLength = castIntByte(pngBytes.length);
+		sock.getOutputStream().write(dataLength);
+		sock.getOutputStream().write(pngBytes);
+	}
+	byte[] castIntByte(int len){
+		byte[] b = new byte[4];
+		b[0] = (byte)((len >>> 24 ) & 0xFF);
+		b[1] = (byte)((len >>> 16 ) & 0xFF);
+		b[2] = (byte)((len >>> 8  ) & 0xFF);
+		b[3] = (byte)((len >>> 0  ) & 0xFF);
+		return b;
+	}
 	
-	
+	BufferedImage createBimg()throws IOException{
+		BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
+		return bimg;
+	}
 	void printFramebufferUpdate(){
 	
 		System.out.println("messageType=" + messageType);
 		System.out.println("rectangles="+rectangles);
 		System.out.println("encoding=" + encoding);
+		switch(encoding){
+		case RfbProto.EncodingRaw:
+			System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
+			break;
+		default:
+		}
 	}
-	
-	
 }