📄 genfunc.c
字号:
#include "Dload.h"
#ifndef GENFUNC_H
#include"GenFunc.h"
#define GENFUNC_H
#endif
/*this file contains url refer nthread*/
int SaveThreadStat(DLoadFileInfo *pFileInfo, char *zxwfile)
{
int statfd,i;
//char filestat[256];
//sprintf(filestat,"%s.ly", pDloadTask);//heihei
if(!zxwfile)
return -1;
statfd=open(zxwfile, O_RDWR|O_CREAT);
strncpy(pFileInfo->magiccode, HTTPFTP_MAGICCODE, sizeof(pFileInfo->magiccode));
write(statfd,pFileInfo->magiccode,sizeof(pFileInfo->magiccode));
write(statfd,pFileInfo->host,sizeof(pFileInfo->host));
write(statfd,pFileInfo->user,sizeof(pFileInfo->user));
write(statfd,pFileInfo->pass,sizeof(pFileInfo->pass));
write(statfd,&pFileInfo->hosttype,sizeof(int));
if(pFileInfo->hosttype==DLOAD_PROTOCOL_HTTP)
write(statfd,&(pFileInfo->httpPort),sizeof(int));
else if(pFileInfo->hosttype==DLOAD_PROTOCOL_FTP)
write(statfd,&(pFileInfo->ftpPort),sizeof(int));
write(statfd,pFileInfo->filename,sizeof(pFileInfo->filename));
write(statfd,pFileInfo->path,sizeof(pFileInfo->path));
write(statfd,&(pFileInfo->filelength),sizeof(int));
write(statfd,&(pFileInfo->sizeget),sizeof(int));
write(statfd,&(pFileInfo->nthread),sizeof(int));
write(statfd, pFileInfo->localpath, sizeof(pFileInfo->localpath));
for(i=0;i<pFileInfo->nthread;i++){
write(statfd,&pFileInfo->astPthread[i],sizeof(struct arg));
}
close(statfd);
return 0;
}
// likun
void printprocess(int sizeget, int filelength)
{
int i=0,j=0;
static a=0;
a++;
if(a==3)
a=0;
j=100*((float)sizeget/(float)filelength);
if(j>=100)
j=100;
//for(i=0;i<=200;i++)
//{
// printf("\b");
//}
fprintf(stderr,"\xd");
if(a==0)
fprintf(stderr,"-");
else if(a==1)
fprintf(stderr,"\\");
else if(a==2)
fprintf(stderr,"/");
fprintf(stderr,"|:");
for(i=0;i<=50;i++)
{
if(i*2<=j)
fprintf(stderr,">");
else
fprintf(stderr,"=");
}
fprintf(stderr,"|");
fprintf(stderr,"%3.1f%% ",100*((float)sizeget/(float)filelength)>100?100:(100*((float)sizeget/(float)filelength)));
fprintf(stderr,"sizeget: %d, filelength: %d", sizeget,filelength);
}
int GetThreadStat(DLoadFileInfo *pFileInfo, char *zxwfile)
{
int zxwfilefd,i;
if((zxwfilefd=open(zxwfile,O_RDONLY))==-1){
fprintf(stdout,"%s","ERROR:ly file not exists.\n");
return -1;
}
read(zxwfilefd,pFileInfo->magiccode,sizeof(pFileInfo->magiccode));
if(strcmp(pFileInfo->magiccode,HTTPFTP_MAGICCODE)){
printf("ERROR:wrong file");
return -1;
}
read(zxwfilefd,pFileInfo->host,sizeof(pFileInfo->host));
read(zxwfilefd,pFileInfo->user,sizeof(pFileInfo->user));
read(zxwfilefd,pFileInfo->pass,sizeof(pFileInfo->pass));
read(zxwfilefd,&(pFileInfo->hosttype),sizeof(int));
if(pFileInfo->hosttype==DLOAD_PROTOCOL_HTTP)
read(zxwfilefd,&(pFileInfo->httpPort),sizeof(int));
else if(pFileInfo->hosttype==DLOAD_PROTOCOL_FTP)
read(zxwfilefd,&(pFileInfo->ftpPort),sizeof(int));
read(zxwfilefd,pFileInfo->filename,sizeof(pFileInfo->filename));
read(zxwfilefd,pFileInfo->path,sizeof(pFileInfo->path));
read(zxwfilefd,&(pFileInfo->filelength),sizeof(int));
read(zxwfilefd,&(pFileInfo->sizeget),sizeof(int));
read(zxwfilefd,&(pFileInfo->nthread),sizeof(int));
read(zxwfilefd, pFileInfo->localpath, sizeof(pFileInfo->localpath));
printf("\r\nhosttype %d port %d filename %s path %s filelength %d sizeget %d nthread %d localPath: %s",
pFileInfo->hosttype,pFileInfo->ftpPort,
pFileInfo->filename,pFileInfo->path,
pFileInfo->filelength,pFileInfo->sizeget,
pFileInfo->nthread, pFileInfo->localpath);
for(i=0;i<pFileInfo->nthread;i++){
read(zxwfilefd,&pFileInfo->astPthread[i],sizeof(struct arg));
printf("thread number %d a1 %d a2 %d booldown %d allowothre%d bytesleft %d boolend %d\n",
pFileInfo->astPthread[i].threadnumber,pFileInfo->astPthread[i].a1,
pFileInfo->astPthread[i].a2,pFileInfo->astPthread[i].booldown,
pFileInfo->astPthread[i].allowotherdown,pFileInfo->astPthread[i].bytesleft,
pFileInfo->astPthread[i].boolend);
}
close(zxwfilefd);
return 0;
}
//select, n timeout
int ReadEn(int sock,int n)
{
fd_set rfdset;
struct timeval tv;
tv.tv_sec=n;
tv.tv_usec=0;
int readen=0;
FD_ZERO(&rfdset);
FD_SET(sock,&rfdset);
readen = select(sock+1,&rfdset,NULL,NULL,&tv);
return readen;
}
/*likun ,重命名这个函数*/
int GetConnectSocket(char* host,int iport)
{
int sd;
struct sockaddr_in pin;
struct hostent *nlp_host;
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_port = htons(iport);
if(host)
{
pin.sin_addr.s_addr = inet_addr(host);
if(INADDR_NONE == pin.sin_addr.s_addr)
{
nlp_host = gethostbyname(host);
if(!nlp_host || (nlp_host->h_addrtype != AF_INET))
fprintf(stderr,"Error resolving local host\n",0);
memcpy(&(pin.sin_addr),nlp_host->h_addr_list[0], sizeof(struct in_addr));
}
}
if((sd = socket(PF_INET,SOCK_STREAM,0))==-1)
sdd_ErrReturn("Error opening socket\n",0);
if(connect(sd,(struct sockaddr*)&pin,sizeof(pin))==-1)
sdd_ErrReturn("Error connecting to socket\n",0);
return sd;
}
//obsolete name
//int GetThreadSize(void)
/*get every thread download data size*/
int GetThreadSize(DLoadFileInfo *pFileInfo)
{
int threadsize;
int ntemp;
int i;
if((threadsize=(pFileInfo->filelength/pFileInfo->nthread))>=2048){
ntemp=threadsize/2048;
threadsize=ntemp*2048;
}
else{
threadsize=2048;//the least size of each thread
for(i=1;i<=pFileInfo->nthread;i++){
/*如果平均每个线程下载数据 < 2048,调整每个线程下载数据大小为
2048,并调整总的线程数目*/
if(i*threadsize>=pFileInfo->filelength){
pFileInfo->nthread=i;
break;
}
}
}
return threadsize;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -