mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-11-29 18:27:43 +08:00
Add check the client connection status (#2191)
This commit is contained in:
parent
284d14b8ca
commit
23c561f072
@ -1,3 +1,4 @@
|
|||||||
|
#include <trantor/utils/Logger.h>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#else
|
#else
|
||||||
@ -15,8 +16,10 @@ int main()
|
|||||||
// sent to Drogon
|
// sent to Drogon
|
||||||
app().registerHandler(
|
app().registerHandler(
|
||||||
"/",
|
"/",
|
||||||
[](const HttpRequestPtr &,
|
[](const HttpRequestPtr &request,
|
||||||
std::function<void(const HttpResponsePtr &)> &&callback) {
|
std::function<void(const HttpResponsePtr &)> &&callback) {
|
||||||
|
LOG_INFO << "connected:"
|
||||||
|
<< (request->connected() ? "true" : "false");
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setBody("Hello, World!");
|
resp->setBody("Hello, World!");
|
||||||
callback(resp);
|
callback(resp);
|
||||||
|
@ -504,6 +504,8 @@ class DROGON_EXPORT HttpRequest
|
|||||||
virtual void setContentTypeString(const char *typeString,
|
virtual void setContentTypeString(const char *typeString,
|
||||||
size_t typeStringLength) = 0;
|
size_t typeStringLength) = 0;
|
||||||
|
|
||||||
|
virtual bool connected() const noexcept = 0;
|
||||||
|
|
||||||
virtual ~HttpRequest()
|
virtual ~HttpRequest()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -600,6 +600,7 @@ void HttpRequestImpl::swap(HttpRequestImpl &that) noexcept
|
|||||||
swap(streamFinishCb_, that.streamFinishCb_);
|
swap(streamFinishCb_, that.streamFinishCb_);
|
||||||
swap(streamExceptionPtr_, that.streamExceptionPtr_);
|
swap(streamExceptionPtr_, that.streamExceptionPtr_);
|
||||||
swap(startProcessing_, that.startProcessing_);
|
swap(startProcessing_, that.startProcessing_);
|
||||||
|
swap(connPtr_, that.connPtr_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *HttpRequestImpl::versionString() const
|
const char *HttpRequestImpl::versionString() const
|
||||||
|
@ -26,9 +26,12 @@
|
|||||||
#include <trantor/utils/Logger.h>
|
#include <trantor/utils/Logger.h>
|
||||||
#include <trantor/utils/MsgBuffer.h>
|
#include <trantor/utils/MsgBuffer.h>
|
||||||
#include <trantor/utils/NonCopyable.h>
|
#include <trantor/utils/NonCopyable.h>
|
||||||
|
#include <trantor/net/TcpConnection.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <future>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -97,6 +100,7 @@ class HttpRequestImpl : public HttpRequest
|
|||||||
streamFinishCb_ = nullptr;
|
streamFinishCb_ = nullptr;
|
||||||
streamExceptionPtr_ = nullptr;
|
streamExceptionPtr_ = nullptr;
|
||||||
startProcessing_ = false;
|
startProcessing_ = false;
|
||||||
|
connPtr_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
trantor::EventLoop *getLoop()
|
trantor::EventLoop *getLoop()
|
||||||
@ -326,6 +330,11 @@ class HttpRequestImpl : public HttpRequest
|
|||||||
peerCertificate_ = cert;
|
peerCertificate_ = cert;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setConnectionPtr(const std::shared_ptr<trantor::TcpConnection> &ptr)
|
||||||
|
{
|
||||||
|
connPtr_ = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
void addHeader(const char *start, const char *colon, const char *end);
|
void addHeader(const char *start, const char *colon, const char *end);
|
||||||
|
|
||||||
void removeHeader(std::string key) override
|
void removeHeader(std::string key) override
|
||||||
@ -554,6 +563,15 @@ class HttpRequestImpl : public HttpRequest
|
|||||||
return keepAlive_;
|
return keepAlive_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool connected() const noexcept override
|
||||||
|
{
|
||||||
|
if (auto conn = connPtr_.lock())
|
||||||
|
{
|
||||||
|
return conn->connected();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool isOnSecureConnection() const noexcept override
|
bool isOnSecureConnection() const noexcept override
|
||||||
{
|
{
|
||||||
return isOnSecureConnection_;
|
return isOnSecureConnection_;
|
||||||
@ -705,6 +723,7 @@ class HttpRequestImpl : public HttpRequest
|
|||||||
RequestStreamReaderPtr streamReaderPtr_;
|
RequestStreamReaderPtr streamReaderPtr_;
|
||||||
std::exception_ptr streamExceptionPtr_;
|
std::exception_ptr streamExceptionPtr_;
|
||||||
bool startProcessing_{false};
|
bool startProcessing_{false};
|
||||||
|
std::weak_ptr<trantor::TcpConnection> connPtr_;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string content_;
|
std::string content_;
|
||||||
|
@ -220,6 +220,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf)
|
|||||||
req->setCreationDate(trantor::Date::date());
|
req->setCreationDate(trantor::Date::date());
|
||||||
req->setSecure(conn->isSSLConnection());
|
req->setSecure(conn->isSSLConnection());
|
||||||
req->setPeerCertificate(conn->peerCertificate());
|
req->setPeerCertificate(conn->peerCertificate());
|
||||||
|
req->setConnectionPtr(conn);
|
||||||
// TODO: maybe call onRequests() directly in stream mode
|
// TODO: maybe call onRequests() directly in stream mode
|
||||||
requests.push_back(req);
|
requests.push_back(req);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user