changeset 17:718cdde720d4

add setInitData()
author one
date Tue, 28 Aug 2012 17:36:49 +0900
parents e654b2e4de64
children 075fb190d5d4
files src/main/java/com/glavsoft/rfb/encoding/ServerInitMessage.java src/main/java/com/glavsoft/transport/Reader.java src/main/java/com/glavsoft/transport/Writer.java src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptThread.java src/main/java/jp/ac/u_ryukyu/treevnc/server/CreateThread.java src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java
diffstat 6 files changed, 150 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/rfb/encoding/ServerInitMessage.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/ServerInitMessage.java	Tue Aug 28 17:36:49 2012 +0900
@@ -24,6 +24,8 @@
 
 package com.glavsoft.rfb.encoding;
 
+import java.io.IOException;
+
 import com.glavsoft.exceptions.TransportException;
 import com.glavsoft.transport.Reader;
 
@@ -40,8 +42,10 @@
     protected int frameBufferHeight;
     protected PixelFormat pixelFormat;
     protected String name;
+    protected byte[] initData;
 
 	public ServerInitMessage(Reader reader) throws TransportException {
+		readServerInit(reader);
 		frameBufferWidth = reader.readUInt16();
 		frameBufferHeight = reader.readUInt16();
 		pixelFormat = new PixelFormat();
@@ -77,4 +81,18 @@
     	", server-pixel-format: " + pixelFormat +
     	"]";
     }
+    
+	public void readServerInit(Reader reader) throws TransportException {
+
+		reader.mark(255);
+		reader.skypBytes(20);
+		int nlen = reader.readInt32();
+		int blen = 20 + 4 + nlen;
+		initData = new byte[blen];
+		reader.reset();
+
+		reader.mark(blen);
+		reader.readBytes(initData);
+		reader.reset();
+	}
 }
--- a/src/main/java/com/glavsoft/transport/Reader.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/com/glavsoft/transport/Reader.java	Tue Aug 28 17:36:49 2012 +0900
@@ -85,28 +85,29 @@
 	}
 
 	/**
-	 * Read string by it length.
-	 * Use this method only when sure no character accept ASCII will be read.
-	 * Use readBytes and character encoding conversion instead.
-	 *
+	 * Read string by it length. Use this method only when sure no character
+	 * accept ASCII will be read. Use readBytes and character encoding
+	 * conversion instead.
+	 * 
 	 * @return String read
 	 */
 	public String readString(int length) throws TransportException {
-        return new String(readBytes(length));
+		return new String(readBytes(length));
 	}
 
 	/**
-	 * Read 32-bit string length and then string themself by it length
-	 * Use this method only when sure no character accept ASCII will be read.
-	 * Use readBytes and character encoding conversion instead.
-	 *
+	 * Read 32-bit string length and then string themself by it length Use this
+	 * method only when sure no character accept ASCII will be read. Use
+	 * readBytes and character encoding conversion instead.
+	 * 
 	 * @return String read
 	 * @throws TransportException
 	 */
 	public String readString() throws TransportException {
 		// unset most sighificant (sign) bit 'cause InputStream#readFully
 		// reads
-		// [int] length bytes from stream. Change when realy need read sthing more
+		// [int] length bytes from stream. Change when realy need read sthing
+		// more
 		// than
 		// 2147483647 bytes lenght
 		int length = readInt32() & Integer.MAX_VALUE;
@@ -118,27 +119,68 @@
 		return readBytes(b, 0, length);
 	}
 
