2026-03-26 17:22:52 +08:00
|
|
|
#pragma once
|
2026-03-30 11:11:05 +08:00
|
|
|
// 定义管道引擎 包含队列、运行状态、工作线程、计数器、插件管理器
|
2026-03-26 17:22:52 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <thread>
|
2026-03-31 11:02:36 +08:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include <QHash>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QString>
|
2026-03-26 17:22:52 +08:00
|
|
|
|
|
|
|
|
#include "core/plugin_system/PluginManager.h"
|
2026-04-07 14:20:04 +08:00
|
|
|
#include "core/plugin_system/IProtocolFramerPlugin.h"
|
2026-03-26 17:22:52 +08:00
|
|
|
#include "core/memory/LockFreeQueue.h"
|
2026-03-31 11:02:36 +08:00
|
|
|
#include "core/memory/MemoryPool.h"
|
|
|
|
|
#include "core/threading/MutexQueue.h"
|
2026-03-26 17:22:52 +08:00
|
|
|
#include "message_bus/pipeline/PipelineContext.h"
|
2026-03-31 11:02:36 +08:00
|
|
|
#include "message_bus/pipeline/stages/1_framers/PassthroughFramer.h"
|
2026-03-26 17:22:52 +08:00
|
|
|
|
|
|
|
|
namespace softbus::message_bus::pipeline
|
|
|
|
|
{
|
|
|
|
|
|
2026-03-31 11:02:36 +08:00
|
|
|
struct PipelineRuntimeConfig
|
|
|
|
|
{
|
|
|
|
|
bool parallelPipeline{false};
|
|
|
|
|
int parseWorkerCount{2};
|
|
|
|
|
bool orderedEgress{true};
|
|
|
|
|
|
|
|
|
|
std::size_t framedQueueMax{4096};
|
|
|
|
|
std::size_t maxReorderDepth{256};
|
|
|
|
|
|
2026-04-07 14:20:04 +08:00
|
|
|
std::size_t modbusMaxAdu{256};
|
|
|
|
|
int modbusInterFrameTimeoutMs{0};
|
2026-03-31 11:02:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EndpointFramingState;
|
|
|
|
|
|
2026-03-26 17:22:52 +08:00
|
|
|
class PipelineEngine
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct Counters
|
|
|
|
|
{
|
|
|
|
|
std::atomic<std::uint64_t> in{0};
|
|
|
|
|
std::atomic<std::uint64_t> out{0};
|
|
|
|
|
std::atomic<std::uint64_t> drop{0};
|
|
|
|
|
std::atomic<std::uint64_t> error{0};
|
2026-03-31 11:02:36 +08:00
|
|
|
std::atomic<std::uint64_t> mergeDrop{0};
|
2026-03-26 17:22:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit PipelineEngine(std::size_t queueCapacity = 4096);
|
|
|
|
|
~PipelineEngine();
|
|
|
|
|
|
|
|
|
|
void start();
|
|
|
|
|
void stop();
|
|
|
|
|
void drain();
|
|
|
|
|
|
|
|
|
|
bool enqueueIngress(PipelineContext ctx);
|
|
|
|
|
bool enqueueEgress(PipelineContext ctx);
|
|
|
|
|
|
|
|
|
|
bool tryPopEgress(PipelineContext& out);
|
|
|
|
|
void setPluginManager(std::shared_ptr<softbus::core::plugin_system::PluginManager> pluginManager);
|
2026-03-31 11:02:36 +08:00
|
|
|
void setMemoryPool(std::shared_ptr<softbus::core::memory::MemoryPool> pool);
|
|
|
|
|
void setRuntimeConfig(PipelineRuntimeConfig cfg);
|
|
|
|
|
void loadRuntimeConfigFromJson(const QJsonObject& obj);
|
2026-03-26 17:22:52 +08:00
|
|
|
|
|
|
|
|
const Counters& counters() const { return m_counters; }
|
|
|
|
|
|
|
|
|
|
private:
|
2026-03-31 11:02:36 +08:00
|
|
|
struct MergeItem
|
|
|
|
|
{
|
|
|
|
|
QString endpoint;
|
|
|
|
|
std::uint64_t seq{0};
|
|
|
|
|
PipelineContext ctx;
|
|
|
|
|
bool pipelineOk{false};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MergeState
|
|
|
|
|
{
|
|
|
|
|
std::uint64_t nextExpected{1};
|
|
|
|
|
QHash<std::uint64_t, std::pair<PipelineContext, bool>> buffer;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void runSingleWorker();
|
|
|
|
|
void parseWorkerLoop();
|
|
|
|
|
void mergerLoop();
|
|
|
|
|
|
|
|
|
|
void ensureEndpointFramer(const QString& endpoint);
|
|
|
|
|
|
|
|
|
|
void dispatchFramer(PipelineContext& chunkIn, std::vector<PipelineContext>& completeFramesOut);
|
|
|
|
|
void assignFrameSequence(PipelineContext& ctx);
|
|
|
|
|
bool runStages234(PipelineContext& ctx);
|
|
|
|
|
void ingestMergeItem(MergeItem item);
|
|
|
|
|
|
2026-03-26 17:22:52 +08:00
|
|
|
bool stage2Parser(PipelineContext& ctx);
|
|
|
|
|
bool stage3Filter(PipelineContext& ctx);
|
|
|
|
|
bool stage4Validator(PipelineContext& ctx);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
softbus::core::memory::LockFreeQueue<PipelineContext> m_ingressQ;
|
|
|
|
|
softbus::core::memory::LockFreeQueue<PipelineContext> m_egressQ;
|
|
|
|
|
std::atomic<bool> m_running{false};
|
2026-03-31 11:02:36 +08:00
|
|
|
std::thread m_worker;
|
|
|
|
|
Counters m_counters;
|
2026-03-26 17:22:52 +08:00
|
|
|
std::shared_ptr<softbus::core::plugin_system::PluginManager> m_pluginManager;
|
2026-03-31 11:02:36 +08:00
|
|
|
std::shared_ptr<softbus::core::memory::MemoryPool> m_pool;
|
|
|
|
|
|
|
|
|
|
PipelineRuntimeConfig m_rtConfig;
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<softbus::message_bus::pipeline::stages::framers::PassthroughFramer> m_passthroughFramer;
|
2026-04-07 14:20:04 +08:00
|
|
|
std::mutex m_framerSessionMutex;
|
|
|
|
|
QHash<QString, std::shared_ptr<softbus::core::plugin_system::IProtocolFramerSession>> m_framerSessions;
|
2026-03-31 11:02:36 +08:00
|
|
|
|
|
|
|
|
std::mutex m_seqMutex;
|
|
|
|
|
QHash<QString, std::uint64_t> m_nextFrameSeqByEndpoint;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<softbus::core::threading::MutexQueue<PipelineContext>> m_framedQ;
|
|
|
|
|
std::unique_ptr<softbus::core::threading::MutexQueue<MergeItem>> m_mergeQ;
|
|
|
|
|
std::vector<std::thread> m_parseThreads;
|
|
|
|
|
std::thread m_mergerThread;
|
|
|
|
|
|
|
|
|
|
std::mutex m_endpointMutex;
|
|
|
|
|
QHash<QString, std::shared_ptr<EndpointFramingState>> m_endpointFramers;
|
|
|
|
|
|
|
|
|
|
std::mutex m_mergeStateMutex;
|
|
|
|
|
QHash<QString, MergeState> m_mergeByEndpoint;
|
2026-04-02 11:18:17 +08:00
|
|
|
int m_count{0}; // 计数器
|
2026-03-12 14:56:53 +08:00
|
|
|
};
|
2026-03-26 17:22:52 +08:00
|
|
|
|
|
|
|
|
} // namespace softbus::message_bus::pipeline
|