rawbusmessage
This commit is contained in:
@@ -13,14 +13,14 @@ namespace softbus::message_bus::pipeline
|
||||
|
||||
struct EndpointFramingState
|
||||
{
|
||||
softbus::core::memory::LockFreeQueue<PipelineContext> chunkQ;
|
||||
softbus::core::memory::LockFreeQueue<softbus::core::models::RawBusMessage> chunkQ;
|
||||
std::atomic<bool> running{true};
|
||||
std::thread th;
|
||||
std::uint64_t nextSeq{1};
|
||||
QString endpoint;
|
||||
std::shared_ptr<softbus::core::memory::MemoryPool> pool;
|
||||
std::function<void(PipelineContext&, std::vector<PipelineContext>&)> feed;
|
||||
std::function<void(PipelineContext)> pushFramed;
|
||||
std::function<void(softbus::core::models::RawBusMessage&, std::vector<softbus::core::models::RawBusMessage>&)> feed;
|
||||
std::function<void(QString, std::uint64_t, softbus::core::models::RawBusMessage)> pushFramed;
|
||||
|
||||
explicit EndpointFramingState(std::size_t cap)
|
||||
: chunkQ(cap)
|
||||
@@ -34,19 +34,19 @@ namespace
|
||||
void endpointFramerThread(std::shared_ptr<EndpointFramingState> st)
|
||||
{
|
||||
while (st->running.load(std::memory_order_acquire)) {
|
||||
PipelineContext ch;
|
||||
softbus::core::models::RawBusMessage ch;
|
||||
if (!st->chunkQ.pop(ch)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
std::vector<PipelineContext> out;
|
||||
std::vector<softbus::core::models::RawBusMessage> out;
|
||||
if (st->feed) {
|
||||
st->feed(ch, out);
|
||||
}
|
||||
for (auto& x : out) {
|
||||
x.frameSeq = st->nextSeq++;
|
||||
const std::uint64_t seq = st->nextSeq++;
|
||||
if (st->pushFramed) {
|
||||
st->pushFramed(std::move(x));
|
||||
st->pushFramed(st->endpoint, seq, std::move(x));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,7 +113,7 @@ void PipelineEngine::start()
|
||||
// 合并队列是用来存储合并的帧的
|
||||
// 合并线程是用来处理合并的帧的
|
||||
if (m_rtConfig.parallelPipeline) {
|
||||
m_framedQ = std::make_unique<softbus::core::threading::MutexQueue<PipelineContext>>(m_rtConfig.framedQueueMax);
|
||||
m_framedQ = std::make_unique<softbus::core::threading::MutexQueue<MergeItem>>(m_rtConfig.framedQueueMax);
|
||||
for (int i = 0; i < m_rtConfig.parseWorkerCount; ++i) {
|
||||
// 创建解析线程 解析线程从帧队列中取出帧 进行处理
|
||||
// emplace_back 是用来创建解析线程的 参数是 lambda 表达式
|
||||
@@ -187,7 +187,7 @@ void PipelineEngine::stop()
|
||||
// 逻辑 :从 ingress 队列中取出数据 直到队列为空
|
||||
void PipelineEngine::drain()
|
||||
{
|
||||
PipelineContext tmp;
|
||||
softbus::core::models::RawBusMessage tmp;
|
||||
while (m_ingressQ.pop(tmp)) {
|
||||
}
|
||||
}
|
||||
@@ -195,7 +195,7 @@ void PipelineEngine::drain()
|
||||
// 参数 :ctx 数据
|
||||
// 返回值 :是否成功
|
||||
// 逻辑 :如果并行管道启用 则根据端点确保帧解析器 然后推送到帧队列 如果并行管道未启用 则直接推送到 ingress 队列
|
||||
bool PipelineEngine::enqueueIngress(PipelineContext ctx)
|
||||
bool PipelineEngine::enqueueIngress(softbus::core::models::RawBusMessage ctx)
|
||||
{
|
||||
if (m_rtConfig.parallelPipeline) {
|
||||
if (ctx.endpoint.isEmpty()) {
|
||||
@@ -227,7 +227,7 @@ bool PipelineEngine::enqueueIngress(PipelineContext ctx)
|
||||
// 参数 :ctx 数据
|
||||
// 返回值 :是否成功
|
||||
// 逻辑 :将数据推送到 egress 队列
|
||||
bool PipelineEngine::enqueueEgress(PipelineContext ctx)
|
||||
bool PipelineEngine::enqueueEgress(softbus::core::models::RawBusMessage ctx)
|
||||
{
|
||||
return m_egressQ.push(std::move(ctx));
|
||||
}
|
||||
@@ -235,7 +235,7 @@ bool PipelineEngine::enqueueEgress(PipelineContext ctx)
|
||||
// 参数 :out 数据
|
||||
// 返回值 :是否成功
|
||||
// 逻辑 :从 egress 队列中取出数据 如果队列为空 则返回false 如果队列不为空 则返回true
|
||||
bool PipelineEngine::tryPopEgress(PipelineContext& out)
|
||||
bool PipelineEngine::tryPopEgress(softbus::core::models::RawBusMessage& out)
|
||||
{
|
||||
return m_egressQ.pop(out);
|
||||
}
|
||||
@@ -262,14 +262,19 @@ void PipelineEngine::ensureEndpointFramer(const QString& endpoint)
|
||||
auto st = std::make_shared<EndpointFramingState>(qcap > 0 ? qcap : 4096);
|
||||
st->endpoint = endpoint;
|
||||
st->pool = m_pool;
|
||||
st->feed = [this](PipelineContext& in, std::vector<PipelineContext>& out) {
|
||||
st->feed = [this](softbus::core::models::RawBusMessage& in, std::vector<softbus::core::models::RawBusMessage>& out) {
|
||||
this->dispatchFramer(in, out);
|
||||
};
|
||||
st->pushFramed = [this](PipelineContext c) {
|
||||
st->pushFramed = [this](QString endpoint, std::uint64_t seq, softbus::core::models::RawBusMessage c) {
|
||||
if (!m_framedQ) {
|
||||
return;
|
||||
}
|
||||
if (!m_framedQ->tryPush(std::move(c))) {
|
||||
MergeItem item;
|
||||
item.endpoint = std::move(endpoint);
|
||||
item.seq = seq;
|
||||
item.ctx = std::move(c);
|
||||
item.pipelineOk = true;
|
||||
if (!m_framedQ->tryPush(std::move(item))) {
|
||||
++m_counters.drop;
|
||||
LOG_WARNING() << "PipelineEngine: framed queue full drop";
|
||||
}
|
||||
@@ -281,7 +286,8 @@ void PipelineEngine::ensureEndpointFramer(const QString& endpoint)
|
||||
// 参数 :chunkIn 数据
|
||||
// 返回值 :无
|
||||
// 逻辑 :根据协议提示 选择合适的帧解析器 进行帧解析
|
||||
void PipelineEngine::dispatchFramer(PipelineContext& chunkIn, std::vector<PipelineContext>& completeFramesOut)
|
||||
void PipelineEngine::dispatchFramer(
|
||||
softbus::core::models::RawBusMessage& chunkIn, std::vector<softbus::core::models::RawBusMessage>& completeFramesOut)
|
||||
{
|
||||
if (m_pluginManager) {
|
||||
auto plugin = m_pluginManager->selectProtocolFramerPlugin(chunkIn.protocolHint);
|
||||
@@ -307,29 +313,26 @@ void PipelineEngine::dispatchFramer(PipelineContext& chunkIn, std::vector<Pipeli
|
||||
}
|
||||
}
|
||||
// 功能 :分配帧序列号
|
||||
void PipelineEngine::assignFrameSequence(PipelineContext& ctx)
|
||||
std::uint64_t PipelineEngine::assignFrameSequence(const QString& endpoint)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_seqMutex);
|
||||
ctx.frameSeq = ++m_nextFrameSeqByEndpoint[ctx.endpoint];
|
||||
return ++m_nextFrameSeqByEndpoint[endpoint];
|
||||
}
|
||||
|
||||
void PipelineEngine::runSingleWorker()
|
||||
{
|
||||
// 单线程工作线程 从 ingress 队列中取出数据 进行处理 然后推送到 egress 队列中
|
||||
while (m_running.load(std::memory_order_acquire)) {
|
||||
PipelineContext ctx;
|
||||
softbus::core::models::RawBusMessage ctx;
|
||||
if (!m_ingressQ.pop(ctx)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<PipelineContext> batch;
|
||||
std::vector<softbus::core::models::RawBusMessage> batch;
|
||||
dispatchFramer(ctx, batch);
|
||||
|
||||
for (auto& c : batch) {
|
||||
if (c.frameKind != FrameKind::CompleteFrame) {
|
||||
continue;
|
||||
}
|
||||
//
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setfill('0');
|
||||
@@ -342,8 +345,8 @@ void PipelineEngine::runSingleWorker()
|
||||
const QString hexPreview = QString::fromStdString(oss.str());
|
||||
LOG_INFO() << "the data is"<< hexPreview;
|
||||
// 分配帧序列号
|
||||
assignFrameSequence(c);
|
||||
if (!runStages234(c)) {
|
||||
const bool ok = runStages234(c);
|
||||
if (!ok) {
|
||||
++m_counters.error;
|
||||
continue;
|
||||
}
|
||||
@@ -359,9 +362,8 @@ void PipelineEngine::runSingleWorker()
|
||||
void PipelineEngine::parseWorkerLoop()
|
||||
{
|
||||
while (true) {
|
||||
PipelineContext ctx;
|
||||
|
||||
const bool have = m_framedQ && m_framedQ->popWaitFor(ctx, m_running, std::chrono::milliseconds(50)); // 从帧队列中取出帧 , 如果帧队列为空 则等待50毫秒
|
||||
MergeItem item;
|
||||
const bool have = m_framedQ && m_framedQ->popWaitFor(item, m_running, std::chrono::milliseconds(50));
|
||||
if (!have) {
|
||||
if (!m_running.load(std::memory_order_acquire)) {
|
||||
break;
|
||||
@@ -369,21 +371,16 @@ void PipelineEngine::parseWorkerLoop()
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
const bool ok = runStages234(ctx);
|
||||
const bool ok = runStages234(item.ctx);
|
||||
if (m_rtConfig.orderedEgress && m_mergeQ) {
|
||||
MergeItem it;
|
||||
it.endpoint = ctx.endpoint;
|
||||
it.seq = ctx.frameSeq;
|
||||
it.pipelineOk = ok;
|
||||
it.ctx = std::move(ctx);
|
||||
m_mergeQ->push(std::move(it));
|
||||
item.pipelineOk = ok;
|
||||
m_mergeQ->push(std::move(item));
|
||||
} else {
|
||||
if (!ok) {
|
||||
++m_counters.error;
|
||||
continue;
|
||||
}
|
||||
if (!m_egressQ.push(std::move(ctx))) {
|
||||
if (!m_egressQ.push(std::move(item.ctx))) {
|
||||
++m_counters.drop;
|
||||
continue;
|
||||
}
|
||||
@@ -426,7 +423,7 @@ void PipelineEngine::ingestMergeItem(MergeItem item)
|
||||
auto pr = st.buffer.take(st.nextExpected);
|
||||
++st.nextExpected;
|
||||
const bool pipelineOk = pr.second;
|
||||
PipelineContext ctx = std::move(pr.first);
|
||||
softbus::core::models::RawBusMessage ctx = std::move(pr.first);
|
||||
if (!pipelineOk) {
|
||||
++m_counters.error;
|
||||
continue;
|
||||
@@ -443,11 +440,8 @@ void PipelineEngine::ingestMergeItem(MergeItem item)
|
||||
// 参数 :ctx 数据
|
||||
// 返回值 :是否成功
|
||||
// 逻辑 :如果帧类型不是完整帧 则返回false 如果阶段2解析失败 则返回false 如果阶段3过滤失败 则返回false 如果阶段4验证失败 则返回false 如果都成功 则返回true
|
||||
bool PipelineEngine::runStages234(PipelineContext& ctx)
|
||||
bool PipelineEngine::runStages234(const softbus::core::models::RawBusMessage& ctx)
|
||||
{
|
||||
if (ctx.frameKind != FrameKind::CompleteFrame) {
|
||||
return false;
|
||||
}
|
||||
auto messages = stage2Parser(ctx);
|
||||
LOG_INFO() << "PipelineEngine: stage2Parser messages size=" << messages.size();
|
||||
if (messages.empty()) {
|
||||
@@ -478,7 +472,7 @@ bool PipelineEngine::stage4Validator(const softbus::core::models::DOMMessage& me
|
||||
// 参数 :ctx 数据
|
||||
// 返回值 :是否成功
|
||||
// 逻辑 :如果插件管理器为空 则返回true 如果插件管理器不为空 则选择合适的插件 创建会话 如果会话为空 则返回false 如果会话不为空 则返回true
|
||||
std::vector<softbus::core::models::DOMMessage> PipelineEngine::stage2Parser(const PipelineContext& ctx)
|
||||
std::vector<softbus::core::models::DOMMessage> PipelineEngine::stage2Parser(const softbus::core::models::RawBusMessage& ctx)
|
||||
{
|
||||
if (!m_pluginManager) {
|
||||
return {};
|
||||
@@ -505,7 +499,7 @@ std::vector<softbus::core::models::DOMMessage> PipelineEngine::stage2Parser(cons
|
||||
parserSession = it.value();
|
||||
}
|
||||
softbus::message_bus::pipeline::protocol::ProtocolEnvelope envelope; // 协议封装
|
||||
if (!parserSession->parse(ctx.raw(), envelope)) {
|
||||
if (!parserSession->parse(ctx, envelope)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -540,7 +534,7 @@ bool PipelineEngine::stage3Filter(softbus::core::models::DOMMessage& message)
|
||||
}
|
||||
|
||||
softbus::core::models::EnrichedMessage PipelineEngine::applyTransformAndEnrich(
|
||||
const PipelineContext& rawCtx, softbus::core::models::DOMMessage& message) const
|
||||
const softbus::core::models::RawBusMessage& rawCtx, softbus::core::models::DOMMessage& message) const
|
||||
{
|
||||
auto rules = softbus::message_bus::pipeline::DynamicRuleRegistry::instance().snapshotTransformRules(
|
||||
rawCtx.endpoint, rawCtx.deviceId, rawCtx.stableKey);
|
||||
|
||||
Reference in New Issue
Block a user