📄 ceinet.cpp
字号:
else
CEThrowInternetException(m_dwContext);
}
}
else
{
ASSERT(m_bCallbackEnabled);
if (m_bCallbackEnabled)
{
InternetSetStatusCallback(m_hSession, NULL);
m_bCallbackEnabled = FALSE;
}
}
return bResult;
}
#ifdef _DEBUG
void CCEInternetSession::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "m_hSession = " << m_hSession;
dc << "\nm_dwContext = " << m_dwContext;
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CCEInternetConnection
IMPLEMENT_DYNAMIC(CCEInternetConnection, CObject)
CCEInternetConnection::CCEInternetConnection(CCEInternetSession* pSession,
LPCTSTR pstrServerName,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */,
DWORD dwContext /* = 1 */)
: m_strServerName(pstrServerName)
{
ASSERT(pSession != NULL);
ASSERT_VALID(pSession);
ASSERT(pstrServerName != NULL);
m_nPort = nPort;
m_pSession = pSession;
m_hConnection = NULL;
if (dwContext == 1)
dwContext = pSession->GetContext();
m_dwContext = dwContext;
}
CCEInternetConnection::~CCEInternetConnection()
{
if (m_hConnection != NULL)
{
Close();
}
}
CCEInternetConnection::operator HINTERNET() const
{
return this->m_hConnection;
}
BOOL CCEInternetConnection::SetOption(DWORD dwOption, LPVOID lpBuffer,
DWORD dwBufferLength, DWORD dwFlags /* = 0 */)
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
ASSERT(AfxIsValidAddress(lpBuffer, dwBufferLength, FALSE));
ASSERT(dwBufferLength != 0);
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
// bogus flag?
ASSERT(dwFlags == 0 || WCE_IF(FALSE, ((dwFlags & ISO_VALID_FLAGS) == dwFlags)));
return WCE_FCTN(InternetSetOptionEx)(m_hConnection, dwOption,
lpBuffer, dwBufferLength, dwFlags);
}
BOOL CCEInternetConnection::QueryOption(DWORD dwOption, LPVOID lpBuffer,
LPDWORD lpdwBufferLength) const
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
ASSERT_POINTER(lpdwBufferLength, DWORD);
ASSERT(AfxIsValidAddress(lpBuffer, *lpdwBufferLength));
ASSERT(*lpdwBufferLength != 0);
return InternetQueryOption(m_hConnection, dwOption,
lpBuffer, lpdwBufferLength);
}
BOOL CCEInternetConnection::QueryOption(DWORD dwOption, DWORD& dwValue) const
{
DWORD dwLen = sizeof(DWORD);
return InternetQueryOption(m_hConnection, dwOption,
&dwValue, &dwLen);
}
BOOL CCEInternetConnection::QueryOption(DWORD dwOption, CString& refString) const
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
return _CEQueryCStringInternetOption(m_hConnection, dwOption, refString);
}
void CCEInternetConnection::Close()
{
if (m_hConnection != NULL)
{
InternetCloseHandle(m_hConnection);
_CESessionMap.RemoveKey(m_hConnection);
m_hConnection = NULL;
}
}
CString CCEInternetConnection::GetServerName() const
{
return m_strServerName;
}
DWORD CCEInternetConnection::GetContext() const
{
return m_dwContext;
}
CCEInternetSession* CCEInternetConnection::GetSession() const
{
return m_pSession;
}
#ifdef _DEBUG
void CCEInternetConnection::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "m_hConnection = " << m_hConnection;
}
void CCEInternetConnection::AssertValid() const
{
CObject::AssertValid();
}
#endif
//---------------------------------------------------------------------------
IMPLEMENT_DYNAMIC(CCEFtpConnection, CCEInternetConnection)
CCEFtpConnection::~CCEFtpConnection()
{
if(m_hConnection!=NULL)
{
Close();
}
}
CCEFtpConnection::CCEFtpConnection(CCEInternetSession* pSession,
HINTERNET hConnected, LPCTSTR pstrServer, DWORD dwContext)
: CCEInternetConnection(pSession, pstrServer, INTERNET_INVALID_PORT_NUMBER,
dwContext)
{
ASSERT(pSession != NULL);
ASSERT(AfxIsValidString(pstrServer));
BOOL bBadType = FALSE;
if (CEGetInternetHandleType(hConnected) != INTERNET_HANDLE_TYPE_CONNECT_FTP)
{
ASSERT(FALSE); // used the wrong handle type
bBadType = TRUE;
}
m_strServerName = pstrServer;
m_hConnection = hConnected;
if (m_hConnection == NULL || bBadType)
CEThrowInternetException(m_dwContext, ERROR_INVALID_HANDLE);
else
_CESessionMap.SetAt(m_hConnection, m_pSession);
}
CCEFtpConnection::CCEFtpConnection(CCEInternetSession* pSession,
LPCTSTR pstrServer, LPCTSTR pstrUserName /* = NULL */,
LPCTSTR pstrPassword /* = NULL */, DWORD dwContext /* = 0 */,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */,
BOOL bPassive /* = FALSE */)
: CCEInternetConnection(pSession, pstrServer, nPort, dwContext)
{
ASSERT(pSession != NULL);
ASSERT_KINDOF(CCEInternetSession, pSession);
ASSERT(AfxIsValidString(pstrServer));
m_strServerName = pstrServer;
m_hConnection = InternetConnect((HINTERNET) *pSession, pstrServer,
nPort, pstrUserName, pstrPassword, INTERNET_SERVICE_FTP,
(bPassive ? INTERNET_FLAG_PASSIVE : 0), m_dwContext);
if (m_hConnection == NULL)
CEThrowInternetException(m_dwContext, ::GetLastError());
else
_CESessionMap.SetAt(m_hConnection, m_pSession);
}
void CCEFtpConnection::Close()
{
CCEInternetConnection::Close();
}
BOOL CCEFtpConnection::Remove(LPCTSTR pstrFileName)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrFileName));
return FtpDeleteFile(m_hConnection, pstrFileName);
}
BOOL CCEFtpConnection::Rename(LPCTSTR pstrExisting, LPCTSTR pstrNew)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrExisting));
ASSERT(AfxIsValidString(pstrNew));
return FtpRenameFile(m_hConnection, pstrExisting, pstrNew);
}
BOOL CCEFtpConnection::CreateDirectory(LPCTSTR pstrDirName)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrDirName));
return FtpCreateDirectory(m_hConnection, pstrDirName);
}
BOOL CCEFtpConnection::RemoveDirectory(LPCTSTR pstrDirName)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrDirName));
return FtpRemoveDirectory(m_hConnection, pstrDirName);
}
BOOL CCEFtpConnection::SetCurrentDirectory(LPCTSTR pstrDirName)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrDirName));
return FtpSetCurrentDirectory(m_hConnection, pstrDirName);
}
BOOL CCEFtpConnection::GetCurrentDirectory(LPTSTR pstrDirName,
LPDWORD lpdwLen) const
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidAddress(pstrDirName, *lpdwLen));
ASSERT(lpdwLen != 0);
return FtpGetCurrentDirectory(m_hConnection, pstrDirName, lpdwLen);
}
BOOL CCEFtpConnection::GetCurrentDirectoryAsURL(CString& strDirName) const
{
CString strDirectory;
if (!GetCurrentDirectory(strDirectory))
return FALSE;
strDirName = _CEURLftp;
strDirName += GetServerName();
if (strDirectory[0] != '/')
strDirName += '/';
strDirName += strDirectory;
return TRUE;
}
BOOL CCEFtpConnection::GetCurrentDirectoryAsURL(LPTSTR pstrName,
LPDWORD lpdwLen) const
{
ASSERT(lpdwLen != NULL);
ASSERT_POINTER(lpdwLen, DWORD);
ASSERT(AfxIsValidAddress(pstrName, *lpdwLen));
ASSERT(*lpdwLen != 0);
CString strTemp;
if (lpdwLen == NULL || !GetCurrentDirectoryAsURL(strTemp))
return FALSE;
if (pstrName == NULL)
*lpdwLen = strTemp.GetLength();
else
wcsncpy(pstrName, (LPCTSTR) strTemp, max(0, *lpdwLen -1));
return TRUE;
}
BOOL CCEFtpConnection::GetCurrentDirectory(CString& strDirName) const
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
DWORD dwLen = INTERNET_MAX_PATH_LENGTH;
LPTSTR pstrTarget = strDirName.GetBufferSetLength(dwLen);
BOOL bRet = FtpGetCurrentDirectory(m_hConnection, pstrTarget, &dwLen);
if (bRet)
strDirName.ReleaseBuffer(dwLen);
else
strDirName.ReleaseBuffer(0);
return bRet;
}
CCEInternetFile* CCEFtpConnection::OpenFile(LPCTSTR pstrFileName,
DWORD dwAccess /* = GENERIC_READ */,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */,
DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(dwAccess != (GENERIC_READ | GENERIC_WRITE));
ASSERT(dwAccess == GENERIC_READ || dwAccess == GENERIC_WRITE);
ASSERT(AfxIsValidString(pstrFileName));
HINTERNET hFile;
if (dwContext == 1)
dwContext = m_dwContext;
hFile = FtpOpenFile(m_hConnection, pstrFileName, dwAccess,
dwFlags, dwContext);
if (hFile == NULL)
CEThrowInternetException(dwContext);
CCEInternetFile* pFile = new CCEInternetFile(hFile, pstrFileName, this,
(dwAccess == GENERIC_READ));
return pFile;
}
BOOL CCEFtpConnection::PutFile(LPCTSTR pstrLocalFile, LPCTSTR pstrRemoteFile,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */,
DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrRemoteFile));
ASSERT(AfxIsValidString(pstrLocalFile));
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
if (dwContext == 1)
dwContext = m_dwContext;
return FtpPutFile(m_hConnection, pstrLocalFile, pstrRemoteFile,
dwFlags, dwContext);
}
BOOL CCEFtpConnection::GetFile(LPCTSTR pstrRemoteFile, LPCTSTR pstrLocalFile,
BOOL bFailIfExists /* = FALSE */,
DWORD dwAttributes /* = FILE_ATTRIBUTE_NORMAL */,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */, DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrRemoteFile));
ASSERT(AfxIsValidString(pstrLocalFile));
ASSERT(!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY));
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
if (dwContext == 1)
dwContext = m_dwContext;
return FtpGetFile(m_hConnection, pstrRemoteFile, pstrLocalFile,
bFailIfExists, dwAttributes, dwFlags, dwContext);
}
#ifdef _DEBUG
void CCEFtpConnection::Dump(CDumpContext& dc) const
{
CCEInternetConnection::Dump(dc);
dc << "\nm_strServerName = " << m_strServerName;
}
void CCEFtpConnection::AssertValid() const
{
ASSERT(m_pSession != NULL);
if (m_hConnection != NULL)
{
ASSERT(CEGetInternetHandleType(m_hConnection)
== INTERNET_HANDLE_TYPE_CONNECT_FTP);
}
}
#endif
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// Internet Files
IMPLEMENT_DYNAMIC(CCEInternetFile, CStdioFile)
CCEInternetFile::CCEInternetFile(HINTERNET hFile, HINTERNET /* hSession */,
LPCTSTR pstrFileName, LPCTSTR pstrServer, DWORD dwContext, BOOL bReadMode)
: m_dwContext(dwContext)
{
// caller must set _CESessionMap()!
ASSERT(AfxIsValidString(pstrServer));
ASSERT(AfxIsValidString(pstrFileName));
ASSERT(hFile != NULL);
m_strFileName = pstrFileName;
m_strServerName = pstrServer;
m_hFile = hFile;
m_bReadMode = bReadMode;
m_pbReadBuffer = NULL;
m_pbWriteBuffer = NULL;
m_nReadBufferSize = 0;
m_nReadBufferPos = 0;
m_nWriteBufferSize = 0;
m_nWriteBufferPos = 0;
m_nReadBufferBytes = 0;
}
CCEInternetFile::CCEInternetFile(HINTERNET hFile,
LPCTSTR pstrFileName, CCEInternetConnection* pConnection, BOOL bReadMode)
{
ASSERT(AfxIsValidString(pstrFileName));
ASSERT(pConnection != NULL);
ASSERT_VALID(pConnection);
ASSERT(hFile != NULL);
_CESessionMap.SetAt(hFile, pConnection->GetSession());
m_strFileName = pstrFileName;
m_dwContext = pConnection->GetContext();
m_strServerName = pConnection->GetServerName();
m_hFile = hFile;
m_bReadMode = bReadMode;
m_pbReadBuffer = NULL;
m_pbWriteBuffer = NULL;
m_nReadBufferSize = 0;
m_nReadBufferPos = 0;
m_nWriteBufferSize = 0;
m_nWriteBufferPos = 0;
m_nReadBufferBytes = 0;
}
BOOL CCEInternetFile::QueryOption(DWORD dwOption, LPVOID lpBuffer,
LPDWORD lpdwBufferLength) const
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
ASSERT_POINTER(lpdwBufferLength, DWORD);
ASSERT(AfxIsValidAddress(lpBuffer, *lpdwBufferLength));
ASSERT(*lpdwBufferLength != 0);
ASSERT(m_hFile != NULL);
return InternetQueryOption(m_hFile, dwOption,
lpBuffer, lpdwBufferLength);
}
BOOL CCEInternetFile::QueryOption(DWORD dwOption, DWORD& dwValue) const
{
ASSERT(m_hFile != NULL);
DWORD dwLen = sizeof(DWORD);
return InternetQueryOption(m_hFile, dwOption,
&dwValue, &dwLen);
}
BOOL CCEInternetFile::QueryOption(DWORD dwOption, CString& refString) const
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
ASSERT(m_hFile != NULL);
return _CEQueryCStringInternetOption(m_hFile, dwOption, refString);
}
BOOL CCEInternetFile::SetOption(DWORD dwOption, LPVOID lpBuffer,
DWORD dwBufferLength, DWORD dwFlags /* = 0 */)
{
ASSERT(dwOption >= INTERNET_FIRST_OPTION &&
dwOption <= INTERNET_LAST_OPTION);
ASSERT(AfxIsValidAddress(lpBuffer, dwBufferLength, FALSE));
ASSERT(dwBufferLength != 0);
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
// bogus flag?
ASSERT(dwFlags == 0 || WCE_IF(FALSE, ((dwFlags & ISO_VALID_FLAGS) == dwFlags)));
return WCE_FCTN(InternetSetOptionEx)(m_hFile, dwOption,
lpBuffer, dwBufferLength, dwFlags);
}
BOOL CCEInternetFile::SetReadBufferSize(UINT nReadSize)
{
ASSERT_VALID(this);
BOOL bRet = TRUE;
if (nReadSize != -1 && nReadSize != m_nReadBufferSize)
{
if (m_nReadBufferPos > nReadSize)
bRet = FALSE;
else
{
if (nReadSize == 0)
{
delete [] m_pbReadBuffer;
m_pbReadBuffer = NULL;
}
else if (m_pbReadBuffer == NULL)
{
m_pbReadBuffer = new BYTE[nReadSize];
m_nReadBufferPos = nReadSize;
}
else
{
DWORD dwMoved = m_nReadBufferSize - m_nReadBufferPos;
LPBYTE pbTemp = m_pbReadBuffer;
m_pbReadBuffer = new BYTE[nReadSize];
if (dwMoved > 0)
{
WCE_FCTN(memcpy)(m_pbReadBuffer, pbTemp + m_nReadBufferPos, dwMoved);
m_nReadBufferPos = 0;
m_nReadBufferBytes = dwMoved;
}
else
{
m_nReadBufferBytes = 0;
m_nReadBufferPos = nReadSize;
}
delete [] pbTemp;
}
m_nReadBufferSize = nReadSize;
}
}
return bRet;
}
CCEInternetFile::~CCEInternetFile()
{
if (m_hFile != NULL)
{
#ifdef _DEBUG
TRACE1("Warning: destroying an open with handle %8.8X\n",
m_hFile);
#endif
Close();
}
if (m_pbReadBuffer != NULL)
delete m_pbReadBuffer;
if (m_pbWriteBuffer != NULL)
delete m_pbWriteBuffer;
}
void CCEInternetFile::Abort()
{
ASSERT_VALID(this);
if (m_hFile != NULL)
Close();
m_strFileName.Empty();
}
void CCEInternetFile::Close()
{
if (m_hFile != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -