改进api

This commit is contained in:
codeboss 2025-06-27 00:56:06 +08:00
parent e9d2d011f1
commit c990efbd43
3 changed files with 42 additions and 137 deletions

View File

@ -59,7 +59,7 @@ QList<QString> RtWsEntity::inputTypes() const
{
QList<QString> types;
for (auto c : this->_comps_list) {
for (auto f : c->signatureTypes())
for (auto f : c->respondSignatures())
types.append(f.first);
}
return types.toSet().toList();
@ -67,21 +67,22 @@ QList<QString> RtWsEntity::inputTypes() const
#include <QVariant>
#include <QJsonArray>
QList<WsRespondEntry> RtWsEntity::getEntryWithSignature(const WsRespondSignatureType& t) const
QList<WsRespondEntry> RtWsEntity::getRespondWithSignature(const WsRespondSignatureType& t) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntryWithSignature(t));
list.append(c->getRespondWithSignature(t));
return list;
}
QList<WsRespondEntry> RtWsEntity::getEntryWithInType(const QString& msg_type) const
QList<WsRespondEntry> RtWsEntity::getRespondWithInType(const QString& msg_type) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntrysWithInType(msg_type));
list.append(c->getRespondWithInType(msg_type));
return list;
}
void RtWsEntity::recoveryFrom(const QJsonObject& obj)
{
this->_entity_id = obj["entity_id"].toVariant().toULongLong();
@ -143,19 +144,19 @@ std::shared_ptr<WsEntity> RtEntityManager::defaultNew() const
return nullptr;
}
QList<WsRespondEntry> RtEntityManager::getEntryWithSignature(const WsRespondSignatureType& t) const
QList<WsRespondEntry> RtEntityManager::getRespondWithSignature(const WsRespondSignatureType& t) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntryWithSignature(t));
list.append(c->getRespondWithSignature(t));
return list;
}
QList<WsRespondEntry> RtEntityManager::getEntryWithInType(const QString& msg_type) const
QList<WsRespondEntry> RtEntityManager::getRespondWithInType(const QString& msg_type) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntrysWithInType(msg_type));
list.append(c->getRespondWithInType(msg_type));
return list;
}
@ -197,38 +198,6 @@ QList<std::shared_ptr<WsComponent>> RtEntityManager::components() const
return this->_comps_list.values();
}
ImmediateKernel::ImmediateKernel(std::shared_ptr<WsEntity> bind) :_bind_entity(bind) {}
uint64_t ImmediateKernel::entityManagerID() const
{
return RtEntityManager::const_id;
}
QList<std::shared_ptr<WsMessage>> ImmediateKernel::execute(const WsRespondSignatureType& resp_signature, std::shared_ptr<const WsMessage> in)
{
QList<std::shared_ptr<WsMessage>> rets;
auto list = this->_bind_entity->getEntryWithSignature(resp_signature);
for (auto func : list) {
func(shared_from_this(), in, rets);
}
return rets;
}
QList<std::shared_ptr<WsMessage>> ImmediateKernel::execute(std::shared_ptr<const WsMessage> in)
{
QList<std::shared_ptr<WsMessage>> rets;
auto list = this->_bind_entity->getEntryWithInType(in->topicString());
for (auto func : list) {
func(shared_from_this(), in, rets);
}
return rets;
}
uint64_t ImmediateKernel::resourceManagerID() const
{
return RtResourceManager::const_id;
}
QString RtResourceManager::templetName() const
{
return "RtResourceManager";
@ -249,19 +218,19 @@ std::shared_ptr<WsEntity> RtResourceManager::defaultNew() const
return nullptr;
}
QList<WsRespondEntry> RtResourceManager::getEntryWithSignature(const WsRespondSignatureType& t) const
QList<WsRespondEntry> RtResourceManager::getRespondWithSignature(const WsRespondSignatureType& t) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntryWithSignature(t));
list.append(c->getRespondWithSignature(t));
return list;
}
QList<WsRespondEntry> RtResourceManager::getEntryWithInType(const QString& msg_type) const
QList<WsRespondEntry> RtResourceManager::getRespondWithInType(const QString& msg_type) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntrysWithInType(msg_type));
list.append(c->getRespondWithInType(msg_type));
return list;
}

