我在不同线程中运行各种mqtt客户端并与代理通信客户端。我还以不同线程的形式模拟传感器数据,并在随机时间(通常以毫秒或几秒)触发它们。我发布了我面临问题的下面的代码。问题是有效负载消息并不总是被访问(订阅的消息数据)。有效载荷在复制到字符串时显示为空白。
{
std::lock_guard<std::mutex> l(sub_mtx);
msg_strm<<(char *) message->payload;
sub_msg=msg_strm.str();
}
std::cout<<sub_msg<<std::endl;
//}
int remaining_lngth;
if(message->qos !=0){
std::cout<<"QOS IS NOT ZERO HERE"<<std::endl;
remaining_lngth=2+topicLen+2+message->payloadlen;
} else{
remaining_lngth=2+topicLen+message->payloadlen;
}
// std::cout<<"YOUR PAYLOAD SUBSCRIBED IS"<< sub_msg<<std::endl;
{
std::lock_guard<std::mutex> l(sub_mtx);
std::cout << "PUBLISH MESSAGE LENGTH IS" << message->payloadlen << std::endl;
sub_dmp_pkt_strm << "Message Type : 8" << "\n";
sub_dmp_pkt_strm << "Duplicate Flag :" << message->dup << "\n";
sub_dmp_pkt_strm << "QoS Level : " << message->qos << "\n";
sub_dmp_pkt_strm << "RETAIN Flag : " << message->retained << "\n";
sub_dmp_pkt_strm << "Remaining Length :" << remaining_lngth << "\n";
sub_dmp_pkt_strm << "Topic Name String Length (MSB) : 0 " << "\n";
sub_dmp_pkt_strm << "Topic Name String Length (LSB) : " << topicLen << "\n";
sub_dmp_pkt_strm << "Topic Name : " << topicName << "\n";
sub_dmp_pkt_strm << "Message ID (MSB) : " << message->msgid << "\n";
sub_dmp_pkt_strm << "Message ID (LSB) : 0 " << "\n";
sub_dmp_pkt_strm << "Publish Message : " + sub_msg + "\n";
sub_dmp_pkt = sub_dmp_pkt_strm.str();
log_packet_event(clnt_id, "SUBSCRIBE", sub_dmp_pkt.c_str(), remaining_lngth);
}
然后我将整个数据包格式写入数据库。