📄 ftpclientdlg.cpp
字号:
if(!hParent)return "";
strPath = tree.GetItemText(hti);
while (hParent != hRoot) //Currunt isn't drive.
{
strPath = tree.GetItemText(hParent) + "/" + strPath;
hParent = tree.GetParentItem(hParent);
}
strPath += "/";
strPath="/"+strPath;
// drive = strPath.Mid(strPath.Find(":") - 2 + 1,2);
// drive = "/";
// tail = strPath.Right(strPath.GetLength()- 2);
// strPath = drive + tail;
return strPath;
}
void CFTPClientDlg::OnItemexpandingRemoteTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hti = pNMTreeView->itemNew.hItem;
CTreeCtrl &tree = m_treeRemote;
if (hti != tree.GetRootItem())
{
//清空儿子
HTREEITEM hChild = tree.GetChildItem(hti);
while (hChild)
{
tree.DeleteItem(hChild);
hChild = tree.GetChildItem(hti);
}
if (!GetRemoteSubSiblingItem(hti,GetRemoteSel(hti)))
HideButton(hti);
}
*pResult = 0;
}
BOOL CFTPClientDlg::GetRemoteSubSiblingItem(HTREEITEM hItem, LPCTSTR szCurDir)
{
CTreeCtrl &m_fileTreeCtrl=m_treeRemote;
BOOL bHaveChild = FALSE;
HTREEITEM hti;
if(!m_pControlSocket)
{
AddActivity("Client NOT yet connected.");
GetDlgItem(IDC_CONNECT_SERVER)->SetWindowText("Connect");
return bHaveChild;
}
m_remotePath=GetRemoteSel(hItem);
if(!(m_pControlSocket->GetListData(m_remotePath)))
return bHaveChild;
if(m_listData.IsEmpty())
return bHaveChild;
else
bHaveChild=true;
while(!m_listData.IsEmpty())
{
CString tmp=m_listData.GetHead();
m_listData.RemoveHead();
while(tmp.Replace(" "," "));
CString fName;
for(int i=0;i<=8;i++)
AfxExtractSubString(fName,tmp,i,' ');
if(tmp.Left(1)=='d') //Add fold to first;
{
hti = m_fileTreeCtrl.InsertItem(fName,6,6,hItem,TVI_FIRST);
m_fileTreeCtrl.SetItemData(hti,DIR);
DisplayButtonR(hti);
}else //Add files to last;
{
hti = m_fileTreeCtrl.InsertItem(fName,1,1,hItem,TVI_LAST);
m_fileTreeCtrl.SetItemData(hti,DIR);
HideButtonR(hti);
}
}
return bHaveChild;
}
bool CFTPClientDlg::VertifyRemote(LPCTSTR m_remotePath, bool isDir)
{
if(!(m_pControlSocket->GetListData(m_remotePath)))
return false;
Sleep(500);
if(m_listData.IsEmpty())
return false;
if(m_listData.GetCount()==1)
{
CString tmp=m_listData.GetHead();
m_listData.RemoveHead();
if(tmp.Left(1)=='d')
{
if(isDir)
return true;
else
return false;
}else
{
if(isDir)
return false;
else
return true;
}
}else
{
if(isDir)
return true;
else
return false;
}
}
void CFTPClientDlg::OnUploadFile()
{
if(!logined)
{
AddActivity("Client NOT yet connected.");
return;
}
if(VertifyPath(m_localPath,false)==false)
{
MessageBox("Please choose a local file.");
return;
}
if(VertifyRemote(m_remotePath,true)==false)
{
MessageBox("Please choose a remote folder.");
return;
}
//Open data connection for file upload;
if(!OpenFileSocket())
{
AddActivity("Open file socket failed.");
return;
}
// Prepare CFile for file read;
if (m_file.m_hFile != NULL)
{
m_file.Close();
}
// open source file (bug fix by Mutex)
if (!m_file.Open(m_localPath, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary))
{
return ;
}
DWORD sizeToTransfer = m_file.GetLength();
DWORD sizeTransfered = 0;
MSG msg;
byte data[4096];
int dwBytes;
m_transportProgress.SetRange32(0,sizeToTransfer);
//Lock user list command;
GetDlgItem(IDC_TREE_REMOTE)->EnableWindow(false);
while(sizeTransfered < sizeToTransfer)
{
//Continue to process window message ;
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// allocate space to store data
m_file.Seek(sizeTransfered, CFile::begin);
DWORD dwRead = m_file.Read(data, 4096);
if ((dwBytes =m_pFileSocket-> Send(data, dwRead)) == SOCKET_ERROR)
{
if (GetLastError() == WSAEWOULDBLOCK)
{
Sleep(0);
dwBytes=0;
}
else
{
// close file.
m_file.Close();
m_file.m_hFile = NULL;
// close the data connection.
m_pFileSocket->Close();
sizeTransfered = 0;
sizeToTransfer = 0;
AddActivity("File socket closed transfer aborted.");
//Enable user list.
GetDlgItem(IDC_TREE_REMOTE)->EnableWindow(true);
return;
}
}
else
{
m_transportProgress.SetStep(1+dwBytes);
m_transportProgress.StepIt();
sizeTransfered += dwBytes;
}
}
//Enable user list.
GetDlgItem(IDC_TREE_REMOTE)->EnableWindow(true);
if (sizeTransfered == sizeToTransfer)
{
// close file.
m_file.Close();
m_file.m_hFile = NULL;
// close the data connection.
m_pFileSocket->Close();
sizeTransfered = 0;
sizeToTransfer = 0;
AddActivity("Transfer file finished.");
}else
AddActivity("Error occured when transfer data.");
}
void CFTPClientDlg::OnDownloadFile()
{
if(!logined)
{
AddActivity("Client NOT yet connected.");
return;
}
if(VertifyPath(m_localPath,true)==false)
{
MessageBox("Please choose a local folder.");
return;
}
if(VertifyRemote(m_remotePath,false)==false)
{
MessageBox("Please choose a remote file.");
return;
}
//Get file size;
if(!(m_pControlSocket->GetListData(m_remotePath)))
return ;
Sleep(500);
if(m_listData.GetCount()!=1)
return ;
CString tmp=m_listData.GetHead();
m_listData.RemoveHead();
while(tmp.Replace(" "," "));
CString fileSize;
for(int i=0;i<=4;i++)
AfxExtractSubString(fileSize,tmp,i,' ');
DWORD dwFile=atol(fileSize);
CString fileName=m_remotePath;
fileName.TrimRight('/');
int index=fileName.ReverseFind('/');
fileName=fileName.Mid(index+1);
m_localPath.TrimRight('\\');
m_localPath=m_localPath+'\\'+fileName+".bf!";
//Restart at the old file size;
DWORD restartOffset=0L;
CFileStatus status;
if(CFile::GetStatus(m_localPath, status))
{
restartOffset=status.m_size;
}
char buf[32];
memset(buf,32*sizeof(char),0);
ltoa(restartOffset,buf,10);
m_pControlSocket->m_userCommandList.AddTail("REST "+CString(buf));
m_pControlSocket->OnSend(0);
if(!m_pControlSocket->WaitFor("350"))
return ;
//Open data connection for file download;
if(!OpenFileSocketD())
{
AddActivity("Open file socket failed.");
return;
}
// Prepare CFile for file write;
if (m_file.m_hFile != NULL)
{
m_file.Close();
}
// open destination file
if (!m_file.Open(m_localPath, CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyWrite))
{
return ;
}
//Set file length and prepare to store data;
m_file.SetLength(restartOffset);
m_file.SeekToEnd();
//Send command to retrive file;
m_pControlSocket->m_userCommandList.AddTail("RETR "+m_remotePath);
m_pControlSocket->OnSend(0);
Sleep(500);
m_state=STATE_DOWNLOAD;
if(!m_pControlSocket->WaitFor("150"))
{
m_file.Close();
m_file.m_hFile=NULL;
return ;
}
m_toTransfer=dwFile-restartOffset;
if(m_toTransfer<0)m_toTransfer=0;
m_transfered=restartOffset;
while(m_transfered<m_toTransfer)
{
MSG msg;
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if((m_pControlSocket->WaitFor("226"))&&m_toTransfer==m_transfered)
{
m_file.Close();
m_file.m_hFile=0;
m_pFileSocket->Close();
AddActivity("File download finished.");
m_state=STATE_LIST;
}else
{
m_file.Close();
m_file.m_hFile=0;
m_pFileSocket->Close();
AddActivity("File download errored;Try re-download.");
m_state=STATE_LIST;
}
}
bool CFTPClientDlg::OpenFileSocket()
{
CDataSocket* &m_pDataSocket=m_pFileSocket;
if(m_pDataSocket==0)
m_pDataSocket=new CDataSocket(m_pControlSocket);
if(m_pDataSocket==0)
{
AddActivity("New m_pFileScoket failed.");
return false;
}
m_pDataSocket->Close();
int ret;
ret=m_pDataSocket->Create();
if(ret==0)
{
AddActivity("m_pFileScoket->Create() failed.");
return false;
}
ret=m_pDataSocket->Listen();
if(ret==0)
{
AddActivity("m_pFileScoket->Listen() failed.");
return false;
}
CString tmp,dotIP,port;
UINT sockPort;
if(m_pControlSocket)
m_pControlSocket->GetSockName(dotIP,sockPort);
else
return false;
m_pDataSocket->GetSockName(tmp,sockPort);
while(dotIP.Replace('.',','));
port.Format(",%d,%d",sockPort/256,sockPort%256);
// m_command="PORT "+dotIP+port;
m_pControlSocket->m_userCommandList.AddTail("PORT "+dotIP+port);
m_pControlSocket->OnSend(0);
if(!m_pControlSocket->WaitFor("200"))
return false;
m_pControlSocket->m_userCommandList.AddTail("CWD "+m_remotePath);
m_pControlSocket->OnSend(0);
if(!m_pControlSocket->WaitFor("250"))
return false;
int index=m_localPath.ReverseFind('\\');
if(index==-1) return false;
CString m_pFileSocket=m_localPath.Mid(index+1);
m_pControlSocket->m_userCommandList.AddTail("STOR "+m_pFileSocket);
m_pControlSocket->OnSend(0);
if(!m_pControlSocket->WaitFor("150"))
return false;
return true;
}
bool CFTPClientDlg::OpenFileSocketD()
{
CDataSocket* &m_pDataSocket=m_pFileSocket;
if(m_pDataSocket==0)
m_pDataSocket=new CDataSocket(m_pControlSocket);
if(m_pDataSocket==0)
{
AddActivity("New m_pFileScoket failed.");
return false;
}
m_pDataSocket->Close();
int ret;
ret=m_pDataSocket->Create();
if(ret==0)
{
AddActivity("m_pFileScoket->Create() failed.");
return false;
}
ret=m_pDataSocket->Listen();
if(ret==0)
{
AddActivity("m_pFileScoket->Listen() failed.");
return false;
}
//Send "PORT" for data
CString tmp,dotIP,port;
UINT sockPort;
if(m_pControlSocket)
m_pControlSocket->GetSockName(dotIP,sockPort);
else
return false;
m_pDataSocket->GetSockName(tmp,sockPort);
while(dotIP.Replace('.',','));
port.Format(",%d,%d",sockPort/256,sockPort%256);
m_pControlSocket->m_userCommandList.AddTail("PORT "+dotIP+port);
m_pControlSocket->OnSend(0);
if(!m_pControlSocket->WaitFor("200"))
return false;
return true;
}
int CFTPClientDlg::SaveToFile(CSocket* pSocket)
{
TCHAR data[4096];
int readCount=pSocket->Receive(data,4096);
switch(readCount)
{
case 0: //transfer finished,download succesfully;
m_file.Close();
m_file.m_hFile=0;
pSocket->Close();
AddActivity("File download finished.");
m_state=STATE_LIST;
//Rename the file;
break;
case SOCKET_ERROR:
if(GetLastError()!=WSAEWOULDBLOCK)
{
m_file.Close();
m_file.m_hFile=0;
pSocket->Close();
AddActivity("File download aborted;Try re-download.");
m_state=STATE_LIST;
}
break;
default:
data[readCount]=0;
TRY{
m_file.Write(data,readCount);
m_transfered+=readCount;
Sleep(10);
}CATCH_ALL(e)
{
m_file.Close();
m_file.m_hFile=0;
pSocket->Close();
AddActivity("450 Can't access to file;Transfer aborted.");
m_state=STATE_LIST;
return -1;
}
END_CATCH_ALL;
break;
}
return readCount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -