diff src/treeVnc/RfbProto.java @ 3:3b341997141a

broadcast socket trying
author one
date Mon, 23 Apr 2012 20:18:23 +0900
parents 5d72f4c7371d
children 657c691c2936
line wrap: on
line diff
--- a/src/treeVnc/RfbProto.java	Sat Apr 07 14:55:34 2012 +0900
+++ b/src/treeVnc/RfbProto.java	Mon Apr 23 20:18:23 2012 +0900
@@ -1,4 +1,5 @@
 package treeVnc;
+
 //
 //  Copyright (C) 2001-2004 HorizonLive.com, Inc.  All Rights Reserved.
 //  Copyright (C) 2001-2006 Constantin Kaplinsky.  All Rights Reserved.
@@ -27,16 +28,21 @@
 
 import java.io.*;
 import java.awt.event.*;
+import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.net.SocketException;
 import java.nio.ByteBuffer;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.spi.AbstractSelector;
+import java.nio.channels.spi.SelectorProvider;
 import java.util.LinkedList;
 import java.util.zip.*;
 
 public class RfbProto {
 
 	final static String versionMsg_3_3 = "RFB 003.003\n",
-			versionMsg_3_7 = "RFB 003.007\n", versionMsg_3_8 = "RFB 003.008\n",versionMsg_3_855 = "RFB 003.855\n";
-
+			versionMsg_3_7 = "RFB 003.007\n", versionMsg_3_8 = "RFB 003.008\n",
+			versionMsg_3_855 = "RFB 003.855\n";
 
 	// Vendor signatures: standard VNC/RealVNC, TridiaVNC, and TightVNC
 	final static String StandardVendor = "STDV", TridiaVncVendor = "TRDV",
@@ -61,7 +67,7 @@
 	// Standard server-to-client messages
 	final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2,
 			ServerCutText = 3;
-	
+
 	// Check Delay Top form Bottom
 	final static int CheckDelay = 11;
 
@@ -80,7 +86,7 @@
 
 	// Supported encodings and pseudo-encodings
 	final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2,
- 			EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6,
+			EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6,
 			EncodingTight = 7, EncodingZRLEE = 15, EncodingZRLE = 16,
 			EncodingCompressLevel0 = 0xFFFFFF00,
 			EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = 0xFFFFFF10,
@@ -90,8 +96,7 @@
 			SigEncodingCopyRect = "COPYRECT", SigEncodingRRE = "RRE_____",
 			SigEncodingCoRRE = "CORRE___", SigEncodingHextile = "HEXTILE_",
 			SigEncodingZlib = "ZLIB____", SigEncodingTight = "TIGHT___",
-			SigEncodingZRLEE = "ZRLEE___",
-			SigEncodingZRLE = "ZRLE____",
+			SigEncodingZRLEE = "ZRLEE___", SigEncodingZRLE = "ZRLE____",
 			SigEncodingCompressLevel0 = "COMPRLVL",
 			SigEncodingQualityLevel0 = "JPEGQLVL",
 			SigEncodingXCursor = "X11CURSR",
@@ -114,6 +119,7 @@
 			TightFilterCopy = 0x00, TightFilterPalette = 0x01,
 			TightFilterGradient = 0x02;
 
+	static AbstractSelector selector;
 	String host;
 	int port;
 	Socket sock;
@@ -126,16 +132,15 @@
 	// Input stream is declared private to make sure it can be accessed
 	// only via RfbProto methods. We have to do this because we want to
 	// count how many bytes were read.
-//	private DataInputStream is;
+	// private DataInputStream is;
 	protected DataInputStream is;
-//	private long numBytesRead = 0;
+	// private long numBytesRead = 0;
 	protected long numBytesRead = 0;
 
 	public long getNumBytesRead() {
 		return numBytesRead;
 	}
 
-
 	// Java on UNIX does not call keyPressed() on some keys, for example
 	// swedish keys To prevent our workaround to produce duplicate
 	// keypresses on JVMs that actually works, keep track of if
@@ -178,9 +183,9 @@
 	CapsContainer encodingCaps;
 
 	// If true, informs that the RFB socket was closed.
-//	private boolean closed;
+	// private boolean closed;
 	protected boolean closed;
-	
+
 	private byte[] broadCastBuf = new byte[64000];
 
 	//
@@ -192,7 +197,7 @@
 		port = p;
 
 		if (viewer.socketFactory == null) {
-			sock = new Socket(host, port);
+			sock = newSocket(host, port);
 		} else {
 			try {
 				Class factoryClass = Class.forName(viewer.socketFactory);
@@ -215,29 +220,13 @@
 		timeWaitedIn100us = 5;
 		timedKbits = 0;
 	}
-	
+
 	RfbProto(String h, int p) throws IOException {
 		host = h;
 		port = p;
 
-		sock = new Socket(host, port);
-
-		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),16384));
-		os = sock.getOutputStream();
+		sock = newSocket(host, port);
 
