libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] MHD_USE_POLL trouble


From: Evgeny Grin
Subject: Re: [libmicrohttpd] MHD_USE_POLL trouble
Date: Mon, 16 Jan 2023 15:39:41 +0300
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1

Hi kginbg,

MHD_run() (and other MHD_run_*() functions) cannot be used with MHD started with internal threads. It is written in doxy description. If MHD is started with any kind of internal threads all the traffic on the set port is handled by MHD in MHD's thread. The caller thread does not need to call any of MHD functions in a loop.
See doc\examples\hellobrowser.c or other examples.

You may register callback with MHD_OPTION_NOTIFY_CONNECTION to track the number of served connection. In this callback you may set some flag variable to non-zero, when the last connection is successfully closed.
And handle everything in main loop like:

------------------------
volatile bool ready_to_stop = 0;

....

  while(! ready_to_stop)
    sleep (1);
  MHD_stop_daemon (d);
------------------------

The code is a bit ugly, but easy to implement.

Alternatively, you can implement it differently, with external polling (without MHD internal threads). In such case you do not need to use callbacks, but you need to manage timeouts (global application timeout and MHD timeouts together).

Other problems in your code:
* MHD_get_timeout() cannot be used with MHD internal threads
* tvp is assigned but not used at all
* Your code never exits ("while(1)" without any "break").

While you developing with MHD it's higly recommended to enable MHD_USE_ERROR_LOG flag. You can leave this flag enabled for production as well.

--
Wishes,
Evgeny


15.01.2023 17:50, klemens wrote:


Hi all,

thanks first for your quick reply but this didn't solve the
issue.  Most of my code is derived from the "post_example.c",
though I only use the GET method to pick up some values from
connecting users. I create a session for each incoming new
request and record session start time.  The answer is a simple
webpage dynamically created for the URL from a database. After
receiving the filled form/submitted page from the user, I send a
confirmation that details the received values. All this works
nicely and timely.

After a sucessful receipt I want to expire the session soon. In
case the session remains idle for 15 Min I want to expire it too.
Here is my code:

        daemon = MHD_start_daemon (     MHD_USE_INTERNAL_POLLING_THREAD
                                                                | MHD_USE_POLL,
                                                                8888,
                                                                
&on_client_connect,
                                                                myclient_ip,
                                                                
&create_response, NULL,
                                                                
MHD_OPTION_CONNECTION_TIMEOUT,
                                                                (unsigned int) 
900,
                                                                MHD_OPTION_END
                                                          );
        if (NULL == daemon)
                return (emsg ("Can't create daemon"));
        while (1)
        {
                expire_sessions ();
                if (MHD_get_timeout (daemon, &mhd_timeout) == MHD_YES)
            {
                tv.tv_sec = mhd_timeout / 1000;
                tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
                tvp = &tv;
            }
                else
                        tvp = NULL;
                MHD_run (daemon);
        }
        MHD_stop_daemon (daemon);

I thought I would block the mainloop for 900 sec, in case there is no
new request, but this seems to cause the idle-loop. Any hints how to
fix this problem and expire completed/idle sessions is highly appre-
ciated.

kginbg.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]