|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r72142 - in trunk/libs/asio/example: allocation buffers chat echo fork http/server http/server2 http/server3 iostreams nonblocking porthopper serialization ssl timeouts tutorial/daytime3 tutorial/daytime7 windows
From: chris_at_[hidden]
Date: 2011-05-24 19:33:57
Author: chris_kohlhoff
Date: 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
New Revision: 72142
URL: http://svn.boost.org/trac/boost/changeset/72142
Log:
Treat accept failure as a non-fatal error in most examples.
Text files modified:
trunk/libs/asio/example/allocation/server.cpp | 9 +++++----
trunk/libs/asio/example/buffers/reference_counted.cpp | 9 +++++----
trunk/libs/asio/example/chat/chat_server.cpp | 11 +++++++----
trunk/libs/asio/example/echo/async_tcp_echo_server.cpp | 15 +++++++++------
trunk/libs/asio/example/fork/process_per_connection.cpp | 17 +++++++++++++----
trunk/libs/asio/example/http/server/server.cpp | 31 +++++++++++++++++++++----------
trunk/libs/asio/example/http/server/server.hpp | 3 +++
trunk/libs/asio/example/http/server2/server.cpp | 24 ++++++++++++++----------
trunk/libs/asio/example/http/server2/server.hpp | 3 +++
trunk/libs/asio/example/http/server3/server.cpp | 21 +++++++++++++--------
trunk/libs/asio/example/http/server3/server.hpp | 3 +++
trunk/libs/asio/example/iostreams/daytime_server.cpp | 8 ++++++--
trunk/libs/asio/example/nonblocking/third_party_lib.cpp | 3 ++-
trunk/libs/asio/example/porthopper/server.cpp | 12 ++++++------
trunk/libs/asio/example/serialization/server.cpp | 19 ++++++-------------
trunk/libs/asio/example/ssl/server.cpp | 19 +++++++++++--------
trunk/libs/asio/example/timeouts/server.cpp | 12 +++++++-----
trunk/libs/asio/example/tutorial/daytime3/server.cpp | 3 ++-
trunk/libs/asio/example/tutorial/daytime7/server.cpp | 3 ++-
trunk/libs/asio/example/windows/transmit_file.cpp | 3 ++-
20 files changed, 140 insertions(+), 88 deletions(-)
Modified: trunk/libs/asio/example/allocation/server.cpp
==============================================================================
--- trunk/libs/asio/example/allocation/server.cpp (original)
+++ trunk/libs/asio/example/allocation/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -199,11 +199,12 @@
if (!error)
{
new_session->start();
- new_session.reset(new session(io_service_));
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
}
+
+ new_session.reset(new session(io_service_));
+ acceptor_.async_accept(new_session->socket(),
+ boost::bind(&server::handle_accept, this, new_session,
+ boost::asio::placeholders::error));
}
private:
Modified: trunk/libs/asio/example/buffers/reference_counted.cpp
==============================================================================
--- trunk/libs/asio/example/buffers/reference_counted.cpp (original)
+++ trunk/libs/asio/example/buffers/reference_counted.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -92,11 +92,12 @@
if (!error)
{
new_session->start();
- new_session.reset(new session(io_service_));
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
}
+
+ new_session.reset(new session(io_service_));
+ acceptor_.async_accept(new_session->socket(),
+ boost::bind(&server::handle_accept, this, new_session,
+ boost::asio::placeholders::error));
}
private:
Modified: trunk/libs/asio/example/chat/chat_server.cpp
==============================================================================
--- trunk/libs/asio/example/chat/chat_server.cpp (original)
+++ trunk/libs/asio/example/chat/chat_server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -182,6 +182,11 @@
: io_service_(io_service),
acceptor_(io_service, endpoint)
{
+ start_accept();
+ }
+
+ void start_accept()
+ {
chat_session_ptr new_session(new chat_session(io_service_, room_));
acceptor_.async_accept(new_session->socket(),
boost::bind(&chat_server::handle_accept, this, new_session,
@@ -194,11 +199,9 @@
if (!error)
{
session->start();
- chat_session_ptr new_session(new chat_session(io_service_, room_));
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&chat_server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
}
+
+ start_accept();
}
private:
Modified: trunk/libs/asio/example/echo/async_tcp_echo_server.cpp
==============================================================================
--- trunk/libs/asio/example/echo/async_tcp_echo_server.cpp (original)
+++ trunk/libs/asio/example/echo/async_tcp_echo_server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -36,6 +36,7 @@
boost::asio::placeholders::bytes_transferred));
}
+private:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
@@ -67,7 +68,6 @@
}
}
-private:
tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length];
@@ -80,6 +80,12 @@
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
+ start_accept();
+ }
+
+private:
+ void start_accept()
+ {
session* new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
@@ -92,18 +98,15 @@
if (!error)
{
new_session->start();
- new_session = new session(io_service_);
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
}
else
{
delete new_session;
}
+
+ start_accept();
}
-private:
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};
Modified: trunk/libs/asio/example/fork/process_per_connection.cpp
==============================================================================
--- trunk/libs/asio/example/fork/process_per_connection.cpp (original)
+++ trunk/libs/asio/example/fork/process_per_connection.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -43,11 +43,16 @@
void handle_signal_wait()
{
- // Reap completed child processes so that we don't end up with zombies.
- int status = 0;
- while (waitpid(-1, &status, WNOHANG) > 0) {}
+ // Only the parent process should check for this signal. We can determine
+ // whether we are in the parent by checking if the acceptor is still open.
+ if (acceptor_.is_open())
+ {
+ // Reap completed child processes so that we don't end up with zombies.
+ int status = 0;
+ while (waitpid(-1, &status, WNOHANG) > 0) {}
- start_signal_wait();
+ start_signal_wait();
+ }
}
void start_accept()
@@ -76,6 +81,9 @@
// acceptor. It remains open in the parent.
acceptor_.close();
+ // The child process is not interested in processing the SIGCHLD signal.
+ signal_.cancel();
+
start_read();
}
else
@@ -93,6 +101,7 @@
else
{
std::cerr << "Accept error: " << ec.message() << std::endl;
+ start_accept();
}
}
Modified: trunk/libs/asio/example/http/server/server.cpp
==============================================================================
--- trunk/libs/asio/example/http/server/server.cpp (original)
+++ trunk/libs/asio/example/http/server/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -21,8 +21,7 @@
signals_(io_service_),
acceptor_(io_service_),
connection_manager_(),
- new_connection_(new connection(io_service_,
- connection_manager_, request_handler_)),
+ new_connection_(),
request_handler_(doc_root)
{
// Register to handle the signals that indicate when the server should exit.
@@ -43,9 +42,8 @@
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
+
+ start_accept();
}
void server::run()
@@ -57,17 +55,30 @@
io_service_.run();
}
+void server::start_accept()
+{
+ new_connection_.reset(new connection(io_service_,
+ connection_manager_, request_handler_));
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error));
+}
+
void server::handle_accept(const boost::system::error_code& e)
{
+ // Check whether the server was stopped by a signal before this completion
+ // handler had a chance to run.
+ if (!acceptor_.is_open())
+ {
+ return;
+ }
+
if (!e)
{
connection_manager_.start(new_connection_);
- new_connection_.reset(new connection(io_service_,
- connection_manager_, request_handler_));
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
}
+
+ start_accept();
}
void server::handle_stop()
Modified: trunk/libs/asio/example/http/server/server.hpp
==============================================================================
--- trunk/libs/asio/example/http/server/server.hpp (original)
+++ trunk/libs/asio/example/http/server/server.hpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -35,6 +35,9 @@
void run();
private:
+ /// Initiate an asynchronous accept operation.
+ void start_accept();
+
/// Handle completion of an asynchronous accept operation.
void handle_accept(const boost::system::error_code& e);
Modified: trunk/libs/asio/example/http/server2/server.cpp
==============================================================================
--- trunk/libs/asio/example/http/server2/server.cpp (original)
+++ trunk/libs/asio/example/http/server2/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -19,8 +19,7 @@
: io_service_pool_(io_service_pool_size),
signals_(io_service_pool_.get_io_service()),
acceptor_(io_service_pool_.get_io_service()),
- new_connection_(new connection(
- io_service_pool_.get_io_service(), request_handler_)),
+ new_connection_(),
request_handler_(doc_root)
{
// Register to handle the signals that indicate when the server should exit.
@@ -41,9 +40,8 @@
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
+
+ start_accept();
}
void server::run()
@@ -51,17 +49,23 @@
io_service_pool_.run();
}
+void server::start_accept()
+{
+ new_connection_.reset(new connection(
+ io_service_pool_.get_io_service(), request_handler_));
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error));
+}
+
void server::handle_accept(const boost::system::error_code& e)
{
if (!e)
{
new_connection_->start();
- new_connection_.reset(new connection(
- io_service_pool_.get_io_service(), request_handler_));
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
}
+
+ start_accept();
}
void server::handle_stop()
Modified: trunk/libs/asio/example/http/server2/server.hpp
==============================================================================
--- trunk/libs/asio/example/http/server2/server.hpp (original)
+++ trunk/libs/asio/example/http/server2/server.hpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -37,6 +37,9 @@
void run();
private:
+ /// Initiate an asynchronous accept operation.
+ void start_accept();
+
/// Handle completion of an asynchronous accept operation.
void handle_accept(const boost::system::error_code& e);
Modified: trunk/libs/asio/example/http/server3/server.cpp
==============================================================================
--- trunk/libs/asio/example/http/server3/server.cpp (original)
+++ trunk/libs/asio/example/http/server3/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -22,7 +22,7 @@
: thread_pool_size_(thread_pool_size),
signals_(io_service_),
acceptor_(io_service_),
- new_connection_(new connection(io_service_, request_handler_)),
+ new_connection_(),
request_handler_(doc_root)
{
// Register to handle the signals that indicate when the server should exit.
@@ -43,9 +43,8 @@
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
+
+ start_accept();
}
void server::run()
@@ -64,16 +63,22 @@
threads[i]->join();
}
+void server::start_accept()
+{
+ new_connection_.reset(new connection(io_service_, request_handler_));
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error));
+}
+
void server::handle_accept(const boost::system::error_code& e)
{
if (!e)
{
new_connection_->start();
- new_connection_.reset(new connection(io_service_, request_handler_));
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
}
+
+ start_accept();
}
void server::handle_stop()
Modified: trunk/libs/asio/example/http/server3/server.hpp
==============================================================================
--- trunk/libs/asio/example/http/server3/server.hpp (original)
+++ trunk/libs/asio/example/http/server3/server.hpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -36,6 +36,9 @@
void run();
private:
+ /// Initiate an asynchronous accept operation.
+ void start_accept();
+
/// Handle completion of an asynchronous accept operation.
void handle_accept(const boost::system::error_code& e);
Modified: trunk/libs/asio/example/iostreams/daytime_server.cpp
==============================================================================
--- trunk/libs/asio/example/iostreams/daytime_server.cpp (original)
+++ trunk/libs/asio/example/iostreams/daytime_server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -34,8 +34,12 @@
for (;;)
{
tcp::iostream stream;
- acceptor.accept(*stream.rdbuf());
- stream << make_daytime_string();
+ boost::system::error_code ec;
+ acceptor.accept(*stream.rdbuf(), ec);
+ if (!ec)
+ {
+ stream << make_daytime_string();
+ }
}
}
catch (std::exception& e)
Modified: trunk/libs/asio/example/nonblocking/third_party_lib.cpp
==============================================================================
--- trunk/libs/asio/example/nonblocking/third_party_lib.cpp (original)
+++ trunk/libs/asio/example/nonblocking/third_party_lib.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -208,8 +208,9 @@
if (!error)
{
new_connection->start();
- start_accept();
}
+
+ start_accept();
}
tcp::acceptor acceptor_;
Modified: trunk/libs/asio/example/porthopper/server.cpp
==============================================================================
--- trunk/libs/asio/example/porthopper/server.cpp (original)
+++ trunk/libs/asio/example/porthopper/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -56,13 +56,13 @@
boost::asio::async_read(*socket, request->to_buffers(),
boost::bind(&server::handle_control_request, this,
boost::asio::placeholders::error, socket, request));
-
- // Start waiting for a new control connection.
- tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
- acceptor_.async_accept(*new_socket,
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error, new_socket));
}
+
+ // Start waiting for a new control connection.
+ tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
+ acceptor_.async_accept(*new_socket,
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error, new_socket));
}
// Handle a new control request.
Modified: trunk/libs/asio/example/serialization/server.cpp
==============================================================================
--- trunk/libs/asio/example/serialization/server.cpp (original)
+++ trunk/libs/asio/example/serialization/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -72,20 +72,13 @@
conn->async_write(stocks_,
boost::bind(&server::handle_write, this,
boost::asio::placeholders::error, conn));
-
- // Start an accept operation for a new connection.
- connection_ptr new_conn(new connection(acceptor_.get_io_service()));
- acceptor_.async_accept(new_conn->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error, new_conn));
- }
- else
- {
- // An error occurred. Log it and return. Since we are not starting a new
- // accept operation the io_service will run out of work to do and the
- // server will exit.
- std::cerr << e.message() << std::endl;
}
+
+ // Start an accept operation for a new connection.
+ connection_ptr new_conn(new connection(acceptor_.get_io_service()));
+ acceptor_.async_accept(new_conn->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error, new_conn));
}
/// Handle completion of a write operation.
Modified: trunk/libs/asio/example/ssl/server.cpp
==============================================================================
--- trunk/libs/asio/example/ssl/server.cpp (original)
+++ trunk/libs/asio/example/ssl/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -107,10 +107,7 @@
context_.use_private_key_file("server.pem", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");
- session* new_session = new session(io_service_, context_);
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
+ start_accept();
}
std::string get_password() const
@@ -118,21 +115,27 @@
return "test";
}
+ void start_accept()
+ {
+ session* new_session = new session(io_service_, context_);
+ acceptor_.async_accept(new_session->socket(),
+ boost::bind(&server::handle_accept, this, new_session,
+ boost::asio::placeholders::error));
+ }
+
void handle_accept(session* new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
- new_session = new session(io_service_, context_);
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
}
else
{
delete new_session;
}
+
+ start_accept();
}
private:
Modified: trunk/libs/asio/example/timeouts/server.cpp
==============================================================================
--- trunk/libs/asio/example/timeouts/server.cpp (original)
+++ trunk/libs/asio/example/timeouts/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -365,6 +365,11 @@
subscriber_ptr bc(new udp_broadcaster(io_service_, broadcast_endpoint));
channel_.join(bc);
+ start_accept();
+ }
+
+ void start_accept()
+ {
tcp_session_ptr new_session(new tcp_session(io_service_, channel_));
acceptor_.async_accept(new_session->socket(),
@@ -377,12 +382,9 @@
if (!ec)
{
session->start();
-
- tcp_session_ptr new_session(new tcp_session(io_service_, channel_));
-
- acceptor_.async_accept(new_session->socket(),
- boost::bind(&server::handle_accept, this, new_session, _1));
}
+
+ start_accept();
}
private:
Modified: trunk/libs/asio/example/tutorial/daytime3/server.cpp
==============================================================================
--- trunk/libs/asio/example/tutorial/daytime3/server.cpp (original)
+++ trunk/libs/asio/example/tutorial/daytime3/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -92,8 +92,9 @@
if (!error)
{
new_connection->start();
- start_accept();
}
+
+ start_accept();
}
tcp::acceptor acceptor_;
Modified: trunk/libs/asio/example/tutorial/daytime7/server.cpp
==============================================================================
--- trunk/libs/asio/example/tutorial/daytime7/server.cpp (original)
+++ trunk/libs/asio/example/tutorial/daytime7/server.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -91,8 +91,9 @@
if (!error)
{
new_connection->start();
- start_accept();
}
+
+ start_accept();
}
tcp::acceptor acceptor_;
Modified: trunk/libs/asio/example/windows/transmit_file.cpp
==============================================================================
--- trunk/libs/asio/example/windows/transmit_file.cpp (original)
+++ trunk/libs/asio/example/windows/transmit_file.cpp 2011-05-24 19:33:55 EDT (Tue, 24 May 2011)
@@ -132,8 +132,9 @@
if (!error)
{
new_connection->start();
- start_accept();
}
+
+ start_accept();
}
tcp::acceptor acceptor_;
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