提交 7e54a267 编写于 作者: Y youngwolf

Extract function start_listen from current implementations.

上级 f8ae6611
......@@ -99,11 +99,10 @@ public:
return socket_ptr ? (socket_ptr->force_shutdown(), true) : false;
}
bool send_msg(const std::string& name, const std::string& msg) {return send_msg(name, std::string(msg));}
bool send_msg(const std::string& name, std::string&& msg)
template<typename T> bool send_msg(const std::string& name, T&& msg)
{
auto socket_ptr = find(find_link(name));
return socket_ptr ? socket_ptr->send_msg(std::move(msg)) : false;
return socket_ptr ? send_msg(name, std::forward<T>(msg)) : false;
}
protected:
......
......@@ -47,8 +47,48 @@ public:
}
const asio::ip::tcp::endpoint& get_server_addr() const {return server_addr;}
void stop_listen() {asio::error_code ec; acceptor.cancel(ec); acceptor.close(ec);}
bool start_listen()
{
asio::error_code ec;
if (!acceptor.is_open()) {acceptor.open(server_addr.protocol(), ec); assert(!ec);} //user maybe has opened this acceptor (to set options for example)
#ifndef ASCS_NOT_REUSE_ADDRESS
acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true), ec); assert(!ec);
#endif
acceptor.bind(server_addr, ec); assert(!ec);
if (ec) {unified_out::error_out("bind failed."); return false;}
auto num = async_accept_num();
assert(num > 0);
if (num <= 0)
num = 16;
std::list<typename Pool::object_type> sockets;
unified_out::info_out("begin to pre-create %d server socket...", num);
while (--num >= 0)
{
auto socket_ptr(create_object());
if (!socket_ptr)
break;
sockets.push_back(std::move(socket_ptr));
}
if (num >= 0)
unified_out::info_out("finished pre-creating server sockets, but failed %d time(s).", num + 1);
else
unified_out::info_out("finished pre-creating server sockets.");
#if ASIO_VERSION >= 101100
acceptor.listen(asio::ip::tcp::acceptor::max_listen_connections, ec); assert(!ec);
#else
acceptor.listen(asio::ip::tcp::acceptor::max_connections, ec); assert(!ec);
#endif
if (ec) {unified_out::error_out("listen failed."); return false;}
ascs::do_something_to_all(sockets, [this](typename Pool::object_ctype& item) {this->do_async_accept(item);});
return true;
}
bool is_listening() const {return acceptor.is_open();}
void stop_listen() {asio::error_code ec; acceptor.cancel(ec); acceptor.close(ec);}
asio::ip::tcp::acceptor& next_layer() {return acceptor;}
const asio::ip::tcp::acceptor& next_layer() const {return acceptor;}
......@@ -113,48 +153,7 @@ public:
protected:
virtual int async_accept_num() {return ASCS_ASYNC_ACCEPT_NUM;}
virtual bool init()
{
asio::error_code ec;
if (!acceptor.is_open()) {acceptor.open(server_addr.protocol(), ec); assert(!ec);} //user maybe has opened this acceptor (to set options for example)
#ifndef ASCS_NOT_REUSE_ADDRESS
acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true), ec); assert(!ec);
#endif
acceptor.bind(server_addr, ec); assert(!ec);
if (ec) {unified_out::error_out("bind failed."); return false;}
auto num = async_accept_num();
assert(num > 0);
if (num <= 0)
num = 16;
std::list<typename Pool::object_type> sockets;
unified_out::info_out("begin to pre-create %d server socket...", num);
while (--num >= 0)
{
auto socket_ptr(create_object());
if (!socket_ptr)
break;
sockets.push_back(std::move(socket_ptr));
}
if (num >= 0)
unified_out::info_out("finished pre-creating server sockets, but failed %d time(s).", num + 1);
else
unified_out::info_out("finished pre-creating server sockets.");
#if ASIO_VERSION >= 101100
acceptor.listen(asio::ip::tcp::acceptor::max_listen_connections, ec); assert(!ec);
#else
acceptor.listen(asio::ip::tcp::acceptor::max_connections, ec); assert(!ec);
#endif
if (ec) {unified_out::error_out("listen failed."); return false;}
ascs::do_something_to_all(sockets, [this](typename Pool::object_ctype& item) {this->do_async_accept(item);});
this->start();
return true;
}
virtual bool init() {return start_listen() ? (this->start(), true) : false;}
virtual void uninit() {this->stop(); stop_listen(); force_shutdown();} //if you wanna graceful shutdown, call graceful_shutdown before service_pump::stop_service invocation.
virtual bool on_accept(typename Pool::object_ctype& socket_ptr) {return true;}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册