Skip to content

Commit 23c561f

Browse files
authored
Add check the client connection status (#2191)
1 parent 284d14b commit 23c561f

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

examples/helloworld/main.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <trantor/utils/Logger.h>
12
#ifdef _WIN32
23
#include <ws2tcpip.h>
34
#else
@@ -15,8 +16,10 @@ int main()
1516
// sent to Drogon
1617
app().registerHandler(
1718
"/",
18-
[](const HttpRequestPtr &,
19+
[](const HttpRequestPtr &request,
1920
std::function<void(const HttpResponsePtr &)> &&callback) {
21+
LOG_INFO << "connected:"
22+
<< (request->connected() ? "true" : "false");
2023
auto resp = HttpResponse::newHttpResponse();
2124
resp->setBody("Hello, World!");
2225
callback(resp);

lib/inc/drogon/HttpRequest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ class DROGON_EXPORT HttpRequest
504504
virtual void setContentTypeString(const char *typeString,
505505
size_t typeStringLength) = 0;
506506

507+
virtual bool connected() const noexcept = 0;
508+
507509
virtual ~HttpRequest()
508510
{
509511
}

lib/src/HttpRequestImpl.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ void HttpRequestImpl::swap(HttpRequestImpl &that) noexcept
600600
swap(streamFinishCb_, that.streamFinishCb_);
601601
swap(streamExceptionPtr_, that.streamExceptionPtr_);
602602
swap(startProcessing_, that.startProcessing_);
603+
swap(connPtr_, that.connPtr_);
603604
}
604605

605606
const char *HttpRequestImpl::versionString() const

lib/src/HttpRequestImpl.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include <trantor/utils/Logger.h>
2727
#include <trantor/utils/MsgBuffer.h>
2828
#include <trantor/utils/NonCopyable.h>
29+
#include <trantor/net/TcpConnection.h>
2930
#include <algorithm>
31+
#include <functional>
32+
#include <memory>
3033
#include <string>
31-
#include <thread>
34+
#include <future>
3235
#include <unordered_map>
3336
#include <assert.h>
3437
#include <stdio.h>
@@ -97,6 +100,7 @@ class HttpRequestImpl : public HttpRequest
97100
streamFinishCb_ = nullptr;
98101
streamExceptionPtr_ = nullptr;
99102
startProcessing_ = false;
103+
connPtr_.reset();
100104
}
101105

102106
trantor::EventLoop *getLoop()
@@ -326,6 +330,11 @@ class HttpRequestImpl : public HttpRequest
326330
peerCertificate_ = cert;
327331
}
328332

333+
void setConnectionPtr(const std::shared_ptr<trantor::TcpConnection> &ptr)
334+
{
335+
connPtr_ = ptr;
336+
}
337+
329338
void addHeader(const char *start, const char *colon, const char *end);
330339

331340
void removeHeader(std::string key) override
@@ -554,6 +563,15 @@ class HttpRequestImpl : public HttpRequest
554563
return keepAlive_;
555564
}
556565

566+
bool connected() const noexcept override
567+
{
568+
if (auto conn = connPtr_.lock())
569+
{
570+
return conn->connected();
571+
}
572+
return false;
573+
}
574+
557575
bool isOnSecureConnection() const noexcept override
558576
{
559577
return isOnSecureConnection_;
@@ -705,6 +723,7 @@ class HttpRequestImpl : public HttpRequest
705723
RequestStreamReaderPtr streamReaderPtr_;
706724
std::exception_ptr streamExceptionPtr_;
707725
bool startProcessing_{false};
726+
std::weak_ptr<trantor::TcpConnection> connPtr_;
708727

709728
protected:
710729
std::string content_;

lib/src/HttpServer.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf)
220220
req->setCreationDate(trantor::Date::date());
221221
req->setSecure(conn->isSSLConnection());
222222
req->setPeerCertificate(conn->peerCertificate());
223+
req->setConnectionPtr(conn);
223224
// TODO: maybe call onRequests() directly in stream mode
224225
requests.push_back(req);
225226
}

0 commit comments

Comments
 (0)