Guest User

Untitled

a guest
Jan 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. struct socket_pair
  2. {
  3. net::io_context ioc1;
  4. net::ip::tcp::socket s1;
  5.  
  6. net::io_context ioc2;
  7. net::ip::tcp::socket s2;
  8.  
  9. socket_pair()
  10. : s1(ioc1)
  11. , s2(ioc2)
  12. {
  13. net::io_context ioc;
  14. net::ip::tcp::acceptor a(ioc);
  15. net::ip::tcp::endpoint ep(
  16. net::ip::make_address_v4("127.0.0.1"), 0);
  17. a.open(ep.protocol());
  18. a.set_option(
  19. net::socket_base::reuse_address(true));
  20. a.bind(ep);
  21. a.listen(
  22. net::socket_base::max_listen_connections);
  23. a.async_accept(s2,
  24. [](error_code ec)
  25. {
  26. if(ec)
  27. BOOST_THROW_EXCEPTION(system_error{ec});
  28. });
  29. s1.async_connect(a.local_endpoint(),
  30. [](error_code ec)
  31. {
  32. if(ec)
  33. BOOST_THROW_EXCEPTION(system_error{ec});
  34. });
  35. for(;;)
  36. if(
  37. ioc.poll() +
  38. ioc1.poll() +
  39. ioc2.poll() == 0)
  40. break;
  41. BOOST_ASSERT(s1.is_open());
  42. BOOST_ASSERT(s2.is_open());
  43. BOOST_ASSERT(
  44. s1.remote_endpoint() ==
  45. s2.local_endpoint());
  46. BOOST_ASSERT(
  47. s2.remote_endpoint() ==
  48. s1.local_endpoint());
  49. }
  50. };
Add Comment
Please, Sign In to add comment