如何从一个套接字读取 asio 缓冲区的“1”字节,并从另一个套接字读取“read_some”更多字节?
所以我正在尝试实现定时http连接Keep-Alive。我需要有能力在暂停时杀死它。所以目前我有(或者至少我想有):
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 读取 1 个字节。 read
(如 stream.read(buffer, 1);
),然后通过套接字 API 将 read_some
读取到该缓冲区(它看起来像 int字节读取= socket.read_some(boost::asio::buffer(buffer, buffer_size));
并使用真实的 bytes_read
值调用我的 parse_buffer
函数)
顺便说一句,看起来就像最后一个字节会出现一个非常悲伤的问题..(
So I am trying to implement timed http connection Keep-Alive. And I need to be capable of killing it on some time-out. So currently I have (or at least I would like to have):
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;
}
But there is no read_some in tcp::iostream, so compiler gives me an error:
Error 1 error C2039: 'read_some' : is not a member of 'boost::asio::basic_socket_iostream<Protocol>'
That is why I wonder - how to read 1 byte via stream.read
(like stream.read(buffer, 1);
) and than read_some
to that very buffer via socket API ( it would look like int bytes_read = socket.read_some(boost::asio::buffer(buffer, buffer_size));
and than call my parse_buffer
function with real bytes_read
value)
BTW it seems like there will be a really sad problem of 1 last byte..(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,有点粗暴,但是您阅读过文档吗?套接字 iostream 应该像普通 iostream 一样工作,例如 cin 和 cout。只需执行
stream >>变量
。也许您想要basic_stream_socket::read_some
?Sorry to be a bit rough, but did you read the documentation? The socket iostream is supposed to work like the normal iostream, like
cin
andcout
. Just dostream >> var
. Maybe you wantbasic_stream_socket::read_some
instead?