提交 ce58c0c7 编写于 作者: Y youngwolf

A small refactoring based on commit ec7877.

上级 160c48ba
......@@ -131,7 +131,7 @@ public:
#if ASIO_VERSION > 101100
asio::io_context::executor_type get_executor() {return assign_io_context().get_executor();}
#endif
asio::io_context& assign_io_context() //pick the context which has the least references
asio::io_context& assign_io_context(bool increase_ref = true) //pick the context which has the least references
{
if (single_io_context)
return context_can.front().io_context;
......@@ -152,7 +152,9 @@ public:
if (nullptr != ctx)
{
++ctx->refs;
if (increase_ref)
++ctx->refs;
return ctx->io_context;
}
......
......@@ -336,8 +336,9 @@ protected:
// include user timers(created by set_timer()) and user async calls(started via post(), dispatch() or defer()), this means you can clean up any resource
// in this socket except this socket itself, because this socket maybe is being maintained by object_pool.
//otherwise (bigger than zero), socket simply call this callback ASCS_DELAY_CLOSE seconds later after link down, no any guarantees.
//if you overwrote this callback, do not forget to call parent class' on_close at the end.
virtual void on_close() {unified_out::info_out(ASCS_LLF " on_close()", id());}
virtual void after_close() = 0; //a good case for using this is to reconnect the server (please refer to client_socket_base) and return io_context to service_pump.
virtual void after_close() {} //a good case for using this is to reconnect the server, please refer to client_socket_base.
#ifdef ASCS_SYNC_DISPATCH
//return positive value if handled some messages (include all messages), if some msg left behind, socket will re-dispatch them asynchronously
......
......@@ -169,21 +169,22 @@ protected:
return false;
}
//reconnect at here rather than in on_recv_error to make sure no async invocations performed on this socket before reconnecting.
//if you don't want to reconnect the server after link broken, rewrite this virtual function and do nothing in it or call close_reconnt().
//if you want to control the retry times and delay time after reconnecting failed, rewrite prepare_reconnect virtual function.
virtual void after_close()
virtual void on_close()
{
if (need_reconnect)
this->start();
else if (nullptr != matrix)
if (!need_reconnect && nullptr != matrix)
#if ASIO_VERSION < 101100
matrix->get_service_pump().return_io_context(this->next_layer().get_io_service());
#else
matrix->get_service_pump().return_io_context(this->next_layer().get_executor().context());
#endif
super::on_close();
}
//reconnect at here rather than in on_recv_error to make sure no async invocations performed on this socket before reconnecting.
//if you don't want to reconnect the server after link broken, rewrite this virtual function and do nothing in it or call close_reconnt().
//if you want to control the retry times and delay time after reconnecting failed, rewrite prepare_reconnect virtual function.
virtual void after_close() {if (need_reconnect) this->start();}
virtual bool bind() {return true;}
private:
......
......@@ -33,15 +33,11 @@ public:
virtual const char* type_name() const {return "TCP (server endpoint)";}
virtual int type_id() const {return 2;}
virtual void reset()
{
#if ASIO_VERSION < 101100
server.get_service_pump().assign_io_context(this->lowest_layer().get_io_service());
virtual void reset() {server.get_service_pump().assign_io_context(this->lowest_layer().get_io_service()); super::reset();}
#else
server.get_service_pump().assign_io_context(this->lowest_layer().get_executor().context());
virtual void reset() {server.get_service_pump().assign_io_context(this->lowest_layer().get_executor().context()); super::reset();}
#endif
super::reset();
}
virtual void take_over(std::shared_ptr<generic_server_socket> socket_ptr) {} //restore this socket from socket_ptr
void disconnect() {force_shutdown();}
......@@ -88,9 +84,9 @@ protected:
virtual void on_async_shutdown_error() {force_shutdown();}
virtual bool on_heartbeat_error() {this->show_info("server link:", "broke unexpectedly."); force_shutdown(); return false;}
#if ASIO_VERSION < 101100
virtual void after_close() {server.get_service_pump().return_io_context(this->lowest_layer().get_io_service());}
virtual void on_close() {server.get_service_pump().return_io_context(this->lowest_layer().get_io_service()); super::on_close();}
#else
virtual void after_close() {server.get_service_pump().return_io_context(this->lowest_layer().get_executor().context());}
virtual void on_close() {server.get_service_pump().return_io_context(this->lowest_layer().get_executor().context()); super::on_close();}
#endif
private:
......
......@@ -233,14 +233,20 @@ protected:
return true;
}
virtual void on_close()
{
#ifdef ASCS_SYNC_SEND
virtual void on_close() {if (sending_msg.p) sending_msg.p->set_value(sync_call_result::NOT_APPLICABLE); super::on_close();}
if (sending_msg.p)
sending_msg.p->set_value(sync_call_result::NOT_APPLICABLE);
#endif
if (nullptr != matrix)
#if ASIO_VERSION < 101100
virtual void after_close() {if (nullptr != matrix) matrix->get_service_pump().return_io_context(this->lowest_layer().get_io_service());}
matrix->get_service_pump().return_io_context(this->lowest_layer().get_io_service());
#else
virtual void after_close() {if (nullptr != matrix) matrix->get_service_pump().return_io_context(this->lowest_layer().get_executor().context());};
matrix->get_service_pump().return_io_context(this->lowest_layer().get_executor().context());
#endif
super::on_close();
}
private:
using super::close;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册