[Libwebsockets] minimal-ws-client with foreign libevent loop

John Dunn John.Dunn at qsc.com
Tue Feb 9 22:22:50 CET 2021


On Tuesday, February 9, 2021 12:25 PM, Andy Green wrote:
> If you create the libevent loop first - which is not something you come back from - and move the lws
> context creation to happen at the libevent timer callback inside the libevent context, everything will start 
> working without any further changes.

Thank you for the suggestion. I removed any libwebsocket initialization code from main and changed my timer function to code below and still get the same output on the console and saw no changes in behavior - connect_callback is never called. The 1us was used in the original sample code to 'connect immediately' but I changed that to 1 second just to make sure that wasn't the issue.

I'm looking at the code in libevent.c ( part of libwebsocket ) and it's not entirely clear to me how it is supposed to work. I'm guessing that lws_sul_schedule is going be driven off a libevent timer but the only places I see evtimer_add being called is either within a timer callback or in lws_event_cb which I think is the event related to the connection socket. None of those code paths are being called and to be honest I don't see how they possibly could - I need a timer to schedule the connection but the timer isn't created before the connection had been created.

-John

Here's what I changed :

static void
timer_cb_event(int fd, short event, void *arg)
{
  printf("---> this is a timer %i!\n", count);
  if (count == 0)
  {
    lwsl_user("---> creating context!\n");
    memset(&info, 0, sizeof info);
    info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_LIBEVENT;
    info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
    info.protocols = protocols;
    void *foreign_loops[1];
    foreign_loops[0] = loop_event;
    info.foreign_loops = foreign_loops;
    info.fd_limit_per_thread = 1 + 1 + 1;

    context = lws_create_context(&info);
    if (!context) {
      lwsl_err("lws init failed\n");
      return;
    }
  }
  else if (count == 1)
  {
    lwsl_user("---> connecting to host!\n");
    lws_sul_schedule(context, 0, &mco.sul, connect_client, LWS_US_PER_SEC);
  }
  count++;
}




-----Original Message-----
From: Andy Green <andy at warmcat.com> 
Sent: Tuesday, February 9, 2021 12:25 PM
To: John Dunn <John.Dunn at qsc.com>; libwebsockets at ml.libwebsockets.org
Subject: Re: [Libwebsockets] minimal-ws-client with foreign libevent loop

-External-



On 2/9/21 6:35 PM, John Dunn wrote:
> I am trying to modify the minimal-ws-client example code to use an 
> externally supplied libevent event loop. I am basing my changes off of 
> the minimal-http-server-eventlib-foreign sample. This is on Windows 10 
> using VS 2015 if that matters. Here's what I've done

The problem is that your loop doesn't exist before the context is created... during the context creation it's making callbacks and starting to do things, but there is no loop to attach things to, and he's dead in the water.

Afterwards you create the context you create the libevent loop, but there is nothing that can have hooked into it, it did not exist until too late.

If you create the libevent loop first - which is not something you come back from - and move the lws context creation to happen at the libevent timer callback inside the libevent context, everything will start working without any further changes.

BTW the interval for lws_sul is in us, not seconds.  You can use LWS_US_PER_SEC to get one second.

-Andy



More information about the Libwebsockets mailing list