warmcat / libwebsockets

canonical libwebsockets.org networking library
https://libwebsockets.org
Other
4.67k stars 1.46k forks source link

MQTT client externall poll problem. Internal loop works fine. #2938

Open marcindelta opened 12 months ago

marcindelta commented 12 months ago

Hi all, I am experiencing a problem with mqtt-client example from the libwebsockets repositories. After compilation like it is (original example), it works fine but any attempt of modification to the external poll it stops on the same point. My example:

/*
 * lws-minimal-mqtt-client
 *
 * Written in 2010-2020 by Andy Green <andy@warmcat.com>
 *                         Sakthi Kannan <saktr@amazon.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>
#include <string.h>
#include <signal.h>
#if defined(WIN32)
#define HAVE_STRUCT_TIMESPEC
#if defined(pid_t)
#undef pid_t
#endif
#endif
#include <pthread.h>
#include <assert.h>
#include <iostream>
#include <ctime>
const struct timespec timeout_ = {0, (long)2e8};
sigset_t origmask_;
volatile unsigned int nfds_ = 0;
pollfd* pollfds_ = new pollfd[10];

enum {
    STATE_SUBSCRIBE,    /* subscribe to the topic */
    STATE_PUBLISH_QOS0, /* Send the message in QoS0 */
    STATE_WAIT_ACK0,    /* Wait for the synthetic "ack" */
    STATE_PUBLISH_QOS1, /* Send the message in QoS1 */
    STATE_WAIT_ACK1,    /* Wait for the real ack (or timeout + retry) */

    STATE_TEST_FINISH
};

static int interrupted = 0, bad = 1, do_ssl;

static const lws_retry_bo_t retry = {
    .secs_since_valid_ping      = 20, /* if idle, PINGREQ after secs */
    .secs_since_valid_hangup    = 25, /* hangup if still idle secs */
};

static const lws_mqtt_client_connect_param_t client_connect_param = {
    .client_id          = "lwsMqttClient",
    .keep_alive         = 60,
    .clean_start            = 1,
    .client_id_nofree       = 1,
    .username_nofree        = 1,
    .password_nofree        = 1,
    .will_param = {
        .topic          = "good/bye",
        .message        = "sign-off",
        .qos            = (lws_mqtt_qos_levels_t)0,
        .retain         = 0,
    },
    .username           = "lwsUser",
    .password           = "mySecretPassword",
};

static lws_mqtt_publish_param_t pub_param;

static lws_mqtt_topic_elem_t topics[] = {
    [0] = { .name = "qwe", .qos = QOS0 },
    [1] = { .name = "test/topic1", .qos = QOS1 },
};

static lws_mqtt_subscribe_param_t sub_param = {
    .num_topics                     = LWS_ARRAY_SIZE(topics),
    .topic              = &topics[0],
};

static const char * const test_string =
    "No one would have believed in the last years of the nineteenth "
    "century that this world was being watched keenly and closely by "
    "intelligences greater than man's and yet as mortal as his own; that as "
    "men busied themselves about their various concerns they were "
    "scrutinised and studied, perhaps almost as narrowly as a man with a "
    "microscope might scrutinise the transient creatures that swarm and "
    "multiply in a drop of water.  With infinite complacency men went to "
    "and fro over this globe about their little affairs, serene in their "
    "assurance of their empire over matter. It is possible that the "
    "infusoria under the microscope do the same.  No one gave a thought to "
    "the older worlds of space as sources of human danger, or thought of "
    "them only to dismiss the idea of life upon them as impossible or "
    "improbable.  It is curious to recall some of the mental habits of "
    "those departed days.  At most terrestrial men fancied there might be "
    "other men upon Mars, perhaps inferior to themselves and ready to "
    "welcome a missionary enterprise. Yet across the gulf of space, minds "
    "that are to our minds as ours are to those of the beasts that perish, "
    "intellects vast and cool and unsympathetic, regarded this earth with "
    "envious eyes, and slowly and surely drew their plans against us.  And "
    "early in the twentieth century came the great disillusionment. ";

/* this reflects the length of the string above */
#define TEST_STRING_LEN 1337

struct pss {
    int     state;
    size_t      pos;
    int     retries;
};

static void
sigint_handler(int sig)
{
    interrupted = 1;
}

static int
connect_client(struct lws_context *context)
{
    struct lws_client_connect_info i;

    memset(&i, 0, sizeof i);

    i.mqtt_cp = &client_connect_param;
    i.address = "172.25.146.89"; //"localhost";
    i.host = "localhost";
    i.protocol = "mqtt";
    i.context = context;
    i.method = "MQTT";
    i.alpn = "mqtt";
    i.port = 1883;

    if (do_ssl) {
        i.ssl_connection = LCCSCF_USE_SSL;
        i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
        i.port = 8883;
    }

    if (!lws_client_connect_via_info(&i)) {
        lwsl_err("%s: Client Connect Failed\n", __func__);

        return 1;
    }

    return 0;
}

// static int
// system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
//       int current, int target)
// {
//  struct lws_context *context =(struct lws_context*)mgr->parent;
//
//  if (current != LWS_SYSTATE_OPERATIONAL ||
//      target != LWS_SYSTATE_OPERATIONAL)
//      return 0;
//
//  /*
//  * We delay trying to do the client connection until
//  * the protocols have been initialized for each
//  * vhost... this happens after we have network and
//  * time so we can judge tls cert validity.
//  */
//
//  if (connect_client(context))
//      interrupted = 0;
//
//  return 0;
//  }
struct lws_context *context = nullptr;
// static int call(pollfd* p) {
//  lws_service_fd(context, p);
// }

