Commit 99e1602e authored by deuce's avatar deuce
Browse files

Do *NOT* copy the whole file into malloc()ed memory! Use a 16k buffer

instead.

This should fix the unable to send large files problem.
parent f915ecd6
......@@ -69,11 +69,12 @@ int sendfilesocket(int sock, int file, long *offset, long count)
return((int)count);
return(i);
#else
char* buf;
char buf[1024*16];
long len;
int rd;
int wr;
int total;
int i;
if(offset!=NULL)
if(lseek(file,*offset,SEEK_SET)<0)
......@@ -86,31 +87,29 @@ int sendfilesocket(int sock, int file, long *offset, long count)
count-=tell(file); /* don't try to read beyond EOF */
}
if((buf=(char*)malloc(count))==NULL) {
errno=ENOMEM;
return(-1);
}
rd=read(file,buf,count);
if(rd!=count) {
free(buf);
return(-1);
}
for(total=wr=0;total<count;total+=wr) {
wr=sendsocket(sock,buf+total,count-total);
if(wr>0)
continue;
if(wr==SOCKET_ERROR && ERROR_VALUE==EWOULDBLOCK) {
wr=0;
SLEEP(1);
continue;
total=0;
while(total!=count) {
rd=read(file,buf,sizeof(buf));
if(rd==-1)
return(-1);
if(rd==0)
break;
for(i=wr=0;i<rd;i+=wr) {
wr=sendsocket(sock,buf+i,rd-i);
if(wr>0)
continue;
if(wr==SOCKET_ERROR && ERROR_VALUE==EWOULDBLOCK) {
wr=0;
SLEEP(1);
continue;
}
return(wr);
}
break;
if(i!=rd)
return(-1);
total+=rd;
}
free(buf);
if(offset!=NULL)
(*offset)+=total;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment