Skip to content

Commit 8ca21f1

Browse files
authored
Add address() method to the TcpServer class (#99)
1 parent cdf853a commit 8ca21f1

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

trantor/net/TcpServer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ const std::string TcpServer::ipPort() const
179179
return acceptorPtr_->addr().toIpPort();
180180
}
181181

182+
const trantor::InetAddress &TcpServer::address() const
183+
{
184+
return acceptorPtr_->addr();
185+
}
186+
182187
void TcpServer::enableSSL(const std::string &certPath,
183188
const std::string &keyPath)
184189
{

trantor/net/TcpServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class TcpServer : NonCopyable
8383
return serverName_;
8484
}
8585
const std::string ipPort() const;
86+
const trantor::InetAddress &address() const;
8687
EventLoop *getLoop() const
8788
{
8889
return loop_;

trantor/net/inner/Acceptor.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Acceptor::Acceptor(EventLoop *loop,
3838
sock_.setReusePort(reUsePort);
3939
sock_.bindAddress(addr_);
4040
acceptChannel_.setReadCallback(std::bind(&Acceptor::readCallback, this));
41+
if (addr_.toPort() == 0)
42+
{
43+
addr_ = InetAddress{Socket::getLocalAddr(sock_.fd())};
44+
}
4145
}
4246
Acceptor::~Acceptor()
4347
{

0 commit comments

Comments
 (0)