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

📄 rfbproto.java

📁 一个远程登陆器的原代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			writeRfbFileTransferMsg(
									rfbCommand,
									rfbCFileDelete,
									0,
									temp.length(),
									temp);
		}
		catch (IOException e)
		{
			System.err.println(e);
		}
	}

	//Internally used.
	// Handles acknowledgement that the directory has been created on the server
	void createRemoteDirectoryFeedback() throws IOException
	{
		is.readInt();
		int length = is.readInt();
		String f="";
		for (int i = 0; i < length; i++)
		{
			f += (char)is.readUnsignedByte();
		}
		viewer.ftp.refreshRemoteLocation();	
		viewer.ftp.historyComboBox.insertItemAt(new String(" > Created Directory on Remote Machine: " + f.substring(0, f.length()-1)),0);
		viewer.ftp.historyComboBox.setSelectedIndex(0);
	}

	//Call this method to create a directory at server
	void createRemoteDirectory(String text)
	{
		try
		{
			String temp = text;
			writeRfbFileTransferMsg(
				rfbCommand,
				rfbCDirCreate,
				0,
				temp.length(),
				temp);
		}
		catch (IOException e)
		{
			System.err.println(e);
		}
	}

	//Call this method to get a file from the server
	void requestRemoteFile(String text, String localPath)
	{
		try
		{
			String temp = text;
			receivePath = localPath;
					
			writeRfbFileTransferMsg(
									rfbFileTransferRequest,
									0,
									1, // 0 : compression not supported - 1 : compression supported
									temp.length(),
									temp);
		}
		catch (IOException e)
		{
			System.err.println(e);
		}
	}

	//Internally used when transferring file from server. Here, the server sends
	//a rfb packet signalling that it is ready to send the file requested
	void receiveFileHeader() throws IOException
	{
		fFileReceptionRunning = true;
		fFileReceptionError = false;
		viewer.ftp.disableButtons();
		int size = is.readInt();
		int length = is.readInt();
		
		String tempName = "";
		for (int i = 0; i < length; i++)
		{
			tempName += (char) is.readUnsignedByte();
		}

		// sf@2004 - Read the high part of file size (not yet in rfbFileTransferMsg for 
		// backward compatibility reasons...)
		int sizeH = is.readInt();
		long lSize = ((long)(sizeH) << 32) + size;
		
		receiveFileSize = lSize;
		viewer.ftp.connectionStatus.setText("Received: 0 bytes of " + lSize + " bytes");
		fileSize=0;
		fileChunkCounter = 0;
		String fileName = receivePath;
		fos = new FileOutputStream(fileName);
		writeRfbFileTransferMsg(rfbFileHeader, 0, 0, 0, null);
	}

	//Internally used when transferring file from server. This method receives one chunk
	//of the file
	void receiveFileChunk() throws IOException
	{
		// sf@2004 - Size = 0 means file chunck not compressed
		int size = is.readInt();
		boolean fCompressed = (size != 0);
		int length = is.readInt();
		fileChunkCounter++;

		// sf@2004 - allocates buffers for file chunck reception and decompression 
		byte[] ReceptionBuffer = new byte[length + 32];

		// Read the incoming file data
		// Todo: check error !
		is.readFully(ReceptionBuffer,0, length);
		
		if (fCompressed)
		{
			int bufSize = sz_rfbBlockSize + 1024; // Todo: set a more accurate value here
			int decompressedSize = 0;
			byte[] DecompressionBuffer = new byte[bufSize];
			Inflater myInflater = new Inflater();
			myInflater.setInput(ReceptionBuffer);
			try
			{
				decompressedSize = myInflater.inflate(DecompressionBuffer);
			}
			catch (DataFormatException e)
			{
				System.err.println(e);
			}
			// Todo: check error !
			fos.write(DecompressionBuffer, 0, decompressedSize);
			fileSize += decompressedSize;
		}
		else
		{
			//	 Todo: check error !
			fos.write(ReceptionBuffer, 0, length);
			fileSize += length;
		}
		
		/*
		for (int i = 0; i < length; i++) 
		{
			fos.write(is.readUnsignedByte());
			fileSize++;
		}
		*/
		
		// viewer.ftp.connectionStatus.setText("Received: " + fileSize + " bytes of "+ receiveFileSize+ " bytes" );
		viewer.ftp.jProgressBar.setValue((int)((fileSize * 100) / receiveFileSize));
		viewer.ftp.connectionStatus.setText(">>> Receiving File: " + receivePath + " - Size: " + receiveFileSize + " bytes - Progress: " + ((fileSize * 100) / receiveFileSize) + "%");
		
		if (fAbort == true)
		{
			fAbort = false;
			fFileReceptionError = true;
			writeRfbFileTransferMsg(rfbAbortFileTransfer, 0, 0, 0, null);
			
		}
		// sf@2004 - For old FT protocole only
		/*
		if(fileChunkCounter==10)
		{
			writeRfbFileTransferMsg(rfbFileHeader,0,0,0,null);
			fileChunkCounter=0;
		}
		*/
	}
	
	//Internally used when transferring file from server. Server signals end of file.
	void endOfReceiveFile(boolean fReceptionOk) throws IOException
	{
		int size = is.readInt();
		int length = is.readInt();
		fileSize=0;
		fos.close();
		
		viewer.ftp.refreshLocalLocation();
		if (fReceptionOk && !fFileReceptionError)
		{
			viewer.ftp.connectionStatus.setText(" > File successfully received");
			viewer.ftp.historyComboBox.insertItemAt(new String(" > File: <" + receivePath + "> received from Remote Machine" ),0);
		}
		else
		{
			// sf@2004 - Delete the incomplete receieved file for now (until we use Delta Transfer)
			File f = new File(receivePath);
			f.delete();		
			viewer.ftp.connectionStatus.setText(" > Error - File NOT received");
			viewer.ftp.historyComboBox.insertItemAt(new String(" > Error - File: <" + receivePath + "> not correctly received from Remote Machine (aborted by user or error)") ,0);
		}

		fFileReceptionError = false;
		fFileReceptionRunning = false;
		viewer.ftp.historyComboBox.setSelectedIndex(0);
		viewer.ftp.enableButtons();
	}

	//Call this method to read the contents of the server directory
	void readServerDirectory(String text)
	{
		try
		{
			String temp = text;
			writeRfbFileTransferMsg(
									rfbDirContentRequest,
									rfbRDirContent,
									0,
									temp.length(),
									temp);
		}
		catch (IOException e)
		{
			System.err.println(e);
		}

	}

	//Internally used to receive list of drives available on the server
	void readFTPMsgDriveList() throws IOException
	{
		String str = "";
		for (int i = 0; i < 4; i++)
		{
			is.readUnsignedByte();
		}
		int length = is.readInt();
		for (int i = 0; i < length; i++)
		{
			char temp = (char) is.readUnsignedByte();
			if (temp != '\0')
			{
				str += temp;
			}
		}
		viewer.ftp.printDrives(str);
		
		// sf@2004
		// Finds the first readable drive and populates the local directory
		viewer.ftp.changeLocalDirectory(viewer.ftp.getFirstReadableLocalDrive());
		// Populate the remote directory
		viewer.ftp.changeRemoteDrive();
		viewer.ftp.refreshRemoteLocation();
		
	}

	//Internally used to receive directory content from server
	//Here, the server marks the start of the directory listing
	void readFTPMsgDirectoryList() throws IOException
	{
		is.readInt();
		int length = is.readInt();
		if (length == 0)
		{
			readFTPMsgDirectorydriveNotReady();
			inDirectory2 = false;
		}
		else
		{
			// sf@2004 - New FT protocole sends remote directory name
			String str = "";
			for (int i = 0; i < length; i++)
			{
				char temp = (char) is.readUnsignedByte();
				if (temp != '\0')
				{
					str += temp;
				}
			}
			// viewer.ftp.changeRemoteDirectory(str);
			
		}
	}

	//Internally used to receive directory content from server
	//Here, the server sends one file/directory with it's attributes
	void readFTPMsgDirectoryListContent() throws IOException
	{
		String fileName = "", alternateFileName = "";
		byte contentType = 0;
		int contentParamT = 0;
		int contentParam = 0;
		byte temp = 0;
		int dwFileAttributes,
			nFileSizeHigh,
			nFileSizeLow,
			dwReserved0,
			dwReserved1;
		long ftCreationTime, ftLastAccessTime, ftLastWriteTime;
		char cFileName, cAlternateFileName;
		int length = 0;
		is.readInt();
		length = is.readInt();
		dwFileAttributes = is.readInt();
		length -= 4;
		ftCreationTime = is.readLong();
		length -= 8;
		ftLastAccessTime = is.readLong();
		length -= 8;
		ftLastWriteTime = is.readLong();
		length -= 8;
		nFileSizeHigh = is.readInt();
		length -= 4;
		nFileSizeLow = is.readInt();
		length -= 4;
		dwReserved0 = is.readInt();
		length -= 4;
		dwReserved1 = is.readInt();
		length -= 4;
		cFileName = (char) is.readUnsignedByte();
		length--;
		while (cFileName != '\0')
		{
			fileName += cFileName;
			cFileName = (char) is.readUnsignedByte();
			length--;
		}
		cAlternateFileName = (char) is.readByte();
		length--;
		while (length != 0)
		{
			alternateFileName += cAlternateFileName;
			cAlternateFileName = (char) is.readUnsignedByte();
			length--;
		}
		if (dwFileAttributes == 268435456
			|| dwFileAttributes == 369098752
			|| dwFileAttributes == 285212672 
			|| dwFileAttributes == 271056896
			|| dwFileAttributes == 824705024
			||	dwFileAttributes == 807927808
			|| dwFileAttributes == 371720192
			|| dwFileAttributes == 369623040)
		{
			fileName = " [" + fileName + "]";
			remoteDirsList.add(fileName); // sf@2004
		}
		else
		{
			remoteFilesList.add(" " + fileName); // sf@2004
		}
	
		// a.add(fileName);
	}

	//Internally used to read directory content of server.
	//Here, server signals end of directory.
	void readFTPMsgDirectoryListEndContent() throws IOException
	{
		is.readInt();
		int length = is.readInt();

		// sf@2004
		a.clear();
		for (int i = 0; i < remoteDirsList.size(); i++) 
			a.add(remoteDirsList.get(i));
		for (int i = 0; i < remoteFilesList.size(); i++) 
			a.add(remoteFilesList.get(i));
		remoteDirsList.clear();
		remoteFilesList.clear();
		
		viewer.ftp.printDirectory(a);
	}

	//Internally used to signify the drive requested is not ready

	void readFTPMsgDirectorydriveNotReady() throws IOException
	{
		System.out.println("Remote Drive unavailable");
		viewer.ftp.connectionStatus.setText(" > WARNING - Remote Drive unavailable (possibly restricted access or media not present)");
		viewer.ftp.remoteStatus.setText("WARNING: Remote Drive unavailable");
	}

	//Call this method to request the list of drives on the server.
	void readServerDriveList()
	{
		try
		{
			viewer.rfb.writeRfbFileTransferMsg(
												RfbProto.rfbDirContentRequest,
												RfbProto.rfbRDrivesList,
												0,
												0,
												null);
		}
		catch (IOException e)
		{
			System.err.println(e);
		}
	}

	// sf@2004 - Read the destination file checksums data
	// We don't use it for now
	void ReceiveDestinationFileChecksums() throws IOException
	{
		int size = is.readInt();
		int length = is.readInt();
		
		byte[] ReceptionBuffer = new byte[length + 32];

		// Read the incoming file data
		is.readFully(ReceptionBuffer,0, length);

		/*
		String csData = "";
		for (int i = 0; i < length; i++)
		{
			csData += (char) is.readUnsignedByte();
		}
		*/
	
		// viewer.ftp.connectionStatus.setText("Received: 0 bytes of " + size + " bytes");
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	//
	// Write a FramebufferUpdateRequest message
	//

	void writeFramebufferUpdateRequest(
		int x,
		int y,
		int w,
		int h,
		boolean incremental)
		throws IOException {
			if (!viewer.ftp.isVisible()) {
		byte[] b = new byte[10];

		b[0] = (byte) FramebufferUpdateRequest;
		b[1] = (byte) (incremental ? 1 : 0);
		b[2] = (byte) ((x >> 8) & 0xff);
		b[3] = (byte) (x & 0xff);
		b[4] = (byte) ((y >> 8) & 0xff);
		b[5] = (byte) (y & 0xff);
		b[6] = (byte) ((w >> 8) & 0xff);
		b[7] = (byte) (w & 0xff);
		b[8] = (byte) ((h >> 8) & 0xff);
		b[9] = (byte) (h & 0xff);

		os.write(b);
		}
	}

	//
	// Write a SetPixelFormat message
	//

	void writeSetPixelFormat(
		int bitsPerPixel,
		int depth,
		boolean bigEndian,
		boolean trueColour,
		int redMax,
		int greenMax,
		int blueMax,
		int redShift,
		int greenShift,
		int blueShift,
		boolean fGreyScale) // sf@2005
		throws IOException {

⌨️ 快捷键说明

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