devicesbus read data
This commit is contained in:
36
src/message_bus/pipeline/PayloadRef.h
Normal file
36
src/message_bus/pipeline/PayloadRef.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include <QMetaType>
|
||||
|
||||
#include "core/memory/MemoryPool.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline
|
||||
{
|
||||
|
||||
struct PayloadRef
|
||||
{
|
||||
softbus::core::memory::MemoryPool::BlockPtr block;
|
||||
std::size_t offset{0};
|
||||
std::size_t length{0};
|
||||
|
||||
const std::uint8_t* bytes() const
|
||||
{
|
||||
if (!block || !block->data || offset > block->capacity) {
|
||||
return nullptr;
|
||||
}
|
||||
return block->data + offset;
|
||||
}
|
||||
|
||||
bool valid() const
|
||||
{
|
||||
return block && block->data && (offset + length <= block->capacity);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline
|
||||
|
||||
Q_DECLARE_METATYPE(softbus::message_bus::pipeline::PayloadRef)
|
||||
@@ -1,7 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder for pipeline context.
|
||||
class PipelineContext {
|
||||
public:
|
||||
PipelineContext() = default;
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QMetaType>
|
||||
#include <QString>
|
||||
|
||||
#include "message_bus/pipeline/PayloadRef.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline
|
||||
{
|
||||
|
||||
struct PipelineContext
|
||||
{
|
||||
int deviceId{-1}; // 设备ID
|
||||
QString stableKey;
|
||||
QString endpoint; // 端点
|
||||
QDateTime timestamp{QDateTime::currentDateTimeUtc()}; // 时间戳
|
||||
QString protocolHint; // 协议提示
|
||||
QString traceId; // 跟踪ID
|
||||
|
||||
PayloadRef payload; // 有效载荷
|
||||
std::size_t payloadSize{0}; // 有效载荷大小
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline
|
||||
|
||||
Q_DECLARE_METATYPE(softbus::message_bus::pipeline::PipelineContext)
|
||||
|
||||
142
src/message_bus/pipeline/PipelineEngine.cpp
Normal file
142
src/message_bus/pipeline/PipelineEngine.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "message_bus/pipeline/PipelineEngine.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "utils/logs/logging.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline
|
||||
{
|
||||
|
||||
PipelineEngine::PipelineEngine(std::size_t queueCapacity)
|
||||
: m_ingressQ(queueCapacity), m_egressQ(queueCapacity)
|
||||
{
|
||||
}
|
||||
|
||||
PipelineEngine::~PipelineEngine()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
void PipelineEngine::start()
|
||||
{
|
||||
if (m_running.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
m_worker = std::thread([this]() { run(); });
|
||||
}
|
||||
|
||||
void PipelineEngine::stop()
|
||||
{
|
||||
if (!m_running.exchange(false)) {
|
||||
return;
|
||||
}
|
||||
if (m_worker.joinable()) {
|
||||
m_worker.join();
|
||||
}
|
||||
}
|
||||
|
||||
void PipelineEngine::drain()
|
||||
{
|
||||
PipelineContext tmp;
|
||||
while (m_ingressQ.pop(tmp)) {
|
||||
}
|
||||
}
|
||||
|
||||
bool PipelineEngine::enqueueIngress(PipelineContext ctx)
|
||||
{
|
||||
if (!m_ingressQ.push(std::move(ctx))) {
|
||||
++m_counters.drop;
|
||||
return false;
|
||||
}
|
||||
++m_counters.in;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PipelineEngine::enqueueEgress(PipelineContext ctx)
|
||||
{
|
||||
return m_egressQ.push(std::move(ctx));
|
||||
}
|
||||
|
||||
bool PipelineEngine::tryPopEgress(PipelineContext& out)
|
||||
{
|
||||
return m_egressQ.pop(out);
|
||||
}
|
||||
|
||||
void PipelineEngine::setPluginManager(std::shared_ptr<softbus::core::plugin_system::PluginManager> pluginManager)
|
||||
{
|
||||
m_pluginManager = std::move(pluginManager);
|
||||
}
|
||||
|
||||
void PipelineEngine::run()
|
||||
{
|
||||
while (m_running.load()) {
|
||||
PipelineContext ctx;
|
||||
if (!m_ingressQ.pop(ctx)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool framed = stage1Framer(ctx);
|
||||
const bool parsed = framed && stage2Parser(ctx);
|
||||
const bool filtered = parsed && stage3Filter(ctx);
|
||||
const bool valid = filtered && stage4Validator(ctx);
|
||||
if (!valid) {
|
||||
++m_counters.error;
|
||||
continue;
|
||||
}
|
||||
if (!m_egressQ.push(std::move(ctx))) {
|
||||
++m_counters.drop;
|
||||
continue;
|
||||
}
|
||||
++m_counters.out;
|
||||
}
|
||||
}
|
||||
|
||||
bool PipelineEngine::stage1Framer(PipelineContext& ctx)
|
||||
{
|
||||
if (!ctx.payload.valid()) {
|
||||
return false;
|
||||
}
|
||||
if (ctx.payloadSize == 0) {
|
||||
ctx.payloadSize = ctx.payload.length;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PipelineEngine::stage4Validator(PipelineContext& ctx)
|
||||
{
|
||||
if (!ctx.payload.valid()) {
|
||||
return false;
|
||||
}
|
||||
if (ctx.endpoint.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (!ctx.timestamp.isValid()) {
|
||||
ctx.timestamp = QDateTime::currentDateTimeUtc();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PipelineEngine::stage2Parser(PipelineContext& ctx)
|
||||
{
|
||||
if (!m_pluginManager) {
|
||||
return true;
|
||||
}
|
||||
auto plugin = m_pluginManager->selectProtocolPlugin(ctx.protocolHint);
|
||||
if (!plugin) {
|
||||
return true;
|
||||
}
|
||||
auto session = plugin->createSession();
|
||||
if (!session) {
|
||||
return false;
|
||||
}
|
||||
return session->feed(ctx);
|
||||
}
|
||||
|
||||
bool PipelineEngine::stage3Filter(PipelineContext& ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace softbus::message_bus::pipeline
|
||||
@@ -1,7 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder for pipeline engine.
|
||||
class PipelineEngine {
|
||||
public:
|
||||
PipelineEngine() = default;
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include "core/plugin_system/PluginManager.h"
|
||||
#include "core/memory/LockFreeQueue.h"
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline
|
||||
{
|
||||
|
||||
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};
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
const Counters& counters() const { return m_counters; }
|
||||
|
||||
private:
|
||||
void run();
|
||||
bool stage1Framer(PipelineContext& ctx);
|
||||
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};
|
||||
std::thread m_worker;
|
||||
Counters m_counters;
|
||||
std::shared_ptr<softbus::core::plugin_system::PluginManager> m_pluginManager;
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "message_bus/pipeline/stages/1_framers/IFramer.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline::stages::framers
|
||||
{
|
||||
|
||||
// Placeholder TU for stage1 concrete framer implementations.
|
||||
|
||||
} // namespace softbus::message_bus::pipeline::stages::framers
|
||||
15
src/message_bus/pipeline/stages/1_framers/IFramer.h
Normal file
15
src/message_bus/pipeline/stages/1_framers/IFramer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline::stages::framers
|
||||
{
|
||||
|
||||
class IFramer
|
||||
{
|
||||
public:
|
||||
virtual ~IFramer() = default;
|
||||
virtual bool frame(softbus::message_bus::pipeline::PipelineContext& ctx) = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline::stages::framers
|
||||
15
src/message_bus/pipeline/stages/2_parsers/IParserStage.h
Normal file
15
src/message_bus/pipeline/stages/2_parsers/IParserStage.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline::stages::parsers
|
||||
{
|
||||
|
||||
class IParserStage
|
||||
{
|
||||
public:
|
||||
virtual ~IParserStage() = default;
|
||||
virtual bool parse(softbus::message_bus::pipeline::PipelineContext& ctx) = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline::stages::parsers
|
||||
15
src/message_bus/pipeline/stages/3_filters/IFilterStage.h
Normal file
15
src/message_bus/pipeline/stages/3_filters/IFilterStage.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline::stages::filters
|
||||
{
|
||||
|
||||
class IFilterStage
|
||||
{
|
||||
public:
|
||||
virtual ~IFilterStage() = default;
|
||||
virtual bool apply(softbus::message_bus::pipeline::PipelineContext& ctx) = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline::stages::filters
|
||||
15
src/message_bus/pipeline/stages/4_validators/IValidator.h
Normal file
15
src/message_bus/pipeline/stages/4_validators/IValidator.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::message_bus::pipeline::stages::validators
|
||||
{
|
||||
|
||||
class IValidator
|
||||
{
|
||||
public:
|
||||
virtual ~IValidator() = default;
|
||||
virtual bool validate(softbus::message_bus::pipeline::PipelineContext& ctx) = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::message_bus::pipeline::stages::validators
|
||||
Reference in New Issue
Block a user