|
@@ -54,8 +54,9 @@ struct UDPServer::Data {
|
|
|
*/
|
|
|
Data(io_service& io_service, const ip::address& addr, const uint16_t port,
|
|
|
SimpleCallback* checkin, DNSLookup* lookup, DNSAnswer* answer) :
|
|
|
- io_(io_service), done_(false), checkin_callback_(checkin),
|
|
|
- lookup_callback_(lookup), answer_callback_(answer)
|
|
|
+ io_(io_service), done_(false), stopped_by_hand_(false),
|
|
|
+ checkin_callback_(checkin),lookup_callback_(lookup),
|
|
|
+ answer_callback_(answer)
|
|
|
{
|
|
|
// We must use different instantiations for v4 and v6;
|
|
|
// otherwise ASIO will bind to both
|
|
@@ -77,6 +78,7 @@ struct UDPServer::Data {
|
|
|
*/
|
|
|
Data(const Data& other) :
|
|
|
io_(other.io_), socket_(other.socket_), done_(false),
|
|
|
+ stopped_by_hand_(false),
|
|
|
checkin_callback_(other.checkin_callback_),
|
|
|
lookup_callback_(other.lookup_callback_),
|
|
|
answer_callback_(other.answer_callback_)
|
|
@@ -140,6 +142,9 @@ struct UDPServer::Data {
|
|
|
size_t bytes_;
|
|
|
bool done_;
|
|
|
|
|
|
+ //whether user implicitly stop the server
|
|
|
+ bool stopped_by_hand_;
|
|
|
+
|
|
|
// Callback functions provided by the caller
|
|
|
const SimpleCallback* checkin_callback_;
|
|
|
const DNSLookup* lookup_callback_;
|
|
@@ -167,6 +172,12 @@ UDPServer::operator()(error_code ec, size_t length) {
|
|
|
/// a switch statement, inline variable declarations are not
|
|
|
/// permitted. Certain variables used below can be declared here.
|
|
|
|
|
|
+ /// if user stopped the server, we won't enter the coroutine body
|
|
|
+ /// just return
|
|
|
+ if (data_->stopped_by_hand_) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
CORO_REENTER (this) {
|
|
|
do {
|
|
|
/*
|
|
@@ -267,6 +278,13 @@ UDPServer::asyncLookup() {
|
|
|
data_->query_message_, data_->answer_message_, data_->respbuf_, this);
|
|
|
}
|
|
|
|
|
|
+/// Stop the UDPServer
|
|
|
+void
|
|
|
+UDPServer::stop() {
|
|
|
+ data_->stopped_by_hand_ = true;
|
|
|
+ data_->socket_->close();
|
|
|
+}
|
|
|
+
|
|
|
/// Post this coroutine on the ASIO service queue so that it will
|
|
|
/// resume processing where it left off. The 'done' parameter indicates
|
|
|
/// whether there is an answer to return to the client.
|