Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,16 @@ namespace crow // NOTE: Already documented in "crow/app.h"
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;
}

self_t& onaccept(std::function<void(const crow::request&, std::optional<crow::response>&, void**)>&& callback)
template<typename Func>
self_t& onaccept(Func f)
{
accept_handler_ = std::move(callback);
return *this;
Expand Down Expand Up @@ -533,8 +541,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
std::function<void(crow::websocket::connection&, const std::string&, uint16_t)> close_handler_;
std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
std::function<void(const crow::request&, std::optional<crow::response>&, void**)> accept_handler_;
bool mirror_protocols_ = false;
std::pair<std::function<void(crow::websocket::connection&, const std::string&)>, uint64_t> timeout_handler_;
std::function<bool(const crow::request&, void**)> accept_handler_;
uint64_t max_payload_;
bool max_payload_override_ = false;
std::vector<std::string> subprotocols_;
Expand Down
30 changes: 29 additions & 1 deletion include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
std::function<void(crow::websocket::connection&, const std::string&, uint16_t)> 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<void(const crow::request&, std::optional<crow::response>&, void**)> accept_handler,
bool mirror_protocols)
{
Expand All @@ -128,7 +129,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::move(open_handler),
std::move(message_handler),
std::move(close_handler),
std::move(error_handler),
std::move(error_handler),
std::move(receiver_timeout_handler),
std::move(accept_handler)));

// Perform handshake validation
Expand Down Expand Up @@ -344,6 +346,26 @@ namespace crow // NOTE: Already documented in "crow/app.h"
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 @@ -362,6 +384,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return;
}

start_deadline();

is_reading = true;
switch (state_)
{
Expand Down Expand Up @@ -846,7 +870,11 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
std::function<void(crow::websocket::connection&, const std::string&, uint16_t status_code)> 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<void(const crow::request&, std::optional<crow::response>&, void**)> accept_handler_;

detail::task_timer task_timer_;
detail::task_timer::identifier_type task_id_;
};
} // namespace websocket
} // namespace crow
Loading
Loading