harmonize semantics of io_sendfile and iob_send

master
leitner 22 years ago
parent 85bfbeb420
commit e86a457f5c

@ -2,6 +2,9 @@
add Linux SIGIO support to IO add Linux SIGIO support to IO
expand IO api to be able to cope with edge triggered event expand IO api to be able to cope with edge triggered event
notification: introduce io_eagain notification: introduce io_eagain
the integer scan routines should only write *dest if they actually
scanned something
io_sendfile and iob_send should return -1 to -3 just like io_trywrite
0.16: 0.16:
add buffer_fromsa (make buffer from stralloc) add buffer_fromsa (make buffer from stralloc)

@ -11,7 +11,7 @@ int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
int r=sendfile(fd,s,off,n,0,&sbytes,0); int r=sendfile(fd,s,off,n,0,&sbytes,0);
if (r==0) return n; if (r==0) return n;
if (r==-1) if (r==-1)
return (errno==EAGAIN?sbytes:-1); return (errno==EAGAIN?(sbytes?sbytes:-1):-3);
} }
#elif defined(__linux__) #elif defined(__linux__)
@ -26,7 +26,16 @@ _syscall4(int,sendfile,int,out,int,in,long *,offset,unsigned long,count)
int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) { int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
off_t o=off; off_t o=off;
return sendfile(s,fd,&o,n); io_entry* e=array_get(&io_fds,sizeof(io_entry),s);
off_t i=sendfile(s,fd,&o,n);
if (i==-1) {
if (e) {
e->canwrite=0;
e->next_write=-1;
}
if (errno!=EAGAIN) i=-3;
}
return i;
} }
#else #else
@ -37,6 +46,7 @@ int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
char buf[BUFSIZE]; char buf[BUFSIZE];
int n,m; int n,m;
uint64 sent=0; uint64 sent=0;
io_entry* e=array_get(&io_fds,sizeof(io_entry),out);
if (lseek(in,off,SEEK_SET) != off) if (lseek(in,off,SEEK_SET) != off)
return -1; return -1;
while (bytes>0) { while (bytes>0) {
@ -44,8 +54,16 @@ int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
if ((n=read(in,tmp,(bytes<BUFSIZE)?bytes:BUFSIZE))<=0) if ((n=read(in,tmp,(bytes<BUFSIZE)?bytes:BUFSIZE))<=0)
return (sent?sent:-1); return (sent?sent:-1);
while (n>0) { while (n>0) {
if ((m=write(out,tmp,n))<0) if ((m=write(out,tmp,n))<0) {
if (m==-1) {
if (e) {
e->canwrite=0;
e->next_write=-1;
}
return errno==EAGAIN?(sent?sent:-1):-3;
}
goto abort; goto abort;
}
sent+=m; sent+=m;
n-=m; n-=m;
tmp+=m; tmp+=m;

@ -59,11 +59,13 @@ int64 iob_send(int64 s,io_batch* b) {
if (r==0) if (r==0)
sent=b->bytesleft; sent=b->bytesleft;
else if (r==-1 && errno==EAGAIN) else if (r==-1 && errno==EAGAIN)
sent=sbytes; if ((sent=sbytes)) sent=-1;
else else
sent=-1; sent=-3;
} else } else {
sent=writev(s,v,headers); sent=writev(s,v,headers);
if (sent==-1 && errno!=EAGAIN) sent=-3;
}
#else #else
#ifdef TCP_CORK #ifdef TCP_CORK
if (b->bufs && b->files && !b->next) { if (b->bufs && b->files && !b->next) {
@ -71,18 +73,16 @@ int64 iob_send(int64 s,io_batch* b) {
setsockopt(s,IPPROTO_TCP,TCP_CORK,&one,sizeof(one)); setsockopt(s,IPPROTO_TCP,TCP_CORK,&one,sizeof(one));
} }
#endif #endif
if (headers) if (headers) {
sent=writev(s,v,headers); sent=writev(s,v,headers);
else if (sent==-1 && errno==EAGAIN) sent=-3;
} else
sent=io_sendfile(s,e->fd,e->offset,e->n); sent=io_sendfile(s,e->fd,e->offset,e->n);
#endif #endif
if (sent>0) if (sent>0)
total+=sent; total+=sent;
else else
if (!total) { return total?total:sent;
if (errno!=EAGAIN) return -3;
return -1;
}
if (sent==b->bytesleft) { if (sent==b->bytesleft) {
b->bytesleft=0; b->bytesleft=0;
#ifdef TCP_CORK #ifdef TCP_CORK

@ -275,7 +275,7 @@ emerge:
struct http_data* h=io_getcookie(i); struct http_data* h=io_getcookie(i);
int64 r=iob_send(i,&h->iob); int64 r=iob_send(i,&h->iob);
/* printf("iob_send returned %lld\n",r); */ /* printf("iob_send returned %lld\n",r); */
if (r==-1) io_eagain(i); if (r==-1) io_eagain(i); else
if (r<=0) { if (r<=0) {
array_trunc(&h->r); array_trunc(&h->r);
iob_reset(&h->iob); iob_reset(&h->iob);

Loading…
Cancel
Save