static int callback_mqtt(struct lws *wsi, enum lws_callback_reasons reason,
          void *user, void *in, size_t len)
{
    struct pss *pss = (struct pss *)user;
    lws_mqtt_publish_param_t *pub;
    size_t chunk;

    lwsl_user("%x %d", wsi, reason);
    switch (reason) {
    case LWS_CALLBACK_ADD_POLL_FD: {
        lws_pollargs * args = reinterpret_cast<lws_pollargs*> ( in );
        std::cerr<<"LWS_CALLBACK_ADD_POLL_FD: "<<args->fd<<std::endl;
        pollfds_[nfds_].fd = args->fd;
        pollfds_[nfds_].events = 0;
        nfds_++;
    }break;
    case LWS_CALLBACK_DEL_POLL_FD: {
        lws_pollargs * args = reinterpret_cast<lws_pollargs*> ( in );
        for(int i = 0; i < nfds_; i++) {
            if(pollfds_[i].fd == args->fd) {
                pollfds_[i].fd = 0;
                pollfds_[i].events = 0;
                nfds_--;
            }
        }
    }break;
    case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
        lws_pollargs * args = reinterpret_cast<lws_pollargs*> ( in );
        for(int i = 0; i < nfds_; i++) {
            if(pollfds_[i].fd == args->fd) {
                pollfds_[i].events = args->events;
            }
        }
    }break;
    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
        lwsl_err("%s: CLIENT_CONNECTION_ERROR: %s\n", __func__,
             in ? (char *)in : "(null)");
        interrupted = 0;
        break;

    case LWS_CALLBACK_MQTT_CLIENT_CLOSED:
        lwsl_user("%s: CLIENT_CLOSED\n", __func__);
        interrupted = 0;
        break;

    case LWS_CALLBACK_MQTT_CLIENT_ESTABLISHED:
        lwsl_user("%s: MQTT_CLIENT_ESTABLISHED\n", __func__);
        lws_callback_on_writable(wsi);

        return 0;

    case LWS_CALLBACK_MQTT_SUBSCRIBED:
        lwsl_user("%s: MQTT_SUBSCRIBED\n", __func__);
        lws_callback_on_writable(wsi);
        break;

    case LWS_CALLBACK_MQTT_CLIENT_WRITEABLE:
        /*
         * Extra WRITEABLE may appear here other than ones we asked
         * for, so we must consult our own state to decide if we want
         * to make use of the opportunity
         */

        switch (pss->state) {
        case STATE_SUBSCRIBE:
            lwsl_user("%s: WRITEABLE: Subscribing\n", __func__);

            if (lws_mqtt_client_send_subcribe(wsi, &sub_param)) {
                lwsl_notice("%s: subscribe failed\n", __func__);

                return -1;
            }
            pss->state++;
            break;

        case STATE_PUBLISH_QOS0:
        case STATE_PUBLISH_QOS1:

            lwsl_user("%s: WRITEABLE: Publish\n", __func__);

            pub_param.topic = "qwe";
            pub_param.topic_len = (uint16_t)strlen(pub_param.topic);
            pub_param.qos = pss->state == STATE_PUBLISH_QOS0 ? QOS0 : QOS1;
            pub_param.payload_len = TEST_STRING_LEN;

            /* We send the message out 300 bytes or less at at time */

            chunk = 300;

            if (chunk > TEST_STRING_LEN - pss->pos)
                chunk = TEST_STRING_LEN - pss->pos;

            if (lws_mqtt_client_send_publish(wsi, &pub_param,
                    test_string + pss->pos, (uint32_t)chunk,
                    (pss->pos + chunk == TEST_STRING_LEN)))
                return -1;

            pss->pos += chunk;

            if (pss->pos == TEST_STRING_LEN) {
                pss->pos = 0;
                pss->state++;
            }
            break;

        default:
            break;
        }

        return 0;

    case LWS_CALLBACK_MQTT_ACK:
        lwsl_user("%s: MQTT_ACK\n", __func__);
        /*
         * We can forget about the message we just sent, it's done.
         *
         * For our test, that's the indication we can close the wsi.
         */

        pss->state++;
        if (pss->state != STATE_TEST_FINISH) {
            lws_callback_on_writable(wsi);
            break;
        }

        /* Oh we are done then */

        bad = 0;
        interrupted = 0;
        lws_cancel_service(lws_get_context(wsi));
        break;

    case LWS_CALLBACK_MQTT_RESEND:
        lwsl_user("%s: MQTT_RESEND\n", __func__);
        /*
         * We must resend the packet ID mentioned in len
         */
        if (++pss->retries == 3) {
            interrupted = 0;
            break;
        }
        pss->state--;
        pss->pos = 0;
        break;

    case LWS_CALLBACK_MQTT_CLIENT_RX:
        lwsl_user("%s: MQTT_CLIENT_RX\n", __func__);

        pub = (lws_mqtt_publish_param_t *)in;
        assert(pub);

//      lwsl_hexdump_notice(pub->topic, pub->topic_len);
//      lwsl_hexdump_notice(pub->payload, pub->payload_len);

        return 0;

    default:
        break;
    }

    return 0;
}

static const struct lws_protocols protocols[] = {
    {
        .name           = "mqtt",
        .callback       = callback_mqtt,
        .per_session_data_size  = sizeof(struct pss)
    },
    LWS_PROTOCOL_LIST_TERM
};

int main(int argc, const char **argv)
{
     memset ( pollfds_, 0, sizeof ( pollfd ) *10 );
//  lws_state_notify_link_t notifier = { { NULL, NULL, NULL },
//                       system_notify_cb, "app" };
//  lws_state_notify_link_t *na[] = { &notifier, NULL };
    struct lws_context_creation_info info;
    int n = 0;

    signal(SIGINT, sigint_handler);
    memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
    lws_cmdline_option_handle_builtin(argc, argv, &info);

    do_ssl = !!lws_cmdline_option(argc, argv, "-s");
    if (do_ssl)
        info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

    lwsl_user("LWS minimal MQTT client %s [-d<verbosity>][-s]\n",
            do_ssl ? "tls enabled": "unencrypted");

    info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
    info.protocols = protocols;
//  info.register_notifier_list = na;
    info.fd_limit_per_thread = 1 + 1 + 1;
    info.retry_and_idle_policy = &retry;

#if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)
    /*
     * OpenSSL uses the system trust store.  mbedTLS has to be told which
     * CA to trust explicitly.
     */
    info.client_ssl_ca_filepath = "./mosq-ca.crt";
#endif

    lws_set_log_level (LLL_ERR|LLL_WARN|LLL_NOTICE|LLL_INFO|LLL_DEBUG|LLL_HEADER|LLL_EXT|LLL_PARSER|LLL_CLIENT|LLL_LATENCY|LLL_USER|LLL_THREAD|LLL_COUNT|LLLF_SECRECY_PII|LLLF_SECRECY_BEARER|LLLF_LOG_TIMESTAMP|LLLF_LOG_CONTEXT_AWARE, nullptr );
    context = lws_create_context(&info);
    if (!context) {
        lwsl_err("lws init failed\n");
        return 1;
    }

    if( connect_client(context) )
        return 1;

    while (!interrupted) {
        int res = ppoll(pollfds_, nfds_, &timeout_, &origmask_);
        for(int i = 0; i < nfds_; i++) {
            std::cerr<<"fd:="<<pollfds_[i].fd<<"|"<<pollfds_[i].events<<"/"<<pollfds_[i].revents<<" ";
        }
        std::cerr<<std::endl;
        for(int i = 0; i < nfds_; i++) {
            lws_service_fd(context, &pollfds_[i]);
            if( pollfds_[i].revents == 0 )
                continue;
        }
//      std::clock_t t = std::clock();
//      lws_service(context, -1);
//      std::cerr<<std::clock() - t<<std::endl;
    }

    /* Event loop */
//  while (n >= 0 && !interrupted)
//      n = lws_service(context, 0);

    lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
    lws_context_destroy(context);

    return bad;
}

and logs from the runtime session:

