summaryrefslogtreecommitdiff
path: root/libs/asio/example/cpp03/spawn
diff options
context:
space:
mode:
Diffstat (limited to 'libs/asio/example/cpp03/spawn')
-rw-r--r--libs/asio/example/cpp03/spawn/Jamfile.v240
-rw-r--r--libs/asio/example/cpp03/spawn/echo_server.cpp122
2 files changed, 162 insertions, 0 deletions
diff --git a/libs/asio/example/cpp03/spawn/Jamfile.v2 b/libs/asio/example/cpp03/spawn/Jamfile.v2
new file mode 100644
index 000000000..cea0fc107
--- /dev/null
+++ b/libs/asio/example/cpp03/spawn/Jamfile.v2
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+#
+# Distributed under the Boost Software License, Version 1.0. (See accompanying
+# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+#
+
+import os ;
+
+if [ os.name ] = SOLARIS
+{
+ lib socket ;
+ lib nsl ;
+}
+else if [ os.name ] = NT
+{
+ lib ws2_32 ;
+ lib mswsock ;
+}
+else if [ os.name ] = HPUX
+{
+ lib ipv6 ;
+}
+
+exe server
+ : echo_server.cpp
+ /boost/context//boost_context
+ /boost/coroutine//boost_coroutine
+ /boost/system//boost_system
+ : <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <os>SOLARIS:<library>socket
+ <os>SOLARIS:<library>nsl
+ <os>NT:<define>_WIN32_WINNT=0x0501
+ <os>NT,<toolset>gcc:<library>ws2_32
+ <os>NT,<toolset>gcc:<library>mswsock
+ <os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
+ <os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
+ <os>HPUX:<library>ipv6
+ ;
diff --git a/libs/asio/example/cpp03/spawn/echo_server.cpp b/libs/asio/example/cpp03/spawn/echo_server.cpp
new file mode 100644
index 000000000..b5e870f59
--- /dev/null
+++ b/libs/asio/example/cpp03/spawn/echo_server.cpp
@@ -0,0 +1,122 @@
+//
+// echo_server.cpp
+// ~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <boost/asio/deadline_timer.hpp>
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/spawn.hpp>
+#include <boost/asio/write.hpp>
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <iostream>
+
+using boost::asio::ip::tcp;
+
+class session : public boost::enable_shared_from_this<session>
+{
+public:
+ explicit session(boost::asio::io_service& io_service)
+ : strand_(io_service),
+ socket_(io_service),
+ timer_(io_service)
+ {
+ }
+
+ tcp::socket& socket()
+ {
+ return socket_;
+ }
+
+ void go()
+ {
+ boost::asio::spawn(strand_,
+ boost::bind(&session::echo,
+ shared_from_this(), _1));
+ boost::asio::spawn(strand_,
+ boost::bind(&session::timeout,
+ shared_from_this(), _1));
+ }
+
+private:
+ void echo(boost::asio::yield_context yield)
+ {
+ try
+ {
+ char data[128];
+ for (;;)
+ {
+ timer_.expires_from_now(boost::posix_time::seconds(10));
+ std::size_t n = socket_.async_read_some(boost::asio::buffer(data), yield);
+ boost::asio::async_write(socket_, boost::asio::buffer(data, n), yield);
+ }
+ }
+ catch (std::exception& e)
+ {
+ socket_.close();
+ timer_.cancel();
+ }
+ }
+
+ void timeout(boost::asio::yield_context yield)
+ {
+ while (socket_.is_open())
+ {
+ boost::system::error_code ignored_ec;
+ timer_.async_wait(yield[ignored_ec]);
+ if (timer_.expires_from_now() <= boost::posix_time::seconds(0))
+ socket_.close();
+ }
+ }
+
+ boost::asio::io_service::strand strand_;
+ tcp::socket socket_;
+ boost::asio::deadline_timer timer_;
+};
+
+void do_accept(boost::asio::io_service& io_service,
+ unsigned short port, boost::asio::yield_context yield)
+{
+ tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
+
+ for (;;)
+ {
+ boost::system::error_code ec;
+ boost::shared_ptr<session> new_session(new session(io_service));
+ acceptor.async_accept(new_session->socket(), yield[ec]);
+ if (!ec) new_session->go();
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc != 2)
+ {
+ std::cerr << "Usage: echo_server <port>\n";
+ return 1;
+ }
+
+ boost::asio::io_service io_service;
+
+ boost::asio::spawn(io_service,
+ boost::bind(do_accept,
+ boost::ref(io_service), atoi(argv[1]), _1));
+
+ io_service.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "Exception: " << e.what() << "\n";
+ }
+
+ return 0;
+}