gpt4 book ai didi

c++ - 使用 C++ MJPEG 流媒体服务器时缺少 boost::asio 随机 header

转载 作者:行者123 更新时间:2023-11-28 04:08:58 36 4
gpt4 key购买 nike

我认为我正面临 boost::asio 线程安全问题。让我解释一下:

上下文:

我在 Ubuntu 14.04 上使用 Boost 1.68 和 G++ 4.8.4

我正在编写 C++ 代码来制作 MJPEG 服务器。

这是我的一些代码示例,它的工作原理非常简单:

代码解释

(1)填写HTTP头

(2) 以HandleMJPEG为回调调用doWrite

(3) doWrite 调用async_write 回调HandleMJPEG

(4) 处理MJPEG:

(4.1) 将 --jpegBoundary 添加到流中

(4.2) 回调 doWrite 再次调用 HandleMJPEG (4.3)

实际代码

void Connection::main()
{
streamInitReply(_reply); // (1)
doWrite(
std::bind(&net::Reply::toBuffers, _reply),
std::bind(&Connection::handleMJPEG, this),
"'MJPEG Stream init header'"); // (2)

}

void streamInitReply(net::Reply& rep)
{
rep.status = net::Reply::Status::ok;
rep.headers.clear();
rep.content.clear();
rep.headers.push_back(net::Header("Connection", "close"));
rep.headers.push_back(net::Header("Max-Age", "0"));
rep.headers.push_back(net::Header("Expires", "0"));
rep.headers.push_back(net::Header("Cache-Control", "no-cache"));
rep.headers.push_back(net::Header("Pragma", "no-cache"));

rep.headers.push_back(
net::Header(
"Content-Type",
(boost::format("multipart/x-mixed-replace; boundary=%1%") % jpegBoundary).str()
)
);

void Connection::handleMJPEG()
{


// Write Boundaries, then write the actual image buffer
// then recall handleMJPEG() to loop and keep writing incoming image

streamBoundariesReply(_reply); // (4.1)
auto self(shared_from_this());
doWrite( // (4.2)
std::bind(&net::Reply::contentToBuffers, _reply),
[this, self](){
imageReply(_server.getImage(), _reply);
doWrite(
std::bind(&net::Reply::toBuffersWithoutStatus, _reply),
std::bind(&Connection::handleMJPEG, this), // 4.3
"'MJPEG Image'"
);
},
"'MJPEG Boundary'"
);
}

void Connection::doWrite(
std::function<std::vector<boost::asio::const_buffer>()> toBuffer,
std::function<void()> callbackOnSuccess,
const std::string& logInfo)
{

auto self(shared_from_this());

boost::asio::async_write(
_socket,
toBuffer(),
boost::asio::bind_executor(strand_, [this, self, logInfo, callbackOnSuccess](const boost::system::error_code& ec, std::size_t)
{

if(ec != boost::system::errc::success)
{
doStop();
}
else
{
callbackOnSuccess(); // (3)
}
}
));
}

长话短说

问题如下:这段代码在 90% 的时间里都能正常工作。有时,当我想在浏览器(适用于 Linux 的 Firefox 66.0.3)上查看流时,没有显示图像,而是出现了一个“下载”弹出窗口。

当我调查这些包时,似乎有一些标题丢失或拼写错误:

1/一切正常时

here when everything is OK

2/不对的时候

here when it goes wrong

or here also

这就是为什么我怀疑 boost asio 中存在线程安全问题,但我不知道在哪里查看...

最佳答案

您有悬垂引用。您的问题是 std::bind

简介:当你有一个类Foo,它有成员函数bar:

class Foo {
void bar() {}
};

Foo f;

然后调用 std::bind(&Foo::bar, f)std::bind 返回一个存储 f 的仿函数> 作为拷贝。

现在让我们回到您的代码:

doWrite(
std::bind(&net::Reply::toBuffers, _reply), // <---- [1]
std::bind(&Connection::handleMJPEG, this),
"'MJPEG Stream init header'");

在 [1] 中,您调用 bind,它创建存储 _reply 拷贝的仿函数。在 doWrite 中:

void Connection::doWrite(
std::function<std::vector<boost::asio::const_buffer>()> toBuffer, // [2]
std::function<void()> callbackOnSuccess,
const std::string& logInfo)
{

auto self(shared_from_this());

boost::asio::async_write(
_socket,
toBuffer(), // [3]
boost::asio::bind_executor( ....
...
));
// [4]
}

在 [2] 中,toBuffer 是从 std::bind 返回的仿函数的包装器。 toBuffer 是局部变量。在第 [3] 行中,您调用 toBuffer() 返回 _reply 拷贝的 const-buffers - 它不引用原始 _reply ! async_write 立即返回 [4]。 doWrite 结束并且 toBuffer 被销毁,而 Boost-lib 机制 中的某处由 async_write 启动的任务工作并且它正在访问从 toBuffer 返回的常量缓冲区 - 这里有悬空引用。

您可以在调用 bind 时通过指针传递 _reply - 这样您将引用同一个 _reply 实例:

std::bind(&net::Reply::toBuffers, &_reply),

关于c++ - 使用 C++ MJPEG 流媒体服务器时缺少 boost::asio 随机 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221918/

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