gcoap: don't allocate memo for clients without response handlers

This commit is contained in:
Martine Lenders 2018-06-08 07:56:52 +02:00
parent fd1a987bf1
commit b03aa528e8

View File

@ -796,10 +796,16 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
gcoap_resp_handler_t resp_handler)
{
gcoap_request_memo_t *memo = NULL;
unsigned msg_type = (*buf & 0x30) >> 4;
uint32_t timeout = 0;
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);
/* Find empty slot in list of open requests. */
for (int i = 0; i < GCOAP_REQ_WAITING_MAX; i++) {
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
memo = &_coap_state.open_reqs[i];
@ -813,8 +819,6 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
return 0;
}
unsigned msg_type = (*buf & 0x30) >> 4;
uint32_t timeout = 0;
memo->resp_handler = resp_handler;
memcpy(&memo->remote_ep, remote, sizeof(sock_udp_ep_t));
@ -856,11 +860,13 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
if (memo->state == GCOAP_MEMO_UNUSED) {
return 0;
}
}
/* Memos complete; send msg and start timer */
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
* 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
@ -884,10 +890,12 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
}
}
if (res <= 0) {
if (memo != NULL) {
if (msg_type == COAP_TYPE_CON) {
*memo->msg.data.pdu_buf = 0; /* clear resend buffer */
}
memo->state = GCOAP_MEMO_UNUSED;
}
DEBUG("gcoap: sock send failed: %d\n", (int)res);
}
return (size_t)((res > 0) ? res : 0);