如何解决如何通过zmq分段消息发送文件?
我正在尝试通过ZeroMQ基础结构发送视频,然后将视频分成多个块进行发送。当我将视频放入向量中以通过zmq::send_multipart
发送时,我会得到很高的RAM内存使用率,因此有时我会遇到分段错误错误。
令我头痛的是,当我在发送多部分消息并运行程序的行上进行注释时,向量是正常制作的,并且没有分割错误,尽管RAM内存的消耗不是那么大。 / p>
服务器代码:
#include <fstream>
#include <sstream>
#include <chrono>
#include <thread>
#include <iostream>
#include <future>
#include <zmq.hpp>
#include <zmq_addon.hpp>
using namespace std::chrono_literals;
const int size1MB = 1024 * 1024;
template <typename T>
void debug(T x)
{
std::cout << x << std::endl;
}
//Generate new chunks
std::unique_ptr<std::ofstream> createChunkFile(std::vector<std::string> &vecFilenames)
{
std::stringstream filename;
filename << "chunk" << vecFilenames.size() << ".mp4";
vecFilenames.push_back(filename.str());
return std::make_unique<std::ofstream>(filename.str(),std::ios::trunc);
}
//Split the file into chunks
void split(std::istream &inStream,int nMegaBytesPerChunk,std::vector<std::string> &vecFilenames)
{
std::unique_ptr<char[]> buffer(new char[size1MB]);
int nCurrentMegaBytes = 0;
std::unique_ptr<std::ostream> pOutStream = createChunkFile(vecFilenames);
while (!inStream.eof())
{
inStream.read(buffer.get(),size1MB);
pOutStream->write(buffer.get(),inStream.gcount());
++nCurrentMegaBytes;
if (nCurrentMegaBytes >= nMegaBytesPerChunk)
{
pOutStream = createChunkFile(vecFilenames);
nCurrentMegaBytes = 0;
}
}
}
int main()
{
zmq::context_t context(1);
zmq::socket_t socket(context,zmq::socket_type::rep);
socket.bind("tcp://*:5555");
std::ifstream img("video2.mp4",std::ifstream::in | std::ios::binary);
std::ifstream aux;
std::vector<std::string> vecFilenames;
std::vector<zmq::const_buffer> data;
std::ostringstream os;
std::async(std::launch::async,[&img,&vecFilenames]() {
split(img,100,vecFilenames);
});
img.close();
zmq::message_t message,aux2;
socket.recv(message,zmq::recv_flags::none);
//Put the chunks into the vector
std::async([&data,&aux,&os,&vecFilenames]() {
for (int i = 0; i < vecFilenames.size(); i++)
{
std::async([&aux,&i]() {
aux.open("chunk" + std::to_string(i) + ".mp4",std::ifstream::in | std::ios::binary);
});
os << aux.rdbuf();
data.push_back(zmq::buffer(os.str()));
os.clear();
aux.close();
}
});
//Send the vector for the client
std::async([&socket,&data] {
zmq::send_multipart(socket,data);
});
}
客户端:
#include <fstream>
#include <sstream>
#include <iostream>
#include <thread>
#include <chrono>
#include <string>
#include <zmq.hpp>
#include <zmq_addon.hpp>
#include <queue>
#include <deque>
#include <future>
#include <vector>
using namespace std::chrono_literals;
template <typename T>
void debug(T x)
{
std::cout << x << std::endl;
}
int main()
{
zmq::context_t context(1);
zmq::socket_t socket(context,zmq::socket_type::req);
socket.connect("tcp://localhost:5555");
std::ofstream img("teste.mp4",std::ios::out | std::ios::binary);
socket.send(zmq::buffer("ok\n"),zmq::send_flags::none);
std::vector<zmq::message_t> send_msgs;
zmq::message_t size;
std::async([&send_msgs,&img,&socket] {
zmq::recv_multipart(socket,std::back_inserter(send_msgs));
while (send_msgs.size())
{
img << send_msgs[0].to_string();
send_msgs.erase(send_msgs.begin());
}
});
}
解决方法
尝试通过多部分消息移动所有数据的确会将所有数据收集到一个巨大的BLOB中,再加上重复的O / S级特定于传输类的缓冲区,最可能的结果是崩溃。
将video-BLOB的各个块作为单独的简单消息有效载荷发送并重建BLOB(最好通过索引编号,可以选择重新请求未到达接收器端的任何部分)。
将此std::async
模式与REQ/REP
一起使用似乎有点棘手,因为此原型必须保持其dFSA交错的.recv()-。send()-。recv()-。send序列。 ()-...无限...如果不这样做,它将陷入无法挽回的相互僵局。
对于流式视频(例如CV /场景检测),还有更多技巧可以使用-其中之一是使用ZMQ_CONFLATE
选项,但只发送最近的视频帧,而不发送浪费时间在已经是历史的一部分的“过时”场景图像上,因此总是将最近的视频帧始终传递到接收端处理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。