streambuf

boost::asio read n bytes from socket to streambuf

自闭症网瘾萝莉.ら 提交于 2019-12-03 03:58:36
问题 I have a serialized structure, which is being sent via socket. I need to read it in chunks, since one of its fields contains the size of the data remaining: I need to read first few bytes, find out the length and read the rest. This is what I have got: boost::asio::streambuf buffer; boost::system::error_code err_code; // here I need to read only first 16 bytes boost::asio::read(socket, buffer, err_code); std::istream is(&buffer); boost::archive::binary_iarchive ia(is); ia >> my_struct; I have

boost::asio read n bytes from socket to streambuf

流过昼夜 提交于 2019-12-02 17:21:14
I have a serialized structure, which is being sent via socket. I need to read it in chunks, since one of its fields contains the size of the data remaining: I need to read first few bytes, find out the length and read the rest. This is what I have got: boost::asio::streambuf buffer; boost::system::error_code err_code; // here I need to read only first 16 bytes boost::asio::read(socket, buffer, err_code); std::istream is(&buffer); boost::archive::binary_iarchive ia(is); ia >> my_struct; I have taken a look at boost::asio::async_read(s, boost::asio::buffer(data, size), handler); but it can only

Writing a manipulator for a custom stream class

好久不见. 提交于 2019-12-02 07:27:01
问题 I've written a custom stream class that outputs indented text and that has manipulators that can change the indent level. All of the indenting work is implemented in a custom stream buffer class, which is used by the stream class. The buffer is working (i.e. text is indented in the output), but I can't get my manipulators to work. I was reading in a lot of places how ostream (which my class extends) overloads the operator<< like this: ostream& ostream::operator << ( ostream& (*op)(ostream&))

boost asio streambuf don't release memory after calling consume?

做~自己de王妃 提交于 2019-12-01 06:32:41
boost::asio::streambuf b; ... void handler(const boost::system::error_code& e, std::size_t size) { if (!e) { std::stringstream sstr(std::string((std::istreambuf_iterator<char>(&b)), std::istreambuf_iterator<char>())); b.consume(size); ... } } ... boost::asio::async_read_until(s, b, "END\r\n", handler); when the consume method is called, the memory occupied by streambuf b is not released. The memory will grow up as async_read_until is called multiple times. Is my usage correct? Is there any way to free the memory before the get pointer of streambuf? asio::streambuf is based on std::vector that

boost asio streambuf don't release memory after calling consume?

心不动则不痛 提交于 2019-12-01 05:04:36
问题 boost::asio::streambuf b; ... void handler(const boost::system::error_code& e, std::size_t size) { if (!e) { std::stringstream sstr(std::string((std::istreambuf_iterator<char>(&b)), std::istreambuf_iterator<char>())); b.consume(size); ... } } ... boost::asio::async_read_until(s, b, "END\r\n", handler); when the consume method is called, the memory occupied by streambuf b is not released. The memory will grow up as async_read_until is called multiple times. Is my usage correct? Is there any

Is it possible to “prepare” input from cin?

老子叫甜甜 提交于 2019-12-01 04:14:31
问题 In his answer, specifically in the linked Ideone example, @Nawaz shows how you can change the buffer object of cout to write to something else. This made me think of utilizing that to prepare input from cin , by filling its streambuf : #include <iostream> #include <sstream> using namespace std; int main(){ streambuf *coutbuf = cout.rdbuf(cin.rdbuf()); cout << "this goes to the input stream" << endl; string s; cin >> s; cout.rdbuf(coutbuf); cout << "after cour.rdbuf : " << s; return 0; } But

Reading from serial port with Boost Asio

回眸只為那壹抹淺笑 提交于 2019-11-29 04:36:05
I'm want to check for incoming data packages on the serial port, using boost.asio . Each data packet will start with a header that is one byte long, and will specify what type of the message has been sent. Each different type of message has its own length. The function I want to write should listen for new incoming messages continually, and when it finds one it should read it, and call some other function to parse it. My current code is as follows: void check_for_incoming_messages() { boost::asio::streambuf response; boost::system::error_code error; std::string s1, s2; if (boost::asio::read

redirect std::cout to a custom writer

为君一笑 提交于 2019-11-28 06:38:44
I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do something like while (! oss.eof()) { //add to window text somewhere } Here's the ostringstream docs but

Working with boost::asio::streambuf

半世苍凉 提交于 2019-11-27 21:00:52
问题 Looking for a boost::asio (and with himself boost) decided to write asynchronous server. To store incoming data I use boost::asio::streambuf. Here I have a problem. When I receive a second message from the client and subsequent I see that in the buffer contains a data from previous messages. Although I call Consume method at the input buffer. What's wrong? class tcp_connection // Using shared_ptr and enable_shared_from_this // because we want to keep the tcp_connection object alive // as long

C++ streams confusion: istreambuf_iterator vs istream_iterator?

牧云@^-^@ 提交于 2019-11-27 11:13:13
What is the difference between istreambuf_iterator and istream_iterator ? And in general what is the difference between streams and streambufs? I really can't find any clear explanation for this so decided to ask here. IOstreams use streambufs to as their source / target of input / output. Effectively, the streambuf-family does all the work regarding IO and the IOstream-family is only used for formatting and to-string / from-string transformation. Now, istream_iterator takes a template argument that says what the unformatted string-sequence from the streambuf should be formatted as, like