Beast: How to operate the multibuffer?

Created on 10 Dec 2017  路  11Comments  路  Source: boostorg/beast

Hi @Vinnei Falco,

 I wondered how could I clear the multi buffer in the example websocket-async.cpp . I modified the code based on mine implementation. I read data from server to multi_buffer many times.  I am analog I received the data first  time was  "aaaa",  and second time I received data was "bbbb".

I convert these data to string .
stringstream ss;
ss << buffers(buf.data());
std::string strData = ss.str();
std::cout<< strData << endl;

First print is aaaa . That`s OK

Second print is aaaabbbb. Thats unexpected. What I mean, I want to know how to clear the multibuffer after received a data . Or how to clear the multi buffer data before next async read? I cant find the function like clear or other function to do this.

Best regards,
Lv Linsong

Most helpful comment

beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
...
beast::multi_buffer b;
ws.read(b);
std::cout << beast::buffers_to_string(b.data()) << std::endl;

// Clear the buffer
b.consume(b.size());

If you want to get a std::string, and you are using the latest Asio (from the boostorg/asio master branch) you can use boost::asio::dynamic_buffer to wrap a std::string and pass that instead:

beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
...
std::string s;
auto b = boost::asio::dynamic_buffer(s);
ws.read(b);
std::cout << s << std::endl;

// Clear the string
s.clear();

All 11 comments

beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
...
beast::multi_buffer b;
ws.read(b);
std::cout << beast::buffers_to_string(b.data()) << std::endl;

// Clear the buffer
b.consume(b.size());

If you want to get a std::string, and you are using the latest Asio (from the boostorg/asio master branch) you can use boost::asio::dynamic_buffer to wrap a std::string and pass that instead:

beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
...
std::string s;
auto b = boost::asio::dynamic_buffer(s);
ws.read(b);
std::cout << s << std::endl;

// Clear the string
s.clear();

@Vinniefalco, You are very kind , It works . Thank you very much.

Glad to help!

It looks like this has been resolved so I'll go ahead and close the issue. If you feel that more explanation is needed feel free to re-open it, or to create a new one - thanks!

Hi, can you help me? I try get unsigned char p; from binary read:
void
on_read(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
//boost::ignore_unused(bytes_transferred);
if (ec)
return fail(ec, "read");
// get *p1 pointer to received data...
std::size_t s1 = boost::asio::buffer_size(buffer_);
unsigned char
p1 = boost::asio::buffer_cast(buffer_);
.....

but i not understand how i can read binary /raw data from websocket

If buffer_ is boost::beast::flat_buffer then you can write boost::beast::buffers_front(buffer_).data() to get a pointer, and boost::beast::buffers_front(buffer_).size() to get the size:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__beast__buffers_front.html

@vinniefalco Could you please tell, how can I use the dynamic_string_buffer to read into it several times?

beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
...
std::string s;
auto b = boost::asio::dynamic_buffer(s);
ws.read(b);
std::cout << s << std::endl;

// Clear the string
s.clear();

In this example, If I call s.clear(), the next time I read, the s.size returns 0, although s.data() returns received data.

asio's dynamic buffers dont' work with beast yet.

I don't quite understand what it means "they don't work". Now there is no way to read to the buffer more than once, do you mean this?

Yes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fpingas picture fpingas  路  7Comments

monada99 picture monada99  路  5Comments

djarek picture djarek  路  6Comments

shuras109 picture shuras109  路  6Comments

MarcoRhayden picture MarcoRhayden  路  6Comments