devicesbus read data
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "utils/logs/logging.h"
|
||||
#include "device_bus/DeviceBus.h"
|
||||
#include "message_bus/pipeline/PayloadRef.h"
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
CoreService* CoreService::instance()
|
||||
{
|
||||
@@ -35,6 +37,8 @@ bool CoreService::initialize()
|
||||
}
|
||||
|
||||
LOG_INFO() << "CoreService initializing";
|
||||
qRegisterMetaType<softbus::message_bus::pipeline::PayloadRef>("softbus::message_bus::pipeline::PayloadRef");
|
||||
qRegisterMetaType<softbus::message_bus::pipeline::PipelineContext>("softbus::message_bus::pipeline::PipelineContext");
|
||||
|
||||
// 1. 解析配置路径(放在用户 home 下的相对固定目录:~/softbus/config)
|
||||
const QString configPath =
|
||||
|
||||
@@ -1,7 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder for a lock-free ring queue.
|
||||
class LockFreeQueue {
|
||||
public:
|
||||
LockFreeQueue() = default;
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace softbus::core::memory
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
class LockFreeQueue
|
||||
{
|
||||
public:
|
||||
// SPSC ring queue. Producer and consumer must be single-threaded.
|
||||
explicit LockFreeQueue(std::size_t capacity)
|
||||
: m_capacity(capacity + 1), m_data(m_capacity)
|
||||
{
|
||||
}
|
||||
|
||||
bool push(const T& value)
|
||||
{
|
||||
const auto head = m_head.load(std::memory_order_relaxed);
|
||||
const auto next = increment(head);
|
||||
if (next == m_tail.load(std::memory_order_acquire)) {
|
||||
return false; // queue full
|
||||
}
|
||||
m_data[head] = value;
|
||||
m_head.store(next, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool push(T&& value)
|
||||
{
|
||||
const auto head = m_head.load(std::memory_order_relaxed);
|
||||
const auto next = increment(head);
|
||||
if (next == m_tail.load(std::memory_order_acquire)) {
|
||||
return false; // queue full
|
||||
}
|
||||
m_data[head] = std::move(value);
|
||||
m_head.store(next, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool pop(T& out)
|
||||
{
|
||||
const auto tail = m_tail.load(std::memory_order_relaxed);
|
||||
if (tail == m_head.load(std::memory_order_acquire)) {
|
||||
return false; // queue empty
|
||||
}
|
||||
out = std::move(m_data[tail]);
|
||||
m_tail.store(increment(tail), std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return m_head.load(std::memory_order_acquire) == m_tail.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
std::size_t capacity() const
|
||||
{
|
||||
return m_capacity - 1;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t increment(std::size_t idx) const
|
||||
{
|
||||
return (idx + 1) % m_capacity;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::size_t m_capacity;
|
||||
std::vector<T> m_data;
|
||||
alignas(64) std::atomic<std::size_t> m_head{0};
|
||||
alignas(64) std::atomic<std::size_t> m_tail{0};
|
||||
};
|
||||
|
||||
} // namespace softbus::core::memory
|
||||
|
||||
@@ -1,7 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder for a preallocated memory pool.
|
||||
class MemoryPool {
|
||||
public:
|
||||
MemoryPool() = default;
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
namespace softbus::core::memory
|
||||
{
|
||||
|
||||
class MemoryPool
|
||||
{
|
||||
public:
|
||||
struct PoolBlock
|
||||
{
|
||||
std::uint8_t* data{nullptr};
|
||||
std::size_t capacity{0};
|
||||
std::size_t size{0};
|
||||
std::size_t slotIndex{0};
|
||||
};
|
||||
|
||||
using BlockPtr = std::shared_ptr<PoolBlock>;
|
||||
|
||||
explicit MemoryPool(std::size_t blockSize = 4096, std::size_t blockCount = 1024)
|
||||
: m_blockSize(blockSize), m_storage(blockSize * blockCount), m_inUse(blockCount, false)
|
||||
{
|
||||
for (std::size_t i = 0; i < blockCount; ++i) {
|
||||
m_freeSlots.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
BlockPtr allocate(std::size_t requiredSize)
|
||||
{
|
||||
if (requiredSize > m_blockSize) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::size_t slot = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_mutex);
|
||||
if (m_freeSlots.empty()) {
|
||||
return {};
|
||||
}
|
||||
slot = m_freeSlots.front();
|
||||
m_freeSlots.pop();
|
||||
m_inUse[slot] = true;
|
||||
}
|
||||
|
||||
auto* raw = new PoolBlock{
|
||||
m_storage.data() + slot * m_blockSize,
|
||||
m_blockSize,
|
||||
requiredSize,
|
||||
slot,
|
||||
};
|
||||
return BlockPtr(raw, [this](PoolBlock* b) {
|
||||
if (!b) return;
|
||||
this->release(*b);
|
||||
delete b;
|
||||
});
|
||||
}
|
||||
|
||||
void release(const PoolBlock& block)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_mutex);
|
||||
if (block.slotIndex >= m_inUse.size()) {
|
||||
return;
|
||||
}
|
||||
if (!m_inUse[block.slotIndex]) {
|
||||
return;
|
||||
}
|
||||
m_inUse[block.slotIndex] = false;
|
||||
m_freeSlots.push(block.slotIndex);
|
||||
}
|
||||
|
||||
std::size_t blockSize() const { return m_blockSize; }
|
||||
std::size_t totalBlocks() const { return m_inUse.size(); }
|
||||
|
||||
std::size_t inUseBlocks() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_mutex);
|
||||
std::size_t used = 0;
|
||||
for (const bool v : m_inUse) {
|
||||
if (v) ++used;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t m_blockSize;
|
||||
std::vector<std::uint8_t> m_storage;
|
||||
mutable std::mutex m_mutex;
|
||||
std::queue<std::size_t> m_freeSlots;
|
||||
std::vector<bool> m_inUse;
|
||||
};
|
||||
|
||||
} // namespace softbus::core::memory
|
||||
|
||||
@@ -1,7 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder protocol plugin interface.
|
||||
class IProtocolPlugin {
|
||||
public:
|
||||
virtual ~IProtocolPlugin() = default;
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
|
||||
namespace softbus::core::plugin_system
|
||||
{
|
||||
|
||||
class IProtocolSession
|
||||
{
|
||||
public:
|
||||
virtual ~IProtocolSession() = default;
|
||||
virtual bool feed(softbus::message_bus::pipeline::PipelineContext& ctx) = 0;
|
||||
virtual void reset() = 0;
|
||||
};
|
||||
|
||||
class IProtocolPlugin
|
||||
{
|
||||
public:
|
||||
virtual ~IProtocolPlugin() = default;
|
||||
|
||||
virtual QString pluginId() const = 0;
|
||||
virtual bool supports(const QString& protocolHint) const = 0;
|
||||
virtual std::unique_ptr<IProtocolSession> createSession() = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::core::plugin_system
|
||||
|
||||
24
src/core/plugin_system/PluginManager.cpp
Normal file
24
src/core/plugin_system/PluginManager.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "core/plugin_system/PluginManager.h"
|
||||
|
||||
namespace softbus::core::plugin_system
|
||||
{
|
||||
|
||||
void PluginManager::registerProtocolPlugin(std::shared_ptr<IProtocolPlugin> plugin)
|
||||
{
|
||||
if (!plugin) {
|
||||
return;
|
||||
}
|
||||
m_protocolPlugins.insert(plugin->pluginId(), std::move(plugin));
|
||||
}
|
||||
|
||||
std::shared_ptr<IProtocolPlugin> PluginManager::selectProtocolPlugin(const QString& protocolHint) const
|
||||
{
|
||||
for (auto it = m_protocolPlugins.constBegin(); it != m_protocolPlugins.constEnd(); ++it) {
|
||||
if (it.value() && it.value()->supports(protocolHint)) {
|
||||
return it.value();
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace softbus::core::plugin_system
|
||||
@@ -1,7 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
// Placeholder for plugin manager.
|
||||
class PluginManager {
|
||||
public:
|
||||
PluginManager() = default;
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QHash>
|
||||
#include <QString>
|
||||
|
||||
#include "core/plugin_system/IProtocolPlugin.h"
|
||||
|
||||
namespace softbus::core::plugin_system
|
||||
{
|
||||
|
||||
class PluginManager
|
||||
{
|
||||
public:
|
||||
PluginManager() = default;
|
||||
|
||||
void registerProtocolPlugin(std::shared_ptr<IProtocolPlugin> plugin);
|
||||
std::shared_ptr<IProtocolPlugin> selectProtocolPlugin(const QString& protocolHint) const;
|
||||
|
||||
private:
|
||||
QHash<QString, std::shared_ptr<IProtocolPlugin>> m_protocolPlugins;
|
||||
};
|
||||
|
||||
} // namespace softbus::core::plugin_system
|
||||
|
||||
Reference in New Issue
Block a user