view src/myVncClient/MyRfbProto.java @ 18:4881586aead9

succeed drawFirstimage()!!
author e085711
date Tue, 26 Apr 2011 09:08:14 +0900
parents f9ecb0315303
children 965360af5f0b
line wrap: on
line source

package myVncClient;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import javax.imageio.ImageIO;


class MyRfbProto extends RfbProto {

	private int messageType;
	private int rectangles;
	private int encoding;

	private ServerSocket servSock; 	
	private int acceptPort;
	private byte initData[];
	private LinkedList <Socket> cliList;
	byte[] pngBytes;
	
	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 readPngData()throws IOException{
		// 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){
				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();
		skipBytes(8);
		encoding = readU32();	
		reset();	
	}
	
	
	
	
	void printFramebufferUpdate(){
	
		System.out.println("messageType=" + messageType);
		System.out.println("rectangles="+rectangles);
		System.out.println("encoding=" + encoding);
	}
	
	
}