Skip to content
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

Add optional keep-alive mechanism to WS connections #509

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
13 changes: 11 additions & 2 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ namespace crow
void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override
{
max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload();
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, timeout_handler_, accept_handler_);
}
#ifdef CROW_ENABLE_SSL
void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override
{
new crow::websocket::Connection<SSLAdaptor, App>(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
new crow::websocket::Connection<SSLAdaptor, App>(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, timeout_handler_, accept_handler_);
}
#endif

Expand Down Expand Up @@ -496,6 +496,14 @@ namespace crow
return *this;
}

template<typename Func>
self_t& ontimeout(Func f, uint64_t timeout_in_seconds = 5)
{
timeout_handler_.first = f;
timeout_handler_.second = timeout_in_seconds;
return *this;
}

template<typename Func>
self_t& onaccept(Func f)
{
Expand All @@ -509,6 +517,7 @@ namespace crow
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&, const std::string&)> error_handler_;
std::pair<std::function<void(crow::websocket::connection&, const std::string&)>, uint64_t> timeout_handler_;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use std::chrono for timeout type?

std::function<bool(const crow::request&, void**)> accept_handler_;
uint64_t max_payload_;
bool max_payload_override_ = false;
Expand Down
31 changes: 30 additions & 1 deletion include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace crow
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&, const std::string&)> error_handler,
std::pair<std::function<void(crow::websocket::connection&, const std::string&)>, uint64_t> receiver_timeout_handler,
std::function<bool(const crow::request&, void**)> accept_handler):
adaptor_(std::move(adaptor)),
handler_(handler),
Expand All @@ -84,7 +85,9 @@ namespace crow
message_handler_(std::move(message_handler)),
close_handler_(std::move(close_handler)),
error_handler_(std::move(error_handler)),
accept_handler_(std::move(accept_handler))
timeout_handler_(std::move(receiver_timeout_handler)),
accept_handler_(std::move(accept_handler)),
task_timer_(adaptor_.get_io_service())
{
if (!utility::string_equals(req.get_header_value("upgrade"), "websocket"))
{
Expand Down Expand Up @@ -272,6 +275,26 @@ namespace crow
do_read();
}

void start_deadline(/*int timeout = 5*/)
{
cancel_deadline_timer();

if (close_connection_ || !timeout_handler_.first) return;

task_timer_.set_default_timeout(timeout_handler_.second);
task_id_ = task_timer_.schedule([this] {
timeout_handler_.first(*this, "timeout");
});
CROW_LOG_DEBUG << this << " websocket timer added: " << &task_timer_ << ' ' << task_id_;
}

void cancel_deadline_timer()
{
if (!timeout_handler_.first) return;
CROW_LOG_DEBUG << this << " websocket timer cancelled: " << &task_timer_ << ' ' << task_id_;
task_timer_.cancel(task_id_);
}

/// Read a websocket message.

///
Expand All @@ -290,6 +313,8 @@ namespace crow
return;
}

start_deadline();

is_reading = true;
switch (state_)
{
Expand Down Expand Up @@ -738,7 +763,11 @@ namespace crow
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&, const std::string&)> error_handler_;
std::pair<std::function<void(crow::websocket::connection&, const std::string&)>, uint64_t> timeout_handler_;
std::function<bool(const crow::request&, void**)> accept_handler_;

detail::task_timer task_timer_;
detail::task_timer::identifier_type task_id_;
};
} // namespace websocket
} // namespace crow
16 changes: 16 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,11 @@ TEST_CASE("websocket")
else if (isbin && message == "Hello bin")
conn.send_binary("Hello back bin");
})
.ontimeout([&](websocket::connection& conn, const std::string&) {
CROW_LOG_INFO << "Websocket Time Out";
conn.send_text("TimeOut");
},
2 /* seconds */)
.onclose([&](websocket::connection&, const std::string&) {
CROW_LOG_INFO << "Closing websocket";
});
Expand Down Expand Up @@ -2647,6 +2652,17 @@ TEST_CASE("websocket")
std::string checkstring(std::string(buf).substr(0, 12));
CHECK(checkstring == "\x81\x0AHello back");
}

//----------TimeOut----------
{
std::fill_n(buf, 2048, 0);
CROW_LOG_INFO << "Waiting Time Out";
c.receive(asio::buffer(buf, 2048));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
std::string checkstring(std::string(buf).substr(0, 10));
CHECK(checkstring == "\x81\x07TimeOut");
}

//----------Close----------
{
std::fill_n(buf, 2048, 0);
Expand Down