aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/survey/survey.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-10 18:14:27 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-10 22:47:43 -0700
commitdbbee1a3ea7fbd26c528eb96ebe0dbfd7075e292 (patch)
treec5b94f14fd95725077d2b4e9cd0d3d4d166aa7e2 /src/protocol/survey/survey.c
parent34ceda3c2dd4990d15e0341e86861dd291003f63 (diff)
downloadnng-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/survey/survey.c')
-rw-r--r--src/protocol/survey/survey.c35
1 files changed, 11 insertions, 24 deletions
diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c
index 91fe4ad3..45b06d67 100644
--- a/src/protocol/survey/survey.c
+++ b/src/protocol/survey/survey.c
@@ -33,8 +33,8 @@ struct nni_surv_sock {
nni_time expire;
int raw;
int closing;
- uint32_t nextid; // next id
- uint8_t survid[4]; // outstanding request ID (big endian)
+ uint32_t nextid; // next id
+ uint32_t survid; // outstanding request ID (big endian)
nni_list pipes;
nni_aio aio_getq;
nni_timer_node timer;
@@ -271,16 +271,13 @@ nni_surv_recv_cb(void *arg)
nni_msg_free(msg);
goto failed;
}
- if (nni_msg_append_header(msg, nni_msg_body(msg), 4) != 0) {
+ if (nni_msg_header_append(msg, nni_msg_body(msg), 4) != 0) {
// Should be NNG_ENOMEM
nni_msg_free(msg);
goto failed;
}
- if (nni_msg_trim(msg, 4) != 0) {
- // This should never happen - could be an assert.
- nni_msg_free(msg);
- goto failed;
- }
+ (void) nni_msg_trim(msg, 4);
+
ppipe->aio_putq.a_msg = msg;
nni_msgq_aio_put(ppipe->psock->urq, &ppipe->aio_putq);
return;
@@ -309,7 +306,7 @@ nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
} else {
nni_sock_recverr(psock->nsock, NNG_ESTATE);
}
- memset(psock->survid, 0, sizeof(psock->survid));
+ psock->survid = 0;
nni_timer_cancel(&psock->timer);
}
break;
@@ -381,7 +378,7 @@ nni_surv_timeout(void *arg)
nni_surv_sock *psock = arg;
nni_sock_lock(psock->nsock);
- memset(psock->survid, 0, sizeof(psock->survid));
+ psock->survid = 0;
nni_sock_recverr(psock->nsock, NNG_ESTATE);
nni_msgq_set_get_error(psock->urq, NNG_ETIMEDOUT);
nni_sock_unlock(psock->nsock);
@@ -391,7 +388,6 @@ static nni_msg *
nni_surv_sock_sfilter(void *arg, nni_msg *msg)
{
nni_surv_sock *psock = arg;
- uint32_t id;
if (psock->raw) {
// No automatic retry, and the request ID must
@@ -402,12 +398,9 @@ nni_surv_sock_sfilter(void *arg, nni_msg *msg)
// Generate a new request ID. We always set the high
// order bit so that the peer can locate the end of the
// backtrace. (Pipe IDs have the high order bit clear.)
- id = (psock->nextid++) | 0x80000000u;
-
- // Survey ID is in big endian format.
- NNI_PUT32(psock->survid, id);
+ psock->survid = (psock->nextid++) | 0x80000000u;
- if (nni_msg_append_header(msg, psock->survid, 4) != 0) {
+ if (nni_msg_header_append_u32(msg, psock->survid) != 0) {
// Should be ENOMEM.
nni_msg_free(msg);
return (NULL);
@@ -436,18 +429,12 @@ nni_surv_sock_rfilter(void *arg, nni_msg *msg)
return (msg);
}
- if (nni_msg_header_len(msg) < 4) {
- nni_msg_free(msg);
- return (NULL);
- }
-
- if (memcmp(nni_msg_header(msg), ssock->survid, 4) != 0) {
+ if ((nni_msg_header_len(msg) < sizeof(uint32_t)) ||
+ (nni_msg_header_trim_u32(msg) != ssock->survid)) {
// Wrong request id
nni_msg_free(msg);
return (NULL);
}
- // Prune the survey ID.
- nni_msg_trim_header(msg, 4);
return (msg);
}