Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace crow
struct connection
{
virtual void send_binary(const std::string& msg) = 0;
virtual void send_binary_blocking(const std::string& msg, boost::system::error_code& ec) = 0;
virtual void send_text(const std::string& msg) = 0;
virtual void send_ping(const std::string& msg) = 0;
virtual void send_pong(const std::string& msg) = 0;
Expand Down Expand Up @@ -157,6 +158,36 @@ namespace crow
do_write();
});
}

/// Send a binary encoded message in blocking mode.
void send_binary_blocking(const std::string& msg, boost::system::error_code& ec) override
Copy link
Member

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 put ec there?

Copy link
Author

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()

{
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use adaptor_.get_io_service() instead of creating a new io_service?

Copy link
Author

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Crow already has task_timer, would that be more appropriate?

deadline_timer.expires_from_now(boost::posix_time::seconds(3));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be the same as timeout_ in app.h.


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_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send);
}
});

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(const std::string& msg) override
Expand Down