📄 controlsocket.cpp
字号:
// ControlSocket.cpp : implementation file
//
#include "stdafx.h"
#include "ControlSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////
// CControlSocket
CControlSocket::CControlSocket()
{
m_pClientDlg=0;
welcomed=false;
}
CControlSocket::~CControlSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CControlSocket, CSocket)
//{{AFX_MSG_MAP(CControlSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CControlSocket member functions
void CControlSocket::OnConnect(int nErrorCode)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
m_pClientDlg->m_isConnected=true;
if(nErrorCode)
{
m_pClientDlg->m_isConnected=false;
m_pClientDlg->Disconnect();
}
CSocket::OnConnect(nErrorCode);
}
void CControlSocket::OnSend(int nErrorCode)
{
CSocket::OnSend(nErrorCode);
while(!m_userCommandList.IsEmpty())
{
CString command=m_userCommandList.GetHead();
m_userCommandList.RemoveHead();
int nSent=Send(command+"\r\n",command.GetLength()+2);
switch(nSent)
{
case SOCKET_ERROR:
if(GetLastError()==WSAEWOULDBLOCK)
{
Sleep(0);
m_pClientDlg->AddActivity("GetLastError()==WSEAWOULDBLOCK");
m_userCommandList.AddHead(command);
}else //Error;
{
Close();
m_pClientDlg->AddActivity("Send command error.");
return;
}
break;
default:
m_pClientDlg->AddActivity("Send command successfully.");
break;
}
}
}
void CControlSocket::OnClose(int nErrorCode)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
ShutDown(1);
m_pClientDlg->Disconnect();
CSocket::OnClose(nErrorCode);
}
void CControlSocket::OnReceive(int nErrorCode)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
TCHAR buf[4096];
int nRead=CSocket::Receive(buf,4096);
switch(nRead)
{
case 0: //receive finished;
Close();
m_pClientDlg->AddActivity("Receive finished.");
break;
case SOCKET_ERROR:
if(GetLastError()==WSAEWOULDBLOCK)
{
Sleep(0);
}else
{
Close();
m_pClientDlg->AddActivity("Receive Errored.");
}
break;
default:
buf[nRead]=0;
receiveBuf+=buf;
int index;
while((index=receiveBuf.Find("\r\n"))!=-1)
{
AddServerReply(receiveBuf.Left(index));
receiveBuf=receiveBuf.Mid(index+2);
}
// ProcessCommand();
break;
}
CSocket::OnReceive(nErrorCode);
}
bool CControlSocket::WaitFor(LPCTSTR cmdCode)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
bool waited=false;
CString commandCode;
DWORD dwTickCount = GetTickCount() + 5000;
m_pClientDlg->AddActivity("Wait FTP Server for reply message...");
while(!waited)
{
Sleep(0);
//Continue to process window message ;
MSG msg;
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (dwTickCount < GetTickCount())
{
m_pClientDlg->AddActivity("Wait for message timeout in 5 seconds.");
return false;
}
while(!m_serverCommandList.IsEmpty())
{
commandCode=m_serverCommandList.GetHead();
m_serverCommandList.RemoveHead();
m_pClientDlg->AddActivity("->"+commandCode);
commandCode=commandCode.Left(3);
if(commandCode==cmdCode)
{
waited=true;
break;
}
}
}
return waited;
}
bool CControlSocket::Login(LPCTSTR userName, LPCTSTR userPsw)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
if(WaitFor("220"))
{
CString un=userName,up=userPsw;
m_userCommandList.AddTail("USER "+un);
OnSend(0);
if(WaitFor("331"))
{
m_userCommandList.AddTail("PASS "+up);
OnSend(0);
if(WaitFor("230"))
m_pClientDlg->logined=true;
}
}
return (m_pClientDlg->logined);
}
bool CControlSocket::GetListData(LPCTSTR remotePath)
{
if(m_pClientDlg==0)
m_pClientDlg=(CFTPClientDlg *)(AfxGetApp()->m_pMainWnd);
if(!(m_pClientDlg->logined))
{
m_pClientDlg->AddActivity("Client NOT yet connected.");
return false;
}
if(!CreateDataSocket())
{
m_pClientDlg->AddActivity("Create Data Socket failed.");
return false;
}
CString rp=remotePath;
m_userCommandList.AddTail("LIST "+rp);
OnSend(0);
if(WaitFor("150")) //Start receive list data;
{
bool listEnd=false;
DWORD dwTickCount = GetTickCount() + 5000;
CString tmp;
while(!listEnd)
{
m_pClientDlg->AddActivity("Wait FTP Server for list information...");
Sleep(100);
//Continue to process window message ;
MSG msg;
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (dwTickCount < GetTickCount())
{
m_pClientDlg->AddActivity("Wait for list information timeout in 5 seconds.");
return false;
}
while(!m_serverCommandList.IsEmpty())
{
tmp=m_serverCommandList.GetHead();
m_serverCommandList.RemoveHead();
if(tmp.Left(1)=='-'||tmp.Left(1)=='d') //valid list information.
{
m_pClientDlg->m_listData.AddTail(tmp);
}else
if(tmp.Left(3)=="226")
{
m_pClientDlg->AddActivity(tmp);
listEnd=true;
}else
{
m_serverCommandList.AddHead(tmp);
return true;
}
}
}
}
return true;
}
//DEL CString CControlSocket::ProcessCommand()
//DEL {
//DEL CString commandCode;
//DEL while(!m_serverCommandList.IsEmpty())
//DEL {
//DEL commandCode=m_serverCommandList.GetHead();
//DEL m_pClientDlg->AddActivity("->"+commandCode);
//DEL commandCode=commandCode.Left(3);
//DEL if(commandCode=="220")
//DEL {
//DEL welcomed=true;
//DEL m_serverCommandList.RemoveHead();
//DEL }else
//DEL break;
//DEL }
//DEL return commandCode;
//DEL }
bool CControlSocket::CreateDataSocket()
{
CDataSocket* &m_pDataSocket=m_pClientDlg->m_pDataSocket;
if(m_pDataSocket==0)
m_pDataSocket=new CDataSocket(this);
if(m_pDataSocket==0)
{
m_pClientDlg->AddActivity("New m_pDataSocket failed.");
return false;
}
m_pDataSocket->Close();
int ret;
ret=m_pDataSocket->Create();
if(ret==0)
{
m_pClientDlg->AddActivity("m_pControlSocket->Create() failed.");
return false;
}
ret=m_pDataSocket->Listen();
if(ret==0)
{
m_pClientDlg->AddActivity("m_pDataSocket->Listen() failed.");
return false;
}
CString tmp,dotIP,port;
UINT sockPort;
if(this)
this->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_userCommandList.AddTail("PORT "+dotIP+port);
OnSend(0);
if(!WaitFor("200"))
return false;
return true;
}
/*
void CControlSocket::AddServerReply(LPCTSTR serverReply)
{
m_criticalSection.Lock();
m_serverCommandList.AddTail(serverReply);
m_criticalSection.Unlock();
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -