Skip to content

lkpworkspace/myframe

Repository files navigation

myframe

myframe

概述

C++实现的组件化的编程框架,程序由actor和worker组成;
actor基于消息驱动,actor之间可以进行消息传递;
worker自驱动,可以通过消息与actor交互;
适用于构建中大型项目.

开发/运行环境

C++ 标准支持
C++17
C++20
操作系统支持
Linux
Windows
macOS

构建

Hello,World 示例

#include <string.h>
#include <iostream>

#include "myframe/msg.h"
#include "myframe/actor.h"

using namespace myframe;
/*
  该actor实现:
    自己给自己发送一条消息
*/
class Demo : public Actor
{
 public:
  /* actor模块加载完毕后调用 */
  int Init(const char* param) override {
    /* 构造 hello,world 消息发送给自己 */
    auto mailbox = GetMailbox();
    mailbox->Send("actor.demo.echo_hello_world", std::make_shared<Msg>("hello,world"));
  }

  void Proc(const std::shared_ptr<const Msg>& msg) override {
    /* 获得文本消息, 打印 源actor地址 目的actor地址 消息内容 */
    std::cout << *msg << ": " << msg->GetData() << std::endl;
  }
};

/* 框架根据描述文件创建actor实例函数 */
extern "C" std::shared_ptr<Actor> actor_create(const std::string& actor_name) {
  if (actor_name == "demo") {
    return std::make_shared<Demo>();
  }
  return nullptr;
}

Hello,World 配置文件

{
  "type":"library",
  "lib":"demo",
  "actor":{
    "demo":[
      {
        "instance_name":"echo_hello_world",
        "instance_params":""
      }
    ]
  }
}
  • type: [ library | class ]
  • lib: 库名称
  • actor: 需要创建的actor列表
    • demo: actor名
      • instance_name:实例名称
      • instance_params:实例参数

程序接口

文档