-	public byte[] readBytes(byte[] b, int offset, int length) throws TransportException {
+	public byte[] readBytes(byte[] b, int offset, int length)
+			throws TransportException {
 		try {
 			is.readFully(b, offset, length);
 			return b;
 		} catch (EOFException e) {
 			throw new ClosedConnectionException(e);
 		} catch (IOException e) {
-			throw new TransportException("Cannot read " + length + " bytes array", e);
+			throw new TransportException("Cannot read " + length
+					+ " bytes array", e);
 		}
 	}
-	
-	public byte[] readBytes(byte[]b) throws TransportException {
-		byte[] result = readBytes(b,0,b.length);
+
+	public byte[] readBytes(byte[] b) throws TransportException {
+		byte[] result = readBytes(b, 0, b.length);
 		return result;
 	}
-	
-	public void reset() throws IOException {
-		is.reset();
+
+	public void reset() throws TransportException {
+		try {
+			is.reset();
+		} catch (IOException e) {
+			throw new TransportException("Cannot reset", e);
+		}
 	}
-	
+
 	public void mark(int readLimit) {
 		is.mark(readLimit);
 	}
+
+	public int readByte(byte[] b) throws TransportException {
+		int result = 0;
+		try {
+			result = is.read(b);
+			return result;
+		} catch (IOException e) {
+			throw new TransportException("Cannot readByte", e);
+		}
+	}
+
+	public void close() throws TransportException {
+		try {
+			is.close();
+		} catch (IOException e) {
+			throw new TransportException("Cannot close", e);
+		}
+	}
+
+	public void read(byte[] b) throws TransportException {
+		try {
+			is.read(b);
+		} catch (IOException e) {
+			throw new TransportException("Cannot read", e);
+		}
+	}
+
+	public int skypBytes(int n) throws TransportException {
+		try {
+			int r = is.skipBytes(n);
+			return r;
+		} catch (IOException e) {
+			throw new TransportException("Cannot skipBytes", e);
+		}
+	}
 }
\ No newline at end of file
--- a/src/main/java/com/glavsoft/transport/Writer.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/com/glavsoft/transport/Writer.java	Tue Aug 28 17:36:49 2012 +0900
@@ -84,7 +84,7 @@
 	public void write(byte[] b) throws TransportException {
 		write(b, 0, b.length);
 	}
-
+	
 	public void write(byte[] b, int length) throws TransportException {
 		write(b, 0, length);
 	}
@@ -97,4 +97,17 @@
 			throw new TransportException("Cannot write " + length + " bytes", e);
 		}
 	}
+
+	public void close() throws IOException {
+		os.close();
+	}
+	
+	public void writeInt(int i) throws TransportException {
+		try {
+			os.write(i);
+		} catch (IOException e) {
+			throw new TransportException("Cannot write int", e);
+		}
+	}
+	
 }
\ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptThread.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptThread.java	Tue Aug 28 17:36:49 2012 +0900
@@ -4,6 +4,9 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import com.glavsoft.transport.Reader;
+import com.glavsoft.transport.Writer;
+
 public class AcceptThread implements Runnable {
         MyRfbProtoProxy rfb = null;
         byte[] imageBytes;
@@ -32,7 +35,7 @@
 
 				OutputStream os = newCli.getOutputStream();
 				InputStream is = newCli.getInputStream();
-				rfb.newClient(this, newCli, os, is);
+				rfb.newClient(this, newCli, new Writer(os), new Reader(is));
 			} catch (IOException e) {
 				e.printStackTrace();
 				System.out.println(e);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/CreateThread.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/CreateThread.java	Tue Aug 28 17:36:49 2012 +0900
@@ -11,7 +11,7 @@
 public class CreateThread implements Runnable {
 	ServerSocket echoServer;
 	AcceptClient acceptClient;
-	int port;
+	private int port;
 	
 	public CreateThread(AcceptClient _acc) {
 		acceptClient = _acc;
@@ -57,6 +57,10 @@
 		this.port = port;
 	}
 	
+	public int getPort() {
+		return port;
+	}
+	
 	
 	public void run() {
 		selectPort(port);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java	Mon Aug 27 06:31:14 2012 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java	Tue Aug 28 17:36:49 2012 +0900
@@ -76,8 +76,8 @@
 	private LinkedList<Socket> cliList;
 	boolean createBimgFlag;
 	boolean proxyFlag = true;
-	private Reader reader;
-	private Writer writer;
+	private Reader is;
+	private Writer os;
 	ExecutorService executor;
 
 	byte[] pngBytes;
@@ -88,7 +88,7 @@
 	private int clients = 0;
 	private Inflater inflater = new Inflater();
 	private Deflater deflater = new Deflater();
-	private CreateThread geth;
+	private CreateThread getHost;
 	// private Thread requestThread;
 	private RequestScreenThread rThread;
 	private Thread requestThread;
@@ -99,8 +99,8 @@
 	}
 
 	public void setStream(Reader _reader, Writer _writer) {
-		reader = _reader;
-		writer = _writer;
+		is = _reader;
+		os = _writer;
 	}
 
 	void initServSock(int port) throws IOException {
@@ -293,7 +293,7 @@
 			TransportException {
 		LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
 		ByteBuffer header = ByteBuffer.allocate(16);
-		reader.readBytes(header.array(), 0, 16);
+		is.readBytes(header.array(), 0, 16);
 		header.limit(16);
 		if (header.get(0) == FramebufferUpdate) {
 			int encoding = header.getInt(12);
@@ -302,11 +302,11 @@
 																// already
 				// recompressed
 				ByteBuffer len = ByteBuffer.allocate(4);
-				reader.readBytes(len.array(), 0, 4);
+				is.readBytes(len.array(), 0, 4);
 				len.limit(4);
 				ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
 
-				reader.readBytes(inputData.array(), 0, inputData.capacity());
+				is.readBytes(inputData.array(), 0, inputData.capacity());
 				// System.out.println(dataLen);
 				inputData.limit(dataLen - 20);
 
@@ -345,7 +345,7 @@
 			bufs.add(header);
 			if (dataLen > 16) {
 				ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
-				reader.readBytes(b.array(), 0, dataLen - 16);
+				is.readBytes(b.array(), 0, dataLen - 16);
 				b.limit(dataLen - 16);
 				bufs.add(b);
 			}
@@ -353,7 +353,7 @@
 			// is.reset();
 			return;
 		}
-		reader.reset();
+		is.reset();
 
 		// It may be compressed. We can inflate here to avoid repeating clients
 		// decompressing here,
@@ -364,7 +364,7 @@
 	}
 
 	public void newClient(AcceptThread acceptThread, final Socket newCli,
-			final OutputStream os, final InputStream is) throws IOException {
+			final Writer os, final Reader is) throws IOException {
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
 		// addSock(newCli);
@@ -423,7 +423,7 @@
 				byte b[] = new byte[4096];
 				for (;;) {
 					try {
-						int c = is.read(b);
+						int c = is.readByte(b);
 						if (c <= 0)
 							throw new IOException();
 						// System.out.println("client read "+c);
@@ -433,8 +433,12 @@
 							os.close();
 							is.close();
 						} catch (IOException e1) {
+						} catch (TransportException e1) {
+							e1.printStackTrace();
 						}
 						return;
+					} catch (TransportException e) {
+						e.printStackTrace();
 					}
 				}
 			}
@@ -490,12 +494,14 @@
 					} catch (IOException e1) {
 					}
 					/* if socket closed cliList.remove(newCli); */
+				} catch (TransportException e) {
+					e.printStackTrace();
 				}
 			}
 
