13 #include "../layout/dlist.h"
16 OutBuffer *back, *next;
21 int obc_tryflush(OutBufferChain *ch) {
28 events->cancel_fd(events, ch->fd, OOP_WRITE);
31 if (ch->done_of_head == ob->l) {
32 LIST_UNLINK(ch->obs, ob);
38 r= write(ch->fd, ob->m + ch->done_of_head, ob->l - ch->done_of_head);
40 if (errno==EINTR) continue;
41 if (errno==EWOULDBLOCK) return errno;
42 ch->error(ch,"write",strerror(errno));
46 ch->done_of_head += r;
48 assert(ch->done_of_head <= ob->l);
52 static void *writeable(oop_source *evts, int fd,
53 oop_event evt, void *ch_v) {
54 OutBufferChain *ch= ch_v;
56 assert(evt == OOP_WRITE);
61 static void addlink(OutBufferChain *ch, OutBuffer *ob) {
63 events->on_fd(events, ch->fd, OOP_WRITE, writeable, ch);
64 LIST_LINK_TAIL(ch->obs, ob);
67 if (ch->total > ch->limit) {
69 snprintf(what,sizeof(what)-1,"`%.*s...'", ob->l,ob->m);
70 what[sizeof(what)-1]= 0;
71 ch->error(ch,"buffer limit exceeded",what);
75 void obc_init(OutBufferChain *ch) {
79 if (!ch->limit) ch->limit= 128*1024;
80 r= oop_fd_nonblock(ch->fd, 1);
81 if (r) diee("nonblock(OutBufferChain->fd,1)");
85 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al) {
88 ob= mmalloc(sizeof(*ob));
89 ob->l= vasprintf(&ob->m, fmt, al); if (ob->l <= 0) diem();
93 void oprintf(OutBufferChain *ch, const char *msg, ...) {
100 void voerror(OutBufferChain *ch, const char *fmt, va_list al) {
101 oprintf(ch,"error ");
106 void owrite(OutBufferChain *ch, const char *data, int l) {
108 ob= mmalloc(sizeof(*ob));
111 memcpy(ob->m, data, l);