View File

@ -80,14 +80,14 @@ public:
/// </summary>
/// <param name="t">签名</param>
/// <returns>处理接口</returns>
virtual QList<WsRespondEntry> getEntryWithSignature(const WsRespondSignatureType& t) const;
virtual QList<WsRespondEntry> getRespondWithSignature(const WsRespondSignatureType& t) const;
/// <summary>
/// 通过输入消息类型获取处理入口
/// </summary>
/// <param name="msg_type">输入消息类型</param>
/// <returns>处理接口列表</returns>
virtual QList<WsRespondEntry> getEntryWithInType(const QString& msg_type) const;
virtual QList<WsRespondEntry> getRespondWithInType(const QString& msg_type) const override;
// 通过 Serializable 继承
void recoveryFrom(const QJsonObject& obj) override;
void saveTo(QJsonObject& obj) const override;
@ -111,8 +111,8 @@ public:
QList<std::shared_ptr<WsComponent>> components() const override;
std::shared_ptr<WsEntity> defaultNew() const override;
QList<WsRespondEntry> getEntryWithSignature(const WsRespondSignatureType& t) const override;
QList<WsRespondEntry> getEntryWithInType(const QString& msg_type) const override;
QList<WsRespondEntry> getRespondWithSignature(const WsRespondSignatureType& t) const override;
QList<WsRespondEntry> getRespondWithInType(const QString& msg_type) const override;
void recoveryFrom(const QJsonObject& obj) override;
void saveTo(QJsonObject& obj) const override;
@ -130,8 +130,8 @@ public:
QString name() const override;
std::shared_ptr<WsEntity> defaultNew() const override;
QList<WsRespondEntry> getEntryWithSignature(const WsRespondSignatureType& t) const override;
QList<WsRespondEntry> getEntryWithInType(const QString& msg_type) const override;
QList<WsRespondEntry> getRespondWithSignature(const WsRespondSignatureType& t) const override;
QList<WsRespondEntry> getRespondWithInType(const QString& msg_type) const override;
void recoveryFrom(const QJsonObject& obj) override;
void saveTo(QJsonObject& obj) const override;
@ -141,34 +141,3 @@ public:
QList<std::shared_ptr<WsComponent>> components() const override;
};
/// <summary>
/// 实体内迅捷调用核心
/// </summary>
class SIMSBASIC_EXPORT ImmediateKernel : public Immediate,
public std::enable_shared_from_this<ImmediateKernel> {
private:
std::shared_ptr<WsEntity> _bind_entity = nullptr;
public:
ImmediateKernel(std::shared_ptr<WsEntity> bind);
/// <summary>
/// 获取实体管理器id
/// </summary>
/// <returns></returns>
virtual uint64_t entityManagerID() const override;
virtual uint64_t resourceManagerID() const override;
/// <summary>
/// 实体内指定类型的调用立即执行
/// </summary>
/// <param name="in"></param>
/// <returns></returns>
virtual QList<std::shared_ptr<WsMessage>> execute(const WsRespondSignatureType& resp_signature, std::shared_ptr<const WsMessage> in) override;
/// <summary>
/// 实体内兼容调用立即执行
/// </summary>
/// <param name="in"></param>
/// <returns></returns>
virtual QList<std::shared_ptr<WsMessage>> execute(std::shared_ptr<const WsMessage> in) override;
};

View File

