Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:23

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_TEST_TCP_HPP
0011 #define BOOST_BEAST_TEST_TCP_HPP
0012 
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <boost/beast/core/detail/get_io_context.hpp>
0015 #include <boost/beast/_experimental/unit_test/suite.hpp>
0016 #include <boost/beast/_experimental/test/handler.hpp>
0017 #include <boost/asio/ip/tcp.hpp>
0018 #include <chrono>
0019 
0020 namespace boost {
0021 namespace beast {
0022 namespace test {
0023 
0024 /** Connect two TCP sockets together.
0025 */
0026 template<class Executor>
0027 bool
0028 connect(
0029     net::basic_stream_socket<net::ip::tcp, Executor>& s1,
0030     net::basic_stream_socket<net::ip::tcp, Executor>& s2)
0031 
0032 {
0033     auto ioc1 = beast::detail::get_io_context(s1);
0034     auto ioc2 = beast::detail::get_io_context(s2);
0035     if(! BEAST_EXPECT(ioc1 != nullptr))
0036         return false;
0037     if(! BEAST_EXPECT(ioc2 != nullptr))
0038         return false;
0039     if(! BEAST_EXPECT(ioc1 == ioc2))
0040         return false;
0041     auto& ioc = *ioc1;
0042     try
0043     {
0044         net::basic_socket_acceptor<
0045             net::ip::tcp, Executor> a(s1.get_executor());
0046         auto ep = net::ip::tcp::endpoint(
0047             net::ip::make_address_v4("127.0.0.1"), 0);
0048         a.open(ep.protocol());
0049         a.set_option(
0050             net::socket_base::reuse_address(true));
0051         a.bind(ep);
0052         a.listen(0);
0053         ep = a.local_endpoint();
0054         a.async_accept(s2, test::success_handler());
0055         s1.async_connect(ep, test::success_handler());
0056         run(ioc);
0057         if(! BEAST_EXPECT(
0058             s1.remote_endpoint() == s2.local_endpoint()))
0059             return false;
0060         if(! BEAST_EXPECT(
0061             s2.remote_endpoint() == s1.local_endpoint()))
0062             return false;
0063     }
0064     catch(std::exception const& e)
0065     {
0066         beast::unit_test::suite::this_suite()->fail(
0067             e.what(), __FILE__, __LINE__);
0068         return false;
0069     }
0070     return true;
0071 }
0072 
0073 } // test
0074 } // beast
0075 } // boost
0076 
0077 #endif