changeset 19:965360af5f0b

merged some file
author e085711
date Tue, 26 Apr 2011 15:26:15 +0900
parents 4881586aead9
children 83cdd36be9c0
files src/myVncClient/MyRfbProto.java src/myVncClient/RfbProto.java src/myVncClient/VncCanvas.java src/myVncClient/VncViewer.java src/myVncClient/acceptThread.java
diffstat 5 files changed, 149 insertions(+), 42 deletions(-) [+]
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:
+		}
 	}
-	
-	
 }
--- a/src/myVncClient/RfbProto.java	Tue Apr 26 09:08:14 2011 +0900
+++ b/src/myVncClient/RfbProto.java	Tue Apr 26 15:26:15 2011 +0900
@@ -689,8 +689,6 @@
 	void readFramebufferUpdate() throws IOException {
 		skipBytes(1);
 		updateNRects = readU16();
-		// System.out.println(updateNRects);
-
 		// If the session is being recorded:
 		if (rec != null) {
 			rec.writeByte(FramebufferUpdate);
--- a/src/myVncClient/VncCanvas.java	Tue Apr 26 09:08:14 2011 +0900
+++ b/src/myVncClient/VncCanvas.java	Tue Apr 26 15:26:15 2011 +0900
@@ -52,6 +52,8 @@
 	Graphics memGraphics;
 
 	Image rawPixelsImage;
+	BufferedImage bimg;
+	
 	MemoryImageSource pixelsSource;
 	byte[] pixels8;
 	int[] pixels24;
@@ -386,21 +388,18 @@
 		accept.start();
 
 		while (true) {
-			System.out.println("\ncount=" + count);
+//			System.out.println("\ncount=" + count);
 			count++;
 			
 			rfb.regiFramebufferUpdate();
-			rfb.printFramebufferUpdate();
+			rfb.checkAndMark();
+//			rfb.printFramebufferUpdate();
 			
-			rfb.mark(10000000);
-
 			int bufSize = (int)rfb.getNumBytesRead();
 			
 			
 			// Read message type from the server.
 			int msgType = rfb.readServerMessageType();
-			int len = rfb.available();
-			System.out.println("rfb.available()=" + len);
 
 			// Process the message depending on its type.
 			switch (msgType) {
@@ -427,9 +426,6 @@
 					int rx = rfb.updateRectX, ry = rfb.updateRectY;
 					int rw = rfb.updateRectW, rh = rfb.updateRectH;
 
-					System.out.println("rx="+rx+" ry="+ry+" rw="+rw+" rh="+rh);
-					
-					
 					if (rfb.updateRectEncoding == rfb.EncodingLastRect)
 						break;
 
@@ -550,9 +546,19 @@
 			default:
 				throw new Exception("Unknown RFB message type " + msgType);
 			}
+			
 			bufSize = (int)rfb.getNumBytesRead() - bufSize;
-			System.out.println("bufSize="+bufSize);
+//			System.out.println("bufSize="+bufSize);
 			rfb.bufResetSend(bufSize);
+
+			if(rfb.createBimgFlag){
+				bimg = createBufferedImage(rawPixelsImage);
+				//bimg(BufferedImage) -> rfb.pngBytes(byte[])
+				rfb.createPngBytes(bimg);
+				rfb.sendPngImage();	
+				rfb.createBimgFlag = false;
+			}
+			
 		}
 	}
 
@@ -1911,4 +1917,16 @@
 		memGraphics.drawImage( bimg, 0,0, null);
 		
 	}
+
+	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;
+	}
+	
+	
+	
 }
--- a/src/myVncClient/VncViewer.java	Tue Apr 26 09:08:14 2011 +0900
+++ b/src/myVncClient/VncViewer.java	Tue Apr 26 15:26:15 2011 +0900
@@ -140,8 +140,10 @@
 			
 			rfb = new MyRfbProto(host, port, this);
 			rfb.readServerInit();
+			rfb.readPngData();
 			
 			createCanvas(0, 0);
+			vc.drawFirstImage();
 		} catch (IOException e) {
 			System.out.println("Socket error");
 			System.exit(0);
--- a/src/myVncClient/acceptThread.java	Tue Apr 26 09:08:14 2011 +0900
+++ b/src/myVncClient/acceptThread.java	Tue Apr 26 15:26:15 2011 +0900
@@ -12,15 +12,16 @@
 
 	public void run() {
 		rfb.selectPort();
-		
 		while (true) {
 			try {
 				Socket newCli = rfb.accept();
 				rfb.sendInitData(newCli);
-				rfb.addSock(newCli);
+				rfb.createBimgFlag = true;
+				rfb.addSockTmp(newCli);
 			} catch (IOException e) {
-
+				e.printStackTrace();
+				System.out.println(e);
 			}
 		}
 	}
-}
+}
\ No newline at end of file