@ -74,53 +74,14 @@ public:
/// Respond签名类型
/// </summary>
using WsRespondSignatureType = std::pair<QString, QString>;
/// <summary>
/// 实体内立即执行入口
/// Request签名类型
/// </summary>
class Immediate {
public:
virtual ~Immediate() = default;
/// <summary>
/// 获取实体管理器id
/// </summary>
/// <returns></returns>
virtual uint64_t entityManagerID() const = 0;
/// <summary>
/// 获取资源管理器id
/// </summary>
/// <returns></returns>
virtual uint64_t resourceManagerID() const = 0;
/// <summary>
/// 实体内指定类型的调用立即执行
/// </summary>
/// <param name="in"></param>
/// <returns></returns>
virtual QList<std::shared_ptr<WsMessage>> execute(const WsRespondSignatureType& resp_signature, std::shared_ptr<const WsMessage> in) = 0;
template<typename TypeIn, typename TypeOut>
QList<std::shared_ptr<TypeOut>> execute(std::shared_ptr<const WsMessage> in) {
auto sign = std::make_pair<QString, QString>(TypeIn().topicString(), TypeOut().topicString());
auto rest_list = this->execute(sign, in);
QList<std::shared_ptr<TypeOut>> typed_list;
for (auto it : rest_list) {
typed_list << std::static_pointer_cast<TypeOut>(it);
}
return typed_list;
}
/// <summary>
/// 实体内兼容调用立即执行
/// </summary>
/// <param name="in"></param>
/// <returns></returns>
virtual QList<std::shared_ptr<WsMessage>> execute(std::shared_ptr<const WsMessage> in) = 0;
};
using WsRequestSignatureType = std::pair<QString, QString>;
/// <summary>
/// 所有消息处理单元通用接口
/// </summary>
using WsRespondEntry = std::function<void(std::shared_ptr<Immediate> map, std::shared_ptr<const WsMessage>, QList<std::shared_ptr<WsMessage>>&)>;
using WsRespondEntry = std::function<void(std::shared_ptr<const WsMessage>, QList<std::shared_ptr<WsMessage>>&)>;
/// <summary>
/// 实体抽象接口
@ -153,17 +114,17 @@ public:
virtual std::shared_ptr<WsEntity> defaultNew() const = 0;
/// <summary>
/// 通过指定的签名获取处理入口
/// 通过指定的签名获取消息响应处理入口
/// </summary>
/// <param name="t">签名</param>
/// <returns>处理接口</returns>
virtual QList<WsRespondEntry> getEntryWithSignature(const WsRespondSignatureType& t) const = 0;
virtual QList<WsRespondEntry> getRespondWithSignature(const WsRespondSignatureType& t) const = 0;
/// <summary>
/// 通过输入消息类型获取处理入口
/// </summary>
/// <param name="msg_type">输入消息类型</param>
/// <returns>处理接口列表</returns>
virtual QList<WsRespondEntry> getEntryWithInType(const QString& msg_type) const = 0;
virtual QList<WsRespondEntry> getRespondWithInType(const QString& msg_type) const = 0;
};
/// <summary>
@ -173,6 +134,12 @@ class WsComponent : public Serializable, public std::enable_shared_from_this<WsC
public:
virtual ~WsComponent() = default;
/// <summary>
/// 插件唯一命名
/// </summary>
/// <returns>插件名</returns>
virtual QString name() const = 0;
/// <summary>
/// 克隆一个具有相同数据的实例
/// </summary>
@ -183,30 +150,30 @@ public:
/// </summary>
/// <param name="entity_id">实体实例</param>
virtual void bindEntity(std::weak_ptr<WsEntity> host) = 0;
/// <summary>
/// 插件唯一命
/// 允许发送的消息类型签
/// </summary>
/// <returns>插件名</returns>
virtual QString name() const = 0;
/// <returns></returns>
virtual QList<WsRequestSignatureType> requestSignatures() const = 0;
/// <summary>
/// 允许处理的消息签名类型
/// 允许响应处理的消息签名类型
/// </summary>
/// <returns>消息签名类型集合</returns>
virtual QList<WsRespondSignatureType> signatureTypes() const = 0;
virtual QList<WsRespondSignatureType> respondSignatures() const = 0;
/// <summary>
/// 通过指定的签名获取处理入口
/// 通过指定的签名获取消息响应处理入口
/// </summary>
/// <param name="t">处理签名</param>
/// <returns>处理接口</returns>
virtual QList<WsRespondEntry> getEntryWithSignature(const WsRespondSignatureType& t) const = 0;
virtual QList<WsRespondEntry> getRespondWithSignature(const WsRespondSignatureType& t) const = 0;
/// <summary>
/// 通过输入消息类型获取处理入口
/// </summary>
/// <param name="msg_type">输入消息类型</param>
/// <returns>处理接口列表</returns>
virtual QList<WsRespondEntry> getEntrysWithInType(const QString& msg_type) const = 0;
virtual QList<WsRespondEntry> getRespondWithInType(const QString& msg_type) const = 0;
};
/// <summary>