fix close

This commit is contained in:
qicosmos
2025-10-09 16:48:40 +08:00
parent 09e5f1b708
commit c26fcfe033
3 changed files with 31 additions and 4 deletions
+1 -2
View File
@@ -51,7 +51,6 @@ public:
: socket_(std::move(socket)), conn_id_(conn_id), router_(router),
cross_ending_(cross_ending) {}
~rpc_connection() { close(); }
asio::awaitable<void> start() {
rest_rpc_header header;
auto self = this->shared_from_this();
@@ -164,7 +163,7 @@ public:
std::error_code ec;
socket_.shutdown(asio::socket_base::shutdown_both, ec);
socket_.close(ec);
REST_LOG_INFO << "close socket " << need_cb;
REST_LOG_INFO << "close connection, id " << conn_id_;
if (need_cb && quit_cb_) {
quit_cb_(conn_id_);
}
+10 -2
View File
@@ -195,8 +195,16 @@ private:
auto mtx = weak.lock();
if (mtx) {
std::scoped_lock lock(*mtx);
if (!conns_.empty())
conns_.erase(id);
if (!conns_.empty()) {
std::erase_if(conns_, [id](const auto &pair) {
if (pair.first == id) {
pair.second->close(false);
return true;
}
return false;
});
}
}
});
+20
View File
@@ -210,6 +210,26 @@ asio::awaitable<void> test_router() {
TEST_CASE("test router") { sync_wait(get_global_executor(), test_router()); }
TEST_CASE("test rpc_connection") {
uint64_t conn_id = 999;
size_t num_thread = 4;
asio::io_context io_ctx;
tcp_socket socket(io_ctx);
bool cross_ending_ = false;
rpc_router router;
dummy d{};
router.register_handler<&dummy::add>(&d);
auto conn = std::make_shared<rpc_connection>(std::move(socket), conn_id,
router, cross_ending_);
CHECK_EQ(conn->id(), conn_id);
conn->set_check_timeout(true);
conn->set_last_time();
auto last_rw_time = conn->get_last_rwtime();
std::cout << "topic_id: " << conn->topic_id() << std::endl;
conn->close();
io_ctx.run();
}
TEST_CASE("test context pool") {
io_context_pool pool(0);
CHECK(pool.size() == 1);