$ ./mqtt_client [2023/07/24 16:54:35:9282] U: LWS minimal MQTT client unencrypted [-d][-s] [2023/07/24 16:54:35:9284] I: lws_create_context: ev lib path /usr/local/lib, '(null)' [2023/07/24 16:54:35:9285] D: _realloc: size 8328: context [2023/07/24 16:54:35:9286] D: _realloc: size 128: lws_metric_create [2023/07/24 16:54:35:9286] D: _realloc: size 129: lws_metric_create [2023/07/24 16:54:35:9286] D: _realloc: size 129: lws_metric_create [2023/07/24 16:54:35:9286] D: _realloc: size 129: lws_metric_create [2023/07/24 16:54:35:9287] D: _realloc: size 131: lws_metric_create [2023/07/24 16:54:35:9287] D: _realloc: size 134: lws_metric_create [2023/07/24 16:54:35:9287] D: _realloc: size 130: lws_metric_create [2023/07/24 16:54:35:9287] D: _realloc: size 130: lws_metric_create [2023/07/24 16:54:35:9288] D: _realloc: size 126: lws_metric_create [2023/07/24 16:54:35:9288] N: lws_create_context: LWS: 4.3.99-v4.3.0-276-ge8eb7d6b, NET CLI SRV H1 H2 WS MQTT SS-JSON-POL ConMon ASYNC_DNS IPv6-absent [2023/07/24 16:54:35:9288] I: lws_create_context: Event loop: poll [2023/07/24 16:54:35:9289] D: _realloc: size 72: lws_smd_register [2023/07/24 16:54:35:9289] I: lws_smd_register: peer 0x5635bf1266e0 (count 1) registered [2023/07/24 16:54:35:9289] D: _realloc: size 48: fds table [2023/07/24 16:54:35:9289] I: lws_create_context: ctx: 7008B (2912 ctx + pt(1 thr x 4096)), pt-fds: 6 [2023/07/24 16:54:35:9290] I: lws_create_context: http: ah_data: 4096, ah: 992, max count 6 [2023/07/24 16:54:35:9290] D: _realloc: size 48: lws_lookup [2023/07/24 16:54:35:9290] I: lws_plat_init: mem: platform fd map: 48 B [2023/07/24 16:54:35:9291] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9291] D: : lws_role_transition: wsistate 0x200, ops pipe [2023/07/24 16:54:35:9291] N: lws_lc_tag: ++ [wsi|0|pipe] (1) [2023/07/24 16:54:35:9291] D: [wsi|0|pipe]: insert_wsi_socket_into_fds: tsi=0, sock=4, pos-in-fds=0 [2023/07/24 16:54:35:9292] I: rops_pt_init_destroy_netlink: creating netlink skt [2023/07/24 16:54:35:9292] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9292] D: : lws_role_transition: wsistate 0x200, ops netlink [2023/07/24 16:54:35:9293] N: lws_lc_tag: ++ [vh|0|netlink] (1) [2023/07/24 16:54:35:9293] D: [vh|0|netlink]: insert_wsi_socket_into_fds: tsi=0, sock=5, pos-in-fds=1 [2023/07/24 16:54:35:9294] D: rops_pt_init_destroy_netlink: starting netlink coldplug wait [2023/07/24 16:54:35:9294] I: lws_context_init_ssl_library: Compiled with OpenSSL support [2023/07/24 16:54:35:9295] I: lws_context_init_ssl_library: SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT [2023/07/24 16:54:35:9295] I: lws_server_get_canonical_hostname: canonical_hostname = plkosom [2023/07/24 16:54:35:9295] D: _realloc: size 927: lws_create_vhost [2023/07/24 16:54:35:9295] N: lws_lc_tag: ++ [vh|1|system||-1] (2) [2023/07/24 16:54:35:9296] D: _realloc: size 392: vh plugin table [2023/07/24 16:54:35:9296] D: _realloc: size 168: same vh list [2023/07/24 16:54:35:9296] D: _realloc: size 133: lws_metric_create [2023/07/24 16:54:35:9296] D: _realloc: size 133: lws_metric_create [2023/07/24 16:54:35:9297] I: [vh|1|system||-1]: lws_create_vhost: Creating Vhost 'system' (serving disabled), 7 protocols, IPv6 off [2023/07/24 16:54:35:9298] D: _realloc: size 96: lws_async_dns_server_add [2023/07/24 16:54:35:9298] I: lws_create_adopt_udp: 127.0.0.53:53 [2023/07/24 16:54:35:9298] D: lws_get_idlest_tsi: 2 5 [2023/07/24 16:54:35:9299] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9299] D: : lws_role_transition: wsistate 0x200, ops (unset) [2023/07/24 16:54:35:9299] N: lws_lc_tag: ++ [wsisrv|0|system|asyncdns] (1) [2023/07/24 16:54:35:9299] D: [wsisrv|0|system|asyncdns]: lws_create_new_server_wsi: joining vh system, tsi 0 [2023/07/24 16:54:35:9299] D: [wsisrv|0|system|asyncdns]: lws_vhost_bind_wsi: vh system: wsi none/none, count_bound_wsi 1 [2023/07/24 16:54:35:9300] D: [wsisrv|0|system|asyncdns]: lwsi_set_state: lwsi_set_state 0x20000200 -> 0x20000200 [2023/07/24 16:54:35:9300] D: [wsisrv|0|system|asyncdns]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9300] D: _realloc: size 36: udp struct [2023/07/24 16:54:35:9300] D: [wsisrv|0|system|asyncdns]: lws_role_transition: wsistate 0x119, ops raw-skt [2023/07/24 16:54:35:9300] D: [wsisrv|0|system|asyncdns]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9301] D: _realloc: size 40: lws_metrics_tag_add [2023/07/24 16:54:35:9301] I: lws_async_dns_query: entry 127.0.0.53 [2023/07/24 16:54:35:9301] I: lws_adns_dump: ADNS cache 0 entries [2023/07/24 16:54:35:9301] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9301] I: lws_async_dns_query: 127.0.0.53 uncached [2023/07/24 16:54:35:9302] D: _realloc: size 187: adns-numip [2023/07/24 16:54:35:9302] I: lws_adns_dump: ADNS cache 1 entries [2023/07/24 16:54:35:9302] I: lws_adns_dump: cache: '127.0.0.53', exp: 3599999980us, incomp 0, fl 0x0, refc 0, res 0x5635bf12a690 [2023/07/24 16:54:35:9302] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9303] D: [wsisrv|0|system|asyncdns|raw-skt]: lws_async_dns_complete: q: 0x7ffedcfbd4a0, c: 0x5635bf12a620, refcount 0 -> 1 [2023/07/24 16:54:35:9303] I: [wsisrv|0|system|asyncdns|raw-skt]: lws_sort_dns: sort_dns: 0x5635bf12a690 [2023/07/24 16:54:35:9303] D: _realloc: size 104: lws_sort_dns [2023/07/24 16:54:35:9303] I: [wsisrv|0|system|asyncdns|raw-skt]: lws_sort_dns: unsorted entry (af 2) 127.0.0.53 [2023/07/24 16:54:35:9304] I: [wsisrv|0|system|asyncdns|raw-skt]: lws_sort_dns_dump: 1: (2)127.0.0.53, gw (0)(unset), idi: 0, lbl: 0, prec: 0 [2023/07/24 16:54:35:9304] D: lws_async_dns_freeaddrinfo: c 0x5635bf12a620, 127.0.0.53, refcount 1 -> 0 [2023/07/24 16:54:35:9304] D: [wsisrv|0|system|asyncdns|raw-skt]: insert_wsi_socket_into_fds: tsi=0, sock=6, pos-in-fds=2 [2023/07/24 16:54:35:9305] D: [wsisrv|0|system|asyncdns|raw-skt]: lws_role_transition: wsistate 0x119, ops raw-skt [2023/07/24 16:54:35:9305] D: [wsisrv|0|system|asyncdns|raw-skt]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9305] I: [wsisrv|0|system|asyncdns|raw-skt]: lws_adopt_descriptor_vhost2: vhost [vh|1|system||-1] [2023/07/24 16:54:35:9305] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating mqtt [2023/07/24 16:54:35:9306] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-async-dns [2023/07/24 16:54:35:9306] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-h1 [2023/07/24 16:54:35:9306] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-h2 [2023/07/24 16:54:35:9306] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-ws [2023/07/24 16:54:35:9306] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-mqtt [2023/07/24 16:54:35:9307] D: [vh|1|system||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-raw [2023/07/24 16:54:35:9308] D: _realloc: size 96: __lws_async_dns_server_add [2023/07/24 16:54:35:9308] I: lws_create_adopt_udp: 127.0.0.53:53 [2023/07/24 16:54:35:9309] D: lws_get_idlest_tsi: 3 5 [2023/07/24 16:54:35:9309] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9309] D: : lws_role_transition: wsistate 0x200, ops (unset) [2023/07/24 16:54:35:9309] N: lws_lc_tag: ++ [wsisrv|1|system|asyncdns] (2) [2023/07/24 16:54:35:9309] D: [wsisrv|1|system|asyncdns]: lws_create_new_server_wsi: joining vh system, tsi 0 [2023/07/24 16:54:35:9310] D: [wsisrv|1|system|asyncdns]: lws_vhost_bind_wsi: vh system: wsi none/none, count_bound_wsi 2 [2023/07/24 16:54:35:9310] D: [wsisrv|1|system|asyncdns]: lwsi_set_state: lwsi_set_state 0x20000200 -> 0x20000200 [2023/07/24 16:54:35:9310] D: [wsisrv|1|system|asyncdns]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9310] D: _realloc: size 36: udp struct [2023/07/24 16:54:35:9310] D: [wsisrv|1|system|asyncdns]: lws_role_transition: wsistate 0x119, ops raw-skt [2023/07/24 16:54:35:9311] D: [wsisrv|1|system|asyncdns]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9311] D: _realloc: size 40: lws_metrics_tag_add [2023/07/24 16:54:35:9311] I: lws_async_dns_query: entry 127.0.0.53 [2023/07/24 16:54:35:9311] I: lws_adns_dump: ADNS cache 1 entries [2023/07/24 16:54:35:9311] I: lws_adns_dump: cache: '127.0.0.53', exp: 3599999068us, incomp 0, fl 0x0, refc 0, res 0x5635bf12a690 [2023/07/24 16:54:35:9311] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9312] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9312] I: lws_async_dns_query: 127.0.0.53: using cached, c->results 0x5635bf12a690 [2023/07/24 16:54:35:9312] I: [wsisrv|1|system|asyncdns|raw-skt]: lws_sort_dns: sort_dns: 0x5635bf12a690 [2023/07/24 16:54:35:9312] D: _realloc: size 104: lws_sort_dns [2023/07/24 16:54:35:9312] I: [wsisrv|1|system|asyncdns|raw-skt]: lws_sort_dns: unsorted entry (af 2) 127.0.0.53 [2023/07/24 16:54:35:9313] I: [wsisrv|1|system|asyncdns|raw-skt]: lws_sort_dns_dump: 1: (2)127.0.0.53, gw (0)(unset), idi: 0, lbl: 0, prec: 0 [2023/07/24 16:54:35:9313] D: lws_async_dns_freeaddrinfo: c 0x5635bf12a620, 127.0.0.53, refcount 1 -> 0 [2023/07/24 16:54:35:9313] D: [wsisrv|1|system|asyncdns|raw-skt]: insert_wsi_socket_into_fds: tsi=0, sock=7, pos-in-fds=3 [2023/07/24 16:54:35:9314] D: [wsisrv|1|system|asyncdns|raw-skt]: lws_role_transition: wsistate 0x119, ops raw-skt [2023/07/24 16:54:35:9314] D: [wsisrv|1|system|asyncdns|raw-skt]: lws_ensure_user_space: protocol pss 0, user_space=(nil) [2023/07/24 16:54:35:9314] I: [wsisrv|1|system|asyncdns|raw-skt]: lws_adopt_descriptor_vhost2: vhost [vh|1|system||-1] [2023/07/24 16:54:35:9314] D: _realloc: size 928: lws_create_vhost [2023/07/24 16:54:35:9315] N: lws_lc_tag: ++ [vh|2|default||-1] (3) [2023/07/24 16:54:35:9315] D: _realloc: size 392: vh plugin table [2023/07/24 16:54:35:9315] D: _realloc: size 144: same vh list [2023/07/24 16:54:35:9315] D: _realloc: size 134: lws_metric_create [2023/07/24 16:54:35:9316] D: _realloc: size 134: lws_metric_create [2023/07/24 16:54:35:9316] I: [vh|2|default||-1]: lws_create_vhost: Creating Vhost 'default' (serving disabled), 6 protocols, IPv6 off [2023/07/24 16:54:35:9316] I: lws_context_init_extensions: LWS_MAX_EXTENSIONS_ACTIVE: 1 [2023/07/24 16:54:35:9316] I: lws_create_context: mem: per-conn: 1320 bytes + protocol rx buf [2023/07/24 16:54:35:9316] I: lws_plat_drop_app_privileges: not changing group [2023/07/24 16:54:35:9316] I: lws_plat_drop_app_privileges: not changing user [2023/07/24 16:54:35:9317] D: lws_cancel_service: [2023/07/24 16:54:35:9317] I: lws_protocol_init: [2023/07/24 16:54:35:9317] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating mqtt [2023/07/24 16:54:35:9317] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-h1 [2023/07/24 16:54:35:9317] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-h2 [2023/07/24 16:54:35:9318] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-ws [2023/07/24 16:54:35:9318] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-mqtt [2023/07/24 16:54:35:9318] D: [vh|2|default||-1]: lws_protocol_init_vhost: not instantiating lws-secstream-raw [2023/07/24 16:54:35:9318] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9318] D: : lws_role_transition: wsistate 0x200, ops (unset) [2023/07/24 16:54:35:9319] D: : lws_vhost_bind_wsi: vh default: wsi none/none, count_bound_wsi 1 [2023/07/24 16:54:35:9319] D: rops_client_bind_mqtt: i = 0x7ffedcfbdc30 [2023/07/24 16:54:35:9319] D: _realloc: size 464: client mqtt struct [2023/07/24 16:54:35:9319] D: _realloc: size 31: lws_mqtt_str_create [2023/07/24 16:54:35:9319] N: lws_mqtt_generate_id: User space provided a client ID 'lwsMqttClient' [2023/07/24 16:54:35:9320] I: lws_create_client_mqtt_object: using client id 'lwsMqttClient' [2023/07/24 16:54:35:9320] D: _realloc: size 25: lws_mqtt_str_create [2023/07/24 16:54:35:9320] D: _realloc: size 25: lws_mqtt_str_create [2023/07/24 16:54:35:9320] D: _realloc: size 24: lws_mqtt_str_create [2023/07/24 16:54:35:9320] D: _realloc: size 33: lws_mqtt_str_create [2023/07/24 16:54:35:9320] D: : lws_role_transition: wsistate 0x10000200, ops mqtt [2023/07/24 16:54:35:9321] I: : lws_client_connect_via_info: role binding to mqtt [2023/07/24 16:54:35:9321] I: : lws_client_connect_via_info: vh default protocol binding to mqtt [2023/07/24 16:54:35:9321] D: _realloc: size 24: user space [2023/07/24 16:54:35:9321] I: : lws_client_connect_via_info: : mqtt mqtt entry [2023/07/24 16:54:35:9321] D: _realloc: size 122: client stash [2023/07/24 16:54:35:9322] N: lws_lc_tag: ++ [wsicli|0|MQTT/mqtt/default/172.25.146.89] (1) [2023/07/24 16:54:35:9322] D: _realloc: size 40: lws_metrics_tag_add [2023/07/24 16:54:35:9322] D: rops_client_bind_mqtt: i = (nil) [2023/07/24 16:54:35:9322] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_http_client_connect_via_info2: stash 0x5635bf12ba90 [2023/07/24 16:54:35:9322] D: _realloc: size 10: strdup [2023/07/24 16:54:35:9323] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_2_dnsreq: new conn on no pipeline flag [2023/07/24 16:54:35:9323] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_2_dnsreq: adding as active conn [2023/07/24 16:54:35:9323] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lwsi_set_state: lwsi_set_state 0x10000200 -> 0x10000201 [2023/07/24 16:54:35:9323] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_2_dnsreq: lookup 172.25.146.89:1883 [2023/07/24 16:54:35:9323] I: lws_async_dns_query: entry 172.25.146.89 [2023/07/24 16:54:35:9324] I: lws_adns_dump: ADNS cache 1 entries [2023/07/24 16:54:35:9324] I: lws_adns_dump: cache: '127.0.0.53', exp: 3599997826us, incomp 0, fl 0x0, refc 0, res 0x5635bf12a690 [2023/07/24 16:54:35:9324] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9324] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9324] I: lws_async_dns_query: 172.25.146.89 uncached [2023/07/24 16:54:35:9324] D: _realloc: size 190: adns-numip [2023/07/24 16:54:35:9325] I: lws_adns_dump: ADNS cache 2 entries [2023/07/24 16:54:35:9325] I: lws_adns_dump: cache: '172.25.146.89', exp: 3599999981us, incomp 0, fl 0x0, refc 0, res 0x5635bf12bbe0 [2023/07/24 16:54:35:9325] I: lws_adns_dump: cache: '127.0.0.53', exp: 3599997687us, incomp 0, fl 0x0, refc 0, res 0x5635bf12a690 [2023/07/24 16:54:35:9325] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9325] I: lws_adns_server_dump: nameserver: '127.0.0.53', 0 waiting [2023/07/24 16:54:35:9326] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_async_dns_complete: q: 0x7ffedcfbd880, c: 0x5635bf12bb70, refcount 0 -> 1 [2023/07/24 16:54:35:9326] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_sort_dns: sort_dns: 0x5635bf12bbe0 [2023/07/24 16:54:35:9326] D: _realloc: size 104: lws_sort_dns [2023/07/24 16:54:35:9326] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_sort_dns: unsorted entry (af 2) 172.25.146.89 [2023/07/24 16:54:35:9326] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_sort_dns_dump: 1: (2)172.25.146.89, gw (0)(unset), idi: 0, lbl: 0, prec: 0 [2023/07/24 16:54:35:9327] D: lws_async_dns_freeaddrinfo: c 0x5635bf12bb70, 172.25.146.89, refcount 1 -> 0 [2023/07/24 16:54:35:9327] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: peer_route_uidx 0 [2023/07/24 16:54:35:9327] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: WAITING_CONNECT [2023/07/24 16:54:35:9328] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lwsi_set_state: lwsi_set_state 0x10000201 -> 0x10000202 [2023/07/24 16:54:35:9328] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: __insert_wsi_socket_into_fds: tsi=0, sock=8, pos-in-fds=4 [2023/07/24 16:54:35:9328] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 1 [2023/07/24 16:54:35:9328] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_set_timeout: 20 secs, reason 2 [2023/07/24 16:54:35:9328] N: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: trying 172.25.146.89 [2023/07/24 16:54:35:9330] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: connect: fd 8, EINPROGRESS [2023/07/24 16:54:35:9330] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 5 [2023/07/24 16:54:35:9330] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_via_info: adoption cb 200 to mqtt mqtt [2023/07/24 16:54:35:9338] D: rops_handle_POLLIN_mqtt: wsistate 0x10000202, mqtt pollout 4 [2023/07/24 16:54:35:9339] D: rops_handle_POLLOUT_mqtt [2023/07/24 16:54:35:9339] D: rops_handle_POLLOUT_mqtt: no children [2023/07/24 16:54:35:9339] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 5 -> 1 [2023/07/24 16:54:35:9339] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_check: getsockopt: conn OK errno EINPROGRESS [2023/07/24 16:54:35:9340] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: source ads 172.25.146.202 [2023/07/24 16:54:35:9340] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_3_connect: going into connect_4 [2023/07/24 16:54:35:9340] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_4_established: mqtt mqtt client created own conn (raw 1) vh default st 0x202 [2023/07/24 16:54:35:9340] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_4_established: settings LRS_MQTTC_IDLE [2023/07/24 16:54:35:9340] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lwsi_set_state: lwsi_set_state 0x10000202 -> 0x10000121 [2023/07/24 16:54:35:9341] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: __lws_set_timeout: 15 secs, reason 8 [2023/07/24 16:54:35:9341] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_client_connect_4_established: going to service fd [2023/07/24 16:54:35:9341] D: rops_handle_POLLIN_mqtt: wsistate 0x10000121, mqtt pollout 4 [2023/07/24 16:54:35:9341] D: rops_handle_POLLOUT_mqtt [2023/07/24 16:54:35:9341] D: rops_handle_POLLOUT_mqtt: no children [2023/07/24 16:54:35:9342] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 1 [2023/07/24 16:54:35:9342] I: lws_mqtt_client_socket_service: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: Transport established, send out CONNECT [2023/07/24 16:54:35:9342] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 1 [2023/07/24 16:54:35:9343] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_issue_raw: ssl_capable_write (74) says 74 [2023/07/24 16:54:35:9343] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 1 [2023/07/24 16:54:35:9343] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lwsi_set_state: lwsi_set_state 0x10000121 -> 0x10000022 [2023/07/24 16:54:35:9354] D: rops_handle_POLLIN_mqtt: wsistate 0x10000022, mqtt pollout 1 [2023/07/24 16:54:35:9354] D: _lws_mqtt_rx_parser: 0, len = 4 [2023/07/24 16:54:35:9354] D: _lws_mqtt_rx_parser: received pkt type 0x2 / flags 0x0 [2023/07/24 16:54:35:9368] D: _lws_mqtt_rx_parser: 32, len = 3 [2023/07/24 16:54:35:9368] D: _lws_mqtt_rx_parser: received CONNACK pkt [2023/07/24 16:54:35:9369] D: _lws_mqtt_rx_parser: CONNACK pkt len = 2 [2023/07/24 16:54:35:9369] D: _lws_mqtt_rx_parser: 33, len = 2 [2023/07/24 16:54:35:9369] D: _lws_mqtt_rx_parser: 34, len = 1 [2023/07/24 16:54:35:9369] I: _lws_mqtt_rx_parser: cmd_completion: CONNACK [2023/07/24 16:54:35:9369] D: _realloc: size 1320: lws_wsi_create_with_role [2023/07/24 16:54:35:9370] D: : lws_role_transition: wsistate 0x200, ops (unset) [2023/07/24 16:54:35:9370] N: lws_lc_tag: ++ [mux|0|default|mqtt_sid1] (1) [2023/07/24 16:54:35:9370] D: [mux|0|default|mqtt_sid1]: lws_create_new_server_wsi: joining vh default, tsi 0 [2023/07/24 16:54:35:9370] D: [mux|0|default|mqtt_sid1]: lws_vhost_bind_wsi: vh default: wsi none/none, count_bound_wsi 2 [2023/07/24 16:54:35:9371] D: [mux|0|default|mqtt_sid1]: lwsi_set_state: lwsi_set_state 0x20000200 -> 0x20000200 [2023/07/24 16:54:35:9371] I: [mux|0|default|mqtt_sid1]: lws_wsi_mux_insert: par [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: assign sid 1 (curr 0) [2023/07/24 16:54:35:9371] D: [mux|0|default|mqtt_sid1]: lwsi_set_state: lwsi_set_state 0x20000200 -> 0x20000119 [2023/07/24 16:54:35:9371] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lwsi_set_state: lwsi_set_state 0x10000022 -> 0x10000119 [2023/07/24 16:54:35:9372] D: [mux|0|default|mqtt_sid1]: lwsi_set_role: state 0x10000119 [2023/07/24 16:54:35:9372] D: _realloc: size 464: nwsi mqtt [2023/07/24 16:54:35:9372] D: [mux|0|default|mqtt_sid1]: lws_ensure_user_space: protocol pss 24, user_space=0x5635bf12ba70 [2023/07/24 16:54:35:9372] D: [mux|0|default|mqtt_sid1]: lws_mux_mark_immortal: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default] (0) [2023/07/24 16:54:35:9372] N: _lws_mqtt_rx_parser: migrated nwsi [wsicli|0|MQTT/mqtt/default/172.25.146.89|default] to sid 1 [mux|0|default|mqtt_sid1] [2023/07/24 16:54:35:9373] D: [mux|0|default|mqtt_sid1]: lws_role_transition: wsistate 0x10000119, ops mqtt [2023/07/24 16:54:35:9373] U: callback_mqtt: MQTT_CLIENT_ESTABLISHED [2023/07/24 16:54:35:9373] D: rops_callback_on_writable_mqtt: [mux|0|default|mqtt_sid1] (wsistate 0x10000119) [2023/07/24 16:54:35:9373] I: [mux|0|default|mqtt_sid1]: lws_wsi_mux_mark_parents_needing_writeable: sid 1, pending writable [2023/07/24 16:54:35:9373] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: lws_wsi_mux_mark_parents_needing_writeable: sid 0, pending writable [2023/07/24 16:54:35:9374] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 5 [2023/07/24 16:54:35:9374] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_validity_confirmed_role: setting validity timer 20s (hup 0) [2023/07/24 16:54:35:9374] I: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_validity_confirmed_role: setting validity timer 20s (hup 0) [2023/07/24 16:54:35:9375] D: lws_mqtt_client_socket_service: consuming 0 / 4 [2023/07/24 16:54:36:1378] D: rops_handle_POLLIN_mqtt: wsistate 0x10000119, mqtt pollout 0 [2023/07/24 16:54:36:3381] D: rops_handle_POLLIN_mqtt: wsistate 0x10000119, mqtt pollout 0 [2023/07/24 16:54:36:5385] D: rops_handle_POLLIN_mqtt: wsistate 0x10000119, mqtt pollout 0 [2023/07/24 16:54:36:7389] D: rops_handle_POLLIN_mqtt: wsistate 0x10000119, mqtt pollout 0

