From 2277c5d7bbe1f9595fad512d8f790708473a9bf1 Mon Sep 17 00:00:00 2001 From: rsc Date: Sun, 21 Mar 2004 04:33:13 +0000 Subject: Small tweaks Lots of new code imported. --- src/libplumb/mesg.c | 62 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'src/libplumb/mesg.c') diff --git a/src/libplumb/mesg.c b/src/libplumb/mesg.c index fd95810b..c4094fa4 100755 --- a/src/libplumb/mesg.c +++ b/src/libplumb/mesg.c @@ -13,8 +13,6 @@ static Fid *pfid; int plumbopen(char *name, int omode) { - int fd; - if(fsplumb == nil) fsplumb = nsmount("plumb", ""); if(fsplumb == nil) @@ -47,28 +45,44 @@ plumbopen(char *name, int omode) return pfd; } - fd = fsopenfd(fsplumb, name, omode); - return fd; + return fsopenfd(fsplumb, name, omode); +} + +Fid* +plumbopenfid(char *name, int mode) +{ + if(fsplumb == nil) + fsplumb = nsmount("plumb", ""); + if(fsplumb == nil) + return nil; + return fsopen(fsplumb, name, mode); } int -plumbsend(int fd, Plumbmsg *m) +plumbsendtofid(Fid *fid, Plumbmsg *m) { char *buf; int n; - if(fd != pfd){ - werrstr("fd is not the plumber"); - return -1; - } buf = plumbpack(m, &n); if(buf == nil) return -1; - n = fswrite(pfid, buf, n); + n = fswrite(fid, buf, n); +fprint(2, "fswrite %d\n", n); free(buf); return n; } +int +plumbsend(int fd, Plumbmsg *m) +{ + if(fd != pfd){ + werrstr("fd is not the plumber"); + return -1; + } + return plumbsendtofid(pfid, m); +} + static int Strlen(char *s) { @@ -427,3 +441,31 @@ plumbrecv(int fd) free(buf); return m; } + +Plumbmsg* +plumbrecvfid(Fid *fid) +{ + char *buf; + Plumbmsg *m; + int n, more; + + buf = malloc(8192); + if(buf == nil) + return nil; + n = fsread(fid, buf, 8192); + m = nil; + if(n > 0){ + m = plumbunpackpartial(buf, n, &more); + if(m==nil && more>0){ + /* we now know how many more bytes to read for complete message */ + buf = realloc(buf, n+more); + if(buf == nil) + return nil; + if(fsreadn(fid, buf+n, more) == more) + m = plumbunpackpartial(buf, n+more, nil); + } + } + free(buf); + return m; +} + -- cgit v1.2.3