Boost logo

Boost-Commit :

From: chris_at_[hidden]
Date: 2007-08-20 10:19:49


Author: chris_kohlhoff
Date: 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
New Revision: 38789
URL: http://svn.boost.org/trac/boost/changeset/38789

Log:
Use shutdown() for portable graceful connection closure.

Text files modified:
   trunk/libs/asio/example/http/server/connection.cpp | 6 ++++++
   trunk/libs/asio/example/http/server2/connection.cpp | 24 +++++++++++++-----------
   trunk/libs/asio/example/http/server2/connection.hpp | 3 ---
   trunk/libs/asio/example/http/server3/connection.cpp | 24 +++++++++++++-----------
   trunk/libs/asio/example/http/server3/connection.hpp | 3 ---
   5 files changed, 32 insertions(+), 28 deletions(-)

Modified: trunk/libs/asio/example/http/server/connection.cpp
==============================================================================
--- trunk/libs/asio/example/http/server/connection.cpp (original)
+++ trunk/libs/asio/example/http/server/connection.cpp 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
@@ -82,6 +82,12 @@
 
 void connection::handle_write(const boost::system::error_code& e)
 {
+ if (!e)
+ {
+ // Initiate graceful connection closure.
+ socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
+ }
+
   if (e != boost::asio::error::operation_aborted)
   {
     connection_manager_.stop(shared_from_this());

Modified: trunk/libs/asio/example/http/server2/connection.cpp
==============================================================================
--- trunk/libs/asio/example/http/server2/connection.cpp (original)
+++ trunk/libs/asio/example/http/server2/connection.cpp 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
@@ -36,11 +36,6 @@
         boost::asio::placeholders::bytes_transferred));
 }
 
-void connection::stop()
-{
- socket_.close();
-}
-
 void connection::handle_read(const boost::system::error_code& e,
     std::size_t bytes_transferred)
 {
@@ -72,18 +67,25 @@
             boost::asio::placeholders::bytes_transferred));
     }
   }
- else if (e != boost::asio::error::operation_aborted)
- {
- stop();
- }
+
+ // If an error occurs then no new asynchronous operations are started. This
+ // means that all shared_ptr references to the connection object will
+ // disappear and the object will be destroyed automatically after this
+ // handler returns. The connection class's destructor closes the socket.
 }
 
 void connection::handle_write(const boost::system::error_code& e)
 {
- if (e != boost::asio::error::operation_aborted)
+ if (!e)
   {
- stop();
+ // Initiate graceful connection closure.
+ socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
   }
+
+ // No new asynchronous operations are started. This means that all shared_ptr
+ // references to the connection object will disappear and the object will be
+ // destroyed automatically after this handler returns. The connection class's
+ // destructor closes the socket.
 }
 
 } // namespace server2

Modified: trunk/libs/asio/example/http/server2/connection.hpp
==============================================================================
--- trunk/libs/asio/example/http/server2/connection.hpp (original)
+++ trunk/libs/asio/example/http/server2/connection.hpp 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
@@ -40,9 +40,6 @@
   /// Start the first asynchronous operation for the connection.
   void start();
 
- /// Stop all asynchronous operations associated with the connection.
- void stop();
-
 private:
   /// Handle completion of a read operation.
   void handle_read(const boost::system::error_code& e,

Modified: trunk/libs/asio/example/http/server3/connection.cpp
==============================================================================
--- trunk/libs/asio/example/http/server3/connection.cpp (original)
+++ trunk/libs/asio/example/http/server3/connection.cpp 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
@@ -38,11 +38,6 @@
           boost::asio::placeholders::bytes_transferred)));
 }
 
-void connection::stop()
-{
- socket_.close();
-}
-
 void connection::handle_read(const boost::system::error_code& e,
     std::size_t bytes_transferred)
 {
@@ -77,18 +72,25 @@
               boost::asio::placeholders::bytes_transferred)));
     }
   }
- else if (e != boost::asio::error::operation_aborted)
- {
- stop();
- }
+
+ // If an error occurs then no new asynchronous operations are started. This
+ // means that all shared_ptr references to the connection object will
+ // disappear and the object will be destroyed automatically after this
+ // handler returns. The connection class's destructor closes the socket.
 }
 
 void connection::handle_write(const boost::system::error_code& e)
 {
- if (e != boost::asio::error::operation_aborted)
+ if (!e)
   {
- stop();
+ // Initiate graceful connection closure.
+ socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
   }
+
+ // No new asynchronous operations are started. This means that all shared_ptr
+ // references to the connection object will disappear and the object will be
+ // destroyed automatically after this handler returns. The connection class's
+ // destructor closes the socket.
 }
 
 } // namespace server3

Modified: trunk/libs/asio/example/http/server3/connection.hpp
==============================================================================
--- trunk/libs/asio/example/http/server3/connection.hpp (original)
+++ trunk/libs/asio/example/http/server3/connection.hpp 2007-08-20 10:19:49 EDT (Mon, 20 Aug 2007)
@@ -40,9 +40,6 @@
   /// Start the first asynchronous operation for the connection.
   void start();
 
- /// Stop all asynchronous operations associated with the connection.
- void stop();
-
 private:
   /// Handle completion of a read operation.
   void handle_read(const boost::system::error_code& e,


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk