Skip to content

Commit 556daba

Browse files
author
don-dron
committed
cc core: remove a lot of dynamic_cast in ConnectionBase
commit_hash:972d5f99f935f3b0101285bcd13187f53c560b56
1 parent 6e17aa7 commit 556daba

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

core/src/server/net/connection_base.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ USERVER_NAMESPACE_BEGIN
1010

1111
namespace server::net {
1212

13+
namespace {
14+
15+
int ResolveFd(engine::io::RwBase& socket) noexcept {
16+
if (auto* raw_socket = dynamic_cast<engine::io::Socket*>(&socket)) {
17+
return raw_socket->Fd();
18+
}
19+
if (auto* tls_socket = dynamic_cast<engine::io::TlsWrapper*>(&socket)) {
20+
return tls_socket->GetRawFd();
21+
}
22+
return -2;
23+
}
24+
25+
} // namespace
26+
1327
ConnectionBase::ConnectionBase(
1428
std::unique_ptr<engine::io::RwBase> socket,
1529
std::string peer_name,
@@ -20,6 +34,7 @@ ConnectionBase::ConnectionBase(
2034
: stats_(stats),
2135
reader_(config, std::move(peer_name)),
2236
socket_(std::move(socket)),
37+
fd_(socket_ ? ResolveFd(*socket_) : -2),
2338
config_(config),
2439
request_handler_(request_handler)
2540
{
@@ -73,19 +88,7 @@ void ConnectionBase::StopAcceptingRequests() noexcept { is_accepting_requests_ =
7388

7489
bool ConnectionBase::IsResponseChainValid() const noexcept { return is_response_chain_valid_; }
7590

76-
int ConnectionBase::GetFd() const {
77-
auto* socket = dynamic_cast<engine::io::Socket*>(socket_.get());
78-
if (socket) {
79-
return socket->Fd();
80-
}
81-
82-
auto* tls_socket = dynamic_cast<engine::io::TlsWrapper*>(socket_.get());
83-
if (tls_socket) {
84-
return tls_socket->GetRawFd();
85-
}
86-
87-
return -2;
88-
}
91+
int ConnectionBase::GetFd() const { return fd_; }
8992

9093
bool ConnectionBase::IsValid() const noexcept { return !!socket_; }
9194

core/src/server/net/connection_base.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ConnectionBase {
5252
Stats& stats_;
5353
SocketBufferedReader reader_;
5454
std::unique_ptr<engine::io::RwBase> socket_;
55+
const int fd_;
5556
const ConnectionConfig& config_;
5657
const http::RequestHandlerBase& request_handler_;
5758
bool is_accepting_requests_{true};

0 commit comments

Comments
 (0)