Parcourir la source

[5260] HttpConnection callbacks migrated to shared_ptr.

Marcin Siodelski il y a 8 ans
Parent
commit
4f57c8d2b0
2 fichiers modifiés avec 14 ajouts et 14 suppressions
  1. 14 8
      src/lib/http/connection.cc
  2. 0 6
      src/lib/http/connection.h

+ 14 - 8
src/lib/http/connection.cc

@@ -34,10 +34,6 @@ HttpConnection:: HttpConnection(asiolink::IOService& io_service,
     : request_timer_(io_service),
       request_timeout_(request_timeout),
       socket_(io_service),
-      socket_callback_(boost::bind(&HttpConnection::socketReadCallback, this,
-                                   _1, _2)),
-      socket_write_callback_(boost::bind(&HttpConnection::socketWriteCallback,
-                                         this, _1, _2)),
       acceptor_(acceptor),
       connection_pool_(connection_pool),
       response_creator_(response_creator),
@@ -72,7 +68,8 @@ HttpConnection::stopThisConnection() {
 void
 HttpConnection::asyncAccept() {
     HttpAcceptorCallback cb = boost::bind(&HttpConnection::acceptorCallback,
-                                          this, _1);
+                                          shared_from_this(),
+                                          boost::asio::placeholders::error);
     try {
         acceptor_.asyncAccept(socket_, cb);
 
@@ -86,8 +83,12 @@ void
 HttpConnection::doRead() {
     try {
         TCPEndpoint endpoint;
+        SocketCallback cb(boost::bind(&HttpConnection::socketReadCallback,
+                                      shared_from_this(),
+                                      boost::asio::placeholders::error,
+                                      boost::asio::placeholders::bytes_transferred));
         socket_.asyncReceive(static_cast<void*>(buf_.data()), buf_.size(),
-                             0, &endpoint, socket_callback_);
+                             0, &endpoint, cb);
 
     } catch (const std::exception& ex) {
         stopThisConnection();
@@ -98,9 +99,13 @@ void
 HttpConnection::doWrite() {
     try {
         if (!output_buf_.empty()) {
+            SocketCallback cb(boost::bind(&HttpConnection::socketWriteCallback,
+                                          shared_from_this(),
+                                          boost::asio::placeholders::error,
+                                          boost::asio::placeholders::bytes_transferred));
             socket_.asyncSend(output_buf_.data(),
                               output_buf_.length(),
-                              socket_write_callback_);
+                              cb);
         } else {
             stopThisConnection();
         }
@@ -133,7 +138,8 @@ HttpConnection::acceptorCallback(const boost::system::error_code& ec) {
                   HTTP_REQUEST_RECEIVE_START)
             .arg(getRemoteEndpointAddressAsText())
             .arg(static_cast<unsigned>(request_timeout_/1000));
-        request_timer_.setup(boost::bind(&HttpConnection::requestTimeoutCallback, this),
+        request_timer_.setup(boost::bind(&HttpConnection::requestTimeoutCallback,
+                                         shared_from_this()),
                              request_timeout_, IntervalTimer::ONE_SHOT);
         doRead();
     }

+ 0 - 6
src/lib/http/connection.h

@@ -187,12 +187,6 @@ private:
     /// @brief Socket used by this connection.
     asiolink::TCPSocket<SocketCallback> socket_;
 
-    /// @brief Callback invoked when data received over the socket.
-    SocketCallback socket_callback_;
-
-    /// @brief Callback invoked when data sent over the socket.
-    SocketCallback socket_write_callback_;
-
     /// @brief Reference to the TCP acceptor used to accept new connections.
     HttpAcceptor& acceptor_;