autoecho.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2003-2013 Christopher M. Kohlhoff
  2. // Copyright Oliver Kowalke, Nat Goodspeed 2015.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <chrono>
  7. #include <cstdlib>
  8. #include <iomanip>
  9. #include <iostream>
  10. #include <map>
  11. #include <memory>
  12. #include <mutex>
  13. #include <sstream>
  14. #include <thread>
  15. #include <boost/asio.hpp>
  16. #include <boost/bind.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/fiber/all.hpp>
  19. #include "round_robin.hpp"
  20. #include "yield.hpp"
  21. using boost::asio::ip::tcp;
  22. const int max_length = 1024;
  23. typedef boost::shared_ptr< tcp::socket > socket_ptr;
  24. const char* const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  25. /*****************************************************************************
  26. * thread names
  27. *****************************************************************************/
  28. class ThreadNames {
  29. private:
  30. std::map<std::thread::id, std::string> names_{};
  31. const char* next_{ alpha };
  32. std::mutex mtx_{};
  33. public:
  34. ThreadNames() = default;
  35. std::string lookup() {
  36. std::unique_lock<std::mutex> lk( mtx_);
  37. auto this_id( std::this_thread::get_id() );
  38. auto found = names_.find( this_id );
  39. if ( found != names_.end() ) {
  40. return found->second;
  41. }
  42. BOOST_ASSERT( *next_);
  43. std::string name(1, *next_++ );
  44. names_[ this_id ] = name;
  45. return name;
  46. }
  47. };
  48. ThreadNames thread_names;
  49. /*****************************************************************************
  50. * fiber names
  51. *****************************************************************************/
  52. class FiberNames {
  53. private:
  54. std::map<boost::fibers::fiber::id, std::string> names_{};
  55. unsigned next_{ 0 };
  56. boost::fibers::mutex mtx_{};
  57. public:
  58. FiberNames() = default;
  59. std::string lookup() {
  60. std::unique_lock<boost::fibers::mutex> lk( mtx_);
  61. auto this_id( boost::this_fiber::get_id() );
  62. auto found = names_.find( this_id );
  63. if ( found != names_.end() ) {
  64. return found->second;
  65. }
  66. std::ostringstream out;
  67. // Bake into the fiber's name the thread name on which we first
  68. // lookup() its ID, to be able to spot when a fiber hops between
  69. // threads.
  70. out << thread_names.lookup() << next_++;
  71. std::string name( out.str() );
  72. names_[ this_id ] = name;
  73. return name;
  74. }
  75. };
  76. FiberNames fiber_names;
  77. std::string tag() {
  78. std::ostringstream out;
  79. out << "Thread " << thread_names.lookup() << ": "
  80. << std::setw(4) << fiber_names.lookup() << std::setw(0);
  81. return out.str();
  82. }
  83. /*****************************************************************************
  84. * message printing
  85. *****************************************************************************/
  86. void print_( std::ostream& out) {
  87. out << '\n';
  88. }
  89. template < typename T, typename... Ts >
  90. void print_( std::ostream& out, T const& arg, Ts const&... args) {
  91. out << arg;
  92. print_(out, args...);
  93. }
  94. template < typename... T >
  95. void print( T const&... args ) {
  96. std::ostringstream buffer;
  97. print_( buffer, args...);
  98. std::cout << buffer.str() << std::flush;
  99. }
  100. /*****************************************************************************
  101. * fiber function per server connection
  102. *****************************************************************************/
  103. void session( socket_ptr sock) {
  104. try {
  105. for (;;) {
  106. char data[max_length];
  107. boost::system::error_code ec;
  108. std::size_t length = sock->async_read_some(
  109. boost::asio::buffer( data),
  110. boost::fibers::asio::yield[ec]);
  111. if ( ec == boost::asio::error::eof) {
  112. break; //connection closed cleanly by peer
  113. } else if ( ec) {
  114. throw boost::system::system_error( ec); //some other error
  115. }
  116. print( tag(), ": handled: ", std::string(data, length));
  117. boost::asio::async_write(
  118. * sock,
  119. boost::asio::buffer( data, length),
  120. boost::fibers::asio::yield[ec]);
  121. if ( ec == boost::asio::error::eof) {
  122. break; //connection closed cleanly by peer
  123. } else if ( ec) {
  124. throw boost::system::system_error( ec); //some other error
  125. }
  126. }
  127. print( tag(), ": connection closed");
  128. } catch ( std::exception const& ex) {
  129. print( tag(), ": caught exception : ", ex.what());
  130. }
  131. }
  132. /*****************************************************************************
  133. * listening server
  134. *****************************************************************************/
  135. void server( std::shared_ptr< boost::asio::io_service > const& io_svc, tcp::acceptor & a) {
  136. print( tag(), ": echo-server started");
  137. try {
  138. for (;;) {
  139. socket_ptr socket( new tcp::socket( * io_svc) );
  140. boost::system::error_code ec;
  141. a.async_accept(
  142. * socket,
  143. boost::fibers::asio::yield[ec]);
  144. if ( ec) {
  145. throw boost::system::system_error( ec); //some other error
  146. } else {
  147. boost::fibers::fiber( session, socket).detach();
  148. }
  149. }
  150. } catch ( std::exception const& ex) {
  151. print( tag(), ": caught exception : ", ex.what());
  152. }
  153. io_svc->stop();
  154. print( tag(), ": echo-server stopped");
  155. }
  156. /*****************************************************************************
  157. * fiber function per client
  158. *****************************************************************************/
  159. void client( std::shared_ptr< boost::asio::io_service > const& io_svc, tcp::acceptor & a,
  160. boost::fibers::barrier& barrier, unsigned iterations) {
  161. print( tag(), ": echo-client started");
  162. for (unsigned count = 0; count < iterations; ++count) {
  163. tcp::resolver resolver( * io_svc);
  164. tcp::resolver::query query( tcp::v4(), "127.0.0.1", "9999");
  165. tcp::resolver::iterator iterator = resolver.resolve( query);
  166. tcp::socket s( * io_svc);
  167. boost::asio::connect( s, iterator);
  168. for (unsigned msg = 0; msg < 1; ++msg) {
  169. std::ostringstream msgbuf;
  170. msgbuf << "from " << fiber_names.lookup() << " " << count << "." << msg;
  171. std::string message(msgbuf.str());
  172. print( tag(), ": Sending: ", message);
  173. boost::system::error_code ec;
  174. boost::asio::async_write(
  175. s,
  176. boost::asio::buffer( message),
  177. boost::fibers::asio::yield[ec]);
  178. if ( ec == boost::asio::error::eof) {
  179. return; //connection closed cleanly by peer
  180. } else if ( ec) {
  181. throw boost::system::system_error( ec); //some other error
  182. }
  183. char reply[max_length];
  184. size_t reply_length = s.async_read_some(
  185. boost::asio::buffer( reply, max_length),
  186. boost::fibers::asio::yield[ec]);
  187. if ( ec == boost::asio::error::eof) {
  188. return; //connection closed cleanly by peer
  189. } else if ( ec) {
  190. throw boost::system::system_error( ec); //some other error
  191. }
  192. print( tag(), ": Reply : ", std::string( reply, reply_length));
  193. }
  194. }
  195. // done with all iterations, wait for rest of client fibers
  196. if ( barrier.wait()) {
  197. // exactly one barrier.wait() call returns true
  198. // we're the lucky one
  199. a.close();
  200. print( tag(), ": acceptor stopped");
  201. }
  202. print( tag(), ": echo-client stopped");
  203. }
  204. /*****************************************************************************
  205. * main
  206. *****************************************************************************/
  207. int main( int argc, char* argv[]) {
  208. try {
  209. //[asio_rr_setup
  210. std::shared_ptr< boost::asio::io_service > io_svc = std::make_shared< boost::asio::io_service >();
  211. boost::fibers::use_scheduling_algorithm< boost::fibers::asio::round_robin >( io_svc);
  212. //]
  213. print( "Thread ", thread_names.lookup(), ": started");
  214. //[asio_rr_launch_fibers
  215. // server
  216. tcp::acceptor a( * io_svc, tcp::endpoint( tcp::v4(), 9999) );
  217. boost::fibers::fiber( server, io_svc, std::ref( a) ).detach();
  218. // client
  219. const unsigned iterations = 2;
  220. const unsigned clients = 3;
  221. boost::fibers::barrier b( clients);
  222. for ( unsigned i = 0; i < clients; ++i) {
  223. boost::fibers::fiber(
  224. client, io_svc, std::ref( a), std::ref( b), iterations).detach();
  225. }
  226. //]
  227. //[asio_rr_run
  228. io_svc->run();
  229. //]
  230. print( tag(), ": io_service returned");
  231. print( "Thread ", thread_names.lookup(), ": stopping");
  232. std::cout << "done." << std::endl;
  233. return EXIT_SUCCESS;
  234. } catch ( std::exception const& e) {
  235. print("Exception: ", e.what(), "\n");
  236. }
  237. return EXIT_FAILURE;
  238. }