Used App reference to get websocket count and signals.

Also fixed issue where enabling SSL prevented compilation.
This commit is contained in:
The-EDev 2022-05-15 01:15:39 +03:00
parent 9f99be55e1
commit e5fe4e6e26
No known key found for this signature in database
GPG Key ID: 51C45DC0C413DCD9
3 changed files with 31 additions and 18 deletions

View File

@ -69,7 +69,7 @@ namespace crow
template<typename Adaptor>
void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
{
router_.handle_upgrade(req, res, adaptor, websocket_count);
router_.handle_upgrade(req, res, adaptor);
}
/// Process the request and generate a response for it
@ -118,6 +118,11 @@ namespace crow
return *this;
}
std::vector<int> signals()
{
return signals_;
}
/// Set the port that Crow will handle requests on
self_t& port(std::uint16_t port)
{

View File

@ -46,7 +46,7 @@ namespace crow
}
virtual void handle(request&, response&, const routing_params&) = 0;
virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&, std::atomic<int>&)
virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&)
{
res = response(404);
res.end();
@ -388,9 +388,9 @@ namespace crow
res.end();
}
void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor, std::atomic<int>& websocket_count) override
void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override
{
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), websocket_count, app_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), app_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
}
#ifdef CROW_ENABLE_SSL
void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override
@ -1303,7 +1303,7 @@ namespace crow
// TODO maybe add actual_method
template<typename Adaptor>
void handle_upgrade(const request& req, response& res, Adaptor&& adaptor, std::atomic<int>& websocket_count)
void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
{
if (req.method >= HTTPMethod::InternalMethodCount)
return;
@ -1357,7 +1357,7 @@ namespace crow
// any uncaught exceptions become 500s
try
{
rules[rule_index]->handle_upgrade(req, res, std::move(adaptor), websocket_count);
rules[rule_index]->handle_upgrade(req, res, std::move(adaptor));
}
catch (std::exception& e)
{

View File

@ -71,7 +71,7 @@ namespace crow
///
/// Requires a request with an "Upgrade: websocket" header.<br>
/// Automatically handles the handshake.
Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler, std::atomic<int>& websocket_count,
Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler,
std::function<void(crow::websocket::connection&)> open_handler,
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
std::function<void(crow::websocket::connection&, const std::string&)> close_handler,
@ -79,12 +79,13 @@ namespace crow
std::function<bool(const crow::request&)> accept_handler):
adaptor_(std::move(adaptor)),
handler_(handler),
websocket_count_(websocket_count),
websocket_count_(handler_->websocket_count),
open_handler_(std::move(open_handler)),
message_handler_(std::move(message_handler)),
close_handler_(std::move(close_handler)),
error_handler_(std::move(error_handler)),
accept_handler_(std::move(accept_handler))
accept_handler_(std::move(accept_handler)),
signals_(adaptor_.get_io_service())
{
if (!boost::iequals(req.get_header_value("upgrade"), "websocket"))
{
@ -103,6 +104,11 @@ namespace crow
}
}
signals_.clear();
for (auto snum: handler_->signals())
signals_.add(snum);
// Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
// Sec-WebSocket-Version: 13
std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
@ -110,14 +116,15 @@ namespace crow
s.processBytes(magic.data(), magic.size());
uint8_t digest[20];
s.getDigestBytes(digest);
signals_.async_wait(
[&](const boost::system::error_code& e, int /*signal_number*/){
if (!e){
CROW_LOG_INFO << "quitting " << this;
do_not_destroy_ = true;
close("Quitter");
}
});
signals_.async_wait(
[&](const boost::system::error_code& e, int /*signal_number*/){
if (!e){
CROW_LOG_INFO << "quitting " << this;
do_not_destroy_ = true;
close("Quitter");
}
});
start(crow::utility::base64encode((unsigned char*)digest, 20));
}
@ -658,12 +665,13 @@ namespace crow
// **WARNING**
std::atomic<int>& websocket_count_;
std::function<void(crow::websocket::connection&)> open_handler_;
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
std::function<void(crow::websocket::connection&)> error_handler_;
std::function<bool(const crow::request&)> accept_handler_;
boost::asio::signal_set signals_;
};
} // namespace websocket
} // namespace crow