mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
fix close
This commit is contained in:
@@ -51,7 +51,6 @@ public:
|
|||||||
: socket_(std::move(socket)), conn_id_(conn_id), router_(router),
|
: socket_(std::move(socket)), conn_id_(conn_id), router_(router),
|
||||||
cross_ending_(cross_ending) {}
|
cross_ending_(cross_ending) {}
|
||||||
|
|
||||||
~rpc_connection() { close(); }
|
|
||||||
asio::awaitable<void> start() {
|
asio::awaitable<void> start() {
|
||||||
rest_rpc_header header;
|
rest_rpc_header header;
|
||||||
auto self = this->shared_from_this();
|
auto self = this->shared_from_this();
|
||||||
@@ -164,7 +163,7 @@ public:
|
|||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
socket_.shutdown(asio::socket_base::shutdown_both, ec);
|
socket_.shutdown(asio::socket_base::shutdown_both, ec);
|
||||||
socket_.close(ec);
|
socket_.close(ec);
|
||||||
REST_LOG_INFO << "close socket " << need_cb;
|
REST_LOG_INFO << "close connection, id " << conn_id_;
|
||||||
if (need_cb && quit_cb_) {
|
if (need_cb && quit_cb_) {
|
||||||
quit_cb_(conn_id_);
|
quit_cb_(conn_id_);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,8 +195,16 @@ private:
|
|||||||
auto mtx = weak.lock();
|
auto mtx = weak.lock();
|
||||||
if (mtx) {
|
if (mtx) {
|
||||||
std::scoped_lock lock(*mtx);
|
std::scoped_lock lock(*mtx);
|
||||||
if (!conns_.empty())
|
if (!conns_.empty()) {
|
||||||
conns_.erase(id);
|
std::erase_if(conns_, [id](const auto &pair) {
|
||||||
|
if (pair.first == id) {
|
||||||
|
pair.second->close(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -210,6 +210,26 @@ asio::awaitable<void> test_router() {
|
|||||||
|
|
||||||
TEST_CASE("test router") { sync_wait(get_global_executor(), 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") {
|
TEST_CASE("test context pool") {
|
||||||
io_context_pool pool(0);
|
io_context_pool pool(0);
|
||||||
CHECK(pool.size() == 1);
|
CHECK(pool.size() == 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user