提交 71a7df67 编写于 作者: Y youngwolf

client socket and udp socket support deleting itself from object_pool (via i_Matrix).

上级 3700fad9
......@@ -3,9 +3,6 @@
//configuration
#define ASCS_SERVER_PORT 9527
#define ASCS_DELAY_CLOSE 1 //this demo not used object pool and doesn't need life cycle management,
//so, define this to avoid hooks for async call (and slightly improve efficiency),
//any value which is bigger than zero is okay.
#define ASCS_SYNC_SEND
#define ASCS_SYNC_RECV
//#define ASCS_PASSIVE_RECV //because we not defined this macro, this demo will use mix model to receive messages, which means
......
......@@ -73,6 +73,7 @@ public:
virtual bool socket_exist(uint_fast64_t id) = 0;
virtual std::shared_ptr<tracked_executor> find_socket(uint_fast64_t id) = 0;
virtual bool del_socket(uint_fast64_t id) = 0;
};
namespace tcp
......@@ -80,7 +81,7 @@ namespace tcp
class i_server : public i_matrix
{
public:
virtual bool del_socket(const std::shared_ptr<tracked_executor>& socket_ptr) = 0;
virtual bool del_socket(const std::shared_ptr<tracked_executor>& socket_ptr) = 0; //hide i_matrix::del_socket
virtual bool restore_socket(const std::shared_ptr<tracked_executor>& socket_ptr, uint_fast64_t id, bool init) = 0;
};
} //namespace
......
......@@ -83,6 +83,28 @@ protected:
return exist;
}
bool del_object(uint_fast64_t id)
{
auto object_ptr = object_type();
std::unique_lock<ASCS_SHARED_MUTEX_TYPE> lock(object_can_mutex);
auto iter = object_can.find(id);
if (iter != std::end(object_can))
{
object_ptr = iter->second;
object_can.erase(iter);
}
lock.unlock();
if (object_ptr)
{
std::lock_guard<std::mutex> lock(invalid_object_can_mutex);
try {invalid_object_can.emplace_back(object_ptr);} catch (const std::exception& e) {unified_out::error_out("cannot hold more objects (%s)", e.what());}
}
return !!object_ptr;
}
//you can do some statistic about object creations at here
virtual void on_create(object_ctype& object_ptr) {}
......
......@@ -55,8 +55,10 @@ public:
virtual bool started() const {return this->service_started();}
virtual service_pump& get_service_pump() {return Pool::get_service_pump();}
virtual const service_pump& get_service_pump() const {return Pool::get_service_pump();}
virtual bool socket_exist(uint_fast64_t id) {return this->exist(id);}
virtual std::shared_ptr<tracked_executor> find_socket(uint_fast64_t id) {return this->find(id);}
virtual bool del_socket(uint_fast64_t id) {return this->del_object(id);}
typename Pool::object_type create_object() {return Pool::create_object(*this);}
template<typename Arg> typename Pool::object_type create_object(Arg&& arg) {return Pool::create_object(*this, std::forward<Arg>(arg));}
......
......@@ -143,6 +143,11 @@ protected:
force_shutdown(need_reconnect);
this->status = super::link_status::BROKEN;
#ifndef ASCS_CLEAR_OBJECT_INTERVAL
if (!need_reconnect && nullptr != matrix)
matrix->del_socket(this->id());
#endif
}
virtual void on_async_shutdown_error() {force_shutdown(need_reconnect);}
......
......@@ -102,8 +102,10 @@ public:
virtual bool started() const {return this->service_started();}
virtual service_pump& get_service_pump() {return Pool::get_service_pump();}
virtual const service_pump& get_service_pump() const {return Pool::get_service_pump();}
virtual bool socket_exist(uint_fast64_t id) {return this->exist(id);}
virtual std::shared_ptr<tracked_executor> find_socket(uint_fast64_t id) {return this->find(id);}
virtual bool del_socket(uint_fast64_t id) {unified_out::error_out("use bool del_socket(const std::shared_ptr<tracked_executor>&) please."); assert(false); return false;}
virtual bool del_socket(const std::shared_ptr<tracked_executor>& socket_ptr)
{
......
......@@ -204,6 +204,10 @@ protected:
{
if (asio::error::operation_aborted != ec)
unified_out::error_out(ASCS_LLF " recv msg error (%d %s)", this->id(), ec.value(), ec.message().data());
#ifndef ASCS_CLEAR_OBJECT_INTERVAL
else if (nullptr != matrix)
matrix->del_socket(this->id());
#endif
}
virtual bool on_heartbeat_error()
......
......@@ -58,7 +58,7 @@ public:
void graceful_shutdown() {this->do_something_to_all([](typename Pool::object_ctype& item) {item->graceful_shutdown();});}
protected:
virtual void uninit() {this->stop(); graceful_shutdown();}
virtual void uninit() {this->stop(); force_shutdown();}
};
}} //namespace
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册