Browse Source

[2903] make some more repeatedly-used variables class members.

this will be slightly more efficient as we can avoid construction overhead
per call.  also use asio::mutable_buffers_1() instead of asio::buffer();
the latter is a wrapper for the former, so the direct call should be
(in theory) more efficient.
JINMEI Tatuya 12 years ago
parent
commit
4933a1c628
2 changed files with 17 additions and 14 deletions
  1. 8 14
      src/lib/asiodns/sync_udp_server.cc
  2. 9 0
      src/lib/asiodns/sync_udp_server.h

+ 8 - 14
src/lib/asiodns/sync_udp_server.cc

@@ -42,7 +42,8 @@ SyncUDPServer::SyncUDPServer(asio::io_service& io_service, const int fd,
                              const int af, DNSLookup* lookup) :
     output_buffer_(new isc::util::OutputBuffer(0)),
     query_(new isc::dns::Message(isc::dns::Message::PARSE)),
-    udp_endpoint_(sender_), lookup_callback_(lookup), stopped_(false)
+    udp_endpoint_(sender_), lookup_callback_(lookup), stopped_(false),
+    recv_callback_(boost::bind(&SyncUDPServer::handleRead, this, _1, _2))
 {
     if (af != AF_INET && af != AF_INET6) {
         isc_throw(InvalidParameter, "Address family must be either AF_INET "
@@ -67,9 +68,8 @@ SyncUDPServer::SyncUDPServer(asio::io_service& io_service, const int fd,
 
 void
 SyncUDPServer::scheduleRead() {
-    socket_->async_receive_from(asio::buffer(data_, MAX_LENGTH), sender_,
-                                boost::bind(&SyncUDPServer::handleRead, this,
-                                            _1, _2));
+    socket_->async_receive_from(asio::mutable_buffers_1(data_, MAX_LENGTH),
+                                sender_, recv_callback_);
 }
 
 void
@@ -116,14 +116,12 @@ SyncUDPServer::handleRead(const asio::error_code& ec, const size_t length) {
 
     if (done_) {
         // Good, there's an answer.
-
-        asio::error_code ec;
         socket_->send_to(asio::const_buffers_1(output_buffer_->getData(),
                                                output_buffer_->getLength()),
-                         sender_, 0, ec);
-        if (ec) {
+                         sender_, 0, ecode_);
+        if (ecode_) {
             LOG_ERROR(logger, ASIODNS_UDP_SYNC_SEND_FAIL).
-                      arg(sender_.address().to_string()).arg(ec.message());
+                      arg(sender_.address().to_string()).arg(ecode_.message());
         }
     }
 
@@ -141,10 +139,6 @@ SyncUDPServer::operator()(asio::error_code, size_t) {
 /// Stop the UDPServer
 void
 SyncUDPServer::stop() {
-    // passing error_code to avoid getting exception; we simply ignore any
-    // error on close().
-    asio::error_code ec;
-
     /// Using close instead of cancel, because cancel
     /// will only cancel the asynchronized event already submitted
     /// to io service, the events post to io service after
@@ -153,7 +147,7 @@ SyncUDPServer::stop() {
     /// for it won't be scheduled by io service not matter it is
     /// submit to io service before or after close call. And we will
     /// get bad_descriptor error.
-    socket_->close(ec);
+    socket_->close(ecode_); // pass error_code just to avoid getting exception.
     stopped_ = true;
 }
 

+ 9 - 0
src/lib/asiodns/sync_udp_server.h

@@ -30,6 +30,7 @@
 #include <util/buffer.h>
 #include <exceptions/exceptions.h>
 
+#include <boost/function.hpp>
 #include <boost/noncopyable.hpp>
 #include <boost/scoped_ptr.hpp>
 
@@ -150,6 +151,14 @@ private:
     // This turns true when the server stops. Allows for not sending the
     // answer after we closed the socket.
     bool stopped_;
+    // Placeholder for error code object.  It will be passed to ASIO library
+    // to have it set in case of error.
+    asio::error_code ecode_;
+    // The callback functor for internal asynchronous read event.  This is
+    // stateless (and it will be copied in the ASIO library anyway), so
+    // can be const
+    const boost::function<void(const asio::error_code&, size_t)>
+    recv_callback_;
 
     // Auxiliary functions