mirror of
https://github.com/boostorg/asio.git
synced 2026-01-26 06:12:08 +00:00
Initial merge of Networking TS compatibility.
Merged from chriskohlhoff/asio master branch as of commit 4a4d28b0d24c53236e229bd1b5f378c9964b1ebb.
This commit is contained in:
@@ -24,14 +24,14 @@ int main(int argc, char* argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
tcp::resolver resolver(io_service);
|
||||
tcp::resolver::query query(argv[1], "daytime");
|
||||
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
|
||||
tcp::resolver resolver(io_context);
|
||||
tcp::resolver::results_type endpoints =
|
||||
resolver.resolve(argv[1], "daytime");
|
||||
|
||||
tcp::socket socket(io_service);
|
||||
boost::asio::connect(socket, endpoint_iterator);
|
||||
tcp::socket socket(io_context);
|
||||
boost::asio::connect(socket, endpoints);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
@@ -26,13 +26,13 @@ int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
|
||||
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
tcp::socket socket(io_service);
|
||||
tcp::socket socket(io_context);
|
||||
acceptor.accept(socket);
|
||||
|
||||
std::string message = make_daytime_string();
|
||||
|
||||
@@ -31,9 +31,9 @@ class tcp_connection
|
||||
public:
|
||||
typedef boost::shared_ptr<tcp_connection> pointer;
|
||||
|
||||
static pointer create(boost::asio::io_service& io_service)
|
||||
static pointer create(boost::asio::io_context& io_context)
|
||||
{
|
||||
return pointer(new tcp_connection(io_service));
|
||||
return pointer(new tcp_connection(io_context));
|
||||
}
|
||||
|
||||
tcp::socket& socket()
|
||||
@@ -52,8 +52,8 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
tcp_connection(boost::asio::io_service& io_service)
|
||||
: socket_(io_service)
|
||||
tcp_connection(boost::asio::io_context& io_context)
|
||||
: socket_(io_context)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -69,8 +69,8 @@ private:
|
||||
class tcp_server
|
||||
{
|
||||
public:
|
||||
tcp_server(boost::asio::io_service& io_service)
|
||||
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
|
||||
tcp_server(boost::asio::io_context& io_context)
|
||||
: acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
|
||||
{
|
||||
start_accept();
|
||||
}
|
||||
@@ -79,7 +79,7 @@ private:
|
||||
void start_accept()
|
||||
{
|
||||
tcp_connection::pointer new_connection =
|
||||
tcp_connection::create(acceptor_.get_io_service());
|
||||
tcp_connection::create(acceptor_.get_executor().context());
|
||||
|
||||
acceptor_.async_accept(new_connection->socket(),
|
||||
boost::bind(&tcp_server::handle_accept, this, new_connection,
|
||||
@@ -104,9 +104,9 @@ int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
tcp_server server(io_service);
|
||||
io_service.run();
|
||||
boost::asio::io_context io_context;
|
||||
tcp_server server(io_context);
|
||||
io_context.run();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
||||
@@ -24,13 +24,13 @@ int main(int argc, char* argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
udp::resolver resolver(io_service);
|
||||
udp::resolver::query query(udp::v4(), argv[1], "daytime");
|
||||
udp::endpoint receiver_endpoint = *resolver.resolve(query);
|
||||
udp::resolver resolver(io_context);
|
||||
udp::endpoint receiver_endpoint =
|
||||
*resolver.resolve(udp::v4(), argv[1], "daytime").begin();
|
||||
|
||||
udp::socket socket(io_service);
|
||||
udp::socket socket(io_context);
|
||||
socket.open(udp::v4());
|
||||
|
||||
boost::array<char, 1> send_buf = {{ 0 }};
|
||||
|
||||
@@ -27,9 +27,9 @@ int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));
|
||||
udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
@@ -28,8 +28,8 @@ std::string make_daytime_string()
|
||||
class udp_server
|
||||
{
|
||||
public:
|
||||
udp_server(boost::asio::io_service& io_service)
|
||||
: socket_(io_service, udp::endpoint(udp::v4(), 13))
|
||||
udp_server(boost::asio::io_context& io_context)
|
||||
: socket_(io_context, udp::endpoint(udp::v4(), 13))
|
||||
{
|
||||
start_receive();
|
||||
}
|
||||
@@ -76,9 +76,9 @@ int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
udp_server server(io_service);
|
||||
io_service.run();
|
||||
boost::asio::io_context io_context;
|
||||
udp_server server(io_context);
|
||||
io_context.run();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
||||
@@ -33,9 +33,9 @@ class tcp_connection
|
||||
public:
|
||||
typedef boost::shared_ptr<tcp_connection> pointer;
|
||||
|
||||
static pointer create(boost::asio::io_service& io_service)
|
||||
static pointer create(boost::asio::io_context& io_context)
|
||||
{
|
||||
return pointer(new tcp_connection(io_service));
|
||||
return pointer(new tcp_connection(io_context));
|
||||
}
|
||||
|
||||
tcp::socket& socket()
|
||||
@@ -52,8 +52,8 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
tcp_connection(boost::asio::io_service& io_service)
|
||||
: socket_(io_service)
|
||||
tcp_connection(boost::asio::io_context& io_context)
|
||||
: socket_(io_context)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -68,8 +68,8 @@ private:
|
||||
class tcp_server
|
||||
{
|
||||
public:
|
||||
tcp_server(boost::asio::io_service& io_service)
|
||||
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
|
||||
tcp_server(boost::asio::io_context& io_context)
|
||||
: acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
|
||||
{
|
||||
start_accept();
|
||||
}
|
||||
@@ -78,7 +78,7 @@ private:
|
||||
void start_accept()
|
||||
{
|
||||
tcp_connection::pointer new_connection =
|
||||
tcp_connection::create(acceptor_.get_io_service());
|
||||
tcp_connection::create(acceptor_.get_executor().context());
|
||||
|
||||
acceptor_.async_accept(new_connection->socket(),
|
||||
boost::bind(&tcp_server::handle_accept, this, new_connection,
|
||||
@@ -102,8 +102,8 @@ private:
|
||||
class udp_server
|
||||
{
|
||||
public:
|
||||
udp_server(boost::asio::io_service& io_service)
|
||||
: socket_(io_service, udp::endpoint(udp::v4(), 13))
|
||||
udp_server(boost::asio::io_context& io_context)
|
||||
: socket_(io_context, udp::endpoint(udp::v4(), 13))
|
||||
{
|
||||
start_receive();
|
||||
}
|
||||
@@ -144,10 +144,10 @@ int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
tcp_server server1(io_service);
|
||||
udp_server server2(io_service);
|
||||
io_service.run();
|
||||
boost::asio::io_context io_context;
|
||||
tcp_server server1(io_context);
|
||||
udp_server server2(io_context);
|
||||
io_context.run();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::asio::io_service io;
|
||||
boost::asio::io_context io;
|
||||
|
||||
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
|
||||
t.wait();
|
||||
|
||||
@@ -19,7 +19,7 @@ void print(const boost::system::error_code& /*e*/)
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::asio::io_service io;
|
||||
boost::asio::io_context io;
|
||||
|
||||
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
|
||||
t.async_wait(&print);
|
||||
|
||||
@@ -29,7 +29,7 @@ void print(const boost::system::error_code& /*e*/,
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::asio::io_service io;
|
||||
boost::asio::io_context io;
|
||||
|
||||
int count = 0;
|
||||
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
class printer
|
||||
{
|
||||
public:
|
||||
printer(boost::asio::io_service& io)
|
||||
printer(boost::asio::io_context& io)
|
||||
: timer_(io, boost::posix_time::seconds(1)),
|
||||
count_(0)
|
||||
{
|
||||
@@ -47,7 +47,7 @@ private:
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::asio::io_service io;
|
||||
boost::asio::io_context io;
|
||||
printer p(io);
|
||||
io.run();
|
||||
|
||||
|
||||
@@ -17,14 +17,17 @@
|
||||
class printer
|
||||
{
|
||||
public:
|
||||
printer(boost::asio::io_service& io)
|
||||
printer(boost::asio::io_context& io)
|
||||
: strand_(io),
|
||||
timer1_(io, boost::posix_time::seconds(1)),
|
||||
timer2_(io, boost::posix_time::seconds(1)),
|
||||
count_(0)
|
||||
{
|
||||
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
|
||||
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
|
||||
timer1_.async_wait(boost::asio::bind_executor(strand_,
|
||||
boost::bind(&printer::print1, this)));
|
||||
|
||||
timer2_.async_wait(boost::asio::bind_executor(strand_,
|
||||
boost::bind(&printer::print2, this)));
|
||||
}
|
||||
|
||||
~printer()
|
||||
@@ -40,7 +43,9 @@ public:
|
||||
++count_;
|
||||
|
||||
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
|
||||
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
|
||||
|
||||
timer1_.async_wait(boost::asio::bind_executor(strand_,
|
||||
boost::bind(&printer::print1, this)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,12 +57,14 @@ public:
|
||||
++count_;
|
||||
|
||||
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
|
||||
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
|
||||
|
||||
timer2_.async_wait(boost::asio::bind_executor(strand_,
|
||||
boost::bind(&printer::print2, this)));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::io_context::strand strand_;
|
||||
boost::asio::deadline_timer timer1_;
|
||||
boost::asio::deadline_timer timer2_;
|
||||
int count_;
|
||||
@@ -65,9 +72,9 @@ private:
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::asio::io_service io;
|
||||
boost::asio::io_context io;
|
||||
printer p(io);
|
||||
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
|
||||
boost::thread t(boost::bind(&boost::asio::io_context::run, &io));
|
||||
io.run();
|
||||
t.join();
|
||||
|
||||
|
||||
@@ -292,8 +292,8 @@ Return to \ref tuttimer4
|
||||
/**
|
||||
\page tuttimer5 Timer.5 - Synchronising handlers in multithreaded programs
|
||||
|
||||
This tutorial demonstrates the use of the boost::asio::strand class to synchronise
|
||||
callback handlers in a multithreaded program.
|
||||
This tutorial demonstrates the use of the boost::asio::io_service::strand class to
|
||||
synchronise callback handlers in a multithreaded program.
|
||||
|
||||
The previous four tutorials avoided the issue of handler synchronisation by
|
||||
calling the boost::asio::io_service::run() function from one thread only. As you
|
||||
@@ -329,22 +329,25 @@ tutorial by running two timers in parallel.
|
||||
|
||||
In addition to initialising a pair of boost::asio::deadline_timer members, the
|
||||
constructor initialises the <tt>strand_</tt> member, an object of type
|
||||
boost::asio::strand.
|
||||
boost::asio::io_service::strand.
|
||||
|
||||
An boost::asio::strand guarantees that, for those handlers that are dispatched through
|
||||
it, an executing handler will be allowed to complete before the next one is
|
||||
started. This is guaranteed irrespective of the number of threads that are
|
||||
calling boost::asio::io_service::run(). Of course, the handlers may still execute
|
||||
concurrently with other handlers that were <b>not</b> dispatched through an
|
||||
boost::asio::strand, or were dispatched through a different boost::asio::strand object.
|
||||
An boost::asio::io_service::strand is an executor that guarantees that, for those
|
||||
handlers that are dispatched through it, an executing handler will be allowed
|
||||
to complete before the next one is started. This is guaranteed irrespective of
|
||||
the number of threads that are calling boost::asio::io_service::run(). Of course, the
|
||||
handlers may still execute concurrently with other handlers that were
|
||||
<b>not</b> dispatched through an boost::asio::io_service::strand, or were dispatched
|
||||
through a different boost::asio::io_service::strand object.
|
||||
|
||||
\until {
|
||||
|
||||
When initiating the asynchronous operations, each callback handler is "wrapped"
|
||||
using the boost::asio::strand object. The boost::asio::strand::wrap() function returns a new
|
||||
handler that automatically dispatches its contained handler through the
|
||||
boost::asio::strand object. By wrapping the handlers using the same boost::asio::strand, we
|
||||
are ensuring that they cannot execute concurrently.
|
||||
When initiating the asynchronous operations, each callback handler is "bound"
|
||||
to an boost::asio::io_service::strand object. The
|
||||
boost::asio::io_service::strand::bind_executor() function returns a new handler that
|
||||
automatically dispatches its contained handler through the
|
||||
boost::asio::io_service::strand object. By binding the handlers to the same
|
||||
boost::asio::io_service::strand, we are ensuring that they cannot execute
|
||||
concurrently.
|
||||
|
||||
\until }
|
||||
\until }
|
||||
|
||||
Reference in New Issue
Block a user