diff --git a/include/ascs/service_pump.h b/include/ascs/service_pump.h index cc3655662edd155692a087ca515c2c5321c4ea01..e1cb66d81917e33cec4eba792abc1921e49af35b 100644 --- a/include/ascs/service_pump.h +++ b/include/ascs/service_pump.h @@ -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; } diff --git a/include/ascs/socket.h b/include/ascs/socket.h index 6bb5623bcd7dce3d77d67f36d02768b22bb5df84..ef1c6e274024935495f84307efd56dcabcb9a24c 100644 --- a/include/ascs/socket.h +++ b/include/ascs/socket.h @@ -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 diff --git a/include/ascs/tcp/client_socket.h b/include/ascs/tcp/client_socket.h index 1cccd4ba608119b31765f14938f9e449414ab049..5dfc00a88225b7bb48704e44719760f44897c469 100644 --- a/include/ascs/tcp/client_socket.h +++ b/include/ascs/tcp/client_socket.h @@ -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: diff --git a/include/ascs/tcp/server_socket.h b/include/ascs/tcp/server_socket.h index 6e04b4e40797b061880380ca9216fcaa9974de34..042e1aab2eb88aa6a2fdabfab628e2632fd8a241 100644 --- a/include/ascs/tcp/server_socket.h +++ b/include/ascs/tcp/server_socket.h @@ -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 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: diff --git a/include/ascs/udp/socket.h b/include/ascs/udp/socket.h index 5dfa4972e68a8fbef00a4f2992504a14376d873e..16812bb38ff476288d86c811da01ae8ecedf9044 100644 --- a/include/ascs/udp/socket.h +++ b/include/ascs/udp/socket.h @@ -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;