From ec99454756342da9adc84db90ff9b568cbc8a837 Mon Sep 17 00:00:00 2001 From: flamingcow Date: Sat, 11 May 2019 21:52:49 -0700 Subject: [PATCH] Add close handling --- request.cc | 15 +++++++++++++++ request.h | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/request.cc b/request.cc index cc40259..9924691 100644 --- a/request.cc +++ b/request.cc @@ -20,10 +20,21 @@ Request::Request(Connection* conn) : conn_(conn), out_buf_(64*1024) {} +Request::~Request() { + if (on_close_) { + on_close_(); + } +} + void Request::NewRequest(uint16_t request_id) { + if (on_close_) { + on_close_(); + } + request_id_ = request_id; params_.clear(); body_ = {}; + on_close_ = nullptr; out_buf_.Reset(); body_written_ = false; } @@ -53,6 +64,10 @@ const std::string_view& Request::GetBody() const { return body_; } +void Request::OnClose(const std::function& on_close) { + on_close_ = on_close; +} + void Request::WriteHeader(const std::string_view& name, const std::string_view& value) { std::lock_guard l(output_mu_); diff --git a/request.h b/request.h index 35ad412..b6a27f4 100644 --- a/request.h +++ b/request.h @@ -15,6 +15,7 @@ class Connection; class Request { public: Request(Connection *conn); + ~Request(); void NewRequest(uint16_t request_id); @@ -26,6 +27,8 @@ class Request { const std::string_view& GetParam(const std::string_view& key) const; const std::string_view& GetBody() const; + void OnClose(const std::function& callback); + void WriteHeader(const std::string_view& name, const std::string_view& value); void WriteBody(const std::string_view& body); [[nodiscard]] bool Flush(); @@ -47,6 +50,8 @@ class Request { std::unordered_map params_; std::string_view body_; + std::function on_close_; + firebuf::Buffer out_buf_; bool body_written_; std::recursive_mutex output_mu_;