-			public void writeToClient(final OutputStream os,
+			public void writeToClient(final Writer os,
 					LinkedList<ByteBuffer> bufs, int inputIndex)
-					throws IOException {
+					throws TransportException {
 				while (inputIndex < bufs.size()) {
 					ByteBuffer b = bufs.get(inputIndex++);
 					os.write(b.array(), b.position(), b.limit());
@@ -640,16 +646,16 @@
 		};
 	}
 
-	void sendRfbVersion(OutputStream os) throws IOException {
+	void sendRfbVersion(Writer writer) throws IOException, TransportException {
 		// os.write(versionMsg_3_8.getBytes());
-		os.write(versionMsg_3_855.getBytes());
+		writer.write(versionMsg_3_855.getBytes());
 	}
 
-	int readVersionMsg(InputStream is, OutputStream os) throws IOException {
+	int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException {
 
 		byte[] b = new byte[12];
 
-		is.read(b);
+		reader.readBytes(b);
 
 		if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
 				|| (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
@@ -668,40 +674,41 @@
 		}
 
 		if (rfbMinor == 855) {
-			sendProxyFlag(os);
+			sendProxyFlag(writer);
 			if (proxyFlag)
-				sendPortNumber(os);
+				sendPortNumber(writer);
 		}
 		return rfbMinor;
 	}
 
-	void sendProxyFlag(OutputStream os) throws IOException {
+	void sendProxyFlag(Writer writer) throws TransportException {
 		if (proxyFlag)
-			os.write(1);
+			writer.writeInt(1);
 		else
-			os.write(0);
+			writer.writeInt(0);
 	}
 
-	void sendPortNumber(OutputStream os) throws IOException {
+	void sendPortNumber(Writer writer) throws TransportException {
 		byte[] b = new byte[4];
-		b = castIntByte(geth.port);
-		os.write(b);
+		//b = castIntByte(getHost.getPort());
+		b = castIntByte(9999);
+		writer.write(b);
 	}
 
 	boolean readProxyFlag() throws IOException, TransportException {
-		int flag = reader.readUInt8();
+		int flag = is.readUInt8();
 		if (flag == 1)
 			return true;
 		else
 			return false;
 	}
 
-	void sendSecurityType(OutputStream os) throws IOException {
+	void sendSecurityType(Writer os) throws TransportException {
 		// number-of-security-types
-		os.write(1);
+		os.writeInt(1);
 		// security-types
 		// 1:None
-		os.write(1);
+		os.writeInt(1);
 
 		/*
 		 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35);
@@ -709,14 +716,14 @@
 		 */
 	}
 
-	void readSecType(InputStream is) throws IOException {
+	void readSecType(Reader reader) throws TransportException {
 		byte[] b = new byte[1];
-		is.read(b);
+		reader.read(b);
 	}
 
-	void readSecType(InputStream is, OutputStream os) throws IOException {
+	void readSecType(Reader is, Writer os) throws TransportException {
 		byte[] b = new byte[1];
-		is.read(b);
+		is.readBytes(b);
 
 		int count = 260;
 		int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15,
@@ -745,13 +752,13 @@
 		}
 
 		byte[] c = new byte[256];
-		is.read(c);
+		is.readBytes(c);
 
 		System.out.println(new String(c));
 
 	}
 
-	void sendSecResult(OutputStream os) throws IOException {
+	void sendSecResult(Writer os) throws TransportException {
 		byte[] b = castIntByte(0);
 		os.write(b);
 	}
@@ -765,17 +772,17 @@
 		return b;
 	}
 
-	void readClientInit(InputStream in) throws IOException {
+	void readClientInit(Reader in) throws TransportException {
 		byte[] b = new byte[0];
-		in.read(b);
+		in.readBytes(b);
 	}
 
-	void sendInitData(OutputStream os) throws IOException {
+	void sendInitData(Writer os) throws TransportException {
 		os.write(initData);
 	}
 
 	void writeFramebufferUpdateRequest(int x, int y, int w, int h,
-			boolean incremental) throws IOException, TransportException {
+			boolean incremental) throws TransportException {
 		byte[] b = new byte[10];
 
 		b[0] = (byte) FramebufferUpdateRequest;
@@ -789,7 +796,7 @@
 		b[8] = (byte) ((h >> 8) & 0xff);
 		b[9] = (byte) (h & 0xff);
 
-		writer.write(b);
+		os.write(b);
 	}
 
 }