[实现Rpc] 消息类型的测试 | dynamic_pointer_cast | gdb使用

目录

Request

RpcRequest测试:

[⭕ debug_1:dynamic_pointer_cast](#⭕ debug_1:dynamic_pointer_cast)

TopicRequest测试

ServiceRequest测试

⭕debug_2:gdb

Response

RpcResponse测试

TopicResponse测试

ServiceResponse测试


Request

RpcRequest测试:

复制代码
#include "message.hpp"

using namespace bitrpc;

int main()
{
	RpcRequest::ptr rrp = MessageFactory::create<RpcRequest>();

    Json::Value param;
    param["num1"] = 11;
    param["num2"] = 22;

    rrp->setMethod("Add");
    rrp->setParams(param);

    std::string ret = rrp->serialize();
    std::cout << ret << std::endl;

//

    BaseMessage::ptr bmp = MessageFactory::create(MType::REQ_RPC);
    bool res = bmp->unserialize(ret);
    if(bmp->check() == false)
    {
        return -1;
    }

//!!!!!!! dynamic_pointer_cast的使用
    RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<RpcRequest>(bmp);
    std::cout << rrp2->method() << std::endl;
    std::cout << rrp2->params()["num1"] << std::endl;
    std::cout << rrp2->params()["num2"] << std::endl;

    return 0;
}

运行:

⭕ debug_1:dynamic_pointer_cast

📒dynamic_cast 和 dynamic_Pointer_cast的使用

1.指引或者引用的向上转换,向下转换

例如基类A ,派生类B.。A->B 则为向下转换。。B->A则为向上转换。。向上转换为隐士转换。向下转换需要dynamic_cast或者c的转换方式。

B * b = new B;

A * a = b;

此时b就是向上转换。无需显式转换既可以编译通过。

2.dynamic_cast

一般用于有继承关系的类之间的向下转换。

3.dynamic_pointer_cast

当指针是智能指针时候,向下转换,用dynamic_Cast 则编译不能通过,此时需要使用dynamic_pointer_cast。


TopicRequest测试

复制代码
int main()
{
 	TopicRequest::ptr trp = MessageFactory::create<TopicRequest>();
    trp->setTopicKey("news");
    trp->setOptype(TopicOptype::TOPIC_PUBLISH);
    trp->setTopicMsg("hello world");

    std::string str = trp->serialize();
    std::cout << str << std::endl;

    BaseMessage::ptr bmp = MessageFactory::create(MType::REQ_TOPIC);
    bmp->unserialize(str);

    bool ret = bmp->check();
    TopicRequest::ptr trp2 = std::dynamic_pointer_cast<TopicRequest>(bmp);
    std::cout << trp2->topicKey() << std::endl;
    std::cout << (int)trp2->optype() << std::endl;
    std::cout << trp2->topicMsg() << std::endl;

	return 0;
}

运行:


ServiceRequest测试

复制代码
int main()
{
	ServiceRequest::ptr trp = MessageFactory::create<ServiceRequest>();
    trp->setMethod("Add");
    trp->setOptype(ServiceOptype::SERVICE_REGISTRY);
//服务注册 测试

    trp->setHost(Address("127.0.0.1", 8080));
    
    std::string str = trp->serialize();
    std::cout << str << std::endl;

    BaseMessage::ptr bmp = MessageFactory::create(MType::REQ_SERVICE);
    bmp->unserialize(str);

    bool ret = bmp->check();
    ServiceRequest::ptr trp2 = std::dynamic_pointer_cast<ServiceRequest>(bmp);
    std::cout << trp2->method() << std::endl;
    std::cout << (int)trp2->optype() << std::endl;
    std::cout << trp2->host().first << std::endl;
    std::cout << trp2->host().second << std::endl;

	return 0;
}

⭕debug_2:gdb

调试:

使用 gdb

复制代码
(gdb) break message.hpp:169
run

gdb 调试中 不能使用宏 !!!!!!!!!!!!!!!!!!!!

后续采用 p 打印查看


Response

RpcResponse测试

复制代码
int main()
{
    RpcResponse::ptr trp = MessageFactory::create<RpcResponse>();
    trp->setRCode(RCode::RCODE_OK);
    trp->setResult(33);
    std::string str = trp->serialize();
    std::cout << str << std::endl;

    BaseMessage::ptr bmp = MessageFactory::create(MType::RSP_RPC);
    bmp->unserialize(str);

    bool ret = bmp->check();
    RpcResponse::ptr trp2 = std::dynamic_pointer_cast<RpcResponse>(bmp);
    std::cout << (int)trp2->rcode() << std::endl;
    std::cout << trp2->result().asInt() << std::endl;
	
	return 0;
}

运行:


TopicResponse测试

复制代码
int main()
{
	TopicResponse::ptr trp = MessageFactory::create<TopicResponse>();
    trp->setRCode(RCode::RCODE_OK);
    std::string str = trp->serialize();
    std::cout << str << std::endl;

    BaseMessage::ptr bmp = MessageFactory::create(MType::RSP_TOPIC);
    bmp->unserialize(str);

    bool ret = bmp->check();
    TopicResponse::ptr trp2 = std::dynamic_pointer_cast<TopicResponse>(bmp);
    std::cout << (int)trp2->rcode() << std::endl;
	
	return 0;
}

运行:


ServiceResponse测试

复制代码
int main()
{	
	ServiceResponse::ptr trp = MessageFactory::create<ServiceResponse>();
    trp->setRCode(RCode::RCODE_OK);
    trp->setMethod("Add");
    trp->setOptype(ServiceOptype::SERVICE_DISCOVERY);
    
    std::vector<Address> addrs;
    addrs.push_back(Address{"127.0.0.1", 8080});
    addrs.push_back(Address{"127.0.0.2", 9090});
    trp->setHost(addrs);
    std::string str = trp->serialize();
    std::cout << str << std::endl;

    BaseMessage::ptr bmp = MessageFactory::create(MType::RSP_SERVICE);
    bmp->unserialize(str);

    bool ret = bmp->check();
    ServiceResponse::ptr trp2 = std::dynamic_pointer_cast<ServiceResponse>(bmp);
    std::cout << (int)trp2->rcode() << std::endl;
    std::cout << trp2->method() << std::endl;
    std::cout << (int)trp2->optype() << std::endl;
    
    std::vector<Address> addrs1 = trp2->hosts();
    for(auto& addr : addrs1)
    {
        std::cout << addr.first << std::endl;
        std::cout << addr.second << std::endl;
    }

	return 0;
}

运行:

相关推荐
郝学胜_神的一滴9 小时前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天1 天前
C++ 基础入门完全指南
c++
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK3 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境3 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境3 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
AsulTop4 天前
精简版 OpenWrt/LEDE uhttpd/rpc/mod-rpc/ Ubus Json-RPC 从0修复直到可用
rpc·路由器·openwrt·lede·uhttpd·ubus修复
郝学胜_神的一滴4 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境6 天前
C++ 的Eigen 库全解析
c++
卷无止境6 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端