在使用boost :: asio时,我遇到了一个有趣的时刻。问题是我想进行语音聊天并使用OpenAL,我将捕获的数据保存在ALubyte中:
ALubyte* data; // data has the captured data now
int data_size;
所以我发送数据将其转换为char *
boost::asio::streambuf m_buf;
std::ostream out(&m_buf);
out << data_size << ';'; // the simbol ';' is used to separate data from each other
out.write((char*)data, data_size);
out << 'q'; // the simbol 'q' is used for a client side to understand that
//this is the end of the stream
// as a client where the 'out' stream is sent to,
//uses boost::async_read_until --> ooops
boost::asio::async_write(client_sock, m_buf, [](){});
正如您可能已经猜到的那样,问题出在boost :: asio :: async_read_until
//in a client side
boost::asio::streambuf client_buf;
std::istream in(&client_buf);
boost::asio::async_read_until(m_sock, client_buf, 'q', [](){});//---> oooops,
// this boost::asio::async_read_until
//function reads until the first 'q' simbol which 'data' might
//have a lot of 'q' simbol as the captured data is stored as
// a char type
//so the function does not read the whole captured data.
伙计们我知道这是我自己选择这个功能的最大错误,但我现在不能改变它,因为代码有点大 所以你有一些想法在这种情况下该做什么,以便我可以读取整个捕获的数据,或者你认为没有办法 out而不更改boost :: asio :: async_read_until函数?任何想法都非常感谢!!!
答案 0 :(得分:1)
而不是simbol&#39; q,更好的选择是添加更多的符号,例如&#34; cpp&#34; 在这种情况下,将捕获的数据保存在char类型中的数据可能没有相同的签名,&#39; q&#39; simbols可能在数据中很多,但使用&#34; cpp&#34; ...等风险将少于一个&#39; q&#39; simbol,这意味着添加的符号越多,风险越小!