[Libwebsockets] user parameter in protocol callback

Joel Winarske joel.winarske at gmail.com
Thu Oct 1 17:53:29 CEST 2020


Okay cool. I settled on the below pattern which works well.  BTW I like the
features you've been building out.  Looks great!

Thanks Andy!

m_protocols[0] = {.name = "myprotocol",
                  .callback = mycallback,
                  .user = this};

int MyObject::mycallback(struct lws* wsi,
                            enum lws_callback_reasons reason,
                            void* user,
                            void* in,
                            size_t len) {
MyObject* self = nullptr;
auto protocol = lws_get_protocol(wsi);
if (protocol) {
  self = static_cast<MyObject*>(lws_get_protocol(wsi)->user);
}


On Thu, Oct 1, 2020 at 8:27 AM Andy Green <andy at warmcat.com> wrote:

>
>
> On 10/1/20 4:09 PM, Joel Winarske wrote:
> > Hello,
> >
> > How does one pass an opaque data pointer so it shows up *everytime* as
> > the "user" parameter in the protocol callback? I've tried a few
> > variations and am not seeing a consistent pattern.
> >
> > I do see a consistent pattern when setting the opaque_user_data in
> > lws_client_connect_info, and accessing it with lws_get_opaque_user_data.
>
> They are different things.
>
> For client connections, you decide you want to create one and can tag it
> with whatever opaque user data
>
>
> https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-client.h#n152
>
> and / or userdata you want at creation time
>
>
> https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-client.h#n99
>
> Typically, you want the userdata assigned automatically, because its
> size depends on the protocol and eg for ws, that is something negotiated
> at connection time, not creation time.
>
>
> For server connections, they are happening randomly and not under your
> control.  You probably want the automatic userdata allocated based again
> on negotiated protocol.
>
> There are various kinds of events that can call your callback, some of
> them like PROTOCOL_INIT are not really related to any wsi at all, a fake
> wsi is passed in so you can at least always get the context and vhost
> from it.  You shouldn't assume that only wsi you have tagged appear there.
>
> -Andy
>
> > Thanks,
> > Joel
> >
> > _______________________________________________
> > Libwebsockets mailing list
> > Libwebsockets at ml.libwebsockets.org
> > https://libwebsockets.org/mailman/listinfo/libwebsockets
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20201001/786e1184/attachment.htm>


More information about the Libwebsockets mailing list