⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfbproto.java

📁 teamviewer source code vc++
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			rec = null;
	    }
		// close Tunnel
		if (is != null)	
			is.close();
		if (os != null)
			os.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

	//
	// Read server's protocol version message
	//

	int serverMajor, serverMinor;

	void readVersionMsg() throws Exception {

		byte[] b = new byte[12];

		is.readFully(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')
			|| (b[6] < '0')
			|| (b[6] > '9')
			|| (b[7] != '.')
			|| (b[8] < '0')
			|| (b[8] > '9')
			|| (b[9] < '0')
			|| (b[9] > '9')
			|| (b[10] < '0')
			|| (b[10] > '9')
			|| (b[11] != '\n')) {
			throw new Exception(
				"Host " + host + " port " + port + " is not an RFB server");
		}

		serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
		serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
	}

	//
	// Write our protocol version message
	//

	void writeVersionMsg() throws IOException {
		os.write(versionMsg.getBytes());
	}

	//
	// Find out the authentication scheme.
	//

	int readAuthScheme() throws Exception {
		int authScheme = is.readInt();

		switch (authScheme) {

			case ConnFailed :
				int reasonLen = is.readInt();
				byte[] reason = new byte[reasonLen];
				is.readFully(reason);
				throw new Exception(new String(reason));

			case NoAuth :
			case VncAuth :
				return authScheme;

			default :
				throw new Exception(
					"Unknown authentication scheme from RFB server: "
						+ authScheme);

		}
	}

	//
	// Write the client initialisation message
	//

	void writeClientInit() throws IOException {
		if (viewer.options.shareDesktop) {
			os.write(1);
		} else {
			os.write(0);
		}
		viewer.options.disableShareDesktop();
	}

	//
	// Read the server initialisation message
	//

	String desktopName;
	int framebufferWidth, framebufferHeight;
	int bitsPerPixel, depth;
	boolean bigEndian, trueColour;
	int redMax, greenMax, blueMax, redShift, greenShift, blueShift;

	void readServerInit() throws IOException {
		framebufferWidth = is.readUnsignedShort();
		framebufferHeight = is.readUnsignedShort();
		bitsPerPixel = is.readUnsignedByte();
		depth = is.readUnsignedByte();
		bigEndian = (is.readUnsignedByte() != 0);
		trueColour = (is.readUnsignedByte() != 0);
		redMax = is.readUnsignedShort();
		greenMax = is.readUnsignedShort();
		blueMax = is.readUnsignedShort();
		redShift = is.readUnsignedByte();
		greenShift = is.readUnsignedByte();
		blueShift = is.readUnsignedByte();
		byte[] pad = new byte[3];
		is.readFully(pad);
		int nameLength = is.readInt();
		byte[] name = new byte[nameLength];
		is.readFully(name);
		desktopName = new String(name);

		inNormalProtocol = true;
	}

	//
	// Create session file and write initial protocol messages into it.
	//

	void startSession(String fname) throws IOException {
		rec = new SessionRecorder(fname);
		rec.writeHeader();
		rec.write(versionMsg.getBytes());
		rec.writeIntBE(NoAuth);
		rec.writeShortBE(framebufferWidth);
		rec.writeShortBE(framebufferHeight);
		byte[] fbsServerInitMsg =
			{
				32,
				24,
				0,
				1,
				0,
				(byte) 0xFF,
				0,
				(byte) 0xFF,
				0,
				(byte) 0xFF,
				16,
				8,
				0,
				0,
				0,
				0 };
		rec.write(fbsServerInitMsg);
		rec.writeIntBE(desktopName.length());
		rec.write(desktopName.getBytes());
		numUpdatesInSession = 0;

		if (wereZlibUpdates)
			recordFromBeginning = false;

		zlibWarningShown = false;
		tightWarningShown = false;
	}

	//
	// Close session file.
	//

	void closeSession() throws IOException {
		if (rec != null) {
			rec.close();
			rec = null;
		}
	}

	//
	// Set new framebuffer size
	//

	void setFramebufferSize(int width, int height) {
		framebufferWidth = width;
		framebufferHeight = height;
	}

	//
	// Read the server message type
	//

	int readServerMessageType() throws IOException {
		int msgType = is.readUnsignedByte();

		// If the session is being recorded:
		if (rec != null) {
			if (msgType == Bell) { // Save Bell messages in session files.
				rec.writeByte(msgType);
				if (numUpdatesInSession > 0)
					rec.flush();
			}
		}

		return msgType;
	}

	//
	// Read a FramebufferUpdate message
	//

	int updateNRects;

	void readFramebufferUpdate() throws IOException {
		is.readByte();
		updateNRects = is.readUnsignedShort();

		// If the session is being recorded:
		if (rec != null) {
			rec.writeByte(FramebufferUpdate);
			rec.writeByte(0);
			rec.writeShortBE(updateNRects);
		}

		numUpdatesInSession++;
	}

	// Read a FramebufferUpdate rectangle header

	int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;

	void readFramebufferUpdateRectHdr() throws Exception {
		updateRectX = is.readUnsignedShort();
		updateRectY = is.readUnsignedShort();
		updateRectW = is.readUnsignedShort();
		updateRectH = is.readUnsignedShort();
		updateRectEncoding = is.readInt();

		if (updateRectEncoding == EncodingZlib
			|| updateRectEncoding == EncodingTight)
			wereZlibUpdates = true;

		// If the session is being recorded:
		if (rec != null) {
			if (numUpdatesInSession > 1)
				rec.flush(); // Flush the output on each rectangle.
			rec.writeShortBE(updateRectX);
			rec.writeShortBE(updateRectY);
			rec.writeShortBE(updateRectW);
			rec.writeShortBE(updateRectH);
			if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
				// Here we cannot write Zlib-encoded rectangles because the
				// decoder won't be able to reproduce zlib stream state.
				if (!zlibWarningShown) {
					System.out.println(
						"Warning: Raw encoding will be used "
							+ "instead of Zlib in recorded session.");
					zlibWarningShown = true;
				}
				rec.writeIntBE(EncodingRaw);
			} else {
				rec.writeIntBE(updateRectEncoding);
				if (updateRectEncoding == EncodingTight
					&& !recordFromBeginning
					&& !tightWarningShown) {
					System.out.println(
						"Warning: Re-compressing Tight-encoded "
							+ "updates for session recording.");
					tightWarningShown = true;
				}
			}
		}

		if (updateRectEncoding == EncodingLastRect
			|| updateRectEncoding == EncodingNewFBSize)
			return;

		if (updateRectX + updateRectW > framebufferWidth
			|| updateRectY + updateRectH > framebufferHeight) {
			throw new Exception(
				"Framebuffer update rectangle too large: "
					+ updateRectW
					+ "x"
					+ updateRectH
					+ " at ("
					+ updateRectX
					+ ","
					+ updateRectY
					+ ")");
		}
	}

	// Read CopyRect source X and Y.

	int copyRectSrcX, copyRectSrcY;

	void readCopyRect() throws IOException {
		copyRectSrcX = is.readUnsignedShort();
		copyRectSrcY = is.readUnsignedShort();

		// If the session is being recorded:
		if (rec != null) {
			rec.writeShortBE(copyRectSrcX);
			rec.writeShortBE(copyRectSrcY);
		}
	}

	//
	// Read a ServerCutText message
	//

	String readServerCutText() throws IOException {
		byte[] pad = new byte[3];
		is.readFully(pad);
		int len = is.readInt();
		byte[] text = new byte[len];
		is.readFully(text);
		return new String(text);
	}

	//
	// Read an integer in compact representation (1..3 bytes).
	// Such format is used as a part of the Tight encoding.
	// Also, this method records data if session recording is active and
	// the viewer's recordFromBeginning variable is set to true.
	//

	int readCompactLen() throws IOException {
		int[] portion = new int[3];
		portion[0] = is.readUnsignedByte();
		int byteCount = 1;
		int len = portion[0] & 0x7F;
		if ((portion[0] & 0x80) != 0) {
			portion[1] = is.readUnsignedByte();
			byteCount++;
			len |= (portion[1] & 0x7F) << 7;
			if ((portion[1] & 0x80) != 0) {
				portion[2] = is.readUnsignedByte();
				byteCount++;
				len |= (portion[2] & 0xFF) << 14;
			}
		}

		if (rec != null && recordFromBeginning)
			for (int i = 0; i < byteCount; i++)
				rec.writeByte(portion[i]);

		return len;
	}

	//Author: Kenn Min Chong/////////////////////////////////////////////

	//Read/Write a rfbFileTransferMsg
	/*typedef struct _rfbFileTransferMsg {
	    CARD8 type;			// always rfbFileTransfer
	    CARD8 contentType;  // See defines below
	    CARD16 contentParam;// Other possible content classification (Dir or File name, etc..)
		CARD32 size;		// FileSize or packet index or error or other 
	    CARD32 length;
	    // followed by data char text[length]
	} rfbFileTransferMsg;
	*/

	//	Parsing Rfb message to see what type 

	void readRfbFileTransferMsg() throws IOException
	{
		int contentType = is.readUnsignedByte();
		int contentParamT = is.readUnsignedByte();
		int contentParam = contentParamT;
		contentParamT = is.readUnsignedByte();
		contentParamT = contentParamT << 8;
		contentParam = contentParam | contentParamT;
		if (contentType == rfbRDrivesList || contentType == rfbDirPacket)
		{
			readDriveOrDirectory(contentParam);
		}
		else if (contentType == rfbFileHeader)
		{
			receiveFileHeader();
		}
		else if (contentType == rfbFilePacket)
		{
				receiveFileChunk();
		}
		else if (contentType == rfbEndOfFile)
		{
			endOfReceiveFile(true); // Ok
		}
		else if (contentType == rfbAbortFileTransfer)
		{
			if (fFileReceptionRunning)
			{
				endOfReceiveFile(false); // Error
			}
			else
			{
				// sf@2004 - Todo: Add TestPermission 
				// System.out.println("File Transfer Aborted!");
			}
			
		}
		else if (contentType == rfbCommandReturn)
		{
			createDirectoryorDeleteFile(contentParam);
		}
		else if (contentType == rfbFileAcceptHeader)
		{
			sendFile();
		}
		else if (contentType == rfbFileChecksums)
		{
			ReceiveDestinationFileChecksums();
		}
		else
		{
			System.out.println("ContentType: " + contentType);
		}
	}

	//Refactored from readRfbFileTransferMsg()
	public void createDirectoryorDeleteFile(int contentParam)
		throws IOException {
		if (contentParam == rfbADirCreate)
		{
			createRemoteDirectoryFeedback();
		}
		else if (contentParam == rfbAFileDelete)
		{
			deleteRemoteFileFeedback();
		}
	}

	//Refactored from readRfbFileTransferMsg()
	public void readDriveOrDirectory(int contentParam) throws IOException {
		if (contentParam == rfbADrivesList)
		{
			readFTPMsgDriveList();
		}
		else if (contentParam == rfbADirectory && !inDirectory2)
		{
			inDirectory2 = true;
			readFTPMsgDirectoryList();
		}
		else if (contentParam == rfbADirectory && inDirectory2)
		{
			readFTPMsgDirectoryListContent();
		}
		else if (contentParam == 0)
		{
			readFTPMsgDirectoryListEndContent();
			inDirectory2 = false;
		}
		else
		{
			System.out.println("ContentParam: " + contentParam);
		}
	}

	// Internally used. Write an Rfb message to the server
	void writeRfbFileTransferMsg(
								int contentType,
								int contentParam,
								long size, // 0 : compression not supported - 1 : compression supported
								long length,
								String text) throws IOException
	{
		byte b[] = new byte[12];

		b[0] = (byte) rfbFileTransfer;
		b[1] = (byte) contentType;
		b[2] = (byte) contentParam;

		byte by = 0;
		long c = 0;
		length++;
		c = size & 0xFF000000;
		by = (byte) (c >>> 24);
		b[4] = by;
		c = size & 0xFF0000;
		by = (byte) (c >>> 16);
		b[5] = by;
		c = size & 0xFF00;
		by = (byte) (c >>> 8);
		b[6] = by;
		c = size & 0xFF;
		by = (byte) c;
		b[7] = by;

		c = length & 0xFF000000;
		by = (byte) (c >>> 24);
		b[8] = by;
		c = length & 0xFF0000;
		by = (byte) (c >>> 16);
		b[9] = by;
		c = length & 0xFF00;
		by = (byte) (c >>> 8);
		b[10] = by;
		c = length & 0xFF;
		by = (byte) c;
		b[11] = by;
		os.write(b);
		

		if (text != null)
		{
			byte byteArray[] = text.getBytes();
			byte byteArray2[] = new byte[byteArray.length + 1];
			for (int i = 0; i < byteArray.length; i++) {
				byteArray2[i] = byteArray[i];
			}
			byteArray2[byteArray2.length - 1] = 0;
			os.write(byteArray2);
		}
		
	}

	//Internally used. Write an rfb message to the server for sending files ONLY 
	int writeRfbFileTransferMsgForSendFile(
											int contentType,
											int contentParam,
											long size,
											long length,
											String source
											) throws IOException
	{
		File f = new File(source);
		fis = new FileInputStream(f);
		byte byteBuffer[] = new byte[sz_rfbBlockSize]; 
		int bytesRead = fis.read(byteBuffer);
		long counter=0;
		boolean fError = false;
		
		// sf@ - Manage compression
		boolean fCompress = true;
		Deflater myDeflater = new Deflater();
		byte[] CompressionBuffer = new byte[sz_rfbBlockSize + 1024];
		int compressedSize = 0;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -