mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
on_connection().
This commit is contained in:
parent
eab0802df2
commit
3b4a0ae80f
@ -13,6 +13,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <trantor/utils/AsyncFileLogger.h>
|
#include <trantor/utils/AsyncFileLogger.h>
|
||||||
|
#include <trantor/net/TcpConnection.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -276,8 +277,6 @@ void DWebApplication::run() {
|
|||||||
std::bind(&DWebApplication::on_connection, this, std::placeholders::_1),
|
std::bind(&DWebApplication::on_connection, this, std::placeholders::_1),
|
||||||
_idle_connection_timeout, _ssl_cert_path, _ssl_key_path, _ssl_conf_cmds, _thread_num, _sync_advices, _pre_sending_advices);
|
_idle_connection_timeout, _ssl_cert_path, _ssl_key_path, _ssl_conf_cmds, _thread_num, _sync_advices, _pre_sending_advices);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
assert(ioLoops.size() == _thread_num);
|
assert(ioLoops.size() == _thread_num);
|
||||||
|
|
||||||
for (size_t i = 0; i < _thread_num; ++i) {
|
for (size_t i = 0; i < _thread_num; ++i) {
|
||||||
@ -591,6 +590,8 @@ bool DWebApplication::reuse_port() const {
|
|||||||
|
|
||||||
void DWebApplication::on_async_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
|
void DWebApplication::on_async_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
|
||||||
|
|
||||||
|
LOG_INFO << "on_async_request";
|
||||||
|
|
||||||
LOG_TRACE << "new request:" << req->peerAddr().toIpPort() << "->"
|
LOG_TRACE << "new request:" << req->peerAddr().toIpPort() << "->"
|
||||||
<< req->localAddr().toIpPort();
|
<< req->localAddr().toIpPort();
|
||||||
LOG_TRACE << "Headers " << req->methodString() << " " << req->path();
|
LOG_TRACE << "Headers " << req->methodString() << " " << req->path();
|
||||||
@ -634,6 +635,8 @@ void DWebApplication::on_async_request(const HttpRequestImplPtr &req, std::funct
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DWebApplication::on_new_websock_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const WebSocketConnectionImplPtr &wsConnPtr) {
|
void DWebApplication::on_new_websock_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const WebSocketConnectionImplPtr &wsConnPtr) {
|
||||||
|
LOG_INFO << "on_new_websock_request";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
findSessionForRequest(req);
|
findSessionForRequest(req);
|
||||||
// Route to controller
|
// Route to controller
|
||||||
@ -664,56 +667,60 @@ void DWebApplication::on_new_websock_request(const HttpRequestImplPtr &req, std:
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
void DWebApplication::on_connection(const trantor::TcpConnectionPtr &conn) {
|
void DWebApplication::on_connection(const trantor::TcpConnectionPtr &conn) {
|
||||||
LOG_INFO << "on_connection";
|
|
||||||
|
|
||||||
/*
|
|
||||||
static std::mutex mtx;
|
static std::mutex mtx;
|
||||||
LOG_TRACE << "connect!!!" << maxConnectionNum_
|
LOG_TRACE << "connect!!!" << _max_connection_num
|
||||||
<< " num=" << connectionNum_.load();
|
<< " num=" << _connection_num.load();
|
||||||
|
|
||||||
if (conn->connected()) {
|
if (conn->connected()) {
|
||||||
if (connectionNum_.fetch_add(1, std::memory_order_relaxed) >=
|
if (_connection_num.fetch_add(1, std::memory_order_relaxed) >= _max_connection_num) {
|
||||||
maxConnectionNum_) {
|
|
||||||
LOG_ERROR << "too much connections!force close!";
|
LOG_ERROR << "too much connections!force close!";
|
||||||
conn->forceClose();
|
conn->forceClose();
|
||||||
return;
|
return;
|
||||||
} else if (maxConnectionNumPerIP_ > 0) {
|
} else if (_max_connection_num_per_ip > 0) {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
auto iter = connectionsNumMap_.find(conn->peerAddr().toIp());
|
auto iter = _connections_num_map.find(conn->peerAddr().toIp());
|
||||||
if (iter == connectionsNumMap_.end()) {
|
|
||||||
connectionsNumMap_[conn->peerAddr().toIp()] = 1;
|
if (iter == _connections_num_map.end()) {
|
||||||
} else if (iter->second++ > maxConnectionNumPerIP_) {
|
_connections_num_map[conn->peerAddr().toIp()] = 1;
|
||||||
conn->get_loop()->queueInLoop(
|
} else if (iter->second++ > _max_connection_num_per_ip) {
|
||||||
[conn]() { conn->forceClose(); });
|
conn->getLoop()->queueInLoop([conn]() { conn->forceClose(); });
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto &advice : newConnectionAdvices_) {
|
|
||||||
|
for (auto &advice : _new_connection_advices) {
|
||||||
if (!advice(conn->peerAddr(), conn->localAddr())) {
|
if (!advice(conn->peerAddr(), conn->localAddr())) {
|
||||||
conn->forceClose();
|
conn->forceClose();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (!conn->hasContext()) {
|
if (!conn->hasContext()) {
|
||||||
// If the connection is connected to the SSL port and then
|
// If the connection is connected to the SSL port and then
|
||||||
// disconnected before the SSL handshake.
|
// disconnected before the SSL handshake.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
connectionNum_.fetch_sub(1, std::memory_order_relaxed);
|
|
||||||
if (maxConnectionNumPerIP_ > 0) {
|
_connection_num.fetch_sub(1, std::memory_order_relaxed);
|
||||||
|
|
||||||
|
if (_max_connection_num_per_ip > 0) {
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
auto iter = connectionsNumMap_.find(conn->peerAddr().toIp());
|
|
||||||
if (iter != connectionsNumMap_.end()) {
|
auto iter = _connections_num_map.find(conn->peerAddr().toIp());
|
||||||
|
|
||||||
|
if (iter != _connections_num_map.end()) {
|
||||||
--iter->second;
|
--iter->second;
|
||||||
if (iter->second <= 0) {
|
if (iter->second <= 0) {
|
||||||
connectionsNumMap_.erase(iter);
|
_connections_num_map.erase(iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DWebApplication::find_session_for_request(const HttpRequestImplPtr &req) {
|
void DWebApplication::find_session_for_request(const HttpRequestImplPtr &req) {
|
||||||
|
@ -232,6 +232,7 @@ protected:
|
|||||||
trantor::EventLoop *_loop;
|
trantor::EventLoop *_loop;
|
||||||
|
|
||||||
std::vector<std::function<void()> > _beginning_advices;
|
std::vector<std::function<void()> > _beginning_advices;
|
||||||
|
std::vector<std::function<bool(const trantor::InetAddress &, const trantor::InetAddress &)> > _new_connection_advices;
|
||||||
std::vector<std::function<HttpResponsePtr(const HttpRequestPtr &)> > _sync_advices;
|
std::vector<std::function<HttpResponsePtr(const HttpRequestPtr &)> > _sync_advices;
|
||||||
std::vector<std::function<void(const HttpRequestPtr &, const HttpResponsePtr &)> > _pre_sending_advices;
|
std::vector<std::function<void(const HttpRequestPtr &, const HttpResponsePtr &)> > _pre_sending_advices;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user