After [2023/07/24 16:54:35:9374] D: [wsicli|0|MQTT/mqtt/default/172.25.146.89|default]: _lws_change_pollfd: fd 8 events 1 -> 5 i expecting LWS_CALLBACK_CHANGE_MODE_POLL_FD (which is the case in the example with the internal loop) but in my case no callback. Any idea what I might have missed ? I would be grateful for any help.

lws-team commented 12 months ago

External poll is kind of deprecated.

If you have to cooperate with another event loop, options 1 & 3 in the "Multiple codebases cooperating in one event loop" section of this https://libwebsockets.org/git/libwebsockets/tree/READMEs/README.event-loops-intro.md is the way to go.

marcindelta commented 10 months ago

Hi, It is the continuation of the MQTT client issue this time after transferring the library from EXTERNALL_POLL mode into the eventlib functionality. The result is the same. It stops on established callback: [2023/09/07 14:36:12:2624] I: [mux|0|default|mqtt_sid1]: lws_wsi_mux_insert: par [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: assign sid 1 (curr 0) [2023/09/07 14:36:12:2624] D: [mux|0|default|mqtt_sid1]: lwsi_set_state: lwsi_set_state 0x20000200 -> 0x20000119 [2023/09/07 14:36:12:2624] D: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: lwsi_set_state: lwsi_set_state 0x10000022 -> 0x10000119 [2023/09/07 14:36:12:2624] D: [mux|0|default|mqtt_sid1]: lwsi_set_role: state 0x10000119 [2023/09/07 14:36:12:2624] D: _realloc: size 464: nwsi mqtt [2023/09/07 14:36:12:2624] D: [mux|0|default|mqtt_sid1]: lws_ensure_user_space: protocol pss 24, user_space=0x557799d04490 [2023/09/07 14:36:12:2625] D: [mux|0|default|mqtt_sid1]: lws_mux_mark_immortal: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default] (0) [2023/09/07 14:36:12:2625] N: _lws_mqtt_rx_parser: migrated nwsi [wsicli|0|MQTT/mqtt/default/172.25.146.58|default] to sid 1 [mux|0|default|mqtt_sid1] [2023/09/07 14:36:12:2626] D: [mux|0|default|mqtt_sid1]: lws_role_transition: wsistate 0x10000119, ops mqtt [2023/09/07 14:36:12:2626] U: callback_mqtt -=-=----- 202 [2023/09/07 14:36:12:2626] U: callback_mqtt: MQTT_CLIENT_ESTABLISHED [2023/09/07 14:36:12:2626] D: rops_callback_on_writable_mqtt: [mux|0|default|mqtt_sid1] (wsistate 0x10000119) [2023/09/07 14:36:12:2627] I: [mux|0|default|mqtt_sid1]: lws_wsi_mux_mark_parents_needing_writeable: sid 1, pending writable [2023/09/07 14:36:12:2627] I: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: lws_wsi_mux_mark_parents_needing_writeable: sid 0, pending writable [2023/09/07 14:36:12:2627] D: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: _lws_change_pollfd: fd 11 events 1 -> 5 [2023/09/07 14:36:12:2627] I: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: _lws_validity_confirmed_role: setting validity timer 20s (hup 0) [2023/09/07 14:36:12:2627] I: [wsicli|0|MQTT/mqtt/default/172.25.146.58|default]: _lws_validity_confirmed_role: setting validity timer 20s (hup 0) [2023/09/07 14:36:12:2627] D: lws_mqtt_client_socket_service: consuming 0 / 4 [2023/09/07 14:36:12:2660] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_destroy: destroy msg 0x557799cfbd90 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_destroy: destroy msg 0x557799d04850 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_destroy: destroy msg 0x557799d04920 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2661] I: _lws_smd_msg_destroy: destroy msg 0x557799d049f0 [2023/09/07 14:36:12:2662] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2662] I: _lws_smd_msg_destroy: destroy msg 0x557799d04ac0 [2023/09/07 14:36:12:2662] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2662] I: _lws_smd_msg_destroy: destroy msg 0x557799d04b90 [2023/09/07 14:36:12:2663] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2663] I: _lws_smd_msg_destroy: destroy msg 0x557799d04c60 [2023/09/07 14:36:12:2663] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2663] I: _lws_smd_msg_destroy: destroy msg 0x557799d04d30 [2023/09/07 14:36:12:2664] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2664] I: _lws_smd_msg_destroy: destroy msg 0x557799d04e00 [2023/09/07 14:36:12:2664] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2664] I: _lws_smd_msg_destroy: destroy msg 0x557799d04ed0 [2023/09/07 14:36:12:2664] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2665] I: _lws_smd_msg_destroy: destroy msg 0x557799d04fa0 [2023/09/07 14:36:12:2665] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 42, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2665] I: lws_ss_create: unknown stream type captive_portal_detect [2023/09/07 14:36:12:2665] I: lws_ss_sys_cpd: Create stream failed (policy?) [2023/09/07 14:36:12:2666] I: _lws_smd_msg_destroy: destroy msg 0x557799d05000 [2023/09/07 14:36:12:2666] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2666] I: _lws_smd_msg_destroy: destroy msg 0x557799d050f0 [2023/09/07 14:36:12:2666] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2666] I: _lws_smd_msg_destroy: destroy msg 0x557799d051c0 [2023/09/07 14:36:12:2667] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2667] I: _lws_smd_msg_destroy: destroy msg 0x557799d05290 [2023/09/07 14:36:12:2667] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2668] I: _lws_smd_msg_destroy: destroy msg 0x557799d05360 [2023/09/07 14:36:12:2668] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2668] I: _lws_smd_msg_destroy: destroy msg 0x557799d05430 [2023/09/07 14:36:12:2668] I: _lws_smd_msg_deliver_peer: deliver cl 0x4, len 13, refc 1, to peer 0x557799cfbe80 [2023/09/07 14:36:12:2669] I: _lws_smd_msg_destroy: destroy msg 0x557799d05500 [2023/09/07 14:36:12:3580] D: _lws_state_transition: system: changed 2 'INITIALIZED' -> 3 'IFACE_COLDPLUG' [2023/09/07 14:36:12:3580] D: _lws_state_transition: system: changed 3 'IFACE_COLDPLUG' -> 4 'DHCP' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 4 'DHCP' -> 5 'CPD_PRE_TIME' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 5 'CPD_PRE_TIME' -> 6 'TIME_VALID' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 6 'TIME_VALID' -> 7 'CPD_POST_TIME' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 7 'CPD_POST_TIME' -> 8 'POLICY_VALID' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 8 'POLICY_VALID' -> 9 'REGISTERED' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 9 'REGISTERED' -> 10 'AUTH1' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 10 'AUTH1' -> 11 'AUTH2' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 11 'AUTH2' -> 12 'ONE_TIME_UPDATES' [2023/09/07 14:36:12:3581] D: _lws_state_transition: system: changed 12 'ONE_TIME_UPDATES' -> 13 'OPERATIONAL' [2023/09/07 14:36:12:3582] I: lws_state_transition_steps: INITIALIZED -> OPERATIONAL [2023/09/07 14:36:13:2271] U: Foreign 1Hz timer [2023/09/07 14:36:14:2283] U: Foreign 1Hz timer [2023/09/07 14:36:15:2294] U: Foreign 1Hz timer I prepared a combination of the minimal-mqtt-client and the minimal-http-server-eventlib-foreigin example and after context creation and client call it stops in the same place as mentioned. Example code: `/*

include

include

include

include

include "private.h"

enum { STATE_SUBSCRIBE, / subscribe to the topic / STATE_PUBLISH_QOS0, / Send the message in QoS0 / STATE_WAIT_ACK0, / Wait for the synthetic "ack" / STATE_PUBLISH_QOS1, / Send the message in QoS1 / STATE_WAIT_ACK1, / Wait for the real ack (or timeout + retry) /

STATE_TEST_FINISH

};

static int interrupted, bad = 1, do_ssl;

static const lws_retry_bo_t retry = { .secs_since_valid_ping = 20, / if idle, PINGREQ after secs / .secs_since_valid_hangup = 25, / hangup if still idle secs / };

static const lws_mqtt_client_connect_param_t client_connect_param = { .client_id = "lwsMqttClient", .keep_alive = 60, .clean_start = 1, .client_id_nofree = 1, .username_nofree = 1, .password_nofree = 1, .will_param = { .topic = "good/bye", .message = "sign-off", .qos = 0, .retain = 0, }, .username = "lwsUser", .password = "mySecretPassword", };

static lws_mqtt_publish_param_t pub_param;

static lws_mqtt_topic_elem_t topics[] = { [0] = { .name = "aaaa", .qos = QOS0 }, [1] = { .name = "aaaa", .qos = QOS1 }, };

static lws_mqtt_subscribe_param_t sub_param = { .topic = &topics[0], .num_topics = LWS_ARRAY_SIZE(topics), };

static const char * const test_string = "No one would have believed in the last years of the nineteenth " "century that this world was being watched keenly and closely by " "intelligences greater than man's and yet as mortal as his own; that as " "men busied themselves about their various concerns they were " "scrutinised and studied, perhaps almost as narrowly as a man with a " "microscope might scrutinise the transient creatures that swarm and " "multiply in a drop of water. With infinite complacency men went to " "and fro over this globe about their little affairs, serene in their " "assurance of their empire over matter. It is possible that the " "infusoria under the microscope do the same. No one gave a thought to " "the older worlds of space as sources of human danger, or thought of " "them only to dismiss the idea of life upon them as impossible or " "improbable. It is curious to recall some of the mental habits of " "those departed days. At most terrestrial men fancied there might be " "other men upon Mars, perhaps inferior to themselves and ready to " "welcome a missionary enterprise. Yet across the gulf of space, minds " "that are to our minds as ours are to those of the beasts that perish, " "intellects vast and cool and unsympathetic, regarded this earth with " "envious eyes, and slowly and surely drew their plans against us. And " "early in the twentieth century came the great disillusionment. ";

/ this reflects the length of the string above /

define TEST_STRING_LEN 1337

struct pss { int state; size_t pos; int retries; };

static struct lws_context_creation_info info; static const struct ops ops = NULL; struct lws_context context; int lifetime = 5, reported;

enum { TEST_STATE_CREATE_LWS_CONTEXT, TEST_STATE_DESTROY_LWS_CONTEXT, TEST_STATE_EXIT };

static int sequence = TEST_STATE_CREATE_LWS_CONTEXT;

static const struct lws_http_mount mount = { / .mount_next / NULL, / linked-list "next" / / .mountpoint / "/", / mountpoint URL / / .origin / "./mount-origin", / serve from dir / / .def / "index.html", / default filename / / .protocol / NULL, / .cgienv / NULL, / .extra_mimetypes / NULL, / .interpret / NULL, / .cgi_timeout / 0, / .cache_max_age / 0, / .auth_mask / 0, / .cache_reusable / 0, / .cache_revalidate / 0, / .cache_intermediaries / 0, / .cache_no / 0, / .origin_protocol / LWSMPRO_FILE, / files in a dir / / .mountpoint_len / 1, / char count / / .basic_auth_login_file / NULL, };

void signal_cb(int signum) { lwsl_notice("Signal %d caught, exiting...\n", signum);

switch (signum) {
case SIGTERM:
case SIGINT:
    break;
default:
    break;
}

lws_context_destroy(context);

}

static int callback_mqtt(struct lws wsi, enum lws_callback_reasons reason, void user, void in, size_t len) { struct pss pss = (struct pss )user; lws_mqtt_publish_param_t pub; size_t chunk;

lwsl_user("%s -=-=----- %d", __func__, reason);
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
    lwsl_err("%s: CLIENT_CONNECTION_ERROR: %s\n", __func__,
         in ? (char *)in : "(null)");
    interrupted = 1;
    break;

case LWS_CALLBACK_MQTT_CLIENT_CLOSED:
    lwsl_user("%s: CLIENT_CLOSED\n", __func__);
    interrupted = 1;
    break;

case LWS_CALLBACK_MQTT_CLIENT_ESTABLISHED:
    lwsl_user("%s: MQTT_CLIENT_ESTABLISHED\n", __func__);
    lws_callback_on_writable(wsi);

    return 0;

case LWS_CALLBACK_MQTT_SUBSCRIBED:
    lwsl_user("%s: MQTT_SUBSCRIBED\n", __func__);
    lws_callback_on_writable(wsi);
    break;

case LWS_CALLBACK_MQTT_CLIENT_WRITEABLE:
    /*
     * Extra WRITEABLE may appear here other than ones we asked
     * for, so we must consult our own state to decide if we want
     * to make use of the opportunity
     */

    switch (pss->state) {
    case STATE_SUBSCRIBE:
        lwsl_user("%s: WRITEABLE: Subscribing\n", __func__);

        if (lws_mqtt_client_send_subcribe(wsi, &sub_param)) {
            lwsl_notice("%s: subscribe failed\n", __func__);

            return -1;
        }
        pss->state++;
        break;

    case STATE_PUBLISH_QOS0:
    case STATE_PUBLISH_QOS1:

        lwsl_user("%s: WRITEABLE: Publish\n", __func__);

        pub_param.topic = "aaaa";
        pub_param.topic_len = (uint16_t)strlen(pub_param.topic);
        pub_param.qos = pss->state == STATE_PUBLISH_QOS0 ? QOS0 : QOS1;
        pub_param.payload_len = TEST_STRING_LEN;

        /* We send the message out 300 bytes or less at at time */

        chunk = 300;

        if (chunk > TEST_STRING_LEN - pss->pos)
            chunk = TEST_STRING_LEN - pss->pos;

        if (lws_mqtt_client_send_publish(wsi, &pub_param,
                test_string + pss->pos, (uint32_t)chunk,
                (pss->pos + chunk == TEST_STRING_LEN)))
            return -1;

        pss->pos += chunk;

        if (pss->pos == TEST_STRING_LEN) {
            pss->pos = 0;
            pss->state++;
        }
        break;

    default:
        break;
    }

    return 0;

case LWS_CALLBACK_MQTT_ACK:
    lwsl_user("%s: MQTT_ACK\n", __func__);
    /*
     * We can forget about the message we just sent, it's done.
     *
     * For our test, that's the indication we can close the wsi.
     */

    pss->state++;
    if (pss->state != STATE_TEST_FINISH) {
        lws_callback_on_writable(wsi);
        break;
    }

    /* Oh we are done then */

    bad = 0;
    interrupted = 1;
    lws_cancel_service(lws_get_context(wsi));
    break;

case LWS_CALLBACK_MQTT_RESEND:
    lwsl_user("%s: MQTT_RESEND\n", __func__);
    /*
     * We must resend the packet ID mentioned in len
     */
    if (++pss->retries == 3) {
        interrupted = 1;
        break;
    }
    pss->state--;
    pss->pos = 0;
    break;

case LWS_CALLBACK_MQTT_CLIENT_RX:
    lwsl_user("%s: MQTT_CLIENT_RX\n", __func__);

    pub = (lws_mqtt_publish_param_t *)in;
    assert(pub);

    lwsl_hexdump_notice(pub->topic, pub->topic_len);
    lwsl_hexdump_notice(pub->payload, pub->payload_len);

    return 0;

default:
    break;
}

return 0;

}

static const struct lws_protocols protocols[] = { { .name = "mqtt", .callback = callback_mqtt, .per_session_data_size = sizeof(struct pss) }, LWS_PROTOCOL_LIST_TERM }; static int connect_client() { struct lws_client_connect_info i;

memset(&i, 0, sizeof i);

i.mqtt_cp = &client_connect_param;
i.address = "172.25.146.58";
i.host = "localhost";
i.protocol = "mqtt";
i.context = context;
i.method = "MQTT";
i.alpn = "mqtt";
i.port = 1883;

// if (do_ssl) { // i.ssl_connection = LCCSCF_USE_SSL; // i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED; // i.port = 8883; // }

if (!lws_client_connect_via_info(&i)) {
    lwsl_err("%s: Client Connect Failed\n", __func__);

    return 1;
}

return 0;

}

void foreign_timer_service(void foreign_loop) { void foreign_loops[1];

lwsl_user("Foreign 1Hz timer\n");

if (sequence == TEST_STATE_EXIT && !context && !reported) {
    /*
     * at this point the lws_context_destroy() we did earlier
     * has completed and the entire context is wholly destroyed
     */
    lwsl_user("lws_destroy_context() done, continuing for 5s\n");
    reported = 1;
}

if (--lifetime)
    return;

switch (sequence++) {
case TEST_STATE_CREATE_LWS_CONTEXT:
    /* this only has to exist for the duration of create context */
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
    info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
    info.protocols = protocols;
    info.fd_limit_per_thread = 1 + 1 + 1;
    info.retry_and_idle_policy = &retry;

    info.options |= LWS_SERVER_OPTION_LIBEVENT;
    foreign_loops[0] = foreign_loop;
    info.foreign_loops = foreign_loops;

    context = lws_create_context(&info);
    if (!context) {
        lwsl_err("lws init failed\n");
        return;
    }
    lwsl_user("LWS Context created and will be active for 10s\n");

    connect_client();

    lifetime = 110;
    break;

case TEST_STATE_DESTROY_LWS_CONTEXT:
    /* cleanup the lws part */
    lwsl_user("Destroying lws context and continuing loop for 5s\n");
    lws_context_destroy(context);
    lifetime = 6;
    break;

case TEST_STATE_EXIT:
    lwsl_user("Deciding to exit foreign loop too\n");
    ops->stop();
    break;
default:
    break;
}

}

int main(int argc, const char *argv) { int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE / for LLL_ verbosity above NOTICE to be built into lws,