void udp_listener::listen_handler_( buffer_type bt, const boost::system::error_code& error, const size_t bytes_received ) { if ( error ) { static const int NO_ENOUGHT_BUFFER = 234; if ( error.value() == NO_ENOUGHT_BUFFER ) { enlarge_buffer_( bt ); register_listen_( bt, bytes_received ); } return; } if ( bytes_received == bt->size() && (*bt)[ bytes_received - 1 ] != '\0' ) { enlarge_buffer_( bt ); register_listen_( bt, bytes_received ); } else { message_type data( bt->begin(), bt->begin() + bytes_received ); message m; m.data = data; m.type_ = type_; func_(m); this->register_listen_( ); } }
/// @pre `n <= buf_.size()` static chunk_type get_chunk(buffer_type& buf, size_t n) { CAF_LOG_TRACE(CAF_ARG(buf) << CAF_ARG(n)); chunk_type xs; if (!buf.empty() && n > 0) { xs.reserve(std::min(n, buf.size())); if (n < buf.size()) { auto first = buf.begin(); auto last = first + static_cast<ptrdiff_t>(n); std::move(first, last, std::back_inserter(xs)); buf.erase(first, last); } else { std::move(buf.begin(), buf.end(), std::back_inserter(xs)); buf.clear(); } } return xs; }
void Tourist::read(tcp::socket &socket, buffer_type &buffer, size_t n, rw_handler_method method) { if (n > buffer.size()) { LOG_ERROR << "Read " << n << " bytes into " << buffer.size() << "-byte buffer"; return; } std::shared_ptr<Tourist> p_this = shared_from_this(); boost::asio::async_read(socket, boost::asio::buffer(buffer, n), [p_this, method, &socket, &buffer](const boost::system::error_code &error, size_t n) { LOG_VERBOSE << "[" << p_this->id_ << "] " << "Read " << n << " bytes"; IF_LOG(plog::verbose) { boost::algorithm::hex(buffer.begin(), buffer.begin() + n, std::ostream_iterator<char>(std::cout)); std::cout << std::endl; } if (method) { ((*p_this).*method)(error, n); } });
void instance::write(buffer_type& buf, message_type operation, uint32_t* payload_len, uint64_t operation_data, const node_id& source_node, const node_id& dest_node, actor_id source_actor, actor_id dest_actor, payload_writer* pw) { if (! pw) { binary_serializer bs{std::back_inserter(buf), &get_namespace()}; source_node.serialize(bs); dest_node.serialize(bs); bs.write(source_actor) .write(dest_actor) .write(uint32_t{0}) .write(static_cast<uint32_t>(operation)) .write(operation_data); } else { // reserve space in the buffer to write the payload later on auto wr_pos = static_cast<ptrdiff_t>(buf.size()); char placeholder[basp::header_size]; buf.insert(buf.end(), std::begin(placeholder), std::end(placeholder)); auto pl_pos = buf.size(); { // lifetime scope of first serializer (write payload) binary_serializer bs1{std::back_inserter(buf), &get_namespace()}; (*pw)(bs1); } // write broker message to the reserved space binary_serializer bs2{buf.begin() + wr_pos, &get_namespace()}; auto plen = static_cast<uint32_t>(buf.size() - pl_pos); source_node.serialize(bs2); dest_node.serialize(bs2); bs2.write(source_actor) .write(dest_actor) .write(plen) .write(static_cast<uint32_t>(operation)) .write(operation_data); if (payload_len) *payload_len = plen; } }
const_iterator begin() const { return buf_.begin(); }
iterator begin() { return buf_.begin(); }
void reset() { cur_ = buf_.begin(); end_ = buf_.end(); }
buffer(Elem const* from, size_t length) : buf_(static_cast<value_type const*>(from), static_cast<value_type const*>(from)+length) , cur_(buf_.begin()) , end_(buf_.end()) { }
buffer(InputIterator begin, InputIterator end) : buf_(begin, end) , cur_(buf_.begin()) , end_(buf_.end()) { }
buffer(Source const& source) : buf_(source.begin(), source.end()) , cur_(buf_.begin()) , end_(buf_.end()) { }
buffer(buffer&& source) : buf_(std::move(source.buf_)) , cur_(buf_.begin()) , end_(buf_.end()) { }
buffer() : buf_() , cur_(buf_.begin()) , end_(buf_.end()) { }
/// fills the buffer using the generator void fill_buffer(buffer_type& buffer) { for (typename buffer_type::iterator it=buffer.begin();it!=buffer.end();++it) *it=generator_(); }