2014-12-05 3 views
9

boost::asio::streambuf::consume()boost::asio::streambuf::commit() 호출을 이해하려고합니다. 입력 시퀀스에서 문자를 제거하는 consume() 전화 - 워드 프로세서에서, 우리는 예,언제 boost :: asio :: streambuf :: consume() 및 boost :: asio :: streambuf :: commit()을 호출할까요?

boost::asio::streambuf b; 
std::ostream os(&b); 
os << "Hello, World!\n"; 

// try sending some data in input sequence 
size_t n = sock.send(b.data()); 

b.consume(n); // sent data is removed from input sequence 

boost::asio::streambuf b; 

// reserve 512 bytes in output sequence 
boost::asio::streambuf::mutable_buffers_type bufs = b.prepare(512); 

size_t n = sock.receive(bufs); 

// received data is "committed" from output sequence to input sequence 
b.commit(n); 

std::istream is(&b); 
std::string s; 
is >> s; 

나는이 문서가 그들에 대해 말하는 것을 이해 내가만큼이 두 통화를 이해할 수있다 boost::asio::streambuf 안에 있고 boost::asio::streambuf의 출력 순서에서 입력 순서로 문자를 이동하려면 commit()으로 전화하십시오. 공정하다.

언제 이 실제로입니까? boost::asio::read_until() 소스를 보면, 우리는 문서가 말한대로, boost::asio::read_until()SyncReadStreamread_some()의 관점에서 구현됩니다, 당신은 그것을 볼 수 있습니다

template <typename SyncReadStream, typename Allocator> 
std::size_t read_until(SyncReadStream& s, 
    boost::asio::basic_streambuf<Allocator>& b, char delim, 
    boost::system::error_code& ec) 
{ 
    std::size_t search_position = 0; 
    for (;;) 
    { 
    // Determine the range of the data to be searched. 
    typedef typename boost::asio::basic_streambuf< 
     Allocator>::const_buffers_type const_buffers_type; 
    typedef boost::asio::buffers_iterator<const_buffers_type> iterator; 
    const_buffers_type buffers = b.data(); 
    iterator begin = iterator::begin(buffers); 
    iterator start_pos = begin + search_position; 
    iterator end = iterator::end(buffers); 

    // Look for a match. 
    iterator iter = std::find(start_pos, end, delim); 
    if (iter != end) 
    { 
     // Found a match. We're done. 
     ec = boost::system::error_code(); 
     return iter - begin + 1; 
    } 
    else 
    { 
     // No match. Next search can start with the new data. 
     search_position = end - begin; 
    } 

    // Check if buffer is full. 
    if (b.size() == b.max_size()) 
    { 
     ec = error::not_found; 
     return 0; 
    } 

    // Need more data. 
    std::size_t bytes_to_read = read_size_helper(b, 65536); 
    b.commit(s.read_some(b.prepare(bytes_to_read), ec)); 
    if (ec) 
     return 0; 
    } 
} 

있습니다. 도 boost::asio::read_until()에서의 문서도 SyncReadStream에서 '-

  1. SyncReadStream::read_some()

    이 어느 이들 boost::asio::streambuf::commit()
  2. 전화 않습니다 boost::asio::streambuf::commit()
  3. boost::asio::read_until()를 호출하지 않는 것을 말한다 나에게

    은 문서화되어야하는 것 님의 문서

  4. 내가 boost::asio::streambuf::commit()으로 전화해야하는지 여부를 모르겠다. 내 동기 코드와

나는 확실히 내가 무료 기능 boost::asio::read()boost::asio::read_until()를 호출하고있어하지 않을 때, 그것을 필요로하지 않는 것. 내 처리기의 비동기 코드에 주로 사용 된 예제가 있기 때문에 주로 처리기가 있지만,이 경우 호출하는 것은 확실하지 않습니다. boost::asio::streambufstringstreamstd::string과 함께 사용하려고 시도하면 commit()이 역할을 수행하지 않는 것 같습니다. streambufcommit()을 호출하지 않고 중단되거나 멈추는 것이 없습니다.

누구든지 나를 이것을 분류 할 수 있습니까?

+0

이에 대한 구체적인 문서를보고 싶습니다. 나는 아무것도 못 찾는다. – EML

답변

1

read_until은 구현시 read_some을 사용합니다. 따라서 read_some은 (직접) streambuf::commitread_until으로 전화하지 않습니다.

일반적으로 commitconsume을 호출 할 필요는 없지만 버퍼 데이터로 무엇인가를하려면이 방법이 좋습니다. 예를 들어, 바이너리 프로토콜을 사용하는 경우 read_until으로 제대로 확인할 수 없습니다.

4

Asio는 을 허용하는 숫자를 정의하고 asio::streambuf을 허용하며 preparecommit을 신경 쓰고 있습니다.한편

, 당신은 MutableBufferSequence concept의 모델을 수용 lower-level functionsasio::streambuf를 사용하려는 경우, 당신은 MutableBufferSequence 개념을 충족하는 객체를 반환하는 streambuf::prepare() 전화를해야, 버퍼로이 개체를 전달하고, 이후 이 함수는 그것을 채운다. commit()을 호출한다.

두 경우 모두 streambuf에서 n 바이트의 데이터를 읽은 후 입력 시퀀스를 사용하려면 consume(n) -에 전화해야합니다.