On Wed, Aug 27, 2014 at 1:50 PM, Puneet Bakshi <[email protected]>
wrote:

> Hi,
>
> I have the requirement that when client call comes, mongoose event handler
> should asynchronously read data from other node (a callback fn is provided
> as shown below) and when the callback fn returns with data, mongoose should
> return response to the client. I am a little unsure how to maintain
> connection alive across threads (main thread and callback threads) and how
> to correctly close the connection at the end of callback fn. Can somebody
> also explain MG_MORE a little more ?
>
> I am thinking about the following flow. Please let me know
>
> static int ev_handler(struct mg_connection *conn, enum mg_event ev)
> {
>     if (ev == MG_REQUEST) {
>         async_read_from_other_node(conn);
>         return MG_MORE;                                 // Is it right???
> I do not want to close connection this time and want callback_fn() thread
> to close it.
>

 That's correct. Returning MG_MORE tells mongoose that reply is not sent
yet and mongoose has to keep the request active.


>     } else {
>         return MG_TRUE;
> }
>
> async_read_from_other_node(struct mg_connection *conn)
> {
>     callback_arg = conn;
>     initiate_async_read_from_other_node(callback_fn, callback_arg);
>     return;
> }
>

There are two ways.

1.  When MG_MORE is returned, Mongoose will keep sending MG_POLL to the
connection until MG_POLL handler replies with MG_TRUE. When async client
returns with data, MG_POLL handler should forward the data to the client
and return MG_TRUE.
2. Or, async client might have original connection saved, and push data
straight to the original connection.

Take a look at the async HTTP client example
<https://github.com/cesanta/mongoose/blob/master/examples/http_client/http_client.c>
which
implements (2), it might make things clear for you.

Note that caching connection pointers should be done very carefully, as
Mongoose might invalidate them. It is an error to pass a connection pointer
to the other thread.

-- 
You received this message because you are subscribed to the Google Groups 
"mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to