-->
Page 1 of 2

Use of buffers in the stack with espconn_sent

PostPosted: Mon Jul 20, 2015 11:40 am
by Hari K
The SDK documentation clearly states that espconn_sent() cannot be called again until the espconn_sent_cb() callback is received.

I'm guessing that this also means that the data buffer passed to espconn_sent() cannot also be used until after the callback is received. This makes sense from the SDK implementor's point of view, since they wouldn't have to make another copy of the data.

However, I notice that esphttpd is placing buffers on the stack and then passing those to espconn_sent() in at least two places. If my supposition is true (and once again this is only a guess), this might be an issue.

Has this possibility been considered before? Has anyone from EspressIf explicitly confirmed that the data pointer can be reused on return?

Re: Use of buffers in the stack with espconn_sent

PostPosted: Mon Jul 20, 2015 11:58 pm
by tve
Mhh, interesting point. I perused some older Esp SDK sources and it indeed looks to me like the data should not be changed until the callback occurs. Specifically, what called is https://github.com/nodemcu/nodemcu-firmware/blob/0ad574705d1a22b9ebc62bc198e6db9e792244e0/app/lwip/app/espconn_tcp.c#L152-L199 and it saves away the pointer to any data not yet sent. Now what happens is that with the std MSS there probably is enough buffer space for the amounts that esphttpd tries to send, so probably nothing bad happens. I do know that trying to use the liblwip version with an MSS of 536 bytes fails miserably. This may be a reason for that...

Re: Use of buffers in the stack with espconn_sent

PostPosted: Wed Jul 22, 2015 4:00 am
by Sprite_tm
Hmm, good point... that means that we can send at most 1.5K right now; for more data, we should allocate a static buffer somewhere. Maybe worth looking into.

Re: Use of buffers in the stack with espconn_sent

PostPosted: Wed Jul 22, 2015 8:30 am
by kolban
Could we not malloc() a buffer and pass that buffer in ... and then when we receive a notification that the send has completed, free the storage?

There is also a mysterious property in the espconn_set_opt() function called ESPCONN_COPY that talks about copying the transmitted data ... it talks about a buffer of 2920 bytes ... but doesn't say if this is one buffer or one per send().