diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-08-10 18:14:27 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-08-10 22:47:43 -0700 |
| commit | dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292 (patch) | |
| tree | c5b94f14fd95725077d2b4e9cd0d3d4d166aa7e2 /src/protocol/reqrep | |
| parent | 34ceda3c2dd4990d15e0341e86861dd291003f63 (diff) | |
| download | nng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.tar.gz nng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.tar.bz2 nng-dbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292.zip | |
Unify the msg API.
This makes the operations that work on headers start with
nni_msg_header or nng_msg_header. It also renames _trunc to
_chop (same strlen as _trim), and renames prepend to insert.
We add a shorthand for clearing message content, and make
better use of the endian safe 32-bit accessors too.
This also fixes a bug in inserting large headers into messages.
A test suite for message handling is included.
Diffstat (limited to 'src/protocol/reqrep')
| -rw-r--r-- | src/protocol/reqrep/rep.c | 18 | ||||
| -rw-r--r-- | src/protocol/reqrep/req.c | 9 |
2 files changed, 9 insertions, 18 deletions
diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 1adfd0f8..49bcdce2 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -207,7 +207,6 @@ nni_rep_sock_getq_cb(void *arg) nni_rep_sock *rep = arg; nni_msgq * uwq = rep->uwq; nni_msg * msg; - uint8_t * header; uint32_t id; nni_rep_pipe *rp; int rv; @@ -234,9 +233,7 @@ nni_rep_sock_getq_cb(void *arg) return; } - header = nni_msg_header(msg); - NNI_GET32(header, id); - nni_msg_trim_header(msg, 4); + id = nni_msg_header_trim_u32(msg); // Look for the pipe, and attempt to put the message there // (nonblocking) if we can. If we can't for any reason, then we @@ -291,7 +288,6 @@ nni_rep_pipe_recv_cb(void *arg) nni_rep_sock *rep = rp->rep; nni_msg * msg; int rv; - uint8_t idbuf[4]; uint8_t * body; int hops; @@ -300,13 +296,11 @@ nni_rep_pipe_recv_cb(void *arg) return; } - NNI_PUT32(idbuf, nni_pipe_id(rp->pipe)); - msg = rp->aio_recv.a_msg; rp->aio_recv.a_msg = NULL; // Store the pipe id in the header, first thing. - rv = nni_msg_append_header(msg, idbuf, 4); + rv = nni_msg_header_append_u32(msg, nni_pipe_id(rp->pipe)); if (rv != 0) { goto malformed; } @@ -323,7 +317,7 @@ nni_rep_pipe_recv_cb(void *arg) } body = nni_msg_body(msg); end = (body[0] & 0x80) ? 1 : 0; - rv = nni_msg_append_header(msg, body, 4); + rv = nni_msg_header_append(msg, body, 4); if (rv != 0) { // Presumably this is due to out of memory. // We could just discard and try again, but we @@ -422,9 +416,9 @@ nni_rep_sock_sfilter(void *arg, nni_msg *msg) } // drop anything else in the header... - nni_msg_trunc_header(msg, nni_msg_header_len(msg)); + nni_msg_header_clear(msg); - if (nni_msg_append_header(msg, rep->btrace, rep->btrace_len) != 0) { + if (nni_msg_header_append(msg, rep->btrace, rep->btrace_len) != 0) { nni_free(rep->btrace, rep->btrace_len); rep->btrace = NULL; rep->btrace_len = 0; @@ -463,7 +457,7 @@ nni_rep_sock_rfilter(void *arg, nni_msg *msg) } rep->btrace_len = len; memcpy(rep->btrace, header, len); - nni_msg_trunc_header(msg, len); + nni_msg_header_clear(msg); return (msg); } diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index d0dd3887..20fb07f8 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -437,17 +437,14 @@ nni_req_recv_cb(void *arg) // Malformed message. goto malformed; } - if (nni_msg_append_header(msg, nni_msg_body(msg), 4) != 0) { + if (nni_msg_header_append(msg, nni_msg_body(msg), 4) != 0) { // Arguably we could just discard and carry on. But // dropping the connection is probably more helpful since // it lets the other side see that a problem occurred. // Plus it gives us a chance to reclaim some memory. goto malformed; } - if (nni_msg_trim(msg, 4) != 0) { - // This should never happen - could be an assert. - nni_panic("Failed to trim REQ header from body"); - } + (void) nni_msg_trim(msg, 4); // Cannot fail rp->aio_putq.a_msg = msg; nni_msgq_aio_put(rp->req->urq, &rp->aio_putq); @@ -548,7 +545,7 @@ nni_req_sock_sfilter(void *arg, nni_msg *msg) // Request ID is in big endian format. NNI_PUT32(req->reqid, id); - if (nni_msg_append_header(msg, req->reqid, 4) != 0) { + if (nni_msg_header_append(msg, req->reqid, 4) != 0) { // Should be ENOMEM. nni_msg_free(msg); return (NULL); |
