sending data out of mongoose polling context #3073
-
I am trying to use mongoose library to make a gateway device and the following scenarios require filling up the mg_connection::send buffer out of the mg_poll() context:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I see three possible paths:
PollingAs long as you don't violate the other end maximum wait time, nobody forces you to send a response immediately. When calling your event handler, Mongoose sets the is_resp flag https://mongoose.ws/documentation/#connection-flags , it will be reset when you call mg_http_reply(), but if you don't call it and just return, nothing will be sent and this connection will be kept open and waiting. "Async"If for some reason you don't want to use the poll event, you can store your connection somewhere and call mg_http_reply from some other point in your main loop. This function will fill the TCP buffer but data won't be sent until the manager runs. MultihreadingFrom the docs
If you absolutely need multi-threading, take the time to go through the user guide (linked above) and tutorials, there is one that shows you how to do multi-threading. Please see our documentation, and follow the guidelines in our tutorials. |
Beta Was this translation helpful? Give feedback.
-
Hi Scaprile, thanks for your reply. Although I am using Mongoose in multithreading environment (freeRTOS), the multithreading solution is too complicated (creation/deletion of worker thread and temporary data buffer) and also not very efficient (data prepared by the worker task in a temporary buffer have to be moved into the IO buffer). The other options are also not efficient either due to the same reason. What I am trying to achieve:
The idea is simple, just to define two IO buffers for mg_connection::send, one original an one backup, and use them alternatively in a ping-pong fashion. This way, the producer thread can safely write to the backup IO buffer without worrying about any conflict with the mg_poll() thread (the consumer), When mg_poll() runs, it only needs to check if IO buffer swapping is needed. After the buffers are swapped, the data inside the buffer will be ready to use, no need to move them! This kind of efficiency is import when I have large amount of log messages to be pushed through the WebSocket connection. Interestingly I have tried the following: With the above experiment, I am confident that the ping-pong IO buffer method should work, just wondering if there is anything important that I may have missed before I give it a try. |
Beta Was this translation helpful? Give feedback.
I see three possible paths:
Polling
As long as you don't violate the other end maximum wait time, nobody forces you to send a response immediately. When calling your event handler, Mongoose sets the is_resp flag https://mongoose.ws/documentation/#connection-flags , it will be reset when you call mg_http_reply(), but if you don't call it and just return, nothing will be sent and this connection will be kept open and waiting.
At a poll event, when you know your response is ready, you just call mg_http_reply().
Please read https://mongoose.ws/documentation/#best-practices
If you use
mg_printf()
ormg_send()
instead, don't forget to setContent-Length
andc->i…