• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/config.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/_experimental/unit_test/suite.hpp>
18 
19 #include <boost/beast.hpp>
20 #include <boost/beast/ssl.hpp>
21 #include <boost/asio.hpp>
22 #include <boost/asio/ssl.hpp>
23 
24 namespace {
25 
26 #include "websocket_common.ipp"
27 
28 void
snippets()29 snippets()
30 {
31     stream<tcp_stream> ws(ioc);
32 
33     {
34     //[code_websocket_4_1
35 
36         net::const_buffer b("Hello, world!", 13);
37 
38         // This sets all outgoing messages to be sent as text.
39         // Text messages must contain valid utf8, this is checked
40         // when reading but not when writing.
41 
42         ws.text(true);
43 
44         // Write the buffer as text
45         ws.write(b);
46     //]
47     }
48 
49     {
50     //[code_websocket_4_2
51 
52         // This DynamicBuffer will hold the received message
53         flat_buffer buffer;
54 
55         // Read a complete message into the buffer's input area
56         ws.read(buffer);
57 
58         // Set text mode if the received message was also text,
59         // otherwise binary mode will be set.
60         ws.text(ws.got_text());
61 
62         // Echo the received message back to the peer. If the received
63         // message was in text mode, the echoed message will also be
64         // in text mode, otherwise it will be in binary mode.
65         ws.write(buffer.data());
66 
67         // Discard all of the bytes stored in the dynamic buffer,
68         // otherwise the next call to read will append to the existing
69         // data instead of building a fresh message.
70         buffer.consume(buffer.size());
71 
72     //]
73     }
74 
75     {
76     //[code_websocket_4_3
77 
78         // This DynamicBuffer will hold the received message
79         multi_buffer buffer;
80 
81         // Read the next message in pieces
82         do
83         {
84             // Append up to 512 bytes of the message into the buffer
85             ws.read_some(buffer, 512);
86         }
87         while(! ws.is_message_done());
88 
89         // At this point we have a complete message in the buffer, now echo it
90 
91         // The echoed message will be sent in binary mode if the received
92         // message was in binary mode, otherwise we will send in text mode.
93         ws.binary(ws.got_binary());
94 
95         // This buffer adaptor allows us to iterate through buffer in pieces
96         buffers_suffix<multi_buffer::const_buffers_type> cb{buffer.data()};
97 
98         // Echo the received message in pieces.
99         // This will cause the message to be broken up into multiple frames.
100         for(;;)
101         {
102             if(buffer_bytes(cb) > 512)
103             {
104                 // There are more than 512 bytes left to send, just
105                 // send the next 512 bytes. The value `false` informs
106                 // the stream that the message is not complete.
107                 ws.write_some(false, buffers_prefix(512, cb));
108 
109                 // This efficiently discards data from the adaptor by
110                 // simply ignoring it, but does not actually affect the
111                 // underlying dynamic buffer.
112                 cb.consume(512);
113             }
114             else
115             {
116                 // Only 512 bytes or less remain, so write the whole
117                 // thing and inform the stream that this piece represents
118                 // the end of the message by passing `true`.
119                 ws.write_some(true, cb);
120                 break;
121             }
122         }
123 
124         // Discard all of the bytes stored in the dynamic buffer,
125         // otherwise the next call to read will append to the existing
126         // data instead of building a fresh message.
127         buffer.consume(buffer.size());
128 
129     //]
130     }
131 }
132 
133 struct websocket_4_test
134     : public boost::beast::unit_test::suite
135 {
136     void
run__anonde8036050111::websocket_4_test137     run() override
138     {
139         BEAST_EXPECT(&snippets);
140     }
141 };
142 
143 BEAST_DEFINE_TESTSUITE(beast,doc,websocket_4);
144 
145 } // (anon)
146 
147 #ifdef BOOST_MSVC
148 #pragma warning(pop)
149 #endif
150