0
所以我想實現定時http連接Keep-Alive。而且我需要能夠在一段時間內殺死它。所以目前我有(或至少我想有):如何讀取asio緩衝區從一個套接字讀取`1`字節而不是從另一個讀取`read_some`?
void http_request::timed_receive_base(boost::asio::ip::tcp::socket& socket, int buffer_size, int seconds_to_wait, int seconds_to_parse)
{
this->clear();
http_request_parser_state parser_state = METHOD;
char* buffer = new char[buffer_size];
std::string key = "";
std::string value = "";
boost::asio::ip::tcp::iostream stream;
stream.rdbuf()->assign(boost::asio::ip::tcp::v4(), socket.native());
try
{
do
{
stream.expires_from_now(boost::posix_time::seconds(seconds_to_wait));
int bytes_read = stream.read_some(boost::asio::buffer(buffer, buffer_size));
stream.expires_from_now(boost::posix_time::seconds(seconds_to_parse));
if (stream) // false if read timed out or other error
{
parse_buffer(buffer, parser_state, key, value, bytes_read);
}
else
{
throw std::runtime_error("Waiting for 2 long...");
}
} while (parser_state != OK);
}
catch (...)
{
delete buffer;
throw;
}
delete buffer;
}
但在TCP :: iostream的無read_some,所以編譯器給我一個錯誤:
Error 1 error C2039: 'read_some' : is not a member of 'boost::asio::basic_socket_iostream<Protocol>'
這就是爲什麼我不知道 - 如何通過stream.read
(如stream.read(buffer, 1);
)讀取1個字節,比read_some
通過套接字API是非常緩衝區(它看起來像int bytes_read = socket.read_some(boost::asio::buffer(buffer, buffer_size));
,比打電話給我用實際bytes_read
值parse_buffer
功能)
順便說一句,似乎會有一個真正令人難過的問題,最後1個字節..(