Merge pull request #9310 from miri64/gcoap/enh/clients-without-response-handlers

gcoap: don't allocate memo for clients without response handlers
This commit is contained in:
Ken Bannister 2018-06-15 10:28:30 -04:00 committed by GitHub
commit 4f8c3b7d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -811,10 +811,16 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
gcoap_resp_handler_t resp_handler) gcoap_resp_handler_t resp_handler)
{ {
gcoap_request_memo_t *memo = NULL; gcoap_request_memo_t *memo = NULL;
unsigned msg_type = (*buf & 0x30) >> 4;
uint32_t timeout = 0;
assert(remote != NULL); assert(remote != NULL);
/* Find empty slot in list of open requests. */ /* Only allocate memory if necessary (i.e. if user is interested in the
* response or request is confirmable) */
if ((resp_handler != NULL) || (msg_type == COAP_TYPE_CON)) {
mutex_lock(&_coap_state.lock); mutex_lock(&_coap_state.lock);
/* Find empty slot in list of open requests. */
for (int i = 0; i < GCOAP_REQ_WAITING_MAX; i++) { for (int i = 0; i < GCOAP_REQ_WAITING_MAX; i++) {
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) { if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
memo = &_coap_state.open_reqs[i]; memo = &_coap_state.open_reqs[i];
@ -828,8 +834,6 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
return 0; return 0;
} }
unsigned msg_type = (*buf & 0x30) >> 4;
uint32_t timeout = 0;
memo->resp_handler = resp_handler; memo->resp_handler = resp_handler;
memcpy(&memo->remote_ep, remote, sizeof(sock_udp_ep_t)); memcpy(&memo->remote_ep, remote, sizeof(sock_udp_ep_t));
@ -871,11 +875,13 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
if (memo->state == GCOAP_MEMO_UNUSED) { if (memo->state == GCOAP_MEMO_UNUSED) {
return 0; return 0;
} }
}
/* Memos complete; send msg and start timer */ /* Memos complete; send msg and start timer */
ssize_t res = sock_udp_send(&_sock, buf, len, remote); ssize_t res = sock_udp_send(&_sock, buf, len, remote);
if ((res > 0) && (timeout > 0)) { /* timeout may be zero for non-confirmable */ /* timeout may be zero for non-confirmable */
if ((memo != NULL) && (res > 0) && (timeout > 0)) {
/* We assume gcoap_req_send2() is called on some thread other than /* We assume gcoap_req_send2() is called on some thread other than
* gcoap's. First, put a message in the mbox for the sock udp object, * gcoap's. First, put a message in the mbox for the sock udp object,
* which will interrupt listening on the gcoap thread. (When there are * which will interrupt listening on the gcoap thread. (When there are
@ -899,10 +905,12 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
} }
} }
if (res <= 0) { if (res <= 0) {
if (memo != NULL) {
if (msg_type == COAP_TYPE_CON) { if (msg_type == COAP_TYPE_CON) {
*memo->msg.data.pdu_buf = 0; /* clear resend buffer */ *memo->msg.data.pdu_buf = 0; /* clear resend buffer */
} }
memo->state = GCOAP_MEMO_UNUSED; memo->state = GCOAP_MEMO_UNUSED;
}
DEBUG("gcoap: sock send failed: %d\n", (int)res); DEBUG("gcoap: sock send failed: %d\n", (int)res);
} }
return (size_t)((res > 0) ? res : 0); return (size_t)((res > 0) ? res : 0);