view src/MyRfbProto.java @ 23:b51bb7bc0766

test update
author e085711
date Sun, 24 Apr 2011 16:48:39 +0900
parents 361016476783
children
line wrap: on
line source

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;


class MyRfbProto extends RfbProto {

	private int messageType;
	private int rectangles;
	private int rectX;
	private int rectY;
	private int rectW;
	private int rectH;
	private int encoding;

	private ServerSocket servSock;
	private int acceptPort;
	private byte initData[];
	private LinkedList <Socket> cliList;
	
	MyRfbProto(String h, int p, VncViewer v ) throws IOException {
		super(h, p, v);
		cliList = new LinkedList <Socket>();
	}

	MyRfbProto(String h, int p) throws IOException {
		super(h, p);
		cliList = new LinkedList <Socket>();
	}
	
	void initServSock(int port) throws IOException{
		servSock = new ServerSocket(port);
		acceptPort = port;
	}
	void selectPort(){
		int i = 5550;
		while(true){
			try{
				initServSock(i);
				break;
			}catch(BindException e){
				i++;
				continue;
			}catch(IOException e){

			}
		}
	}
	int getAcceptPort(){
		return acceptPort;
	}
	void setSoTimeout(int num) throws IOException {
		servSock.setSoTimeout(num);
	}
	
	Socket accept() throws IOException {
		return servSock.accept();
	}

	void addSock(Socket sock){
		cliList.add(sock);
	}
	
	void mark(int len) throws IOException {
		is.mark(len);
	}

	void reset() throws IOException {
		is.reset();
 	}

	boolean markSupported() {
		return is.markSupported();
	}
	
	void readServerInit() throws IOException {
		
		mark(255);
		skipBytes(20);
		int nlen = readU32();
		int blen = 20+4+nlen;
		initData = new byte[blen];
		reset();

		mark(blen);
		readFully(initData);
		reset();
		
		framebufferWidth = readU16();
		framebufferHeight = readU16();
		bitsPerPixel = readU8();
		depth = readU8();
		bigEndian = (readU8() != 0);
		trueColour = (readU8() != 0);
		redMax = readU16();
		greenMax = readU16();
		blueMax = readU16();
		redShift = readU8();
		greenShift = readU8();
		blueShift = readU8();
		byte[] pad = new byte[3];
		readFully(pad);
		int nameLength = readU32();
		byte[] name = new byte[nameLength];
		readFully(name);
		desktopName = new String(name);

		// Read interaction capabilities (TightVNC protocol extensions)
		if (protocolTightVNC) {
			int nServerMessageTypes = readU16();
			int nClientMessageTypes = readU16();
			int nEncodingTypes = readU16();
			readU16();
			readCapabilityList(serverMsgCaps, nServerMessageTypes);
			readCapabilityList(clientMsgCaps, nClientMessageTypes);
			readCapabilityList(encodingCaps, nEncodingTypes);
		}

		inNormalProtocol = true;
	}

	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){
				try{
					cli.getOutputStream().write(b, 0, b.length);
				}catch(IOException e){
					// if socket closed
					//				cliList.remove(cli);
					cliList.remove(cli);
				}
			}
//		System.out.println("cliSize="+cliSize());
		}catch(Exception e){
//			System.out.println("cliSize 0");
		}
	}	
	boolean ready() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		return br.ready();
	}	

	int cliSize(){
		return cliList.size();
	}	
	void printNumBytesRead(){
		System.out.println("numBytesRead="+numBytesRead);
	}	
	void bufResetSend(int size) throws IOException {
		reset();
		int len = size;
		if(available() < size )
			len = available();
		byte buffer[] = new byte[len];
		readFully(buffer);
		sendData(buffer);
	}
	void regiFramebufferUpdate()throws IOException{
		mark(16);
		messageType = readU8();
		skipBytes(1);
		rectangles = readU16();
		rectX = readU16();
		rectY = readU16();
		rectW = readU16();
		rectH = readU16();
		encoding = readU32();	
		reset();
	}
	void checkAndMark() throws IOException{
		switch(encoding){
		case RfbProto.EncodingRaw:
			mark(rectW * rectH * 4 + 16);		
			break;
		default:
			mark(1000000);
		}
	}
	
	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:
			
		
		}
	}
	
	
}