Unverified Commit e0c17cfb authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #706 from hyperxor/code_simplification_and_improvement_in_http

Code simplification + add pointer check
parents de643616 fe9ec3d9
......@@ -220,8 +220,6 @@ public:
other.timerFd = -1;
}
~Timeout() { disarm(); }
Timeout &operator=(Timeout &&other) {
handler = other.handler;
transport = other.transport;
......@@ -233,6 +231,8 @@ public:
return *this;
}
~Timeout();
template <typename Duration> void arm(Duration duration) {
Async::Promise<uint64_t> p([=](Async::Deferred<uint64_t> deferred) {
timerFd = TRY_RET(timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK));
......@@ -250,25 +250,15 @@ public:
armed = true;
}
void disarm() {
if (armed) {
transport->disarmTimer(timerFd);
}
}
void disarm();
bool isArmed() const { return armed; }
bool isArmed() const;
private:
Timeout(const Timeout &other)
: handler(other.handler), request(other.request),
transport(other.transport), armed(other.armed), timerFd(other.timerFd),
peer() {}
Timeout(Tcp::Transport *transport_, Handler *handler_, Request request_)
: handler(handler_), request(std::move(request_)), transport(transport_),
armed(false), timerFd(-1), peer() {}
Timeout(const Timeout &other) = default;
template <typename Ptr> void associatePeer(const Ptr &ptr) { peer = ptr; }
Timeout(Tcp::Transport *transport_, Handler *handler_, Request request_,
std::weak_ptr<Tcp::Peer> peer_);
void onTimeout(uint64_t numWakeup);
......@@ -371,7 +361,7 @@ public:
// have to define it ourself
ResponseWriter(ResponseWriter &&other);
ResponseWriter &operator=(ResponseWriter &&other);
ResponseWriter &operator=(ResponseWriter &&other) = default;
void setMime(const Mime::MediaType &mime);
......
......@@ -658,23 +658,13 @@ ResponseWriter::ResponseWriter(ResponseWriter &&other)
ResponseWriter::ResponseWriter(Tcp::Transport *transport, Request request,
Handler *handler, std::weak_ptr<Tcp::Peer> peer)
: response_(request.version()), peer_(peer), buf_(DefaultStreamSize),
transport_(transport), timeout_(transport, handler, std::move(request)) {
timeout_.associatePeer(peer);
}
transport_(transport),
timeout_(transport, handler, std::move(request), peer) {}
ResponseWriter::ResponseWriter(const ResponseWriter &other)
: response_(other.response_), peer_(other.peer_), buf_(DefaultStreamSize),
transport_(other.transport_), timeout_(other.timeout_) {}
ResponseWriter &ResponseWriter::operator=(ResponseWriter &&other) {
response_ = std::move(other.response_);
peer_ = std::move(other.peer_);
transport_ = other.transport_;
buf_ = std::move(other.buf_);
timeout_ = std::move(other.timeout_);
return *this;
}
void ResponseWriter::setMime(const Mime::MediaType &mime) {
auto ct = response_.headers().tryGet<Header::ContentType>();
if (ct) {
......@@ -950,11 +940,25 @@ void Handler::onConnection(const std::shared_ptr<Tcp::Peer> &peer) {
void Handler::onDisconnection(const std::shared_ptr<Tcp::Peer> & /*peer*/) {}
void Handler::onTimeout(const Request &request, ResponseWriter response) {
UNUSED(request)
UNUSED(response)
void Handler::onTimeout(const Request& /*request*/, ResponseWriter /*response*/) {}
Timeout::~Timeout() {
disarm();
}
void Timeout::disarm() {
if (transport && armed) {
transport->disarmTimer(timerFd);
}
}
bool Timeout::isArmed() const { return armed; }
Timeout::Timeout(Tcp::Transport *transport_, Handler *handler_,
Request request_, std::weak_ptr<Tcp::Peer> peer_)
: handler(handler_), request(std::move(request_)), transport(transport_),
armed(false), timerFd(-1), peer(peer_) {}
void Timeout::onTimeout(uint64_t numWakeup) {
UNUSED(numWakeup)
if (!peer.lock())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment