add iob_write (to write an io batch through a callback)

master
leitner 20 years ago
parent ccba2abe9b
commit b48ce77792

@ -10,6 +10,7 @@
cleanups in stralloc and buffer: cleanups in stralloc and buffer:
int -> long for sizes int -> long for sizes
char -> unsigned char for strings char -> unsigned char for strings
buffer_getline is now a function, not a macro
0.20: 0.20:
add errmsg API add errmsg API

@ -0,0 +1,5 @@
#include <buffer.h>
int buffer_getline(buffer* b,unsigned char* x,unsigned long int len) {
return buffer_get_token(b,x,len,"\n",1);
}

@ -100,6 +100,11 @@ int io_passfd(int64 sock,int64 fd);
* process. Return sock if ok, -1 on error, setting errno. */ * process. Return sock if ok, -1 on error, setting errno. */
int64 io_receivefd(int64 sock); int64 io_receivefd(int64 sock);
typedef int64 (*io_write_callback)(int64 s,const void* buf,uint64 n);
/* used internally, but hey, who knows */
int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callback writecb);
#ifdef __MINGW32__ #ifdef __MINGW32__
#include_next <io.h> #include_next <io.h>
#endif #endif

@ -113,91 +113,14 @@ int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
#else #else
#include <unistd.h> #include <iob.h>
#include <sys/types.h>
#include <sys/mman.h>
#define BUFSIZE 16384 static int64 writecb(int64 s,const void* buf,uint64 n) {
return write(s,buf,n);
}
int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) { int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
char buf[BUFSIZE]; return io_mmapwritefile(out,in,off,bytes,writecb);
int n,m;
uint64 sent=0;
io_entry* e=array_get(&io_fds,sizeof(io_entry),out);
if (e) {
const char* c;
long left;
do {
if (e->mmapped) {
/* did we already map the right chunk? */
if (off>=e->mapofs && off<e->mapofs+e->maplen)
goto mapok; /* ok; mmapped the right chunk*/
munmap(e->mmapped,e->maplen);
}
e->mapofs=off&0xffffffffffff0000ull;
if (e->mapofs+0x10000>off+bytes)
e->maplen=off+bytes-e->mapofs;
else
e->maplen=0x10000;
if ((e->mmapped=mmap(0,e->maplen,PROT_READ,MAP_SHARED,in,e->mapofs))==MAP_FAILED) {
e->mmapped=0;
goto readwrite;
}
mapok:
c=(const char*)(e->mmapped)+(off&0xffff);
left=e->maplen-(off&0xffff);
if (left>bytes) left=bytes;
while (left>0) {
m=write(out,c,left);
if (m==-1) {
e->canwrite=0;
e->next_write=-1;
if (errno!=EAGAIN) {
munmap(e->mmapped,e->maplen);
e->mmapped=0;
return -3;
}
return sent?sent:-1;
}
if (m==0) return sent;
sent+=m;
left-=m;
bytes-=m;
off+=m;
c+=m;
}
} while (bytes);
if (e->mmapped) {
munmap(e->mmapped,e->maplen);
e->mmapped=0;
}
return sent;
}
readwrite:
if (lseek(in,off,SEEK_SET) != off)
return -1;
while (bytes>0) {
char* tmp=buf;
if ((n=read(in,tmp,(bytes<BUFSIZE)?bytes:BUFSIZE))<=0)
return (sent?sent:-1);
while (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;
}
sent+=m;
n-=m;
tmp+=m;
}
}
abort:
return sent;
} }
#endif #endif

@ -3,6 +3,7 @@
int iob_addfile(io_batch* b,int64 fd,uint64 off,uint64 n) { int iob_addfile(io_batch* b,int64 fd,uint64 off,uint64 n) {
iob_entry* e; iob_entry* e;
if (n==0) return 1; if (n==0) return 1;
io_fd(fd);
e=array_allocate(&b->b,sizeof(iob_entry), e=array_allocate(&b->b,sizeof(iob_entry),
array_length(&b->b,sizeof(iob_entry))); array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0; if (!e) return 0;

@ -3,6 +3,7 @@
int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) { int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n) {
iob_entry* e; iob_entry* e;
if (n==0) return 1; if (n==0) return 1;
io_fd(fd);
e=array_allocate(&b->b,sizeof(iob_entry), e=array_allocate(&b->b,sizeof(iob_entry),
array_length(&b->b,sizeof(iob_entry))); array_length(&b->b,sizeof(iob_entry)));
if (!e) return 0; if (!e) return 0;

@ -1,8 +1,4 @@
#include "havebsdsf.h" #include "havebsdsf.h"
#ifdef HAVE_BSDSENDFILE
/* for MacOS X. Yep, they blew it again. */
#define SENDFILE 1
#endif
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/uio.h> #include <sys/uio.h>

@ -29,6 +29,7 @@ int iob_adds_free(io_batch* b,const char* s);
int iob_addfile(io_batch* b,int64 fd,uint64 off,uint64 n); int iob_addfile(io_batch* b,int64 fd,uint64 off,uint64 n);
int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n); int iob_addfile_close(io_batch* b,int64 fd,uint64 off,uint64 n);
int64 iob_send(int64 s,io_batch* b); int64 iob_send(int64 s,io_batch* b);
int64 iob_write(int64 s,io_batch* b,io_write_callback cb);
void iob_reset(io_batch* b); void iob_reset(io_batch* b);
void iob_prefetch(io_batch* b,uint64 bytes); void iob_prefetch(io_batch* b,uint64 bytes);

@ -1,17 +1,24 @@
#include <assert.h> #include <assert.h>
#include "iob.h" #include "iob.h"
#include "buffer.h" #include "buffer.h"
#include <unistd.h>
static int64 write_cb(int64 fd,const void* buf,uint64 len) {
if (len>2) len=2;
return write(fd,buf,len);
}
int main() { int main() {
int64 fd; int64 fd;
io_batch* b; io_batch* b;
int64 r;
assert(io_readfile(&fd,"GNUmakefile")); assert(io_readfile(&fd,"GNUmakefile"));
assert(b=iob_new(10)); assert(b=iob_new(10));
assert(iob_addbuf(b,"Huhu",4)); assert(iob_addbuf(b,"Huhu",4));
assert(iob_addbuf(b," fnord\n",7)); assert(iob_addbuf(b," fnord\n",7));
assert(iob_addfile(b,fd,10,10)); assert(iob_addfile(b,fd,10,10));
buffer_puts(buffer_2,"iob_send returned "); do {
buffer_putlong(buffer_2,iob_send(1,b)); r=iob_write(1,b,write_cb);
buffer_putsflush(buffer_2,".\n"); } while (r>0);
return 0; return 0;
} }

Loading…
Cancel
Save