changeset 128:fa2122e5c807

modify ProxyVncCanva
author e085711
date Tue, 02 Aug 2011 20:15:01 +0900
parents 97bb1436e34c
children acd88e63854b
files src/myVncProxy/MyRfbProto.java src/myVncProxy/ProxyVncCanvas.java src/myVncProxy/RfbProto.java src/myVncProxy/VncProxyService.java
diffstat 4 files changed, 111 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Tue Aug 02 13:17:23 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Tue Aug 02 20:15:01 2011 +0900
@@ -3,10 +3,12 @@
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
+import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -48,9 +50,12 @@
 	private LinkedList<Socket> cliList;
 	private LinkedList<Thread> sendThreads;
 	boolean createBimgFlag;
-
+	boolean sendFlag = true;
 	ExecutorService executor;
 
+	// override viewer to VncProxyService from VncViewer
+	VncProxyService viewer;
+	
 	byte[] pngBytes;
 
 	private MulticastQueue<byte[]> multicastqueue = new MulticastQueue<byte[]>();
@@ -65,6 +70,11 @@
 		// executor = Executors.newSingleThreadExecutor();
 	}
 
+	MyRfbProto(String h, int p, VncProxyService v) throws IOException {
+		super(h, p);
+		viewer = v;
+	}
+
 	MyRfbProto(String h, int p) throws IOException {
 		super(h, p);
 		cliList = new LinkedList<Socket>();
@@ -74,7 +84,8 @@
 		// executor = Executors.newCachedThreadPool();
 		// executor = Executors.newSingleThreadExecutor();
 	}
-
+	
+	
 	// over write
 	void writeVersionMsg() throws IOException {
 		clientMajor = 3;
@@ -103,9 +114,9 @@
 		acceptPort = port;
 	}
 
-	// 5550を開けるが、開いてないなら+1のポートを開ける。
+	// open port 5999 for to accept client.
 	void selectPort() {
-		int i = 5955;// i = 5550;
+		int i = 5999;// i = 5550;
 		while (true) {
 			try {
 				initServSock(i);
@@ -317,6 +328,9 @@
 		case RfbProto.EncodingRaw:
 			dataLen = rectW * rectH * 4 + 16;
 			break;
+		case RfbProto.EncodingTight:
+			dataLen = 4000000;
+			break;
 		case RfbProto.EncodingZRLE:
 			int zLen = readU32();
 			dataLen = zLen + 20;
@@ -324,9 +338,9 @@
 		case RfbProto.EncodingRRE:
 		case RfbProto.EncodingCoRRE:
 		case RfbProto.EncodingHextile:
+			dataLen = rectW * rectH * 4 + 16;
+			break;
 		case RfbProto.EncodingZlib:
-		case RfbProto.EncodingTight:
-		// dataLen <= EncodingRaw
 		default:
 			dataLen = rectW * rectH * 4 + 16;
 			break;
@@ -334,7 +348,7 @@
 		System.out.println("dataLen = "+dataLen);
 		is.reset();
 		is.mark(dataLen);
-
+		
 	}
 
 	
@@ -477,8 +491,8 @@
 		System.out.println("rectW * rectH = " + rectW * rectH);
 		switch (encoding) {
 		case RfbProto.EncodingRaw:
-			System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4
-					+ 16);
+			int dataLen = rectW * rectH * 4 + 16;
+			System.out.println("rectW * rectH * 4 + 16 = " + (rectW * rectH * 4 + 16));
 			break;
 		default:
 		}
@@ -520,14 +534,58 @@
 			changeStatusFlag();
 		}
 	}
+	void require() throws IOException {
+		sendFlag = false;
+		System.out.println("setEncodingRaw()");
+		setEncodingRaw();
+		writeFramebufferUpdateRequest(0, 0,
+				16, 16, false);
+		System.out.println("setEncodingZRLE()");
+		setEncodingZRLE();
+		System.out.println("writeFramebufferUpdateRequest");
+		writeFramebufferUpdateRequest(0, 0,
+				framebufferWidth, framebufferHeight, false);
+	}
+
+	void setEncodingRaw() throws IOException{
+		byte[] b = new byte[4 + 4];
+
+		b[0] = (byte) SetEncodings;
+		b[2] = (byte) ((1 >> 8) & 0xff);
+		b[3] = (byte) (1 & 0xff);
+
+		b[4] = (byte)0;
+		b[5] = (byte)0;
+		b[6] = (byte)0;
+		b[7] = (byte)0;
+		
+		os.write(b);
+		
+	}
+
+	void setEncodingZRLE() throws IOException{
+		byte[] b = new byte[4 + 4];
+
+		b[0] = (byte) SetEncodings;
+		b[2] = (byte) ((1 >> 8) & 0xff);
+		b[3] = (byte) (1 & 0xff);
+
+		b[4] = (byte)0;
+		b[5] = (byte)0;
+		b[6] = (byte)0;
+		b[7] = (byte)16;
+		
+		os.write(b);
+		
+	}
 	