-		timing = false;
-		timeWaitedIn100us = 5;
-		timedKbits = 0;
-	}
-	
-	public RfbProto() {
-		
-	}
-	
-	public void changeRfbProto(String h,int port) throws IOException {
-		host = h;
-		sock=null;
-		sock = new Socket(host, port);
 		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
 				16384));
 		os = sock.getOutputStream();
@@ -247,7 +236,42 @@
 		timedKbits = 0;
 	}
 
+	private ServerSocket newSocket(String host, int port) {
+		ServerSocketChannel ssChannel = SelectorProvider.provider().openServerSocketChannel();
+		ssChannel.socket().setReuseAddress(true);
+		// this should work for IPv6/IPv4 dual stack
+		// check this using netstat -an result tcp46.
+		try {
+			InetSocketAddress address = new InetSocketAddress(host, port);
+			ssChannel.socket().bind(address);
+		} catch (SocketException e) {
+			// for some bad IPv6 implementation
+			ssChannel.socket().bind(new InetSocketAddress(port));
+		}
+		ssChannel.configureBlocking(false);
+		return ssChannel.socket();
+	}
 
+	public RfbProto() {
+
+	}
+
+	public void initOnce() throws IOException {
+		selector = SelectorProvider.provider().openSelector();
+	}
+
+	public void changeRfbProto(String h, int port) throws IOException {
+		host = h;
+		sock = null;
+		sock = newSocket(host, port);
+		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
+				16384));
+		os = sock.getOutputStream();
+
+		timing = false;
+		timeWaitedIn100us = 5;
+		timedKbits = 0;
+	}
 
 	synchronized void close() {
 		try {
@@ -314,7 +338,7 @@
 		protocolTightVNC = false;
 		initCapabilities();
 	}
- 
+
 	//
 	// Negotiate the authentication scheme.
 	//
@@ -367,18 +391,17 @@
 				return SecTypeTight;
 			}
 		}
-		
 
 		// Find first supported security type.
 		for (int i = 0; i < nSecTypes; i++) {
-//			if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth) {
-			if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth 
-				|| secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess) {
-					secType = secTypes[i];
+			// if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth)
+			// {
+			if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth
+					|| secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess) {
+				secType = secTypes[i];
 				break;
 			}
