1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #include <boost/beast/_experimental/unit_test/suite.hpp>
11
12 #ifdef BOOST_MSVC
13 #pragma warning(push)
14 #pragma warning(disable: 4459) // declaration hides global declaration
15 #endif
16
17 #include <boost/beast.hpp>
18 #include <boost/beast/ssl.hpp>
19 #include <boost/asio.hpp>
20 #include <boost/asio/ssl.hpp>
21 #include <boost/asio/spawn.hpp>
22
23 namespace {
24
25 #include "websocket_common.ipp"
26
27 void
snippets()28 snippets()
29 {
30 stream<tcp_stream> ws(ioc);
31
32 {
33 //[code_websocket_8_1
34
35 flat_buffer buffer;
36
37 ws.async_read(buffer,
38 [](error_code, std::size_t)
39 {
40 // Do something with the buffer
41 });
42
43 //]
44 }
45
46 multi_buffer b;
47
48 {
49 //[code_websocket_8_2
50
51 ws.async_read(b, [](error_code, std::size_t){});
52 ws.async_read(b, [](error_code, std::size_t){});
53
54 //]
55 }
56
57 {
58 //[code_websocket_8_3
59
60 ws.async_read(b, [](error_code, std::size_t){});
61 ws.async_write(b.data(), [](error_code, std::size_t){});
62 ws.async_ping({}, [](error_code){});
63 ws.async_close({}, [](error_code){});
64
65 //]
66 }
67 }
68
69 // workaround for https://github.com/chriskohlhoff/asio/issues/112
70 //#ifdef BOOST_MSVC
71 //[code_websocket_8_1f
72
echo(stream<tcp_stream> & ws,multi_buffer & buffer,net::yield_context yield)73 void echo(stream<tcp_stream>& ws,
74 multi_buffer& buffer, net::yield_context yield)
75 {
76 ws.async_read(buffer, yield);
77 std::future<std::size_t> fut =
78 ws.async_write(buffer.data(), net::use_future);
79 }
80
81 //]
82 //#endif
83
84 struct websocket_8_test
85 : public boost::beast::unit_test::suite
86 {
87 void
run__anonac5003640111::websocket_8_test88 run() override
89 {
90 BEAST_EXPECT(&snippets);
91 BEAST_EXPECT(&echo);
92 }
93 };
94
95 BEAST_DEFINE_TESTSUITE(beast,doc,websocket_8);
96
97 } // (anon)
98
99 #ifdef BOOST_MSVC
100 #pragma warning(pop)
101 #endif
102