-
 	void newClient(AcceptThread acceptThread, final Socket newCli,
 			final OutputStream os, final InputStream is) throws IOException {
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
 		//		addSock(newCli);
 		final Client<byte[]> c = multicastqueue.newClient();
+		require();
 		Runnable sender = new Runnable() {
 			public void run() {
 				try {
@@ -571,7 +629,10 @@
 							case 's':
 								break;
 							default:
-								startSpeedCheck();
+//								startSpeedCheck();
+								writeFramebufferUpdateRequest(0, 0, framebufferWidth,
+										framebufferHeight, false);
+								
 								break;
 						}
 					}
@@ -584,6 +645,30 @@
 		new Thread(stdin).start();
 	}
 
+	void requireFramebuffer() {
+		Runnable stdin = new Runnable() {
+			public void run() {
+				int c;
+				try {
+					while ((c = System.in.read()) != -1) {
+						sendFlag = false; 
+						switch (c) {
+						default:
+							System.out.println("writeFramebufferUpdateRequest()");
+							writeFramebufferUpdateRequest(0, 0,
+									framebufferWidth, framebufferHeight, false);
+							break;
+						}
+					}
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		};
+
+		new Thread(stdin).start();
+	}
+
 }
 
 
--- a/src/myVncProxy/ProxyVncCanvas.java	Tue Aug 02 13:17:23 2011 +0900
+++ b/src/myVncProxy/ProxyVncCanvas.java	Tue Aug 02 20:15:01 2011 +0900
@@ -352,6 +352,8 @@
 
 		rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
 				rfb.framebufferHeight, false);
+		
+		rfb.requireFramebuffer();
 
 		resetStats();
 		boolean statsRestarted = false;
@@ -374,7 +376,7 @@
 			rfb.regiFramebufferUpdate();
 			rfb.printFramebufferUpdate();
 			
-			int bufSize = (int)rfb.getNumBytesRead();
+			long bytesRead = rfb.getNumBytesRead();
 			
 			// Read message type from the server. 
 			int msgType = rfb.readServerMessageType();
@@ -530,8 +532,8 @@
 				throw new Exception("Unknown RFB message type " + msgType);
 			}
 
-			bufSize = (int)rfb.getNumBytesRead() - bufSize;
-			System.out.println("bufSize="+bufSize);
+			int bufSize = (int)(rfb.getNumBytesRead() - bytesRead);
+			System.out.println("bufSize = " + bufSize);
 			rfb.bufResetSend(bufSize);
 
 
--- a/src/myVncProxy/RfbProto.java	Tue Aug 02 13:17:23 2011 +0900
+++ b/src/myVncProxy/RfbProto.java	Tue Aug 02 20:15:01 2011 +0900
@@ -180,6 +180,7 @@
 	//
 	// Constructor. Make TCP connection to RFB server.
 	//
+
 	RfbProto(String h, int p, VncViewer v) throws IOException {
 		viewer = v;
 		host = h;
@@ -1369,7 +1370,6 @@
 	final int readU8() throws IOException {
 		int r = is.readUnsignedByte();
 		numBytesRead++;
-
 		return r;
 	}
 
--- a/src/myVncProxy/VncProxyService.java	Tue Aug 02 13:17:23 2011 +0900
+++ b/src/myVncProxy/VncProxyService.java	Tue Aug 02 20:15:01 2011 +0900
@@ -208,8 +208,10 @@
 
 		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
 
-		// rfb = new RfbProto(host, port, this);
-		rfb = new MyRfbProto(host, port);
+//		rfb = new RfbProto(host, port, this);
+//		rfb = new MyRfbProto(host, port);
+		rfb = new MyRfbProto(host, port, this);
+		
 		showConnectionStatus("Connected to server");
 
 		rfb.readVersionMsg();
@@ -323,10 +325,11 @@
 		if (preferredEncoding == -1) {
 			long kbitsPerSecond = rfb.kbitsPerSecond();
 
+/*
 			if (nEncodingsSaved < 1) {
 				// Choose Tight or ZRLE encoding for the very first update.
 				System.out.println("Using Tight/ZRLE encodings");
-//				preferredEncoding = RfbProto.EncodingTight;
+				preferredEncoding = RfbProto.EncodingTight;
 			} else if (kbitsPerSecond > 2000
 					&& encodingsSaved[0] != RfbProto.EncodingHextile) {
 				// Switch to Hextile if the connection speed is above 2Mbps.
@@ -338,13 +341,14 @@
 				// Switch to Tight/ZRLE if the connection speed is below 1Mbps.
 				System.out.println("Throughput " + kbitsPerSecond
 						+ " kbit/s - changing to Tight/ZRLE encodings");
-//				preferredEncoding = RfbProto.EncodingTight;
+				preferredEncoding = RfbProto.EncodingTight;
 			} else {
 				// Don't change the encoder.
 				if (autoSelectOnly)
 					return;
 				preferredEncoding = encodingsSaved[0];
 			}
+*/			
 		} else {
 			// Auto encoder selection is not enabled.
 			if (autoSelectOnly)
@@ -359,14 +363,12 @@
 		if (options.useCopyRect) {
 			encodings[nEncodings++] = RfbProto.EncodingCopyRect;
 		}
-/*
 		if (preferredEncoding != RfbProto.EncodingTight) {
 			encodings[nEncodings++] = RfbProto.EncodingTight;
 		}
 		if (preferredEncoding != RfbProto.EncodingZRLE) {
 			encodings[nEncodings++] = RfbProto.EncodingZRLE;
 		}
-*/
 		if (preferredEncoding != RfbProto.EncodingHextile) {
 			encodings[nEncodings++] = RfbProto.EncodingHextile;
 		}
@@ -381,6 +383,7 @@
 		if (preferredEncoding != RfbProto.EncodingRRE) {
 			encodings[nEncodings++] = RfbProto.EncodingRRE;
 		}
+*/
 /*
 		if (options.compressLevel >= 0 && options.compressLevel <= 9) {
 			encodings[nEncodings++] = RfbProto.EncodingCompressLevel0