C++ Actor库orca简介(四):网络消息通信

项目地址:https://github.com/wlgq2/orca

orca网络通信机制

orca的网络部分基于libuv,并对libuv做了一层C++封装:https://github.com/wlgq2/libu...
orca的项目中编译了libuv1.22.0的vs2017及gcc5.50版本,如需使用其他版本,则需自己编译相应版本。
对于orca来说,没有客户端及服务器的概念。orca中每一个Framework都是一个端点(EndPoint)。端点与端点间会建立连接、发送识别协议及通信。orca的每一个端点都有一个唯一识别的id作为通信网络中的识别依据。
对于使用来说,在进行用户消息通信前需要做以下设置:

  1. 指定本地Framework识别ID;
  2. 指定本地Framework绑定的端口及IP;
  3. 指定远程Framework端口及ID;

orca::core::FrameworkConfig类为Framework的配置,原型如下:

struct FrameworkConfig
{
    FrameworkConfig()
    {
        reset();
    }
    void reset()
    {
        id = 0;
        threadCount = 1;
        endPointAddress = nullptr;
    }
    uint32_t id;
    uint32_t threadCount;
    std::shared_ptr<struct EndPointAddress> endPointAddress;

};

其中id为framework指定id(用于通信识别),threadCount为framework开启线程数,endPointAddress为本地绑定地址,定义如下:

struct EndPointAddress
{
    enum IPV
    {
        Ipv4 = 0,
        Ipv6
    };
    EndPointAddress(std::string ip, uint16_t port, IPV ipv)
    {
        this->ip = ip;
        this->port = port;
        this->ipv = ipv;
    }
    std::string ip;
    uint16_t port;
    IPV ipv;
};

分别为ip、端口、ipv4/ipv6设置。
orca通过appendRemoteEndPoint接口添加远程Framework。
原型为:

void appendRemoteEndPoint(struct EndPointAddress& addr);
void appendRemoteEndPoint(std::string ip, uint16_t port, EndPointAddress::IPV ipv = EndPointAddress::Ipv4);

orca的网络消息通信

orca的网络消息通信与本地消息接口一样,只是在发送时需要指定远程Farmework的识别ID。需要说明的是,如果在连接建立前,调用接口发送网络消息,orca会把消息缓存到列队里等待连接建立并发送消息。如果在连接连接后消息发送失败,orca会触发错误回调函数。
一个完整的例子:

#include <iostream>
#include "MessageType.h"

#define   USE_END_POINT_1   1

class ActorTest :public orca::Actor
{
public:
    ActorTest(orca::Framework* framework,std::string name = "")
        :Actor(framework, name)
    {
        registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2));
    }
    void handle(orca::MessagePack& pack, orca::Address& from)
    {
        std::cout << (char*)(pack.enter()) << std::endl;
#if     !USE_END_POINT_1
        //remote message return.远程消息返回。
        send(pack, from);
#endif
    }
};

int main(int argc, char** args)
{
    //complie endpoint 1 编译端点1.
#if    USE_END_POINT_1
    //framework configs.
    orca::FrameworkConfig config;
    config.id = 1024;
    config.threadCount = 1;
    config.endPointAddress = std::make_shared<orca::EndPointAddress>("0.0.0.0",10001, orca::EndPointAddress::Ipv4);

    orca::Framework framework(config);
    //append remote framework.
    framework.appendRemoteEndPoint("127.0.0.1", 10002);

    ActorTest actor(&framework);
    //message pack.
    char data[] = "a message from remote actor";
    orca::MessagePack message;
    message.create(data,sizeof(data));

    //actor->actor2(remote) send message.
    actor.send(message,"actor2",1025);
    framework.loop();
#else
    //complie endpoint 2 编译端点2.

    //framework configs.
    orca::FrameworkConfig config;
    config.id = 1025;
    config.threadCount = 1;
    config.endPointAddress = std::make_shared<orca::EndPointAddress>("0.0.0.0", 10002, orca::EndPointAddress::Ipv4);

    orca::Framework framework(config);
    //append remote framework.
    framework.appendRemoteEndPoint("127.0.0.1", 10001);

    ActorTest actor(&framework,"actor2");
    framework.loop();
#endif
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值