-			}
-		
+		}
 
 		if (secType == SecTypeInvalid) {
 			throw new Exception("Server did not offer supported security type");
@@ -494,9 +517,9 @@
 		encodingCaps.add(EncodingHextile, StandardVendor, SigEncodingHextile,
 				"Standard Hextile encoding");
 		encodingCaps.add(EncodingZRLE, StandardVendor, SigEncodingZRLE,
-	    		"Standard ZRLE encoding");		
+				"Standard ZRLE encoding");
 		encodingCaps.add(EncodingZRLEE, StandardVendor, SigEncodingZRLEE,
-	    		"Standard ZRLE(E) encoding");
+				"Standard ZRLE(E) encoding");
 		encodingCaps.add(EncodingZlib, TridiaVncVendor, SigEncodingZlib,
 				"Zlib encoding");
 		encodingCaps.add(EncodingTight, TightVncVendor, SigEncodingTight,
@@ -589,17 +612,17 @@
 	//
 
 	void writeClientInit() throws IOException {
-/*
-		if (viewer.options.shareDesktop) {
-*/
-		
+		/*
+		 * if (viewer.options.shareDesktop) {
+		 */
+
 		/**
 		 * shared flag
 		 */
-			os.write(1);
-//			os.write(0);
+		os.write(1);
+		// os.write(0);
 
-//		viewer.options.disableShareDesktop();
+		// viewer.options.disableShareDesktop();
 	}
 
 	//
@@ -854,13 +877,12 @@
 	//
 	// Write a FramebufferUpdateRequest message
 	//
-	
-	
+
 	void checkDelayData() throws IOException {
 		System.out.println("sousinn");
-			byte[] b = new byte[1];
-			b[0] = (byte) CheckDelay;
-			os.write(b);
+		byte[] b = new byte[1];
+		b[0] = (byte) CheckDelay;
+		os.write(b);
 	}
 
 	void writeFramebufferUpdateRequest(int x, int y, int w, int h,
@@ -992,8 +1014,6 @@
 	final static int META_MASK = InputEvent.META_MASK;
 	final static int ALT_MASK = InputEvent.ALT_MASK;
 
-
-
 	//
 	// Write a pointer event message. We may need to send modifier key events
 	// around it to set the correct modifier state.
@@ -1007,11 +1027,9 @@
 		int mask2 = 2;
 		int mask3 = 4;
 		/*
-		if (viewer.options.reverseMouseButtons2And3) {
-			mask2 = 4;
-			mask3 = 2;
-		}
-		*/
+		 * if (viewer.options.reverseMouseButtons2And3) { mask2 = 4; mask3 = 2;
+		 * }
+		 */
 
 		// Note: For some reason, AWT does not set BUTTON1_MASK on left
 		// button presses. Here we think that it was the left button if
@@ -1370,49 +1388,39 @@
 		readFully(b, 0, b.length);
 	}
 
-	long before = System.currentTimeMillis(); 
+	long before = System.currentTimeMillis();
+
 	public void readFully(byte b[], int off, int len) throws IOException {
-//		long before = 0;
+		// long before = 0;
 		if (timing)
 			before = System.currentTimeMillis();
 
 		is.readFully(b, off, len);
-		
+
 		/*
-		if(b.length==16) {
-			b[4] = (byte)0;
-			b[5] = (byte)0;
-			b[6] = (byte)0;
-			b[7] = (byte)0;
-			System.out.println("----------------------");
-		}
-		*/
-				//System.out.println("Blength:"+b.length);
-		//for(int i=0 ; i<=b.length ; i++) { 
-			//if(i>b.length/2)
-				//b[i] = 10;
-		//}
+		 * if(b.length==16) { b[4] = (byte)0; b[5] = (byte)0; b[6] = (byte)0;
+		 * b[7] = (byte)0; System.out.println("----------------------"); }
+		 */
+		// System.out.println("Blength:"+b.length);
+		// for(int i=0 ; i<=b.length ; i++) {
+		// if(i>b.length/2)
+		// b[i] = 10;
+		// }
 
 		/*
-		if (timing) {
-			long after = System.currentTimeMillis();
-			long newTimeWaited = (after - before) * 10;
-			int newKbits = len * 8 / 1000;
-
-			// limit rate to between 10kbit/s and 40Mbit/s
-
-			if (newTimeWaited > newKbits * 1000)
-				newTimeWaited = newKbits * 1000;
-			if (newTimeWaited < newKbits / 4)
-				newTimeWaited = newKbits / 4;
-
-			timeWaitedIn100us += newTimeWaited;
-			timedKbits += newKbits;
-			before = after;
-		}
-	*/
+		 * if (timing) { long after = System.currentTimeMillis(); long
+		 * newTimeWaited = (after - before) * 10; int newKbits = len * 8 / 1000;
+		 * 
+		 * // limit rate to between 10kbit/s and 40Mbit/s
+		 * 
+		 * if (newTimeWaited > newKbits * 1000) newTimeWaited = newKbits * 1000;
+		 * if (newTimeWaited < newKbits / 4) newTimeWaited = newKbits / 4;
+		 * 
+		 * timeWaitedIn100us += newTimeWaited; timedKbits += newKbits; before =
+		 * after; }
+		 */
 		numBytesRead += len;
-		//System.out.println("numBytesRead:"+numBytesRead);
+		// System.out.println("numBytesRead:"+numBytesRead);
 	}
 
 	final int available() throws IOException {
@@ -1446,7 +1454,8 @@
 		return r;
 	}
 
-	public LinkedList<ByteBuffer> blockingUpdateRectangle(ByteBuffer input,int w,int h) {
+	public LinkedList<ByteBuffer> blockingUpdateRectangle(
+			LinkedList<ByteBuffer> input, int w, int h) {
 
 		return null;
 	}