Skip to content

Commit 0415747

Browse files
authored
Add stop() method to the TcpServer class (#79)
1 parent f38eba7 commit 0415747

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

trantor/net/TcpServer.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TcpServer::TcpServer(EventLoop *loop,
4141

4242
TcpServer::~TcpServer()
4343
{
44-
loop_->assertInLoopThread();
44+
// loop_->assertInLoopThread();
4545
LOG_TRACE << "TcpServer::~TcpServer [" << serverName_ << "] destructing";
4646
}
4747

@@ -142,7 +142,20 @@ void TcpServer::start()
142142
acceptorPtr_->listen();
143143
});
144144
}
145-
145+
void TcpServer::stop()
146+
{
147+
loop_->runInLoop([this]() { acceptorPtr_.reset(); });
148+
for (auto connection : connSet_)
149+
{
150+
connection->forceClose();
151+
}
152+
loopPoolPtr_.reset();
153+
for (auto iter : timingWheelMap_)
154+
{
155+
iter.second.reset();
156+
}
157+
timingWheelMap_.clear();
158+
}
146159
void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr)
147160
{
148161
LOG_TRACE << "connectionClosed";

trantor/net/TcpServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TcpServer : NonCopyable
3939
bool reUsePort = true);
4040
~TcpServer();
4141
void start();
42+
void stop();
4243
void setIoLoopNum(size_t num)
4344
{
4445
assert(!started_);

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ void TcpConnectionImpl::extendLife()
383383
auto entry = kickoffEntry_.lock();
384384
if (entry)
385385
{
386-
timingWheelPtr_->insertEntry(idleTimeout_, entry);
386+
auto timingWheelPtr = timingWheelWeakPtr_.lock();
387+
if (timingWheelPtr)
388+
timingWheelPtr->insertEntry(idleTimeout_, entry);
387389
}
388390
}
389391
}

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class TcpConnectionImpl : public TcpConnection,
179179
/// Internal use only.
180180

181181
std::weak_ptr<KickoffEntry> kickoffEntry_;
182-
std::shared_ptr<TimingWheel> timingWheelPtr_;
182+
std::weak_ptr<TimingWheel> timingWheelWeakPtr_;
183183
size_t idleTimeout_{0};
184184
Date lastTimingWheelUpdateTime_;
185185

@@ -191,9 +191,9 @@ class TcpConnectionImpl : public TcpConnection,
191191
assert(timeout > 0);
192192
auto entry = std::make_shared<KickoffEntry>(shared_from_this());
193193
kickoffEntry_ = entry;
194-
timingWheelPtr_ = timingWheel;
194+
timingWheelWeakPtr_ = timingWheel;
195195
idleTimeout_ = timeout;
196-
timingWheelPtr_->insertEntry(timeout, entry);
196+
timingWheel->insertEntry(timeout, entry);
197197
}
198198
void extendLife();
199199
#ifndef _WIN32

0 commit comments

Comments
 (0)