[Libwebsockets] client pipelining and wsi context mixups

Zevv libws at zevv.nl
Tue Jan 21 12:13:18 CET 2020


I'm trying to run a multi-clients setup with libws and make use of
pipelining/http keepalive where appropriate. I'm confused though be the way
libws reuses existing lwis, and what the right order is to do things.

This is what is happening:

- I create one lws_context

- I perform a HTTP client request using lws_client_connect_via_info with
  LCCSCF_PIPELINE set. This generates a new wsi, let's call that A

- The HTTP request is perfomed as expected. The events I handle:

  * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: handling the headers
  * LWS_CALLBACK_RECEIVE_CLIENT_HTTP and
  * LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ for reading the data
  * LWS_CALLBACK_COMPLETED_CLIENT_HTTP when the request is done

So far so good, now the interesting part:

- Before wsi A is cleaned up I start a second request with an url on the 
  same host. This gives me wsi B, which also holds the pointer to my
  private data

- lws figures out my intention and finds wsi A to send the request over
  to make use of the existing connection to the server. Great!

- Here things get confusing, now I get wsi callbacks for both wsi A and
  B, but I'm not sure how to handle things:

  * B: LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED

  * B: LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH. But: there are no
       headers available in this wsi! I can 'fix' this with patch [1]
       below, that makes sure I do receive headers, albeit in wsi A,
       not in wsi B, which has my correct user data.

  * A: LWS_CALLBACK_RECEIVE_CLIENT_HTTP. No problem, I don't need to
       access my own data for this so I can just call
       lws_http_client_read

  * B: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ. Here I read my data
       and handle it. 

So, can anyone clarify the semantics behind this? I am problably misssing
something obvious when setting up my requests, but I was not able to deduce
from the docs or code what the right way is to handle this.

Thank you,

Zevv


[1] Changing the wsi context for LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH

--- /lib/roles/http/client/client-http.c
+++ /lib/roles/http/client/client-http.c
@@ -1013,9 +1015,9 @@ lws_client_interpret_server_handshake(struct lws *wsi)
                 * we seem to be good to go, give client last chance to check
                 * headers and OK it
                 */
-               if (w->protocol->callback(w,
+               if (w->protocol->callback(wsi,
                                LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
-                                           w->user_space, NULL, 0)) {
+                                           wsi->user_space, NULL, 0)) {
 




More information about the Libwebsockets mailing list