【发布时间】:2017-05-04 22:21:14
【问题描述】:
我用 casablanca cpprestskd 开发了一个客户端服务器应用程序。 客户端每 5 分钟通过 POST 方法从其任务管理器(进程、cpu 使用情况等)发送信息到服务器。
该项目应该能够管理大约 100 个客户。 每次服务器收到 POST 请求时,他都会打开一个输出文件流(“uploaded.txt”),从客户端(登录名、密码)中提取一些初始信息,管理这些信息,将所有信息保存在与客户端同名的文件中(例如:client1.txt,client2.txt)在追加模式,最后回复客户端一个状态码。 这基本上是我在服务器端的 POST 句柄代码:
void Server::handle_post(http_request request)
{
auto fileBuffer =
std::make_shared<Concurrency::streams::basic_ostream<uint8_t>>();
try
{
auto stream = concurrency::streams::fstream::open_ostream(
U("uploaded.txt"),
std::ios_base::out | std::ios_base::binary).then([request, fileBuffer](pplx::task<Concurrency::streams::basic_ostream<unsigned char>> Previous_task)
{
*fileBuffer = Previous_task.get();
try
{
request.body().read_to_end(fileBuffer->streambuf()).get();
}
catch (const exception&)
{
wcout << L"<exception>" << std::endl;
//return pplx::task_from_result();
}
//Previous_task.get().close();
}).then([=](pplx::task<void> Previous_task)
{
fileBuffer->close();
//Previous_task.get();
}).then([](task<void> previousTask)
{
// This continuation is run because it is value-based.
try
{
// The call to task::get rethrows the exception.
previousTask.get();
}
catch (const exception& e)
{
wcout << e.what() << endl;
}
});
//stream.get().close();
}
catch (const exception& e)
{
wcout << e.what() << endl;
}
ManageClient();
request.reply(status_codes::OK, U("Hello, World!")).then([](pplx::task<void> t) { handle_error(t); });
return;
}
基本上它可以工作,但如果我尝试同时从适当的客户发送信息,有时它会工作,有时它不起作用。 显然,当我打开“uploaded.txt”流文件时会出现问题。 问题:
1) CASABLANCA http_listener 是真正的多任务处理吗?它能够处理多少任务? 2)我没有在文档中找到类似于我的斧头示例,唯一接近我的是“Casalence120”项目,但他使用 Concurrency::Reader_writer_lock 类(它似乎是互斥锁方法)。 我该怎么做才能管理多个 POST? 3)是否可以在开始打开uploaded.txt之前阅读一些客户端信息? 我可以直接使用客户端名称打开输出文件流。 4)如果我通过对upload.txt 文件的互斥锁访问,服务器变为顺序,我认为这不是使用cpprestsdk 的好方法。 我仍在接近 cpprestskd,因此任何建议都会有所帮助。
【问题讨论】:
标签: c++11 visual-studio-2013 client-server httplistener cpprest-sdk