libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] Re: How to store the MHD_Connection object for later use


From: Christian Grothoff
Subject: [libmicrohttpd] Re: How to store the MHD_Connection object for later use?
Date: Sat, 12 Dec 2009 21:43:26 +0100
User-agent: KMail/1.12.2 (Linux/2.6.31-14-generic; KDE/4.3.2; i686; ; )

While the use is quite simple, it is hard to give a good concise example since 
it depends a lot on what you are trying to do in your code in the first place.  
You can find a (complex) example at 
https://ng.gnunet.org/svn/GNUnet/src/transports/http.c, but I don't think that 
will be helpful for understanding.  So let me try to explain a bit more here 
instead.

You can store anything into the void** arg and get it back the next time one 
of your callbacks is asked to do something with respect to the connection.  
Initially (first time you encounter the connection), "*arg" will be NULL, 
after that, whatever you stored in it the last time:

struct MyData *foo = *arg;
if (foo == NULL)
 { 
   // first call
  foo = initialize_my_data ();
  *arg = foo;
 }
else
 {
  // handle connection, use "foo" as desired, will be whatever
  // initialize_my_data returned
 }

Finally, you will most likely register a MHD_RequestCompletedCallback (where 
you again get the void** con_cls) using the MHD_OPTION_NOTIFY_COMPLETED to 
free the "struct MyData*" or whatever it is -- assuming we're talking about a 
dynamic allocation.  Note that while you might think that it would be 
sufficient to free "foo" when you queue the final response, this is not true 
since this may never happen, i.e. if the connection times out or there is some 
other kind of error before you have the chance to queue the response.  

I hope this helps!

Best,

Christian

On Saturday 12 December 2009 09:58:48 am you wrote:
> Hi Christian,
> 
> Thanks for your reply.
> Well, what do you mean by "you may store your own context in the
> void** argument of the callback (if needed)."
> 
> I seached through all your examples, none of the code demonstrate how to
> make use of the last void** argument.
> Is it possible for you to write me some sample code which make use of the
> void** argument?
> 
> Thanks a lot.
> Choy K.H
> 
> On Fri, Dec 11, 2009 at 7:39 PM, Christian Grothoff
> 
> <address@hidden>wrote:
> > Hi!
> >
> > You cannot "store" the connection object for later use.  You must use it
> > during the respective callbacks and you may store your own context in the
> > void** argument of the callback (if needed).
> >
> > Most importantly, there is no way to put connection objects into a queue
> > ---
> > the design is such that MHD tells you when you can (and, depending on
> > your threading model, have to) process connection data.  Putting
> > connections onto a
> > queue for processing, while theoretically possible with "external select"
> > (but
> > very hard and I'm not going to show how, that'd be a LOT of work), is not
> > recommended.
> >
> > Best,
> >
> > Christian
> >
> > On Friday 11 December 2009 12:13:39 you wrote:
> > > Hi Christian,
> > >
> > > I am using your libmicrohttpd as the Final Year Project Embedded Http
> > > Server.
> > > I am facing a problem right now and stuck.
> > >
> > > This is the scenario:
> > > I need to store the MHD_Connection  object somewhere so that i can post
> > >  back the http response back to the browser after i've finished
> >
> > processing
> >
> > >  the logic.
> > > I tried to memcpy like this:
> > >
> > > memcpy(&(q_ptr->connection), connection, sizeof(q_ptr->connection));
> > >
> > > and i store it in a structure and send to the message Queue.
> > >
> > > Another class will get it back from the Queue and use it.
> > >
> > > and it doesn't work when i pass the connection object to
> >
> > MHD_quee_response
> >
> > > MHD_queue_response (&(param1->connection), MHD_HTTP_OK, response);
> > >
> > > Finally my program crashed.
> > >
> > >
> > > Any thought?
> > > Thanks.
> >
> > --
> > http://grothoff.org/christian/
> 




reply via email to

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