我对Asio有一个简单的问题。
我写了三个课程response
,request
和client
。
我想将部分代码放在循环中以尝试连接 将有另一个用于创建请求的循环。
我无法想象for循环的正确位置。
这是我的代码:
// typedef const HTTPResponse&(*Callback) (const HTTPRequest& request,
typedef void (*Callback)(const HTTPRequest &request, const HTTPResponse &response, const system::error_code &ec);
#include <fstream>
// const HTTPResponse& handler(const HTTPRequest& request,
void handler(const HTTPRequest &request, const HTTPResponse &response, const system::error_code &ec) {
if (ec == 0) {
/*
if res.status_code == 200:
for chunk in res.iter_content(DEFAULT_BUFFER_SIZE) :
buffer.write(chunk)
Logger.info("Fetched {0} completed in {1}s".format(id, time.time() - start))
if len(buffer.getbuffer()) <= 0 :
Logger.info("Buffer for {0} is empty ".format(id))
return buffer.getbuffer()
else:
Logger.warn("Request to {0} failed with error code : {1} ".format(url, str(res.status_code)))
*/
std::ofstream outFile("image.bc5", std::ofstream::out, std::ofstream::binary);
outFile << response.get_response().rdbuf();
outFile.close();
} else if (ec == asio::error::operation_aborted) {
std::cout << "Request #" << request.get_id() << " has been cancelled by the user." << std::endl;
} else {
std::cout << "Request #" << request.get_id() << " failed! Error code = " << ec.value()
<< ". Error message = " << ec.message() << std::endl;
}
return response; //////?????????????
}
#include <iomanip>
#include <map>
#include <sstream>
void create_tasks(const symbols_enum &symbol, date day) {
int start = 0;
if (is_dst(day)) {
start = 1;
}
ostringstream oss;
oss << symbol;
std::string url_currency{ oss.str() };
std::ostringstream().swap(oss); // swap m with a default constructed stringstream
std::string url_year{ day.year() };
stringstream ss_month;
ss_month << setw(2) << setfill('0') << ((day.month().as_number()) - 1);
string url_month{ ss_month.str() };
std::stringstream().swap(ss_month); // swap m with a default constructed stringstream
// std::string url_month{std::to_string((day.month().as_number()) - 1)};
stringstream ss_day;
ss_day << setw(2) << setfill('0') << day.day().as_number();
string url_day{ ss_day.str() };
std::stringstream().swap(ss_day); // swap m with a default constructed stringstream
// std::string url_day{day.day()};
std::string URL{ protocol_host_URL + "/" + "datafeed" + "/" + url_currency + "/" + url_year + "/" + url_month +
"/" + url_day + "/" };
HTTPClient client;
// std::shared_ptr<HTTPRequest> request = client.create_request(hour, URL);
std::map<std::string, std::shared_ptr<HTTPRequest> > requests_variables;
// for (int i = 1; i < 3; ++i) {
// std::string varname = "sol" + std::to_string(i);
// variables[varname] = i * i;
//}
for (int hour = 0; hour < 24; hour++) {
stringstream ss_hour;
ss_hour << setw(2) << setfill('0') << hour;
string url_hour{ ss_hour.str() };
std::stringstream().swap(ss_hour); // swap m with a default constructed stringstream
URL = URL + url_hour + "h_ticks.bi5";
std::string request_name = "request_" + std::to_string(hour);
requests_variables[request_name] = client.create_request(hour, URL);
requests_variables[request_name]->set_callback(handler);
requests_variables[request_name]->execute();
/*
for (int i : boost::irange(0, ATTEMPTS))
{
try
{
requests_variables[request_name]->execute();
}
catch (system::system_error &e)
{
std::cout << "Error occured! Error code = " << e.code()
<< ". Message: " << e.what();
return e.code().value();
}
}
*/
}
tasks = [asyncio.ensure_future(get]
return tasks;
}
评论中的代码是循环的猜测位置。
可能有python代码,我试图模仿python异步代码,这个代码在这个地方“如果正确的地方”将在单词后转换。
我不需要转换为c ++,我只需要帮助确定在此代码中使用完成处理程序的试验循环的位置。
来自Boost Asio C ++的asio代码。
如果for循环代码应该在类request
或response
或client
中,请告诉我。
如果有人需要完整代码,请告诉我。我担心我会发布很多“不相关的代码”,否则:)