mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 21:07:26 +02:00
@ -1,6 +1,7 @@
|
|||||||
Version 158:
|
Version 158:
|
||||||
|
|
||||||
* Tidy up end_of_stream javadoc
|
* Tidy up end_of_stream javadoc
|
||||||
|
* Tidy up websocket docs
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -78,4 +78,8 @@ accessed by calling
|
|||||||
operations are being performed may result in undefined behavior.
|
operations are being performed may result in undefined behavior.
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[heading Non-Blocking Mode]
|
||||||
|
|
||||||
|
Please note that websocket streams do not support non-blocking modes.
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
|
@ -61,9 +61,9 @@ all of the buffers representing the message are known ahead of time:
|
|||||||
[ws_snippet_15]
|
[ws_snippet_15]
|
||||||
|
|
||||||
[important
|
[important
|
||||||
Calls to [link beast.ref.boost__beast__websocket__stream.set_option `set_option`]
|
[link beast.ref.boost__beast__websocket__stream `websocket::stream`]
|
||||||
must be made from the same implicit or explicit strand as that used
|
is not thread-safe. Calls to stream member functions must
|
||||||
to perform other operations.
|
all be made from the same implicit or explicit strand.
|
||||||
]
|
]
|
||||||
|
|
||||||
[heading Frames]
|
[heading Frames]
|
||||||
|
@ -152,11 +152,24 @@ boost::asio::ip::tcp::socket sock{ioc};
|
|||||||
{
|
{
|
||||||
stream<boost::asio::ip::tcp::socket> ws{ioc};
|
stream<boost::asio::ip::tcp::socket> ws{ioc};
|
||||||
//[ws_snippet_15
|
//[ws_snippet_15
|
||||||
|
// This DynamicBuffer will hold the received message
|
||||||
multi_buffer buffer;
|
multi_buffer buffer;
|
||||||
|
|
||||||
|
// Read a complete message into the buffer's input area
|
||||||
ws.read(buffer);
|
ws.read(buffer);
|
||||||
|
|
||||||
|
// Set text mode if the received message was also text,
|
||||||
|
// otherwise binary mode will be set.
|
||||||
ws.text(ws.got_text());
|
ws.text(ws.got_text());
|
||||||
|
|
||||||
|
// Echo the received message back to the peer. If the received
|
||||||
|
// message was in text mode, the echoed message will also be
|
||||||
|
// in text mode, otherwise it will be in binary mode.
|
||||||
ws.write(buffer.data());
|
ws.write(buffer.data());
|
||||||
|
|
||||||
|
// Discard all of the bytes stored in the dynamic buffer,
|
||||||
|
// otherwise the next call to read will append to the existing
|
||||||
|
// data instead of building a fresh message.
|
||||||
buffer.consume(buffer.size());
|
buffer.consume(buffer.size());
|
||||||
//]
|
//]
|
||||||
}
|
}
|
||||||
@ -164,26 +177,56 @@ boost::asio::ip::tcp::socket sock{ioc};
|
|||||||
{
|
{
|
||||||
stream<boost::asio::ip::tcp::socket> ws{ioc};
|
stream<boost::asio::ip::tcp::socket> ws{ioc};
|
||||||
//[ws_snippet_16
|
//[ws_snippet_16
|
||||||
|
// This DynamicBuffer will hold the received message
|
||||||
multi_buffer buffer;
|
multi_buffer buffer;
|
||||||
for(;;)
|
|
||||||
if(ws.read_some(buffer, 0))
|
// Read the next message in pieces
|
||||||
break;
|
do
|
||||||
|
{
|
||||||
|
// Append up to 512 bytes of the message into the buffer
|
||||||
|
ws.read_some(buffer, 512);
|
||||||
|
}
|
||||||
|
while(! ws.is_message_done());
|
||||||
|
|
||||||
|
// At this point we have a complete message in the buffer, now echo it
|
||||||
|
|
||||||
|
// The echoed message will be sent in binary mode if the received
|
||||||
|
// message was in binary mode, otherwise we will send in text mode.
|
||||||
ws.binary(ws.got_binary());
|
ws.binary(ws.got_binary());
|
||||||
|
|
||||||
|
// This buffer adapter allows us to iterate through buffer in pieces
|
||||||
buffers_suffix<multi_buffer::const_buffers_type> cb{buffer.data()};
|
buffers_suffix<multi_buffer::const_buffers_type> cb{buffer.data()};
|
||||||
|
|
||||||
|
// Echo the received message in pieces.
|
||||||
|
// This will cause the message to be broken up into multiple frames.
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
using boost::asio::buffer_size;
|
using boost::asio::buffer_size;
|
||||||
if(buffer_size(cb) > 512)
|
if(buffer_size(cb) > 512)
|
||||||
{
|
{
|
||||||
|
// There are more than 512 bytes left to send, just
|
||||||
|
// send the next 512 bytes. The value `false` informs
|
||||||
|
// the stream that the message is not complete.
|
||||||
ws.write_some(false, buffers_prefix(512, cb));
|
ws.write_some(false, buffers_prefix(512, cb));
|
||||||
|
|
||||||
|
// This efficiently discards data from the adapter by
|
||||||
|
// simply ignoring it, but does not actually affect the
|
||||||
|
// underlying dynamic buffer.
|
||||||
cb.consume(512);
|
cb.consume(512);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Only 512 bytes or less remain, so write the whole
|
||||||
|
// thing and inform the stream that this piece represents
|
||||||
|
// the end of the message by passing `true`.
|
||||||
ws.write_some(true, cb);
|
ws.write_some(true, cb);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Discard all of the bytes stored in the dynamic buffer,
|
||||||
|
// otherwise the next call to read will append to the existing
|
||||||
|
// data instead of building a fresh message.
|
||||||
//]
|
//]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user