gpt4 book ai didi

c++ - 读写超时

转载 作者:可可西里 更新时间:2023-11-01 02:52:24 26 4
gpt4 key购买 nike

我一直在寻找一种方法来取消 Boost ASIO 读取或写入操作,如果它占用一定的时间。我的服务器正在发送 HTTP 请求,并从这些请求中读取结果,所以我最初将其编码为同步读/写,如果花费这么长时间,我会继续进行并在结果返回时忽略它们。如果服务器出现故障,这会导致问题,我的服务器会打开许多​​套接字,然后崩溃。所以我决定如果延迟太长我想取消读/写,但显然同步读/写不能在不破坏它们运行的​​线程的情况下取消,我不想这样做。所以我找到了一篇关于如何使用异步调用模拟同步读/写并在超时时取消调用的帖子。 This是我关注的帖子。我知道这篇文章已经很老了,所以我不确定自那个版本和我正在使用的版本(1.48)以来函数调用是否发生了变化,但这似乎不太正确。这是我的代码

bool connection::query_rtb(const std::string &request_information, std::string &reply_information)
{
try
{
boost::optional<boost::system::error_code> timer1_result, timer2_result, write_result, read_result;
boost::array<char,8192> buf;
buf.assign(0);

boost::asio::deadline_timer dt(io_service_);
dt.expires_from_now(boost::posix_time::milliseconds(100));
dt.async_wait(boost::bind(&connection::set_result, this, &timer1_result, _1, "timer1"));
boost::asio::async_write(socket_, boost::asio::buffer(request_information, request_information.size()), boost::bind(&connection::set_result, this, &write_result, _1, "write"));
io_service_.reset();

while(io_service_.run_one())
{
if(write_result)
{
dt.cancel();
}
else if(timer1_result)
{
socket_.cancel();
}
}


boost::asio::deadline_timer dt2(io_service_);
dt2.expires_from_now(boost::posix_time::milliseconds(3000));
dt2.async_wait(boost::bind(&connection::set_result, this, &timer2_result, _1, "timer2"));
boost::asio::async_read(socket_, boost::asio::buffer(buf), boost::bind(&connection::set_result, this, &read_result, _1, "read"));
//socket_.async_receive(boost::asio::buffer(buf), boost::bind(&connection::set_result, this, &read_result, _1, "read"));

io_service_.reset();
while(io_service_.run_one())
{
if(read_result)
{
dt2.cancel();
}
if(timer2_result)
{
socket_.cancel();
}
}


reply_information = buf.data();
std::cout << reply_information << std::endl;
return true;
}catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

void persistent_connection::set_result(boost::optional<boost::system::error_code> *a, boost::system::error_code ec, std::string t)
{
std::cout << t << std::endl;
a->reset(ec);
}

我想知道是否有人看到这段代码有什么问题,或者对为什么它不起作用有任何想法。目前写入似乎没问题,但是直到 dt2 完成其计时器后才会读取。如果您需要更多信息,请告诉我,我很乐意提供一些信息。

编辑:

似乎我让它开始测试我认为我以前测试过的东西。使用 async_receive 而不是 async_read 似乎解决了我遇到的任何问题。任何线索为什么这会导致我出现问题?我想知道我的逻辑是否有问题,或者这是否是 async_read 通常的行为方式。

最佳答案

boost::array<char,8192> buf;

...

boost::asio::async_read(socket_, boost::asio::buffer(buf), boost::bind(&connection::set_result, this, &read_result, _1, "read"));

您已指示您的程序从套接字读取 8192 字节。如果将逻辑从使用 async_read() 自由函数切换到 async_receive() 成员函数可以解决此问题,请咨询 the documentation

Remarks

The receive operation may not receive all of the requested number of bytes. Consider using the async_read function if you need to ensure that the requested amount of data is received before the asynchronous operation completes.

关于c++ - 读写超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com