당신이 찾고있는 것이 asio::streambuf이라고 생각합니다.
기본적으로, 당신은 실제로했다 consume(amount)
에 의해 처리하는 방법을 많이 알려 당신이 맞는 볼만큼 읽기, 숯불 * 같은 시드 streambuf의를 검사하고 있습니다. 클라이언트로 HTTP 헤더를 구문 분석하는 코드 예제 작업
: 프로토콜이 고정되어 나는 그것을 변경할 수 없습니다로
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
namespace asio = boost::asio;
std::string LINE_TERMINATION = "\r\n";
class Connection {
asio::streambuf _buf;
asio::ip::tcp::socket _socket;
public:
Connection(asio::io_service& ioSvc, asio::ip::tcp::endpoint server)
: _socket(ioSvc)
{
_socket.connect(server);
_socket.send(boost::asio::buffer("GET/HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"));
readMore();
}
void readMore() {
// Allocate 13 bytes space on the end of the buffer. Evil prime number to prove algorithm works.
asio::streambuf::mutable_buffers_type buf = _buf.prepare(13);
// Perform read
_socket.async_read_some(buf, boost::bind(
&Connection::onRead, this,
asio::placeholders::bytes_transferred, asio::placeholders::error
));
}
void onRead(size_t read, const boost::system::error_code& ec) {
if ((!ec) && (read > 0)) {
// Mark to buffer how much was actually read
_buf.commit(read);
// Use some ugly parsing to extract whole lines.
const char* data_ = boost::asio::buffer_cast<const char*>(_buf.data());
std::string data(data_, _buf.size());
size_t start = 0;
size_t end = data.find(LINE_TERMINATION, start);
while (end < data.size()) {
std::cout << "LINE:" << data.substr(start, end-start) << std::endl;
start = end + LINE_TERMINATION.size();
end = data.find(LINE_TERMINATION, start);
}
_buf.consume(start);
// Wait for next data
readMore();
}
}
};
int main(int, char**) {
asio::io_service ioSvc;
// Setup a connection and run
asio::ip::address localhost = asio::ip::address::from_string("127.0.0.1");
Connection c(ioSvc, asio::ip::tcp::endpoint(localhost, 80));
ioSvc.run();
}
불행하게도이 가능하지가 :( – losipiuk
@losipiuk 당신은 프로토콜 말을하는지 그래서 현재 양식에 메시지 크기를 나타내는 메타 데이터가 포함되어 있지 않습니다. 메시지의 시작 및 끝 구분 기호가 있습니까? 실제로 메시지가 완료되었다고 어떻게 판단합니까? – hatboyzero
아니오 - 고정 된 크기가 없습니다. 헤더입니다. 실제로 HTTP입니다. – losipiuk