File descriptor associated to a particular connection

29 views
Skip to first unread message

Fabrizio Dini

unread,
Oct 9, 2014, 5:15:34 AM10/9/14
to mongoos...@googlegroups.com
Hi everybody, this is my first post on this group!

I am trying to use mongoose to implement a CGI server on a test project of mine. It has been easy to setup the code so that the CGI requests are handled by a custom event handler. The hard part comes right after. I have a few functions (that I CANNOT touch or change in any way) that generates the responses to the CGI requests. Unfortunately, all these functions takes a file descriptor as input, as they write their output to the file descriptor and close it before returning.

Now the question is: is there any file descriptor  in mg_connection structure (or somewhere else) that I can pass to these functions? I gave a look to the code but I am not sure...

Thanks in advance!

regards,
Fabrizio

Sergey Lyubka

unread,
Oct 9, 2014, 5:58:27 AM10/9/14
to mongoose-users
You can use mg_connection::connection_param to store whatever you want there.

--
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 mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Fabrizio Dini

unread,
Oct 10, 2014, 4:01:40 AM10/10/14
to mongoos...@googlegroups.com
Thanks for the quick reply, but I am not sure you have understood my question.

What will happen to the data sotred in mg_connection::connection_param? I gave a look to the code and it seems to me that connection_param is used to store a 'struct connection' object. I was hoping that maybe the file descriptor I am looking for could be in there. 

Sergey Lyubka

unread,
Oct 10, 2014, 5:19:45 AM10/10/14
to mongoose-users
The file descriptor associated with CGI, or opened file, is hidden from the user code.
"struct connection" is visible only inside mongoose source code, and not exported out.
Only "struct mg_connection" is exported.

Does that address the question?

Fabrizio Dini

unread,
Oct 10, 2014, 10:44:46 AM10/10/14
to mongoos...@googlegroups.com
Indirectly, yes Sergey, that address the question, thanks!. You are saying that there is no way to get a file descriptor binded to the socket connection and use it to send characters to a client. That makes me really sad... :(

Is there any other way to do that? I'll try to re-formulate my problem as follows, to make it clearer.

Given a function:

void generate_content(int fd)

that outputs text to the file descriptor fd (for example, an html page) I am looking for a method to extract a file descriptor from an mg_connection (or an mg_server, or both) so that I can use it to force generate_content() to write directly to the client at the other end of a connection. 

At the moment, the only workaround I found to solve this problem is to open a local file and pass its fd to generate_content(), so that it writes into the local file. Then, open the file with read permission, read the content and send it to the client with mg_send_data(). 

Any hints?

Thanks so much!

Sergey Lyubka

unread,
Oct 10, 2014, 10:49:14 AM10/10/14
to mongoose-users
I suggest creating a pipe or a socketpair, call generate_content() passing one end of a pipe,
read from the other end in a loop, calling mg_send().

Fabrizio Dini

unread,
Oct 10, 2014, 11:13:56 AM10/10/14
to mongoos...@googlegroups.com
Thank you Sergey! That is probably the best solution left, since there's no way to get the fd directly from mongoose.

Again, thanks for you help!

regards,
Fabrizio

Sergey Lyubka

unread,
Oct 10, 2014, 11:25:28 AM10/10/14
to mongoose-users
Welcome Fabrizio
Note that getting FD from Mongoose would not be good,
that FD is non-blocking socket, and your function almost certainly doesn't expect writes to fail.

Fabrizio Dini

unread,
Oct 10, 2014, 11:30:22 AM10/10/14
to mongoos...@googlegroups.com
True. All in all, a pipe should suffice. It would allow me to get the data out of generate_content() without the overhead of handling a true file on the file system.
Reply all
Reply to author
Forward
0 new messages