clean up marshal/unmarshal Reference: /n/atom/patch/applied/aanmarshal2 Date: Sat Feb 22 16:32:04 CET 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/aan.c Sat Feb 22 16:31:24 2014 +++ /sys/src/cmd/aan.c Sat Feb 22 16:31:25 2014 @@ -37,11 +37,17 @@ u32int acked; // Number of messages acked } Hdr; +typedef struct { + uchar nb[4]; + uchar msg[4]; + uchar acked[4]; +} Mhdr; + enum{ Hdrsz = 12, }; -typedef struct t_Buf { +typedef struct { Hdr hdr; uchar buf[Bufsize]; } Buf; @@ -73,14 +79,14 @@ static void synchronize(void); static int sendcommand(ulong, ulong); static void showmsg(int, char *, Buf *); -static int writen(int, uchar *, int); +static int writen(int, void *, int); static int getport(char *); static void dmessage(int, char *, ...); static void timerproc(void *); static Endpoints *getendpoints(char *); static void freeendpoints(Endpoints *); -static void marshal(Hdr*); -static void unmarshal(Hdr*); +static void marshal(Hdr*, Mhdr*); +static void unmarshal(Hdr*, Mhdr*); static void usage(void) @@ -106,6 +112,7 @@ int i, failed; Buf *b; Channel *timer; + Mhdr mhdr; vlong synctime; progname = argv[0]; @@ -202,8 +209,8 @@ hdr.acked = inmsg; hdr.msg = -1; - marshal(&hdr); - if (writen(netfd, (uchar *)&hdr, Hdrsz) < 0) { + marshal(&hdr, &mhdr); + if (writen(netfd, &mhdr, Hdrsz) < 0) { dmessage(2, "main; writen failed; %r\n"); failed = 1; continue; @@ -221,8 +228,8 @@ b->hdr.acked = inmsg; - marshal(&b->hdr); - if (writen(netfd, (uchar *)&b->hdr, Hdrsz) < 0) { + marshal(&b->hdr, &mhdr); + if (writen(netfd, &mhdr, Hdrsz) < 0) { dmessage(2, "main; writen failed; %r\n"); failed = 1; } @@ -272,6 +279,7 @@ fromnet(void*) { static int lastacked; + Mhdr mhdr; Buf *b; b = (Buf *)malloc(sizeof(Buf)); @@ -287,7 +295,7 @@ } // Read the header. - if ((len = readn(netfd, &b->hdr, Hdrsz)) <= 0) { + if ((len = readn(netfd, &mhdr, Hdrsz)) <= 0) { if (len < 0) dmessage(1, "fromnet; (hdr) network failure; %r\n"); else @@ -296,7 +304,7 @@ netfd = -1; continue; } - unmarshal(&b->hdr); + unmarshal(&b->hdr, &mhdr); dmessage(2, "fromnet: Got message, size %d, nb %d, msg %d\n", len, b->hdr.nb, b->hdr.msg); @@ -399,6 +407,7 @@ { Channel *tmp; Buf *b; + Mhdr mhdr; // Ignore network errors here. If we fail during // synchronization, the next alarm will pick up @@ -406,8 +415,8 @@ tmp = chancreate(sizeof(Buf *), Nbuf); while ((b = nbrecvp(unacked)) != nil) { - marshal(&b->hdr); - writen(netfd, (uchar *)&b->hdr, Hdrsz); + marshal(&b->hdr, &mhdr); + writen(netfd, &mhdr, Hdrsz); writen(netfd, b->buf, b->hdr.nb); sendp(tmp, b); } @@ -432,10 +441,12 @@ } static int -writen(int fd, uchar *buf, int nb) +writen(int fd, void *vbuf, int nb) { + uchar *buf; int len = nb; + buf = vbuf; while (nb > 0) { int n; @@ -455,29 +466,19 @@ } static void -marshal(Hdr *h) +marshal(Hdr *h, Mhdr *m) { - u32int x; - - x = h->nb; - putle((uchar*)&h->nb, x, 4); - x = h->msg; - putle((uchar*)&h->msg, x, 4); - x = h->acked; - putle((uchar*)&h->acked, x, 4); + putle(m->nb, h->nb, 4); + putle(m->msg, h->msg, 4); + putle(m->acked, h->acked, 4); } static void -unmarshal(Hdr *h) +unmarshal(Hdr *h, Mhdr *m) { - u32int x; - - x = getle((uchar*)&h->nb, 4); - h->nb = x; - x = getle((uchar*)&h->msg, 4); - h->msg = x; - x = getle((uchar*)&h->msg, 4); - h->msg = x; + h->nb = getle(m->nb, 4); + h->msg = getle(m->msg, 4); + h->acked = getle(m->acked, 4); } static void