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

📄 inet.h

📁 1.INet 2.一組封裝了Wininet的Classes
💻 H
📖 第 1 页 / 共 2 页
字号:
		ATLASSERT(m_hHandle);
		if (!FtpGetFile(m_hHandle,szRemote,szLocal,bFailIfExists,dwAttributes,dwFlags,(DWORD_PTR)(CInternetHandle*)this))
			throw CInternetException(m_hHandle,_T("Failed to download file (%s -> %s)"),szRemote,szLocal);
	}

	void Command(LPCTSTR szCommand,DWORD dwFlags=FTP_TRANSFER_TYPE_BINARY) throw(...)
	{
		ATLASSERT(m_hHandle);
		if (!FtpCommand(m_hHandle,FALSE,dwFlags,szCommand,(DWORD_PTR)(CInternetHandle*)this,NULL))
			throw CInternetException(m_hHandle,_T("Failed to send ftp command (%s)"),szCommand);
	}

	void Command(LPCTSTR szCommand,CInternetHandle& Handle,DWORD dwFlags=FTP_TRANSFER_TYPE_BINARY) throw(...)
	{
		ATLASSERT(m_hHandle);
		ATLASSERT(Handle.m_hHandle==NULL);
		if (!FtpCommand(m_hHandle,TRUE,dwFlags,szCommand,(DWORD_PTR)(CInternetHandle*)this,&Handle.m_hHandle))
			throw CInternetException(m_hHandle,_T("Failed to send ftp command (%s)"),szCommand);
	}

	void FindFirst(CFtpFindFile& FF,LPCTSTR szSearch=_T("*.*"),DWORD dwFlags=0) throw(...);
};

class CInternetFile : public CInternetHandle
{
public:
	class CInfo
	{
	public:
		CInfo(DWORD dwLength=0) throw(...)
		{
			Reset(dwLength);
		}

		CInfo(CHttpFile& File) throw(...);
		CInfo(CFtpFile& File) throw(...);

		void Reset(DWORD dwLength=0)
		{
			m_fLimit=0.0;
			m_dwDataLength=dwLength;
			m_dwSecondsLeft=0;
			m_fDownloadRate=m_fAverageDownloadRate=0.0;
			m_dwRead=0;
			m_dwTimingStart=m_dwTimingLast=GetTickCount();
		}

		void SetRateLimit(double fLimit)
		{
			m_fLimit=fLimit;
		}

		DWORD GetTotalDataLength() const
		{
			return m_dwDataLength;
		}

		DWORD GetTotalRead() const
		{
			return m_dwRead;
		}

		DWORD GetTotalTime() const
		{
			return (m_dwTimingLast-m_dwTimingStart)/1000;
		}

		DWORD GetTimeLeft() const
		{
			return m_dwSecondsLeft;
		}

		double GetDownloadRate() const
		{
			return m_fDownloadRate;
		}

		double GetAverageDownloadRate() const
		{
			return m_fAverageDownloadRate;
		}

	protected:
		DWORD m_dwTimingStart,m_dwTimingLast;
		DWORD m_dwRead;

		DWORD m_dwSecondsLeft;
		double m_fDownloadRate,m_fAverageDownloadRate;
		DWORD m_dwDataLength;

		double m_fLimit;

		friend class CInternetFile;
	};

	DWORD Read(LPVOID pBuffer,DWORD dwLength) throw(...)
	{
		ATLASSERT(m_hHandle);
		DWORD dwRead;
		if (!InternetReadFile(m_hHandle,pBuffer,dwLength,&dwRead))
			throw CInternetException(m_hHandle,_T("Failed to read from network (%u bytes)"),dwLength);
		return dwRead;
	}

	DWORD Read(LPVOID pBuffer,DWORD dwLength,CInfo& Info) throw(...)
	{
		ATLASSERT(m_hHandle);
		DWORD dwRead;
		if (!InternetReadFile(m_hHandle,pBuffer,dwLength,&dwRead))
			throw CInternetException(m_hHandle,_T("Failed to read from network (%u bytes)"),dwLength);
		DWORD dwTimingCurrent=GetTickCount();
		if (Info.m_fLimit>0.0f)
		{
			double fTotalTime=(double)(dwTimingCurrent-Info.m_dwTimingStart);
			double fRate=(double)((double)Info.m_dwRead/fTotalTime);
			if (fRate>Info.m_fLimit)
				Sleep((DWORD)(((fRate*fTotalTime)/Info.m_fLimit)-fTotalTime));
		}
		DWORD dwPreviousRead=Info.m_dwRead;
		Info.m_dwRead+=dwRead;
		DWORD dwTime=dwTimingCurrent-Info.m_dwTimingLast;
		if (dwTime)
		{
			Info.m_fDownloadRate=((double)(Info.m_dwRead)-(double)(dwPreviousRead))/((double)(dwTime));
			Info.m_fAverageDownloadRate=(double)(Info.m_dwRead)/(double)(dwTimingCurrent-Info.m_dwTimingStart);
			Info.m_dwTimingLast=dwTimingCurrent;
			if (Info.m_dwDataLength)
				Info.m_dwSecondsLeft=(DWORD)(((double)dwTimingCurrent-Info.m_dwTimingStart)/Info.m_dwRead*(Info.m_dwDataLength-Info.m_dwRead)/1000);
		}
        return dwRead;
	}

