//---------------------------------------------------------------------------
#include <basepch.h>
#pragma hdrstop
#include "MultiFtp.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TMultiFtp *)
{
new TMultiFtp(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMultiFtp::TMultiFtp(TComponent* Owner)
: TComponent(Owner)
{
lock = false;
isUseFile = false;
runningThreadCnt = 0;
stop = false;
this->Owner = Owner;
}
__fastcall TMultiFtp::~TMultiFtp()
{
fclose(this->globalFile);
if(this->inforImpl.fromToImpl)
delete[] this->inforImpl.fromToImpl;
}
//---------------------------------------------------------------------------
namespace Multiftp
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMultiFtp)};
RegisterComponents("System", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMultiFtp::FreeMemory()
{
if(this->globalFile)
fclose(this->globalFile);
if(this->inforImpl.fromToImpl)
delete[] this->inforImpl.fromToImpl;
}
SOCKET __fastcall TMultiFtp::ConnectFtp(String host ,int port ,String userName ,String pass)
{
this->DoOnTextOut("欢迎使用funinhand多线程,断点续传软件!!");
MultiThreadDealSocket *dealSocket = new MultiThreadDealSocket();
SOCKET client = dealSocket->GetConnect(host,port);
char * buffer = new char[100];
int recLen ;
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
if(client == NULL)
{
this->DoOnException("连接ftp服务器失败!");
delete[] buffer;
return NULL;
}
this->DoOnTextOut("连接ftp服务器成功!");
String user = "USER "+userName+" \r\n";
this->DoOnTextOut(user);
send(client,user.c_str(),user.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
if(GetCode(buffer) == "331")
{
this->DoOnTextOut("服务器要求验证密码。");
String password = "PASS "+pass+" \r\n";
this->DoOnTextOut(password);
send(client,password.c_str(),password.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
int tryTimes = 3;
while(GetCode(buffer) != "230" && tryTimes > 0)
{
send(client,password.c_str(),password.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
tryTimes --;
this->DoOnTextOut("第"+IntToStr(3-tryTimes)+"尝试");
}
if(tryTimes < 0)
{
this->DoOnException(userName +"登录失败!");
delete[] buffer;
return NULL;
}
else
this->DoOnTextOut("登录成功!");
}
char *type = "TYPE I \r\n";
this->DoOnTextOut(type);
send(client,type,strlen(type),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
this->DoOnTextOut(buffer);
delete[] buffer;
return client;
}
void __fastcall TMultiFtp::SetStop(bool stop)
{
this->stop = stop;
}
void __fastcall TMultiFtp::DoOnComplete()
{
if(this->FOnComplete)
this->FOnComplete(this->Owner);
}
void __fastcall TMultiFtp::DoOnGetFileSize(DWORD fileSize)
{
if(this->FOnGetFileSize)
this->FOnGetFileSize(this->Owner,fileSize);
}
void __fastcall TMultiFtp::DoOnTextOut(String text)
{
if(this->FOnTextOut)
{
int index = text.Pos("\r\n");
if(index > 0)
this->FOnTextOut(this->Owner,text.SubString(1,index-1));
else
this->FOnTextOut(this->Owner,text);
}
}
void __fastcall TMultiFtp::DoOnException(String error)
{
if(this->FOnException)
{
int index = error.Pos("\r\n");
if(index > 0)
{
this->FOnException(this->Owner,error.SubString(1,index-1));
}
else
{
this->FOnException(this->Owner,error);
}
}
}
void __fastcall TMultiFtp::DoOnProgress(DWORD pos)
{
if(this->FOnProgress)
this->FOnProgress(this->Owner,pos);
}
void __fastcall TMultiFtp::DoOnGetRate(DWORD cnt)
{
if(this->FOnGetRate)
this->FOnGetRate(this->Owner,cnt);
}
String __fastcall TMultiFtp::GetCode(String revStr)
{
String str;
int index ;
index = revStr.Pos(" ");
str = revStr.SubString(1,index-1);
return str;
}
DWORD __fastcall TMultiFtp::GetFileSize()
{
return this->fileSize;
}
DWORD __fastcall TMultiFtp::GetFtpFileSize(SOCKET client ,String fileName)
{
String size = "SIZE "+this->FFileName+" \r\n";
this->DoOnTextOut(size);
send(client,size.c_str(),size.Length(),0);
char *buffer= new char[100];
int recLen ;
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
this->DoOnTextOut(buffer);
if(GetCode(buffer) != "213")
{
this->DoOnTextOut("尝试第二次获取");
send(client,size.c_str(),size.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
this->DoOnTextOut(buffer);
if(GetCode(buffer) != "213")
{
this->DoOnTextOut("尝试第三次获取");
send(client,size.c_str(),size.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
this->DoOnTextOut(buffer);
if(GetCode(buffer) != "213")
{
this->DoOnException("获取文件大小失败!");
delete[] buffer;
return 0;
}
else
{
this->DoOnTextOut(buffer);
}
}
else
{
this->DoOnTextOut(buffer);
}
}
else
{
this->DoOnTextOut(buffer);
}
String recvStr(buffer);
delete[] buffer;
int index = recvStr.Pos("\r\n");
recvStr = recvStr.SubString(1,index-1);
index = recvStr.Pos(" ");
recvStr = recvStr.SubString(index+1,recvStr.Length()- index);
this->fileSize =StrToInt(recvStr);
return this->fileSize;
}
bool __fastcall TMultiFtp::CheckIsSupportMulti(SOCKET client)
{
String rest = "REST 100 \r\n";
char *buffer = new char[100];
this->DoOnTextOut(rest);
int recLen ;
send(client,rest.c_str(),rest.Length(),0);
recLen = recv(client,buffer,100,0);
buffer[recLen]=0;
this->DoOnTextOut(buffer);
if(GetCode(buffer) != "350")
{
delete[] buffer;
return false;
}
delete[] buffer;
return true;
}
FileTail * __fastcall TMultiFtp::GetFilePosLen()
{
while(lock)
{
Sleep(50);
}
lock = true;
FileTail * temp = new FileTail[1];
if(this->currentFilePos >= fileSize)
{
lock = false;
temp->isOk = true;
return temp;
}
if(this->FPerGetLen + this->currentFilePos > fileSize)
{
temp->pos = ConvertIntToStr(this->currentFilePos);
temp->len = ConvertIntToStr(this->fileSize - this->currentFilePos);
temp->isOk = false;
this->currentFilePos = fileSize;
lock = false;
return temp;
}
temp->pos = ConvertIntToStr(this->currentFilePos);
temp->len = ConvertIntToStr(this->FPerGetLen);
temp->isOk = false;
this->currentFilePos = this->currentFilePos + this->FPerGetLen;
lock = false;
return temp;
}
String __fastcall TMultiFtp::ConvertIntToStr(DWORD len)
{
String str;
str = IntToStr(len);
int strLen;
strLen = str.Length();
for(int i =0 ; i < 10 -strLen ; i++)
{
str = "0"+str;
}
return str;
}
bool __fastcall TMultiFtp::ChangeDirectory(SOCKET client ,String dirName)
{
String dir = "CWD "+dirName+" \r\n";
this->DoOnTextOut(dir);
char *buffer = new char[100];
int rec
评论0