-
-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added send_binary_blocking
function
#355
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,36 @@ namespace crow // NOTE: Already documented in "crow/app.h" | |
{ | ||
send_data(0x2, std::move(msg)); | ||
} | ||
|
||
/// Send a binary encoded message in blocking mode. | ||
void send_binary_blocking(const std::string& msg, boost::system::error_code& ec) override | ||
{ | ||
auto header = build_header(2, msg.size()); | ||
std::vector<boost::asio::const_buffer> buffer; | ||
buffer.emplace_back(boost::asio::buffer(header)); | ||
buffer.emplace_back(boost::asio::buffer(msg)); | ||
|
||
boost::asio::io_service service; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to use it, however, it did not work properly for me. I think there might have been some timing issues where e.g. the deadline_timer is being canceled after write but the async_wait of the timer has not been started yet as the adaptor's IO was too busy? |
||
boost::asio::deadline_timer deadline_timer(service); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Crow already has |
||
deadline_timer.expires_from_now(boost::posix_time::seconds(3)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be the same as |
||
|
||
deadline_timer.async_wait([&](const boost::system::error_code& err) | ||
{ | ||
if(!err) | ||
{ | ||
ec = boost::system::errc::make_error_code(boost::system::errc::timed_out); | ||
adaptor_.shutdown_write(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the adaptor is being shut down, shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my thought was that the caller decides whether he wants to close the connection in an error case or not. That is also the reason why &ec has to be provided. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well if we're shutting down the adaptor I'm not sure there's anything left to do with the connection object, even for the http connection deleting the object was the course of action taken when an error occurs, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that is correct but I had a different approach. I am saving all connections in a map. If I call automatically check_destroy() which internally deletes itself (delete this) then I would run into a segmentation fault next time I tried to use it. In this case I can check the &ec and decide to remove that entry from my map. But does some other user know that he has to do that as well only if a write ran into a timeout? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A use case like yours would probably be best solved with the close handler, maybe we can call the close method and have that delete the connection, and have the close handler deal with removing the connection from the map. The approach I'm trying my best to take with Crow is to make getting something up and running as simple as can be while providing the as close as full access to the protocol internals for the convoluted edge cases that end up occurring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to implement that approach but unfortunatelly I was not able to make it run in my asynchronous state machine. Don't get me wrong. I am fine with your idea to have a very simple library and if you want to have check_destroy() being called in such a case then go for it, however, it did not work for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe returning a bool or enum or something could be applicable. For your state machine, maybe you could check using an atomic bool flag (or using a similar approach), after sending data, if something has to be removed from the map. |
||
} | ||
}); | ||
|
||
dispatch([&, this] | ||
{ | ||
boost::asio::write(adaptor_.socket(), buffer, boost::asio::transfer_all(), ec); | ||
deadline_timer.cancel(); | ||
}); | ||
|
||
service.run(); | ||
} | ||
|
||
/// Send a plaintext message. | ||
void send_text(std::string msg) override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would make sense to add a
send_text_blocking()
. Also is it necessary to putec
there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see my answer above regarding check_destroy()