Вторая часть - база для сессии
Для обработки запроса нужно вызвать функцию request_handler_
This commit is contained in:
parent
df8bdbc185
commit
d2fc912071
@ -31,5 +31,13 @@ class Session : public SessionBase, public std::enable_shared_from_this<Session<
|
|||||||
{
|
{
|
||||||
return this->shared_from_this();
|
return this->shared_from_this();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void HandleRequest(HttpRequest&& request) override;
|
||||||
};
|
};
|
||||||
|
template<typename RequestHandler>
|
||||||
|
void Session<RequestHandler>::HandleRequest(SessionBase::HttpRequest&& request)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
} // namespace http_server
|
} // namespace http_server
|
@ -16,10 +16,37 @@ SessionBase::SessionBase(tcp::socket&& socket)
|
|||||||
}
|
}
|
||||||
void SessionBase::Run()
|
void SessionBase::Run()
|
||||||
{
|
{
|
||||||
|
net::dispatch(stream_.get_executor(),
|
||||||
|
beast::bind_front_handler(&SessionBase::Read,
|
||||||
|
GetSharedThis()));
|
||||||
}
|
}
|
||||||
void SessionBase::Read()
|
void SessionBase::Read()
|
||||||
{
|
{
|
||||||
|
using namespace std::literals;
|
||||||
|
request_ = {};
|
||||||
|
stream_.expires_after(30s);
|
||||||
|
http::async_read(stream_, buffer_, request_,
|
||||||
|
beast::bind_front_handler(&SessionBase::OnRead, GetSharedThis()));
|
||||||
}
|
}
|
||||||
|
void SessionBase::OnRead(beast::error_code ec, size_t bytes_read)
|
||||||
|
{
|
||||||
|
if (ec == http::error::end_of_stream)
|
||||||
|
{
|
||||||
|
return Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ec)
|
||||||
|
{
|
||||||
|
return ReportError(ec, "read"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
HandleRequest(std::move(request_));
|
||||||
|
}
|
||||||
|
void SessionBase::Close()
|
||||||
|
{
|
||||||
|
beast::error_code ec;
|
||||||
|
|
||||||
|
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace http_server
|
} // namespace http_server
|
||||||
|
@ -44,5 +44,11 @@ class SessionBase {
|
|||||||
beast::tcp_stream stream_;
|
beast::tcp_stream stream_;
|
||||||
beast::flat_buffer buffer_;
|
beast::flat_buffer buffer_;
|
||||||
HttpRequest request_;
|
HttpRequest request_;
|
||||||
|
|
||||||
|
void OnRead(beast::error_code ec, [[maybe_unused]] size_t bytes_read);
|
||||||
|
|
||||||
|
void Close();
|
||||||
|
|
||||||
|
virtual void HandleRequest(HttpRequest&& request) = 0;
|
||||||
};
|
};
|
||||||
} // namespace http_server
|
} // namespace http_server
|
Loading…
x
Reference in New Issue
Block a user