Skip to content

Commit 035fafa

Browse files
ESP32: extend sh2lib with distinct recv and send funcs
sh2lib_execute() processes send, then recv. This is fine for a general purpose server, however, it can lead to code like: loop: 1) sh2lib_execute() 2) based on handler called in (1), sh2lib_do_get/post() 3) do other system task ^^This means that any requests queued in (2) cannot be sent until the next sh2lib_execute(). Instead, this commit splits sh2lib_execute() into distinct _send() and _recv() phases, so client use this pattern, which reduces latency between request generation in (2) and request sending in(3): loop: 1) sh2lib_execute_recv() 2) based on handler called in (1), sh2lib_do_get/post() 3) sh2lib_execute_send() 4) do other system task
1 parent 2e74536 commit 035fafa

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

sh2lib/sh2lib.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,39 @@ void sh2lib_free(struct sh2lib_handle *hd)
328328
int sh2lib_execute(struct sh2lib_handle *hd)
329329
{
330330
int ret;
331-
ret = nghttp2_session_send(hd->http2_sess);
331+
ret = sh2lib_execute_send(hd);
332332
if (ret != 0) {
333-
ESP_LOGE(TAG, "[sh2-execute] HTTP2 session send failed %d", ret);
334333
return ret;
335334
}
336335

337-
ret = nghttp2_session_recv(hd->http2_sess);
336+
ret = sh2lib_execute_recv(hd);
338337
if (ret != 0) {
339-
ESP_LOGE(TAG, "[sh2-execute] HTTP2 session recv failed %d", ret);
340338
return ret;
341339
}
342340

343341
return 0;
344342
}
345343

344+
int sh2lib_execute_recv(struct sh2lib_handle *hd)
345+
{
346+
int ret = nghttp2_session_recv(hd->http2_sess);
347+
if (ret != 0) {
348+
ESP_LOGE(TAG, "[sh2-execute] HTTP2 session recv failed %d", ret);
349+
}
350+
351+
return ret;
352+
}
353+
354+
int sh2lib_execute_send(struct sh2lib_handle *hd)
355+
{
356+
int ret = nghttp2_session_send(hd->http2_sess);
357+
if (ret != 0) {
358+
ESP_LOGE(TAG, "[sh2-execute] HTTP2 session send failed %d", ret);
359+
}
360+
361+
return ret;
362+
}
363+
346364
int sh2lib_do_get_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_frame_data_recv_cb_t recv_cb)
347365
{
348366
int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, NULL, recv_cb);

sh2lib/sh2lib.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,38 @@ int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
201201
*/
202202
int sh2lib_execute(struct sh2lib_handle *hd);
203203

204+
/**
205+
* @brief Execute receive on an HTTP/2 connection
206+
*
207+
* While the API sh2lib_do_get(), sh2lib_do_post() setup the requests to be
208+
* initiated with the server, this API performs the actual data send/receive
209+
* operations on the HTTP/2 connection. The callback functions are accordingly
210+
* called during the processing of these requests.
211+
*
212+
* @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'
213+
*
214+
* @return
215+
* - 0 if it succeeds
216+
* - Negative error code from nghttp2 on failure
217+
*/
218+
int sh2lib_execute_recv(struct sh2lib_handle *hd);
219+
220+
/**
221+
* @brief Execute send on an HTTP/2 connection
222+
*
223+
* While the API sh2lib_do_get(), sh2lib_do_post() setup the requests to be
224+
* initiated with the server, this API performs the actual data send/receive
225+
* operations on the HTTP/2 connection. The callback functions are accordingly
226+
* called during the processing of these requests.
227+
*
228+
* @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'
229+
*
230+
* @return
231+
* - 0 if it succeeds
232+
* - Negative error code from nghttp2 on failure
233+
*/
234+
int sh2lib_execute_send(struct sh2lib_handle *hd);
235+
204236
#define SH2LIB_MAKE_NV(NAME, VALUE) \
205237
{ \
206238
(uint8_t *)NAME, (uint8_t *)VALUE, strlen(NAME), strlen(VALUE), \

0 commit comments

Comments
 (0)