0

In my use case my messages are small but there are many and they come very fast. Some times my application has to respond. However, I want it to respond only if it has processed all the messages that it has received.

I receive messages in the following way:

static int lws_event_callback(struct lws* conn, enum lws_callback_reasons reason, void* user, void* data, size_t len)
{
    switch(reason)
    {
        case LWS_CALLBACK_CLIENT_RECEIVE:
        {
            my_callback_client_receive(data);
        }}}

I don't know much about the internals of libwebsockets, but conceptually I can imagine that it's possible that by the time that my_callback_client_receive is called, other messages have arrived.

In my ideal world there would be a variable n which meant "number of messages waiting in a buffer to be processed" that I could pass into my call back

case LWS_CALLBACK_CLIENT_RECEIVE:
{
    my_callback_client_receive(data, n);
}

So that I would know not to respond until I've processed as many messages as possible. Something like:

void my_callback_client_receive(data, n)
{
    process(data);
    if (n>1):
        return; // this msg is not the most recent, keep processing
    else:
        send_response();
        return;
}

Is this or something like this possible with LWS?

xolok
  • 1
  • Most of your approach sounds reasonable. i.e., Using an event handler, detect message, process message. But categorizing the _process message_ section by number of messages seems like a bad idea. messages should be processed as they come in. If you get 2 messages, or 2000 messages. You could perhaps _popen_ the process phase into smaller temporary processes to keep things flowing. I do something very similar when using RS232 communications by setting up a comCallback to detect and process response message traffic from instrumentation. – ryyker May 15 '20 at 18:39
  • Hi, in case you haven't noticed I'm a beginning C programmer :-) When you say popen do you mean this https://linux.die.net/man/3/popen ? If that's the case, I would prefer not to involve new processes. Seems like unnecessary complexity. The issue here is NOT that the messages take long to process and so I need to parallelize. Instead I just want to know not to send a response before I've processed the latest received message. – xolok May 15 '20 at 19:08

0 Answers0