void run_iterator_test() { StreamBuf b; // create a sample data boost::array<char, 20000> sample; std::generate(sample.begin(), sample.end(), make_randgen(boost::as_literal("0123456789"))); std::copy(sample.begin(), sample.begin() + 12000, ybuffers_begin(b.prepare(12000))); b.commit(12000); std::copy(sample.begin() + 12000, sample.begin() + 14000, ybuffers_begin(b.prepare(2000))); b.commit(1000); std::copy(sample.begin() + 13000, sample.begin() + 15000, ybuffers_begin(b.prepare(2000))); b.commit(2000); std::copy(sample.begin() + 15000, sample.begin() + 20000, ybuffers_begin(b.prepare(5000))); b.commit(5000); typename StreamBuf::const_buffers_type bufs = b.data(); assert(boost::equal(boost::make_iterator_range(sample.begin(), sample.end()), boost::make_iterator_range(ybuffers_begin(bufs), ybuffers_end(bufs))) ); assert(ybuffers_end(bufs) - ybuffers_begin(bufs) == 20000); assert(ybuffers_begin(bufs) - ybuffers_end(bufs) == -20000); }
void run_streambuf_test() { StreamBuf b; assert(b.size() == 0); std::size_t q = ystreambuf::chunk_size; assert( size(b.prepare(q-2)) == q-2 ); assert( size(b.prepare(1)) == 1 ); assert( size(b.prepare(q)) == q ); assert( size(b.prepare(q+1)) == q+1 ); assert( size(b.data()) == 0 && 0 == b.size()); b.commit(1); assert( size(b.data()) == 1 && 1 == b.size()); b.commit(q-1); assert( size(b.data()) == q && q == b.size()); b.commit(1); assert( size(b.data()) == q+1 && q+1 == b.size()); b.consume(1); assert( size(b.data()) == q && q == b.size()); b.consume(q-1); assert( size(b.data()) == 1 && 1 == b.size()); assert( size(b.prepare(2*q-1)) == 2*q-1 ); b.consume(1); assert( size(b.data()) == 0 && 0 == b.size()); b.commit(q); assert( size(b.data()) == q && q == b.size()); b.commit(q-1); assert( size(b.data()) == 2*q-1 && 2*q-1 == b.size()); b.consume(2*q-1); assert( size(b.data()) == 0 && 0 == b.size()); }
/** * \param error Describes the error that occured, if any did. * \param bytes_transferred The amount of bytes transferred for this operation. */ void handleRead_(ErrorCode const & error, std::size_t bytes_transferred) { if (error) { throw Error(*this, error); // Otherwise check the received data for a whole message and process it. } else { read_buffer_.commit(bytes_transferred); while (auto message = Protocol::template consumeMessage<ReadMessage>(read_buffer_)) { handleMessage_(*message); } // Get more data. asyncRead_(); } }