	DWORD Write(LPCVOID pBuffer,DWORD dwLength) throw(...)
	{
		ATLASSERT(m_hHandle);
		DWORD dwWritten;
		if (!InternetWriteFile(m_hHandle,pBuffer,dwLength,&dwWritten))
			throw CInternetException(m_hHandle,_T("Failed to write to network (%u bytes)"),dwLength);
		return dwWritten;
	}

	DWORD SetPosition(LONG nDistance,DWORD dwMethod=FILE_BEGIN)
	{
		ATLASSERT(m_hHandle);
		return InternetSetFilePointer(m_hHandle,nDistance,NULL,dwMethod,NULL);
	}

	DWORD GetPosition() const
	{
		ATLASSERT(m_hHandle);
		return InternetSetFilePointer(m_hHandle,0,NULL,FILE_CURRENT,NULL);
	}

	DWORD GetLength() const throw(...)
	{
		ATLASSERT(m_hHandle);
		DWORD dwLength;
		if (!InternetQueryDataAvailable(m_hHandle,&dwLength,0,0))
			throw CInternetException(m_hHandle,_T("Failed to query available data length"));
		return dwLength;
	}
};

class CHttpFile : public CInternetFile
{
public:
	CHttpFile(HINTERNET hConnection,LPCTSTR szVerb,LPCTSTR szObject,LPCTSTR szVersion=NULL,LPCTSTR szReferer=NULL,LPCTSTR* szAcceptTypes=NULL,DWORD dwFlags=INTERNET_FLAG_KEEP_CONNECTION) throw(...)
	{
		ATLASSERT(hConnection);
		static LPCTSTR szAcceptAll[]={_T("*/*"),NULL};
		if (!(m_hHandle=HttpOpenRequest(hConnection,szVerb,szObject,szVersion,szReferer,szAcceptTypes ? szAcceptTypes : szAcceptAll,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
			throw CInternetException(m_hHandle,_T("Failed to open http request (%s %s)"),szVerb,szObject);
	}

	CHttpFile(HINTERNET hSession,LPCTSTR szURL,LPCTSTR szHeaders=NULL,DWORD dwHeadersLength=(DWORD)-1,DWORD dwFlags=INTERNET_FLAG_KEEP_CONNECTION) throw(...)
	{
		ATLASSERT(hSession);
		if (!(m_hHandle=InternetOpenUrl(hSession,szURL,szHeaders,dwHeadersLength,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
			throw CInternetException(m_hHandle,_T("Failed to open http url (%s)"),szURL);
	}

	void SendRequest(LPCTSTR szHeaders=NULL,DWORD dwHeadersLength=(DWORD)-1,LPVOID pOptional=NULL,DWORD dwOptionalLength=0) throw(...)
	{
		ATLASSERT(m_hHandle);
		if (!HttpSendRequest(m_hHandle,szHeaders,dwHeadersLength,pOptional,dwOptionalLength))
			throw CInternetException(m_hHandle,_T("Failed to send http request"));
	}

	void QueryInfo(DWORD dwInfoLevel,LPVOID pBuffer,LPDWORD pdwBufferLength,LPDWORD pdwIndex=NULL) const throw(...)
	{
		ATLASSERT(m_hHandle);
		if (!HttpQueryInfo(m_hHandle,dwInfoLevel,pBuffer,pdwBufferLength,pdwIndex))
			throw CInternetException(m_hHandle,_T("Failed to query http info (%u)"),dwInfoLevel);
	}

	void QueryInfo(DWORD dwInfoLevel,DWORD& dwValue,LPDWORD pdwIndex=NULL) const throw(...)
	{
		DWORD dwLength=sizeof(dwValue);
		QueryInfo(dwInfoLevel|HTTP_QUERY_FLAG_NUMBER,&dwValue,&dwLength,pdwIndex);
	}

	void QueryInfo(DWORD dwInfoLevel,SYSTEMTIME& dtTime,LPDWORD pdwIndex=NULL) const throw(...)
	{
		DWORD dwLength=sizeof(dtTime);
		QueryInfo(dwInfoLevel|HTTP_QUERY_FLAG_SYSTEMTIME,&dtTime,&dwLength,pdwIndex);
	}

#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
	void QueryInfo(DWORD dwInfoLevel,_CSTRING_NS::CString& szBuffer,LPDWORD pdwIndex=NULL) const throw(...)
	{
		ATLASSERT(m_hHandle);
		DWORD dwIndex=pdwIndex ? *pdwIndex : 0,dwLength=0;
		if (HttpQueryInfo(m_hHandle,dwInfoLevel,NULL,&dwLength,&dwIndex) || GetLastError()==ERROR_INSUFFICIENT_BUFFER)
		{
			if (pdwIndex)
				*pdwIndex=dwIndex;
			else
				dwIndex=0;
			BOOL bResult=HttpQueryInfo(m_hHandle,dwInfoLevel,szBuffer.GetBuffer(dwLength/sizeof(TCHAR)-1),&dwLength,pdwIndex ? pdwIndex : &dwIndex);
			szBuffer.ReleaseBuffer();
			if (bResult)
				return;
		}
		throw CInternetException(m_hHandle,_T("Failed to query http info (%u)"),dwInfoLevel);
	}
#endif
};

class CHttpsFile : public CHttpFile
{
public:
	CHttpsFile(HINTERNET hConnection,LPCTSTR szVerb,LPCTSTR szObject,LPCTSTR szVersion=NULL,LPCTSTR szReferer=NULL,LPCTSTR* szAcceptTypes=NULL,DWORD dwFlags=INTERNET_FLAG_KEEP_CONNECTION) throw(...) : CHttpFile(hConnection,szVerb,szObject,szVersion,szReferer,szAcceptTypes,dwFlags|INTERNET_FLAG_SECURE)
	{
	}

	CHttpsFile(HINTERNET hSession,LPCTSTR szURL,LPCTSTR szHeaders=NULL,DWORD dwHeadersLength=(DWORD)-1,DWORD dwFlags=INTERNET_FLAG_KEEP_CONNECTION) throw(...) : CHttpFile(hSession,szURL,szHeaders,dwHeadersLength,dwFlags|INTERNET_FLAG_SECURE)
	{
	}
};

class CFtpFile : public CInternetFile
{
public:
	CFtpFile(HINSTANCE hConnection,LPCTSTR szFilename,DWORD dwAccess,DWORD dwFlags=FTP_TRANSFER_TYPE_UNKNOWN) throw(...)
	{
		ATLASSERT(hConnection);
		if (!(m_hHandle=FtpOpenFile(hConnection,szFilename,dwAccess,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
			throw CInternetException(m_hHandle,_T("Failed to open ftp file (%s)"),szFilename);
	}

	CFtpFile(HINTERNET hSession,LPCTSTR szURL,LPCTSTR szHeaders=NULL,DWORD dwHeadersLength=(DWORD)-1,DWORD dwFlags=INTERNET_FLAG_PASSIVE) throw(...)
	{
		ATLASSERT(hSession);
		if (!(m_hHandle=InternetOpenUrl(hSession,szURL,szHeaders,dwHeadersLength,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
			throw CInternetException(m_hHandle,_T("Failed to open ftp url (%s)"),szURL);
	}

	ULONGLONG GetSize() const
	{
		ATLASSERT(m_hHandle);
		ULARGE_INTEGER Size;
		Size.LowPart=FtpGetFileSize(m_hHandle,&Size.HighPart);
		return Size.QuadPart;
	}
};

template<class T>
class CInternetFindFile : public CInternetHandle
{
public:
	CInternetFindFile()
	{
		ZeroMemory(&m_fd,sizeof(T));
	}

	BOOL FindNext() throw(...)
	{
		ATLASSERT(m_hHandle);
		if (InternetFindNextFile(m_hHandle,&m_fd))
			return TRUE;
		if (GetLastError()==ERROR_NO_MORE_FILES)
			return FALSE;
		throw CInternetException(m_hHandle,_T("Failed to find next file"));
	}

protected:
	T m_fd;
};

class CFtpFindFile : public CInternetFindFile<WIN32_FIND_DATA>
{
public:
	ULONGLONG GetFileSize() const
	{
		ATLASSERT(m_hHandle);
		ULARGE_INTEGER nSize;
		nSize.LowPart=m_fd.nFileSizeLow;
		nSize.HighPart=m_fd.nFileSizeHigh;
		return nSize.QuadPart;
	}

	BOOL GetFileName(LPTSTR lpstrFileName, int cchLength) const
	{
		ATLASSERT(m_hHandle);
		if(lstrlen(m_fd.cFileName)>=cchLength)
			return FALSE;
		return (lstrcpy(lpstrFileName, m_fd.cFileName)!=NULL);
	}

	BOOL GetFileTitle(LPTSTR lpstrFileTitle, int cchLength) const
	{
		ATLASSERT(m_hHandle);
		TCHAR szBuff[MAX_PATH]={0};
		if(!GetFileName(szBuff,MAX_PATH))
			return FALSE;
		TCHAR szNameBuff[_MAX_FNAME]={0};
#ifdef _SECURE_ATL
		_tsplitpath_s(szBuff, NULL, 0, NULL, 0, szNameBuff, _MAX_FNAME, NULL, 0);
#else
		_tsplitpath(szBuff, NULL, NULL, szNameBuff, NULL);
#endif
		if(lstrlen(szNameBuff)>=cchLength)
			return FALSE;
		return (lstrcpy(lpstrFileTitle, szNameBuff)!=NULL);
	}

#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
	_CSTRING_NS::CString GetFileName() const
	{
		ATLASSERT(m_hHandle);
		_CSTRING_NS::CString ret=m_fd.cFileName;
		return ret;
	}

	_CSTRING_NS::CString GetFileTitle() const
	{
		ATLASSERT(m_hHandle);
		_CSTRING_NS::CString strFullName = GetFileName();
		_CSTRING_NS::CString strResult;
#ifdef _SECURE_ATL
		_tsplitpath_s(strFullName, NULL, 0, NULL, 0, strResult.GetBuffer(MAX_PATH), MAX_PATH, NULL, 0);
#else
		_tsplitpath(strFullName, NULL, NULL, strResult.GetBuffer(MAX_PATH), NULL);
#endif
		strResult.ReleaseBuffer();
		return strResult;
	}
#endif

	void GetLastWriteTime(FILETIME* pTimeStamp) const
	{
		ATLASSERT(m_hHandle);
		ATLASSERT(pTimeStamp);
		*pTimeStamp = m_fd.ftLastWriteTime;
	}

	void GetLastAccessTime(FILETIME* pTimeStamp) const
	{
		ATLASSERT(m_hHandle);
		ATLASSERT(pTimeStamp);
		*pTimeStamp = m_fd.ftLastAccessTime;
	}
    
	BOOL GetCreationTime(FILETIME* pTimeStamp) const
	{
		ATLASSERT(m_hHandle);
		ATLASSERT(pTimeStamp);
		*pTimeStamp = m_fd.ftCreationTime;
	}

	BOOL MatchesMask(DWORD dwMask) const
	{
		ATLASSERT(m_hHandle);
		return (m_fd.dwFileAttributes & dwMask);
	}

	BOOL IsDots() const
	{
		ATLASSERT(m_hHandle);
		return (IsDirectory() &&	(m_fd.cFileName[0]=='.' && (m_fd.cFileName[1]=='\0' || (m_fd.cFileName[1]=='.' && m_fd.cFileName[2]=='\0'))));
	}

	BOOL IsReadOnly() const
	{
		return MatchesMask(FILE_ATTRIBUTE_READONLY);
	}

	BOOL IsDirectory() const
	{
		return MatchesMask(FILE_ATTRIBUTE_DIRECTORY);
	}

	BOOL IsCompressed() const
	{
		return MatchesMask(FILE_ATTRIBUTE_COMPRESSED);
	}

	BOOL IsSystem() const
	{
		return MatchesMask(FILE_ATTRIBUTE_SYSTEM);
	}

	BOOL IsHidden() const
	{
		return MatchesMask(FILE_ATTRIBUTE_HIDDEN);
	}

	BOOL IsTemporary() const
	{
		return MatchesMask(FILE_ATTRIBUTE_TEMPORARY);
	}

	BOOL IsNormal() const
	{
		return MatchesMask(FILE_ATTRIBUTE_NORMAL);
	}

	BOOL IsArchived() const
	{
		return MatchesMask(FILE_ATTRIBUTE_ARCHIVE);
	}

protected:
	friend class CFtpConnection;
};

inline void CFtpConnection::FindFirst(CFtpFindFile& FF,LPCTSTR szSearch,DWORD dwFlags) throw(...)
{
	ATLASSERT(m_hHandle);
	ATLASSERT(FF.m_hHandle==NULL);
	if (!(FF.m_hHandle=FtpFindFirstFile(m_hHandle,szSearch,&FF.m_fd,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
		throw CInternetException(m_hHandle,_T("Failed to find files on ftp (%s)"),szSearch);
}

inline CInternetFile::CInfo::CInfo(CHttpFile& File) throw(...)
{
	DWORD dwLength;
	File.QueryInfo(HTTP_QUERY_CONTENT_LENGTH,dwLength);
	Reset(dwLength);
}

inline CInternetFile::CInfo::CInfo(CFtpFile& File) throw(...)
{
	Reset((DWORD)File.GetSize());
}

⌨️ 快捷键说明

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