├── CMakeLists.txt ├── README.md ├── autobuild.sh ├── bin ├── chat_consumer ├── chat_provider ├── config │ ├── log.conf │ └── test.conf ├── consumer ├── f_consumer ├── f_provider ├── log │ └── 2023-4-24-log.txt └── provider ├── example ├── CMakeLists.txt ├── callee │ ├── CMakeLists.txt │ ├── chatservice.cc │ ├── friendservice.cc │ └── userservice.cc ├── caller │ ├── CMakeLists.txt │ ├── callchatservice.cc │ ├── callfriendservice.cc │ └── calluserservice.cc ├── friend.pb.cc ├── friend.pb.h ├── friend.proto ├── publictype.pb.cc ├── publictype.pb.h ├── publictype.proto ├── user.pb.cc ├── user.pb.h └── user.proto ├── lib └── libmprpc.a ├── src ├── CMakeLists.txt ├── asynclogging.cc ├── include │ ├── asynclogging.h │ ├── lockqueue.h │ ├── logger.h │ ├── mprpcapplication.h │ ├── mprpcchannel.h │ ├── mprpcconfig.h │ ├── mprpccontroller.h │ ├── rpcheader.pb.h │ ├── rpcprovider.h │ └── zookeeperutil.h ├── logger.cc ├── mprpcapplication.cc ├── mprpcchannel.cc ├── mprpcconfig.cc ├── mprpccontroller.cc ├── rpcheader.pb.cc ├── rpcheader.proto ├── rpcprovider.cc └── zookeeperutil.cc └── test └── protobuf ├── main ├── main.cc ├── test.pb.cc ├── test.pb.h └── test.proto /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 设置cmake的最低版本和项目名称 2 | cmake_minimum_required(VERSION 3.0) 3 | project(mprpc) 4 | 5 | # 生产debug版本 6 | set(CMAKE_BUILD_TYPE "Debug") 7 | 8 | # 设置项目可执行文件输出的路径 9 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 10 | # 设置项目库文件输出的路径 11 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) 12 | 13 | # 设置项目编译头文件搜索路径 -I 14 | include_directories(${PROJECT_SOURCE_DIR}/src/include) 15 | include_directories(${PROJECT_SOURCE_DIR}/example) 16 | # 设置项目库文件搜索路径 -L 17 | link_directories(${PROJECT_SOURCE_DIR}/lib) 18 | 19 | # src包含了mprpc框架所有的相关代码 20 | add_subdirectory(src) 21 | add_subdirectory(example) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mprpc 2 | ### 一. 介绍 3 | 4 | 本项目是由C++开发的分布式网络通信框架(RPC),为了分布式环境下服务器之间进行远程方法调用提供了一套接口,通过该框架可以快速的实现RPC方法调用。 5 | 6 | ### 二. 技术栈 7 | 8 | 项目开发涉及到以下几个方面 9 | 10 | - RPC远程过程调用原理以及实现 11 | - Protobuf数据序列化和反序列化协议 12 | - ZooKeeper分布式一致性协调服务应用以及编程 13 | - 网络库编程 14 | - conf配置文件读取 15 | - 异步日志模块 16 | - 线程池模块 17 | - CMake构建项目集成编译环境 18 | - github管理项目 19 | 20 | ### 二. 项目环境配置 21 | 22 | 由于项目使用了部分第三方库及构建环境,所以需要提前部署安装。 23 | 24 | 1. **Protobuf安装配置** 25 | 26 | - protobuf(protocol buffffer)是google 的一种数据交换的格式,它独立于平台语言。google 提供了protobuf多种语言的实现:java、c#、c++、go 和 python,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml(20倍) 、json(10倍)进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。 27 | 28 | - **ubuntu下 protobuf**环境搭建 29 | - 在**github**源代码下载**地址:https://github.com/google/protobuf,源码包中的src/README.md,有详细的安装说明,安装过程如下: 30 | 31 | ```tex 32 | 1、解压压缩包:unzip protobuf-master.zip 33 | 2、进入解压后的文件夹:cd protobuf-master 34 | 3、安装所需工具:sudo apt-get install autoconf automake libtool curl make g++ unzip 35 | 4、自动生成confifigure配置文件:./autogen.sh 36 | 5、配置环境:./confifigure 37 | 6、编译源代码(时间比较长):make 38 | 7、安装:sudo make install 39 | 8、刷新动态库:sudo ldconfig 40 | ``` 41 | 42 | 2. **Zookeeper安装配置** 43 | 44 | Zookeeper是在分布式环境中应用非常广泛,它的优秀功能很多,比如分布式环境中全局命名服务,服务注册中心,全局分布式锁等等,本项目只用到服务注册。参考链接:https://www.cnblogs.com/xinyonghu/p/11031729.html 45 | 46 | - 下载地址:`链接: https://pan.baidu.com/s/1yPl1aNGuC7OVabs5icw1Ag?pwd=92xe 提取码: 92xe` 47 | 48 | - ```tex 49 | 1、下载zookeeper-3.4.10.tar.gz解压后进入zookeeper-3.4.10 50 | 2. cd zookeeper-3.4.10/conf 51 | 3. mv zoo_sample.cfg zoo.cfg 52 | 4. 进入bin目录,启动zkServer, ./zkServer.sh start 53 | 5. 可以通过netstat查看zkServer的端口,在bin目录启动zkClient.sh链接zkServer,熟悉zookeeper怎么组织节点 54 | ``` 55 | 56 | - **zk的原生开发API(c/c++接口)** 57 | 58 | - - 进入上面解压目录src/c下面,zookeeper已经提供了原生的C/C++和Java API开发接口,需要通过源码编译生成,过程如下: 59 | 60 | ```tex 61 | ~/package/zookeeper-3.4.10/src/c$ sudo ./confifigure 62 | ~/package/zookeeper-3.4.10/src/c$ sudo make 63 | ~/package/zookeeper-3.4.10/src/c$ sudo make install 64 | ``` 65 | 66 | - 主要关注zookeeper怎么管理节点,zk-c API怎么创建节点,获取节点,删除节点以及watcher机制的API编程。 67 | 68 | - **zk客户端常用命令** 69 | 70 | `ls、get、create、set、delete` 71 | 72 | ### 三. 项目目录结构 73 | 74 | ```tex 75 | bin:可执行文件,这里放的是example编译好的二进制文件 76 | build:项目编译文件 77 | lib:项目库文件 78 | src:源文件 79 | test:放的是一些util和net的测试代码 80 | example:框架代码使用范例 81 | CMakeLists.txt:顶层的cmake文件 82 | README.md:项目自述文件 83 | autobuild.sh:一键编译脚本 84 | ``` 85 | 86 | - `autobuild.sh`是项目的一键编译脚本 87 | - 项目最终会编译完成后提供给用户的是三个静态库和头文件,都放在lib下面 88 | - `libhrpc_net.a`:网络库 89 | - `libhrpc_util.a`:工具库 90 | - `libhrpc.a`:rpc库 91 | - `include目录` 92 | 93 | ### 四. 项目分支 94 | 95 | 目前项目有三个分支: 96 | 97 | - `master`,该分支是基于`muduo`网络库作为通信基础的,所以在使用该分支代码的时候还需要安装`muduo`,安装方法参考链接: 98 | - `https://blog.csdn.net/QIANGWEIYUAN/article/details/89023980` 99 | - `master_net`,该分支网络库是自己基于`reactor`模式开发的一套单线程c++网络库,部分代码参考`muduo`,可以供学习`muduo`。 100 | - `release/1.0`,基于`master`分支拉出来的一个稳定分支。 101 | -------------------------------------------------------------------------------- /autobuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | rm -rf `pwd`/build/* 6 | cd `pwd`/build && 7 | cmake .. && 8 | make 9 | cd .. 10 | cp -r `pwd`/src/include `pwd`/lib -------------------------------------------------------------------------------- /bin/chat_consumer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/chat_consumer -------------------------------------------------------------------------------- /bin/chat_provider: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/chat_provider -------------------------------------------------------------------------------- /bin/config/log.conf: -------------------------------------------------------------------------------- 1 | # log level设置 2 | # TRACE, // 调试,指出比DEBUG粒度更细的一些信息事件(开发过程中使用) 3 | # DEBUG, // 调试,指出细粒度信息事件对调试应用程序是非常有帮助的。(开发过程中使用) 4 | # INFO, // 信息,表明消息在粗粒度级别上突出强调应用程序的运行过程。 5 | # WARN, // 警告,系统能正常运行,但可能会出现潜在错误的情形。 6 | # ERROR, // 错误,指出虽然发生错误事件,但仍然不影响系统的继续运行 7 | # FATAL // 崩溃,指出每个严重的错误事件将会导致应用程序的退出。 8 | loglevel=INFO 9 | logtag=TEST 10 | #log文件存放目录,如果不设置,则只输出到标准IO,不向文件中输出 11 | outdir= -------------------------------------------------------------------------------- /bin/config/test.conf: -------------------------------------------------------------------------------- 1 | # rpc节点ip 2 | rpcserverip=127.0.0.1 3 | # rpc节点port 4 | rpcserverport=10240 5 | # zk的ip 6 | zookeeperip=127.0.0.1 7 | # zk的port 8 | zookeeperport=2181 -------------------------------------------------------------------------------- /bin/consumer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/consumer -------------------------------------------------------------------------------- /bin/f_consumer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/f_consumer -------------------------------------------------------------------------------- /bin/f_provider: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/f_provider -------------------------------------------------------------------------------- /bin/log/2023-4-24-log.txt: -------------------------------------------------------------------------------- 1 | [MPRPC] 2023-4-24 19:27:43 [INFO] [/home/lh/work/rpc_code/mprpc/example/callee/chatservice.cc 119] insert UserService to RpcProvider 2 | [MPRPC] 2023-4-24 19:27:43 [INFO] [/home/lh/work/rpc_code/mprpc/example/callee/chatservice.cc 121] insert FriendService to RpcProvider 3 | -------------------------------------------------------------------------------- /bin/provider: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/bin/provider -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(callee) 2 | add_subdirectory(caller) -------------------------------------------------------------------------------- /example/callee/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SRC_LIST userservice.cc ../publictype.pb.cc ../user.pb.cc) 2 | add_executable(provider ${SRC_LIST}) 3 | target_link_libraries(provider mprpc protobuf) 4 | 5 | set(SRC_LIST_F friendservice.cc ../publictype.pb.cc ../friend.pb.cc) 6 | add_executable(f_provider ${SRC_LIST_F}) 7 | target_link_libraries(f_provider mprpc protobuf) 8 | 9 | set(SRC_LIST_C 10 | chatservice.cc 11 | ../publictype.pb.cc 12 | ../user.pb.cc 13 | ../friend.pb.cc) 14 | add_executable(chat_provider ${SRC_LIST_C}) 15 | target_link_libraries(chat_provider mprpc protobuf) -------------------------------------------------------------------------------- /example/callee/chatservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "friend.pb.h" 6 | #include "user.pb.h" 7 | #include "mprpcapplication.h" 8 | #include "rpcprovider.h" 9 | #include "logger.h" 10 | 11 | /* 12 | UserService目前是一个本地服务 13 | */ 14 | class UserService : public fixbug::UserServiceRpc { 15 | public: 16 | bool Login(std::string name, std::string pwd) { 17 | std::cout << "doing local service: Login" << std::endl; 18 | std::cout << "name: " << name << ", pwd: " << pwd << std::endl;; 19 | return true; 20 | } 21 | 22 | bool Register(uint32_t id, std::string name, std::string pwd) 23 | { 24 | std::cout << "doing local service: Register" << std::endl; 25 | std::cout << "id:" << id << "name:" << name << " pwd:" << pwd << std::endl; 26 | return true; 27 | } 28 | 29 | /** 30 | * 重写基类UserServiceRpc的虚函数 31 | */ 32 | virtual void Login(::google::protobuf::RpcController* controller, 33 | const ::fixbug::LoginRequest* request, 34 | ::fixbug::LoginResponse* response, 35 | ::google::protobuf::Closure* done) 36 | { 37 | std::string name = request->name(); 38 | std::string pwd = request->pwd(); 39 | // 做本地业务 40 | bool login_result = Login(name, pwd); 41 | 42 | // 把响应写入 43 | fixbug::ResultCode* code = response->mutable_result(); 44 | code->set_errcode(0); 45 | code->set_errmsg(""); 46 | response->set_success(login_result); 47 | 48 | // 执行回调操作,由框架完成 49 | done->Run(); 50 | } 51 | 52 | void Register(::google::protobuf::RpcController* controller, 53 | const ::fixbug::RegisterRequest* request, 54 | ::fixbug::RegisterResponse* response, 55 | ::google::protobuf::Closure* done) 56 | { 57 | uint32_t id = request->id(); 58 | std::string name = request->name(); 59 | std::string pwd = request->pwd(); 60 | 61 | bool ret = Register(id, name, pwd); 62 | 63 | response->mutable_result()->set_errcode(0); 64 | response->mutable_result()->set_errmsg(""); 65 | response->set_success(ret); 66 | 67 | done->Run(); 68 | } 69 | }; 70 | 71 | class FriendService : public fixbug::FiendServiceRpc 72 | { 73 | public: 74 | std::vector GetFriendsList(uint32_t userid) 75 | { 76 | std::cout << "do GetFriendsList service! userid:" << userid << std::endl; 77 | std::vector vec; 78 | vec.push_back("gao yang"); 79 | vec.push_back("liu hong"); 80 | vec.push_back("wang shuo"); 81 | return vec; 82 | } 83 | 84 | // 重写基类方法 85 | void GetFriendsList(::google::protobuf::RpcController* controller, 86 | const ::fixbug::GetFriendsListRequest* request, 87 | ::fixbug::GetFriendsListResponse* response, 88 | ::google::protobuf::Closure* done) 89 | { 90 | uint32_t userid = request->userid(); 91 | std::vector friendsList = GetFriendsList(userid); 92 | response->mutable_result()->set_errcode(0); 93 | response->mutable_result()->set_errmsg(""); 94 | for (std::string &name : friendsList) 95 | { 96 | std::string *p = response->add_friends(); 97 | *p = name; 98 | } 99 | done->Run(); 100 | } 101 | }; 102 | 103 | int main(int argc, char **argv) { 104 | // LOG_ERR("ddddd"); 105 | // LOG_INFO("ddddd"); 106 | 107 | // 调用框架的初始化操作 108 | MprpcApplication::Init(argc, argv); 109 | 110 | // 启动日志模块 111 | // 启动日志模块,设置日志等级 112 | Logger::GetInstance().Init("./config/log.conf"); 113 | 114 | // LOG_INFO("MprpcApplication init success"); 115 | // LOG_DEBUG("Test debug"); 116 | // provider是一个rpc网络服务对象。把UserService对象发布到rpc节点上 117 | RpcProvider provider; 118 | provider.NotifyService(new UserService()); 119 | LOG_INFO("insert UserService to RpcProvider"); 120 | provider.NotifyService(new FriendService()); 121 | LOG_INFO("insert FriendService to RpcProvider"); 122 | 123 | // 启动一个rpc服务发布节点 Run以后,进程进入阻塞状态,等待远程的rpc调用请求 124 | provider.Run(); 125 | 126 | return 0; 127 | } -------------------------------------------------------------------------------- /example/callee/friendservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "friend.pb.h" 4 | #include "mprpcapplication.h" 5 | #include "rpcprovider.h" 6 | #include 7 | 8 | class FriendService : public fixbug::FiendServiceRpc 9 | { 10 | public: 11 | std::vector GetFriendsList(uint32_t userid) 12 | { 13 | std::cout << "do GetFriendsList service! userid:" << userid << std::endl; 14 | std::vector vec; 15 | vec.push_back("gao yang"); 16 | vec.push_back("liu hong"); 17 | vec.push_back("wang shuo"); 18 | return vec; 19 | } 20 | 21 | // 重写基类方法 22 | void GetFriendsList(::google::protobuf::RpcController* controller, 23 | const ::fixbug::GetFriendsListRequest* request, 24 | ::fixbug::GetFriendsListResponse* response, 25 | ::google::protobuf::Closure* done) 26 | { 27 | uint32_t userid = request->userid(); 28 | std::vector friendsList = GetFriendsList(userid); 29 | response->mutable_result()->set_errcode(0); 30 | response->mutable_result()->set_errmsg(""); 31 | for (std::string &name : friendsList) 32 | { 33 | std::string *p = response->add_friends(); 34 | *p = name; 35 | } 36 | done->Run(); 37 | } 38 | }; 39 | 40 | int main(int argc, char **argv) 41 | { 42 | // LOG_ERR("ddddd"); 43 | // LOG_INFO("ddddd"); 44 | 45 | // 调用框架的初始化操作 46 | MprpcApplication::Init(argc, argv); 47 | 48 | // provider是一个rpc网络服务对象。把UserService对象发布到rpc节点上 49 | RpcProvider provider; 50 | provider.NotifyService(new FriendService()); 51 | 52 | // 启动一个rpc服务发布节点 Run以后,进程进入阻塞状态,等待远程的rpc调用请求 53 | provider.Run(); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /example/callee/userservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "user.pb.h" 5 | #include "mprpcapplication.h" 6 | #include "rpcprovider.h" 7 | 8 | /* 9 | UserService目前是一个本地服务 10 | */ 11 | class UserService : public fixbug::UserServiceRpc { 12 | public: 13 | bool Login(std::string name, std::string pwd) { 14 | std::cout << "doing local service: Login" << std::endl; 15 | std::cout << "name: " << name << ", pwd: " << pwd << std::endl;; 16 | return true; 17 | } 18 | 19 | bool Register(uint32_t id, std::string name, std::string pwd) 20 | { 21 | std::cout << "doing local service: Register" << std::endl; 22 | std::cout << "id:" << id << "name:" << name << " pwd:" << pwd << std::endl; 23 | return true; 24 | } 25 | 26 | /** 27 | * 重写基类UserServiceRpc的虚函数 28 | */ 29 | virtual void Login(::google::protobuf::RpcController* controller, 30 | const ::fixbug::LoginRequest* request, 31 | ::fixbug::LoginResponse* response, 32 | ::google::protobuf::Closure* done) 33 | { 34 | std::string name = request->name(); 35 | std::string pwd = request->pwd(); 36 | // 做本地业务 37 | bool login_result = Login(name, pwd); 38 | 39 | // 把响应写入 40 | fixbug::ResultCode* code = response->mutable_result(); 41 | code->set_errcode(0); 42 | code->set_errmsg(""); 43 | response->set_success(login_result); 44 | 45 | // 执行回调操作,由框架完成 46 | done->Run(); 47 | } 48 | 49 | void Register(::google::protobuf::RpcController* controller, 50 | const ::fixbug::RegisterRequest* request, 51 | ::fixbug::RegisterResponse* response, 52 | ::google::protobuf::Closure* done) 53 | { 54 | uint32_t id = request->id(); 55 | std::string name = request->name(); 56 | std::string pwd = request->pwd(); 57 | 58 | bool ret = Register(id, name, pwd); 59 | 60 | response->mutable_result()->set_errcode(0); 61 | response->mutable_result()->set_errmsg(""); 62 | response->set_success(ret); 63 | 64 | done->Run(); 65 | } 66 | }; 67 | 68 | int main(int argc, char* argv[]) { 69 | // 框架初始化: provider -i config 70 | MprpcApplication::Init(argc, argv); 71 | 72 | RpcProvider provider; 73 | provider.NotifyService(new UserService()); 74 | 75 | provider.Run(); 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /example/caller/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SRC_LIST calluserservice.cc ../publictype.pb.cc ../user.pb.cc) 2 | add_executable(consumer ${SRC_LIST}) 3 | target_link_libraries(consumer mprpc protobuf) 4 | 5 | set(SRC_LIST_F callfriendservice.cc ../publictype.pb.cc ../friend.pb.cc) 6 | add_executable(f_consumer ${SRC_LIST_F}) 7 | target_link_libraries(f_consumer mprpc protobuf) 8 | 9 | set(SRC_LIST_C 10 | callchatservice.cc 11 | ../publictype.pb.cc 12 | ../user.pb.cc 13 | ../friend.pb.cc) 14 | add_executable(chat_consumer ${SRC_LIST_C}) 15 | target_link_libraries(chat_consumer mprpc protobuf) -------------------------------------------------------------------------------- /example/caller/callchatservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mprpcapplication.h" 4 | #include "mprpcchannel.h" 5 | #include "mprpccontroller.h" 6 | #include "user.pb.h" 7 | #include "friend.pb.h" 8 | 9 | void UseUserServicePrcTest() { 10 | // ------------ 远程调用Login 11 | fixbug::UserServiceRpc_Stub stub(new MprpcChannel()); 12 | fixbug::LoginRequest request; 13 | request.set_name("zhang san"); 14 | request.set_pwd("123456"); 15 | 16 | fixbug::LoginResponse response; 17 | 18 | // 执行rpc远程调用 19 | stub.Login(nullptr, &request, &response, nullptr); 20 | 21 | // 一次rpc远程调用结束 22 | if (response.result().errcode() == 0) { 23 | std::cout << "rpc login response success: " << response.success() << std::endl; 24 | } else { 25 | std::cout << "rpc login response error: " << response.result().errmsg() << std::endl; 26 | } 27 | 28 | // 演示调用远程发布的rpc方法Register 29 | fixbug::RegisterRequest req; 30 | req.set_id(2000); 31 | req.set_name("mprpc"); 32 | req.set_pwd("666666"); 33 | fixbug::RegisterResponse rsp; 34 | 35 | // 以同步的方式发起rpc调用请求,等待返回结果 36 | MprpcController controller; 37 | stub.Register(&controller, &req, &rsp, nullptr); 38 | 39 | // 一次rpc调用完成,读调用的结果 40 | if (controller.Failed()) 41 | { 42 | std::cout << controller.ErrorText() << std::endl; 43 | } else { 44 | if (0 == rsp.result().errcode()) 45 | { 46 | std::cout << "rpc register response success:" << rsp.success() << std::endl; 47 | } 48 | else 49 | { 50 | std::cout << "rpc register response error : " << rsp.result().errmsg() << std::endl; 51 | } 52 | } 53 | } 54 | 55 | void UseFriendServiceRpcTest() { 56 | // 演示调用远程发布的rpc方法Login 57 | fixbug::FiendServiceRpc_Stub stub(new MprpcChannel()); 58 | // rpc方法的请求参数 59 | fixbug::GetFriendsListRequest request; 60 | request.set_userid(1000); 61 | // rpc方法的响应 62 | fixbug::GetFriendsListResponse response; 63 | // 发起rpc方法的调用 同步的rpc调用过程 MprpcChannel::callmethod 64 | MprpcController controller; 65 | stub.GetFriendsList(&controller, &request, &response, nullptr); // RpcChannel->RpcChannel::callMethod 集中来做所有rpc方法调用的参数序列化和网络发送 66 | 67 | // 一次rpc调用完成,读调用的结果 68 | if (controller.Failed()) 69 | { 70 | std::cout << controller.ErrorText() << std::endl; 71 | } 72 | else 73 | { 74 | if (0 == response.result().errcode()) 75 | { 76 | std::cout << "rpc GetFriendsList response success!" << std::endl; 77 | int size = response.friends_size(); 78 | for (int i=0; i < size; ++i) 79 | { 80 | std::cout << "index:" << (i+1) << " name:" << response.friends(i) << std::endl; 81 | } 82 | } 83 | else 84 | { 85 | std::cout << "rpc GetFriendsList response error : " << response.result().errmsg() << std::endl; 86 | } 87 | } 88 | } 89 | 90 | int main(int argc, char* argv[]) { 91 | 92 | // 初始化框架 93 | MprpcApplication::Init(argc, argv); 94 | 95 | UseUserServicePrcTest(); 96 | UseFriendServiceRpcTest(); 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /example/caller/callfriendservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mprpcapplication.h" 3 | #include "mprpccontroller.h" 4 | #include "mprpcchannel.h" 5 | #include "friend.pb.h" 6 | 7 | int main(int argc, char **argv) 8 | { 9 | // 整个程序启动以后,想使用mprpc框架来享受rpc服务调用,一定需要先调用框架的初始化函数(只初始化一次) 10 | MprpcApplication::Init(argc, argv); 11 | 12 | // 演示调用远程发布的rpc方法Login 13 | fixbug::FiendServiceRpc_Stub stub(new MprpcChannel()); 14 | // rpc方法的请求参数 15 | fixbug::GetFriendsListRequest request; 16 | request.set_userid(1000); 17 | // rpc方法的响应 18 | fixbug::GetFriendsListResponse response; 19 | // 发起rpc方法的调用 同步的rpc调用过程 MprpcChannel::callmethod 20 | MprpcController controller; 21 | stub.GetFriendsList(&controller, &request, &response, nullptr); // RpcChannel->RpcChannel::callMethod 集中来做所有rpc方法调用的参数序列化和网络发送 22 | 23 | // 一次rpc调用完成,读调用的结果 24 | if (controller.Failed()) 25 | { 26 | std::cout << controller.ErrorText() << std::endl; 27 | } 28 | else 29 | { 30 | if (0 == response.result().errcode()) 31 | { 32 | std::cout << "rpc GetFriendsList response success!" << std::endl; 33 | int size = response.friends_size(); 34 | for (int i=0; i < size; ++i) 35 | { 36 | std::cout << "index:" << (i+1) << " name:" << response.friends(i) << std::endl; 37 | } 38 | } 39 | else 40 | { 41 | std::cout << "rpc GetFriendsList response error : " << response.result().errmsg() << std::endl; 42 | } 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /example/caller/calluserservice.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mprpcapplication.h" 4 | #include "mprpcchannel.h" 5 | #include "user.pb.h" 6 | 7 | int main(int argc, char* argv[]) { 8 | 9 | // 初始化框架 10 | MprpcApplication::Init(argc, argv); 11 | 12 | // ------------ 远程调用Login 13 | fixbug::UserServiceRpc_Stub stub(new MprpcChannel()); 14 | fixbug::LoginRequest request; 15 | request.set_name("zhang san"); 16 | request.set_pwd("123456"); 17 | 18 | fixbug::LoginResponse response; 19 | 20 | // 执行rpc远程调用 21 | stub.Login(nullptr, &request, &response, nullptr); 22 | 23 | // 一次rpc远程调用结束 24 | if (response.result().errcode() == 0) { 25 | std::cout << "rpc login response success: " << response.success() << std::endl; 26 | } else { 27 | std::cout << "rpc login response error: " << response.result().errmsg() << std::endl; 28 | } 29 | 30 | // 演示调用远程发布的rpc方法Register 31 | fixbug::RegisterRequest req; 32 | req.set_id(2000); 33 | req.set_name("mprpc"); 34 | req.set_pwd("666666"); 35 | fixbug::RegisterResponse rsp; 36 | 37 | // 以同步的方式发起rpc调用请求,等待返回结果 38 | stub.Register(nullptr, &req, &rsp, nullptr); 39 | 40 | // 一次rpc调用完成,读调用的结果 41 | if (0 == rsp.result().errcode()) 42 | { 43 | std::cout << "rpc register response success:" << rsp.success() << std::endl; 44 | } 45 | else 46 | { 47 | std::cout << "rpc register response error : " << rsp.result().errmsg() << std::endl; 48 | } 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /example/friend.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: friend.proto 3 | 4 | #include "friend.pb.h" 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | // @@protoc_insertion_point(includes) 16 | #include 17 | extern PROTOBUF_INTERNAL_EXPORT_publictype_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ResultCode_publictype_2eproto; 18 | namespace fixbug { 19 | class GetFriendsListRequestDefaultTypeInternal { 20 | public: 21 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 22 | } _GetFriendsListRequest_default_instance_; 23 | class GetFriendsListResponseDefaultTypeInternal { 24 | public: 25 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 26 | } _GetFriendsListResponse_default_instance_; 27 | } // namespace fixbug 28 | static void InitDefaultsscc_info_GetFriendsListRequest_friend_2eproto() { 29 | GOOGLE_PROTOBUF_VERIFY_VERSION; 30 | 31 | { 32 | void* ptr = &::fixbug::_GetFriendsListRequest_default_instance_; 33 | new (ptr) ::fixbug::GetFriendsListRequest(); 34 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 35 | } 36 | ::fixbug::GetFriendsListRequest::InitAsDefaultInstance(); 37 | } 38 | 39 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_GetFriendsListRequest_friend_2eproto = 40 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_GetFriendsListRequest_friend_2eproto}, {}}; 41 | 42 | static void InitDefaultsscc_info_GetFriendsListResponse_friend_2eproto() { 43 | GOOGLE_PROTOBUF_VERIFY_VERSION; 44 | 45 | { 46 | void* ptr = &::fixbug::_GetFriendsListResponse_default_instance_; 47 | new (ptr) ::fixbug::GetFriendsListResponse(); 48 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 49 | } 50 | ::fixbug::GetFriendsListResponse::InitAsDefaultInstance(); 51 | } 52 | 53 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_GetFriendsListResponse_friend_2eproto = 54 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, 0, InitDefaultsscc_info_GetFriendsListResponse_friend_2eproto}, { 55 | &scc_info_ResultCode_publictype_2eproto.base,}}; 56 | 57 | static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_friend_2eproto[2]; 58 | static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_friend_2eproto = nullptr; 59 | static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* file_level_service_descriptors_friend_2eproto[1]; 60 | 61 | const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_friend_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 62 | ~0u, // no _has_bits_ 63 | PROTOBUF_FIELD_OFFSET(::fixbug::GetFriendsListRequest, _internal_metadata_), 64 | ~0u, // no _extensions_ 65 | ~0u, // no _oneof_case_ 66 | ~0u, // no _weak_field_map_ 67 | PROTOBUF_FIELD_OFFSET(::fixbug::GetFriendsListRequest, userid_), 68 | ~0u, // no _has_bits_ 69 | PROTOBUF_FIELD_OFFSET(::fixbug::GetFriendsListResponse, _internal_metadata_), 70 | ~0u, // no _extensions_ 71 | ~0u, // no _oneof_case_ 72 | ~0u, // no _weak_field_map_ 73 | PROTOBUF_FIELD_OFFSET(::fixbug::GetFriendsListResponse, result_), 74 | PROTOBUF_FIELD_OFFSET(::fixbug::GetFriendsListResponse, friends_), 75 | }; 76 | static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 77 | { 0, -1, sizeof(::fixbug::GetFriendsListRequest)}, 78 | { 6, -1, sizeof(::fixbug::GetFriendsListResponse)}, 79 | }; 80 | 81 | static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { 82 | reinterpret_cast(&::fixbug::_GetFriendsListRequest_default_instance_), 83 | reinterpret_cast(&::fixbug::_GetFriendsListResponse_default_instance_), 84 | }; 85 | 86 | const char descriptor_table_protodef_friend_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = 87 | "\n\014friend.proto\022\006fixbug\032\020publictype.proto" 88 | "\"\'\n\025GetFriendsListRequest\022\016\n\006userid\030\001 \001(" 89 | "\r\"M\n\026GetFriendsListResponse\022\"\n\006result\030\001 " 90 | "\001(\0132\022.fixbug.ResultCode\022\017\n\007friends\030\002 \003(\014" 91 | "2b\n\017FiendServiceRpc\022O\n\016GetFriendsList\022\035." 92 | "fixbug.GetFriendsListRequest\032\036.fixbug.Ge" 93 | "tFriendsListResponseB\003\200\001\001b\006proto3" 94 | ; 95 | static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_friend_2eproto_deps[1] = { 96 | &::descriptor_table_publictype_2eproto, 97 | }; 98 | static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_friend_2eproto_sccs[2] = { 99 | &scc_info_GetFriendsListRequest_friend_2eproto.base, 100 | &scc_info_GetFriendsListResponse_friend_2eproto.base, 101 | }; 102 | static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_friend_2eproto_once; 103 | static bool descriptor_table_friend_2eproto_initialized = false; 104 | const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_friend_2eproto = { 105 | &descriptor_table_friend_2eproto_initialized, descriptor_table_protodef_friend_2eproto, "friend.proto", 273, 106 | &descriptor_table_friend_2eproto_once, descriptor_table_friend_2eproto_sccs, descriptor_table_friend_2eproto_deps, 2, 1, 107 | schemas, file_default_instances, TableStruct_friend_2eproto::offsets, 108 | file_level_metadata_friend_2eproto, 2, file_level_enum_descriptors_friend_2eproto, file_level_service_descriptors_friend_2eproto, 109 | }; 110 | 111 | // Force running AddDescriptors() at dynamic initialization time. 112 | static bool dynamic_init_dummy_friend_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_friend_2eproto), true); 113 | namespace fixbug { 114 | 115 | // =================================================================== 116 | 117 | void GetFriendsListRequest::InitAsDefaultInstance() { 118 | } 119 | class GetFriendsListRequest::_Internal { 120 | public: 121 | }; 122 | 123 | GetFriendsListRequest::GetFriendsListRequest() 124 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 125 | SharedCtor(); 126 | // @@protoc_insertion_point(constructor:fixbug.GetFriendsListRequest) 127 | } 128 | GetFriendsListRequest::GetFriendsListRequest(const GetFriendsListRequest& from) 129 | : ::PROTOBUF_NAMESPACE_ID::Message(), 130 | _internal_metadata_(nullptr) { 131 | _internal_metadata_.MergeFrom(from._internal_metadata_); 132 | userid_ = from.userid_; 133 | // @@protoc_insertion_point(copy_constructor:fixbug.GetFriendsListRequest) 134 | } 135 | 136 | void GetFriendsListRequest::SharedCtor() { 137 | userid_ = 0u; 138 | } 139 | 140 | GetFriendsListRequest::~GetFriendsListRequest() { 141 | // @@protoc_insertion_point(destructor:fixbug.GetFriendsListRequest) 142 | SharedDtor(); 143 | } 144 | 145 | void GetFriendsListRequest::SharedDtor() { 146 | } 147 | 148 | void GetFriendsListRequest::SetCachedSize(int size) const { 149 | _cached_size_.Set(size); 150 | } 151 | const GetFriendsListRequest& GetFriendsListRequest::default_instance() { 152 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_GetFriendsListRequest_friend_2eproto.base); 153 | return *internal_default_instance(); 154 | } 155 | 156 | 157 | void GetFriendsListRequest::Clear() { 158 | // @@protoc_insertion_point(message_clear_start:fixbug.GetFriendsListRequest) 159 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 160 | // Prevent compiler warnings about cached_has_bits being unused 161 | (void) cached_has_bits; 162 | 163 | userid_ = 0u; 164 | _internal_metadata_.Clear(); 165 | } 166 | 167 | const char* GetFriendsListRequest::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 168 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 169 | while (!ctx->Done(&ptr)) { 170 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 171 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 172 | CHK_(ptr); 173 | switch (tag >> 3) { 174 | // uint32 userid = 1; 175 | case 1: 176 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { 177 | userid_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); 178 | CHK_(ptr); 179 | } else goto handle_unusual; 180 | continue; 181 | default: { 182 | handle_unusual: 183 | if ((tag & 7) == 4 || tag == 0) { 184 | ctx->SetLastTag(tag); 185 | goto success; 186 | } 187 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 188 | CHK_(ptr != nullptr); 189 | continue; 190 | } 191 | } // switch 192 | } // while 193 | success: 194 | return ptr; 195 | failure: 196 | ptr = nullptr; 197 | goto success; 198 | #undef CHK_ 199 | } 200 | 201 | ::PROTOBUF_NAMESPACE_ID::uint8* GetFriendsListRequest::_InternalSerialize( 202 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 203 | // @@protoc_insertion_point(serialize_to_array_start:fixbug.GetFriendsListRequest) 204 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 205 | (void) cached_has_bits; 206 | 207 | // uint32 userid = 1; 208 | if (this->userid() != 0) { 209 | target = stream->EnsureSpace(target); 210 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt32ToArray(1, this->_internal_userid(), target); 211 | } 212 | 213 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 214 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 215 | _internal_metadata_.unknown_fields(), target, stream); 216 | } 217 | // @@protoc_insertion_point(serialize_to_array_end:fixbug.GetFriendsListRequest) 218 | return target; 219 | } 220 | 221 | size_t GetFriendsListRequest::ByteSizeLong() const { 222 | // @@protoc_insertion_point(message_byte_size_start:fixbug.GetFriendsListRequest) 223 | size_t total_size = 0; 224 | 225 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 226 | // Prevent compiler warnings about cached_has_bits being unused 227 | (void) cached_has_bits; 228 | 229 | // uint32 userid = 1; 230 | if (this->userid() != 0) { 231 | total_size += 1 + 232 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt32Size( 233 | this->_internal_userid()); 234 | } 235 | 236 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 237 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 238 | _internal_metadata_, total_size, &_cached_size_); 239 | } 240 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 241 | SetCachedSize(cached_size); 242 | return total_size; 243 | } 244 | 245 | void GetFriendsListRequest::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 246 | // @@protoc_insertion_point(generalized_merge_from_start:fixbug.GetFriendsListRequest) 247 | GOOGLE_DCHECK_NE(&from, this); 248 | const GetFriendsListRequest* source = 249 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 250 | &from); 251 | if (source == nullptr) { 252 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:fixbug.GetFriendsListRequest) 253 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 254 | } else { 255 | // @@protoc_insertion_point(generalized_merge_from_cast_success:fixbug.GetFriendsListRequest) 256 | MergeFrom(*source); 257 | } 258 | } 259 | 260 | void GetFriendsListRequest::MergeFrom(const GetFriendsListRequest& from) { 261 | // @@protoc_insertion_point(class_specific_merge_from_start:fixbug.GetFriendsListRequest) 262 | GOOGLE_DCHECK_NE(&from, this); 263 | _internal_metadata_.MergeFrom(from._internal_metadata_); 264 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 265 | (void) cached_has_bits; 266 | 267 | if (from.userid() != 0) { 268 | _internal_set_userid(from._internal_userid()); 269 | } 270 | } 271 | 272 | void GetFriendsListRequest::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 273 | // @@protoc_insertion_point(generalized_copy_from_start:fixbug.GetFriendsListRequest) 274 | if (&from == this) return; 275 | Clear(); 276 | MergeFrom(from); 277 | } 278 | 279 | void GetFriendsListRequest::CopyFrom(const GetFriendsListRequest& from) { 280 | // @@protoc_insertion_point(class_specific_copy_from_start:fixbug.GetFriendsListRequest) 281 | if (&from == this) return; 282 | Clear(); 283 | MergeFrom(from); 284 | } 285 | 286 | bool GetFriendsListRequest::IsInitialized() const { 287 | return true; 288 | } 289 | 290 | void GetFriendsListRequest::InternalSwap(GetFriendsListRequest* other) { 291 | using std::swap; 292 | _internal_metadata_.Swap(&other->_internal_metadata_); 293 | swap(userid_, other->userid_); 294 | } 295 | 296 | ::PROTOBUF_NAMESPACE_ID::Metadata GetFriendsListRequest::GetMetadata() const { 297 | return GetMetadataStatic(); 298 | } 299 | 300 | 301 | // =================================================================== 302 | 303 | void GetFriendsListResponse::InitAsDefaultInstance() { 304 | ::fixbug::_GetFriendsListResponse_default_instance_._instance.get_mutable()->result_ = const_cast< ::fixbug::ResultCode*>( 305 | ::fixbug::ResultCode::internal_default_instance()); 306 | } 307 | class GetFriendsListResponse::_Internal { 308 | public: 309 | static const ::fixbug::ResultCode& result(const GetFriendsListResponse* msg); 310 | }; 311 | 312 | const ::fixbug::ResultCode& 313 | GetFriendsListResponse::_Internal::result(const GetFriendsListResponse* msg) { 314 | return *msg->result_; 315 | } 316 | void GetFriendsListResponse::clear_result() { 317 | if (GetArenaNoVirtual() == nullptr && result_ != nullptr) { 318 | delete result_; 319 | } 320 | result_ = nullptr; 321 | } 322 | GetFriendsListResponse::GetFriendsListResponse() 323 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 324 | SharedCtor(); 325 | // @@protoc_insertion_point(constructor:fixbug.GetFriendsListResponse) 326 | } 327 | GetFriendsListResponse::GetFriendsListResponse(const GetFriendsListResponse& from) 328 | : ::PROTOBUF_NAMESPACE_ID::Message(), 329 | _internal_metadata_(nullptr), 330 | friends_(from.friends_) { 331 | _internal_metadata_.MergeFrom(from._internal_metadata_); 332 | if (from._internal_has_result()) { 333 | result_ = new ::fixbug::ResultCode(*from.result_); 334 | } else { 335 | result_ = nullptr; 336 | } 337 | // @@protoc_insertion_point(copy_constructor:fixbug.GetFriendsListResponse) 338 | } 339 | 340 | void GetFriendsListResponse::SharedCtor() { 341 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GetFriendsListResponse_friend_2eproto.base); 342 | result_ = nullptr; 343 | } 344 | 345 | GetFriendsListResponse::~GetFriendsListResponse() { 346 | // @@protoc_insertion_point(destructor:fixbug.GetFriendsListResponse) 347 | SharedDtor(); 348 | } 349 | 350 | void GetFriendsListResponse::SharedDtor() { 351 | if (this != internal_default_instance()) delete result_; 352 | } 353 | 354 | void GetFriendsListResponse::SetCachedSize(int size) const { 355 | _cached_size_.Set(size); 356 | } 357 | const GetFriendsListResponse& GetFriendsListResponse::default_instance() { 358 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_GetFriendsListResponse_friend_2eproto.base); 359 | return *internal_default_instance(); 360 | } 361 | 362 | 363 | void GetFriendsListResponse::Clear() { 364 | // @@protoc_insertion_point(message_clear_start:fixbug.GetFriendsListResponse) 365 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 366 | // Prevent compiler warnings about cached_has_bits being unused 367 | (void) cached_has_bits; 368 | 369 | friends_.Clear(); 370 | if (GetArenaNoVirtual() == nullptr && result_ != nullptr) { 371 | delete result_; 372 | } 373 | result_ = nullptr; 374 | _internal_metadata_.Clear(); 375 | } 376 | 377 | const char* GetFriendsListResponse::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 378 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 379 | while (!ctx->Done(&ptr)) { 380 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 381 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 382 | CHK_(ptr); 383 | switch (tag >> 3) { 384 | // .fixbug.ResultCode result = 1; 385 | case 1: 386 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { 387 | ptr = ctx->ParseMessage(_internal_mutable_result(), ptr); 388 | CHK_(ptr); 389 | } else goto handle_unusual; 390 | continue; 391 | // repeated bytes friends = 2; 392 | case 2: 393 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { 394 | ptr -= 1; 395 | do { 396 | ptr += 1; 397 | auto str = _internal_add_friends(); 398 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 399 | CHK_(ptr); 400 | if (!ctx->DataAvailable(ptr)) break; 401 | } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<18>(ptr)); 402 | } else goto handle_unusual; 403 | continue; 404 | default: { 405 | handle_unusual: 406 | if ((tag & 7) == 4 || tag == 0) { 407 | ctx->SetLastTag(tag); 408 | goto success; 409 | } 410 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 411 | CHK_(ptr != nullptr); 412 | continue; 413 | } 414 | } // switch 415 | } // while 416 | success: 417 | return ptr; 418 | failure: 419 | ptr = nullptr; 420 | goto success; 421 | #undef CHK_ 422 | } 423 | 424 | ::PROTOBUF_NAMESPACE_ID::uint8* GetFriendsListResponse::_InternalSerialize( 425 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 426 | // @@protoc_insertion_point(serialize_to_array_start:fixbug.GetFriendsListResponse) 427 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 428 | (void) cached_has_bits; 429 | 430 | // .fixbug.ResultCode result = 1; 431 | if (this->has_result()) { 432 | target = stream->EnsureSpace(target); 433 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: 434 | InternalWriteMessage( 435 | 1, _Internal::result(this), target, stream); 436 | } 437 | 438 | // repeated bytes friends = 2; 439 | for (int i = 0, n = this->_internal_friends_size(); i < n; i++) { 440 | const auto& s = this->_internal_friends(i); 441 | target = stream->WriteBytes(2, s, target); 442 | } 443 | 444 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 445 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 446 | _internal_metadata_.unknown_fields(), target, stream); 447 | } 448 | // @@protoc_insertion_point(serialize_to_array_end:fixbug.GetFriendsListResponse) 449 | return target; 450 | } 451 | 452 | size_t GetFriendsListResponse::ByteSizeLong() const { 453 | // @@protoc_insertion_point(message_byte_size_start:fixbug.GetFriendsListResponse) 454 | size_t total_size = 0; 455 | 456 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 457 | // Prevent compiler warnings about cached_has_bits being unused 458 | (void) cached_has_bits; 459 | 460 | // repeated bytes friends = 2; 461 | total_size += 1 * 462 | ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(friends_.size()); 463 | for (int i = 0, n = friends_.size(); i < n; i++) { 464 | total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( 465 | friends_.Get(i)); 466 | } 467 | 468 | // .fixbug.ResultCode result = 1; 469 | if (this->has_result()) { 470 | total_size += 1 + 471 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( 472 | *result_); 473 | } 474 | 475 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 476 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 477 | _internal_metadata_, total_size, &_cached_size_); 478 | } 479 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 480 | SetCachedSize(cached_size); 481 | return total_size; 482 | } 483 | 484 | void GetFriendsListResponse::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 485 | // @@protoc_insertion_point(generalized_merge_from_start:fixbug.GetFriendsListResponse) 486 | GOOGLE_DCHECK_NE(&from, this); 487 | const GetFriendsListResponse* source = 488 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 489 | &from); 490 | if (source == nullptr) { 491 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:fixbug.GetFriendsListResponse) 492 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 493 | } else { 494 | // @@protoc_insertion_point(generalized_merge_from_cast_success:fixbug.GetFriendsListResponse) 495 | MergeFrom(*source); 496 | } 497 | } 498 | 499 | void GetFriendsListResponse::MergeFrom(const GetFriendsListResponse& from) { 500 | // @@protoc_insertion_point(class_specific_merge_from_start:fixbug.GetFriendsListResponse) 501 | GOOGLE_DCHECK_NE(&from, this); 502 | _internal_metadata_.MergeFrom(from._internal_metadata_); 503 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 504 | (void) cached_has_bits; 505 | 506 | friends_.MergeFrom(from.friends_); 507 | if (from.has_result()) { 508 | _internal_mutable_result()->::fixbug::ResultCode::MergeFrom(from._internal_result()); 509 | } 510 | } 511 | 512 | void GetFriendsListResponse::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 513 | // @@protoc_insertion_point(generalized_copy_from_start:fixbug.GetFriendsListResponse) 514 | if (&from == this) return; 515 | Clear(); 516 | MergeFrom(from); 517 | } 518 | 519 | void GetFriendsListResponse::CopyFrom(const GetFriendsListResponse& from) { 520 | // @@protoc_insertion_point(class_specific_copy_from_start:fixbug.GetFriendsListResponse) 521 | if (&from == this) return; 522 | Clear(); 523 | MergeFrom(from); 524 | } 525 | 526 | bool GetFriendsListResponse::IsInitialized() const { 527 | return true; 528 | } 529 | 530 | void GetFriendsListResponse::InternalSwap(GetFriendsListResponse* other) { 531 | using std::swap; 532 | _internal_metadata_.Swap(&other->_internal_metadata_); 533 | friends_.InternalSwap(&other->friends_); 534 | swap(result_, other->result_); 535 | } 536 | 537 | ::PROTOBUF_NAMESPACE_ID::Metadata GetFriendsListResponse::GetMetadata() const { 538 | return GetMetadataStatic(); 539 | } 540 | 541 | 542 | // =================================================================== 543 | 544 | FiendServiceRpc::~FiendServiceRpc() {} 545 | 546 | const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* FiendServiceRpc::descriptor() { 547 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_friend_2eproto); 548 | return file_level_service_descriptors_friend_2eproto[0]; 549 | } 550 | 551 | const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* FiendServiceRpc::GetDescriptor() { 552 | return descriptor(); 553 | } 554 | 555 | void FiendServiceRpc::GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 556 | const ::fixbug::GetFriendsListRequest*, 557 | ::fixbug::GetFriendsListResponse*, 558 | ::google::protobuf::Closure* done) { 559 | controller->SetFailed("Method GetFriendsList() not implemented."); 560 | done->Run(); 561 | } 562 | 563 | void FiendServiceRpc::CallMethod(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method, 564 | ::PROTOBUF_NAMESPACE_ID::RpcController* controller, 565 | const ::PROTOBUF_NAMESPACE_ID::Message* request, 566 | ::PROTOBUF_NAMESPACE_ID::Message* response, 567 | ::google::protobuf::Closure* done) { 568 | GOOGLE_DCHECK_EQ(method->service(), file_level_service_descriptors_friend_2eproto[0]); 569 | switch(method->index()) { 570 | case 0: 571 | GetFriendsList(controller, 572 | ::PROTOBUF_NAMESPACE_ID::internal::DownCast( 573 | request), 574 | ::PROTOBUF_NAMESPACE_ID::internal::DownCast<::fixbug::GetFriendsListResponse*>( 575 | response), 576 | done); 577 | break; 578 | default: 579 | GOOGLE_LOG(FATAL) << "Bad method index; this should never happen."; 580 | break; 581 | } 582 | } 583 | 584 | const ::PROTOBUF_NAMESPACE_ID::Message& FiendServiceRpc::GetRequestPrototype( 585 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const { 586 | GOOGLE_DCHECK_EQ(method->service(), descriptor()); 587 | switch(method->index()) { 588 | case 0: 589 | return ::fixbug::GetFriendsListRequest::default_instance(); 590 | default: 591 | GOOGLE_LOG(FATAL) << "Bad method index; this should never happen."; 592 | return *::PROTOBUF_NAMESPACE_ID::MessageFactory::generated_factory() 593 | ->GetPrototype(method->input_type()); 594 | } 595 | } 596 | 597 | const ::PROTOBUF_NAMESPACE_ID::Message& FiendServiceRpc::GetResponsePrototype( 598 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const { 599 | GOOGLE_DCHECK_EQ(method->service(), descriptor()); 600 | switch(method->index()) { 601 | case 0: 602 | return ::fixbug::GetFriendsListResponse::default_instance(); 603 | default: 604 | GOOGLE_LOG(FATAL) << "Bad method index; this should never happen."; 605 | return *::PROTOBUF_NAMESPACE_ID::MessageFactory::generated_factory() 606 | ->GetPrototype(method->output_type()); 607 | } 608 | } 609 | 610 | FiendServiceRpc_Stub::FiendServiceRpc_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel) 611 | : channel_(channel), owns_channel_(false) {} 612 | FiendServiceRpc_Stub::FiendServiceRpc_Stub( 613 | ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel, 614 | ::PROTOBUF_NAMESPACE_ID::Service::ChannelOwnership ownership) 615 | : channel_(channel), 616 | owns_channel_(ownership == ::PROTOBUF_NAMESPACE_ID::Service::STUB_OWNS_CHANNEL) {} 617 | FiendServiceRpc_Stub::~FiendServiceRpc_Stub() { 618 | if (owns_channel_) delete channel_; 619 | } 620 | 621 | void FiendServiceRpc_Stub::GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 622 | const ::fixbug::GetFriendsListRequest* request, 623 | ::fixbug::GetFriendsListResponse* response, 624 | ::google::protobuf::Closure* done) { 625 | channel_->CallMethod(descriptor()->method(0), 626 | controller, request, response, done); 627 | } 628 | 629 | // @@protoc_insertion_point(namespace_scope) 630 | } // namespace fixbug 631 | PROTOBUF_NAMESPACE_OPEN 632 | template<> PROTOBUF_NOINLINE ::fixbug::GetFriendsListRequest* Arena::CreateMaybeMessage< ::fixbug::GetFriendsListRequest >(Arena* arena) { 633 | return Arena::CreateInternal< ::fixbug::GetFriendsListRequest >(arena); 634 | } 635 | template<> PROTOBUF_NOINLINE ::fixbug::GetFriendsListResponse* Arena::CreateMaybeMessage< ::fixbug::GetFriendsListResponse >(Arena* arena) { 636 | return Arena::CreateInternal< ::fixbug::GetFriendsListResponse >(arena); 637 | } 638 | PROTOBUF_NAMESPACE_CLOSE 639 | 640 | // @@protoc_insertion_point(global_scope) 641 | #include 642 | -------------------------------------------------------------------------------- /example/friend.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: friend.proto 3 | 4 | #ifndef GOOGLE_PROTOBUF_INCLUDED_friend_2eproto 5 | #define GOOGLE_PROTOBUF_INCLUDED_friend_2eproto 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #if PROTOBUF_VERSION < 3011000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 3011000 < PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include // IWYU pragma: export 33 | #include // IWYU pragma: export 34 | #include 35 | #include 36 | #include "publictype.pb.h" 37 | // @@protoc_insertion_point(includes) 38 | #include 39 | #define PROTOBUF_INTERNAL_EXPORT_friend_2eproto 40 | PROTOBUF_NAMESPACE_OPEN 41 | namespace internal { 42 | class AnyMetadata; 43 | } // namespace internal 44 | PROTOBUF_NAMESPACE_CLOSE 45 | 46 | // Internal implementation detail -- do not use these members. 47 | struct TableStruct_friend_2eproto { 48 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] 49 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] 51 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 52 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[2] 53 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 54 | static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; 55 | static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; 56 | static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; 57 | }; 58 | extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_friend_2eproto; 59 | namespace fixbug { 60 | class GetFriendsListRequest; 61 | class GetFriendsListRequestDefaultTypeInternal; 62 | extern GetFriendsListRequestDefaultTypeInternal _GetFriendsListRequest_default_instance_; 63 | class GetFriendsListResponse; 64 | class GetFriendsListResponseDefaultTypeInternal; 65 | extern GetFriendsListResponseDefaultTypeInternal _GetFriendsListResponse_default_instance_; 66 | } // namespace fixbug 67 | PROTOBUF_NAMESPACE_OPEN 68 | template<> ::fixbug::GetFriendsListRequest* Arena::CreateMaybeMessage<::fixbug::GetFriendsListRequest>(Arena*); 69 | template<> ::fixbug::GetFriendsListResponse* Arena::CreateMaybeMessage<::fixbug::GetFriendsListResponse>(Arena*); 70 | PROTOBUF_NAMESPACE_CLOSE 71 | namespace fixbug { 72 | 73 | // =================================================================== 74 | 75 | class GetFriendsListRequest : 76 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.GetFriendsListRequest) */ { 77 | public: 78 | GetFriendsListRequest(); 79 | virtual ~GetFriendsListRequest(); 80 | 81 | GetFriendsListRequest(const GetFriendsListRequest& from); 82 | GetFriendsListRequest(GetFriendsListRequest&& from) noexcept 83 | : GetFriendsListRequest() { 84 | *this = ::std::move(from); 85 | } 86 | 87 | inline GetFriendsListRequest& operator=(const GetFriendsListRequest& from) { 88 | CopyFrom(from); 89 | return *this; 90 | } 91 | inline GetFriendsListRequest& operator=(GetFriendsListRequest&& from) noexcept { 92 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 93 | if (this != &from) InternalSwap(&from); 94 | } else { 95 | CopyFrom(from); 96 | } 97 | return *this; 98 | } 99 | 100 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 101 | return GetDescriptor(); 102 | } 103 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 104 | return GetMetadataStatic().descriptor; 105 | } 106 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 107 | return GetMetadataStatic().reflection; 108 | } 109 | static const GetFriendsListRequest& default_instance(); 110 | 111 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 112 | static inline const GetFriendsListRequest* internal_default_instance() { 113 | return reinterpret_cast( 114 | &_GetFriendsListRequest_default_instance_); 115 | } 116 | static constexpr int kIndexInFileMessages = 117 | 0; 118 | 119 | friend void swap(GetFriendsListRequest& a, GetFriendsListRequest& b) { 120 | a.Swap(&b); 121 | } 122 | inline void Swap(GetFriendsListRequest* other) { 123 | if (other == this) return; 124 | InternalSwap(other); 125 | } 126 | 127 | // implements Message ---------------------------------------------- 128 | 129 | inline GetFriendsListRequest* New() const final { 130 | return CreateMaybeMessage(nullptr); 131 | } 132 | 133 | GetFriendsListRequest* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 134 | return CreateMaybeMessage(arena); 135 | } 136 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 137 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 138 | void CopyFrom(const GetFriendsListRequest& from); 139 | void MergeFrom(const GetFriendsListRequest& from); 140 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 141 | bool IsInitialized() const final; 142 | 143 | size_t ByteSizeLong() const final; 144 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 145 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 146 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 147 | int GetCachedSize() const final { return _cached_size_.Get(); } 148 | 149 | private: 150 | inline void SharedCtor(); 151 | inline void SharedDtor(); 152 | void SetCachedSize(int size) const final; 153 | void InternalSwap(GetFriendsListRequest* other); 154 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 155 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 156 | return "fixbug.GetFriendsListRequest"; 157 | } 158 | private: 159 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 160 | return nullptr; 161 | } 162 | inline void* MaybeArenaPtr() const { 163 | return nullptr; 164 | } 165 | public: 166 | 167 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 168 | private: 169 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 170 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_friend_2eproto); 171 | return ::descriptor_table_friend_2eproto.file_level_metadata[kIndexInFileMessages]; 172 | } 173 | 174 | public: 175 | 176 | // nested types ---------------------------------------------------- 177 | 178 | // accessors ------------------------------------------------------- 179 | 180 | enum : int { 181 | kUseridFieldNumber = 1, 182 | }; 183 | // uint32 userid = 1; 184 | void clear_userid(); 185 | ::PROTOBUF_NAMESPACE_ID::uint32 userid() const; 186 | void set_userid(::PROTOBUF_NAMESPACE_ID::uint32 value); 187 | private: 188 | ::PROTOBUF_NAMESPACE_ID::uint32 _internal_userid() const; 189 | void _internal_set_userid(::PROTOBUF_NAMESPACE_ID::uint32 value); 190 | public: 191 | 192 | // @@protoc_insertion_point(class_scope:fixbug.GetFriendsListRequest) 193 | private: 194 | class _Internal; 195 | 196 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 197 | ::PROTOBUF_NAMESPACE_ID::uint32 userid_; 198 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 199 | friend struct ::TableStruct_friend_2eproto; 200 | }; 201 | // ------------------------------------------------------------------- 202 | 203 | class GetFriendsListResponse : 204 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.GetFriendsListResponse) */ { 205 | public: 206 | GetFriendsListResponse(); 207 | virtual ~GetFriendsListResponse(); 208 | 209 | GetFriendsListResponse(const GetFriendsListResponse& from); 210 | GetFriendsListResponse(GetFriendsListResponse&& from) noexcept 211 | : GetFriendsListResponse() { 212 | *this = ::std::move(from); 213 | } 214 | 215 | inline GetFriendsListResponse& operator=(const GetFriendsListResponse& from) { 216 | CopyFrom(from); 217 | return *this; 218 | } 219 | inline GetFriendsListResponse& operator=(GetFriendsListResponse&& from) noexcept { 220 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 221 | if (this != &from) InternalSwap(&from); 222 | } else { 223 | CopyFrom(from); 224 | } 225 | return *this; 226 | } 227 | 228 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 229 | return GetDescriptor(); 230 | } 231 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 232 | return GetMetadataStatic().descriptor; 233 | } 234 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 235 | return GetMetadataStatic().reflection; 236 | } 237 | static const GetFriendsListResponse& default_instance(); 238 | 239 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 240 | static inline const GetFriendsListResponse* internal_default_instance() { 241 | return reinterpret_cast( 242 | &_GetFriendsListResponse_default_instance_); 243 | } 244 | static constexpr int kIndexInFileMessages = 245 | 1; 246 | 247 | friend void swap(GetFriendsListResponse& a, GetFriendsListResponse& b) { 248 | a.Swap(&b); 249 | } 250 | inline void Swap(GetFriendsListResponse* other) { 251 | if (other == this) return; 252 | InternalSwap(other); 253 | } 254 | 255 | // implements Message ---------------------------------------------- 256 | 257 | inline GetFriendsListResponse* New() const final { 258 | return CreateMaybeMessage(nullptr); 259 | } 260 | 261 | GetFriendsListResponse* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 262 | return CreateMaybeMessage(arena); 263 | } 264 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 265 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 266 | void CopyFrom(const GetFriendsListResponse& from); 267 | void MergeFrom(const GetFriendsListResponse& from); 268 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 269 | bool IsInitialized() const final; 270 | 271 | size_t ByteSizeLong() const final; 272 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 273 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 274 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 275 | int GetCachedSize() const final { return _cached_size_.Get(); } 276 | 277 | private: 278 | inline void SharedCtor(); 279 | inline void SharedDtor(); 280 | void SetCachedSize(int size) const final; 281 | void InternalSwap(GetFriendsListResponse* other); 282 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 283 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 284 | return "fixbug.GetFriendsListResponse"; 285 | } 286 | private: 287 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 288 | return nullptr; 289 | } 290 | inline void* MaybeArenaPtr() const { 291 | return nullptr; 292 | } 293 | public: 294 | 295 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 296 | private: 297 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 298 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_friend_2eproto); 299 | return ::descriptor_table_friend_2eproto.file_level_metadata[kIndexInFileMessages]; 300 | } 301 | 302 | public: 303 | 304 | // nested types ---------------------------------------------------- 305 | 306 | // accessors ------------------------------------------------------- 307 | 308 | enum : int { 309 | kFriendsFieldNumber = 2, 310 | kResultFieldNumber = 1, 311 | }; 312 | // repeated bytes friends = 2; 313 | int friends_size() const; 314 | private: 315 | int _internal_friends_size() const; 316 | public: 317 | void clear_friends(); 318 | const std::string& friends(int index) const; 319 | std::string* mutable_friends(int index); 320 | void set_friends(int index, const std::string& value); 321 | void set_friends(int index, std::string&& value); 322 | void set_friends(int index, const char* value); 323 | void set_friends(int index, const void* value, size_t size); 324 | std::string* add_friends(); 325 | void add_friends(const std::string& value); 326 | void add_friends(std::string&& value); 327 | void add_friends(const char* value); 328 | void add_friends(const void* value, size_t size); 329 | const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& friends() const; 330 | ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_friends(); 331 | private: 332 | const std::string& _internal_friends(int index) const; 333 | std::string* _internal_add_friends(); 334 | public: 335 | 336 | // .fixbug.ResultCode result = 1; 337 | bool has_result() const; 338 | private: 339 | bool _internal_has_result() const; 340 | public: 341 | void clear_result(); 342 | const ::fixbug::ResultCode& result() const; 343 | ::fixbug::ResultCode* release_result(); 344 | ::fixbug::ResultCode* mutable_result(); 345 | void set_allocated_result(::fixbug::ResultCode* result); 346 | private: 347 | const ::fixbug::ResultCode& _internal_result() const; 348 | ::fixbug::ResultCode* _internal_mutable_result(); 349 | public: 350 | 351 | // @@protoc_insertion_point(class_scope:fixbug.GetFriendsListResponse) 352 | private: 353 | class _Internal; 354 | 355 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 356 | ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField friends_; 357 | ::fixbug::ResultCode* result_; 358 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 359 | friend struct ::TableStruct_friend_2eproto; 360 | }; 361 | // =================================================================== 362 | 363 | class FiendServiceRpc_Stub; 364 | 365 | class FiendServiceRpc : public ::PROTOBUF_NAMESPACE_ID::Service { 366 | protected: 367 | // This class should be treated as an abstract interface. 368 | inline FiendServiceRpc() {}; 369 | public: 370 | virtual ~FiendServiceRpc(); 371 | 372 | typedef FiendServiceRpc_Stub Stub; 373 | 374 | static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* descriptor(); 375 | 376 | virtual void GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 377 | const ::fixbug::GetFriendsListRequest* request, 378 | ::fixbug::GetFriendsListResponse* response, 379 | ::google::protobuf::Closure* done); 380 | 381 | // implements Service ---------------------------------------------- 382 | 383 | const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* GetDescriptor(); 384 | void CallMethod(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method, 385 | ::PROTOBUF_NAMESPACE_ID::RpcController* controller, 386 | const ::PROTOBUF_NAMESPACE_ID::Message* request, 387 | ::PROTOBUF_NAMESPACE_ID::Message* response, 388 | ::google::protobuf::Closure* done); 389 | const ::PROTOBUF_NAMESPACE_ID::Message& GetRequestPrototype( 390 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const; 391 | const ::PROTOBUF_NAMESPACE_ID::Message& GetResponsePrototype( 392 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const; 393 | 394 | private: 395 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FiendServiceRpc); 396 | }; 397 | 398 | class FiendServiceRpc_Stub : public FiendServiceRpc { 399 | public: 400 | FiendServiceRpc_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel); 401 | FiendServiceRpc_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel, 402 | ::PROTOBUF_NAMESPACE_ID::Service::ChannelOwnership ownership); 403 | ~FiendServiceRpc_Stub(); 404 | 405 | inline ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel() { return channel_; } 406 | 407 | // implements FiendServiceRpc ------------------------------------------ 408 | 409 | void GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 410 | const ::fixbug::GetFriendsListRequest* request, 411 | ::fixbug::GetFriendsListResponse* response, 412 | ::google::protobuf::Closure* done); 413 | private: 414 | ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel_; 415 | bool owns_channel_; 416 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FiendServiceRpc_Stub); 417 | }; 418 | 419 | 420 | // =================================================================== 421 | 422 | 423 | // =================================================================== 424 | 425 | #ifdef __GNUC__ 426 | #pragma GCC diagnostic push 427 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" 428 | #endif // __GNUC__ 429 | // GetFriendsListRequest 430 | 431 | // uint32 userid = 1; 432 | inline void GetFriendsListRequest::clear_userid() { 433 | userid_ = 0u; 434 | } 435 | inline ::PROTOBUF_NAMESPACE_ID::uint32 GetFriendsListRequest::_internal_userid() const { 436 | return userid_; 437 | } 438 | inline ::PROTOBUF_NAMESPACE_ID::uint32 GetFriendsListRequest::userid() const { 439 | // @@protoc_insertion_point(field_get:fixbug.GetFriendsListRequest.userid) 440 | return _internal_userid(); 441 | } 442 | inline void GetFriendsListRequest::_internal_set_userid(::PROTOBUF_NAMESPACE_ID::uint32 value) { 443 | 444 | userid_ = value; 445 | } 446 | inline void GetFriendsListRequest::set_userid(::PROTOBUF_NAMESPACE_ID::uint32 value) { 447 | _internal_set_userid(value); 448 | // @@protoc_insertion_point(field_set:fixbug.GetFriendsListRequest.userid) 449 | } 450 | 451 | // ------------------------------------------------------------------- 452 | 453 | // GetFriendsListResponse 454 | 455 | // .fixbug.ResultCode result = 1; 456 | inline bool GetFriendsListResponse::_internal_has_result() const { 457 | return this != internal_default_instance() && result_ != nullptr; 458 | } 459 | inline bool GetFriendsListResponse::has_result() const { 460 | return _internal_has_result(); 461 | } 462 | inline const ::fixbug::ResultCode& GetFriendsListResponse::_internal_result() const { 463 | const ::fixbug::ResultCode* p = result_; 464 | return p != nullptr ? *p : *reinterpret_cast( 465 | &::fixbug::_ResultCode_default_instance_); 466 | } 467 | inline const ::fixbug::ResultCode& GetFriendsListResponse::result() const { 468 | // @@protoc_insertion_point(field_get:fixbug.GetFriendsListResponse.result) 469 | return _internal_result(); 470 | } 471 | inline ::fixbug::ResultCode* GetFriendsListResponse::release_result() { 472 | // @@protoc_insertion_point(field_release:fixbug.GetFriendsListResponse.result) 473 | 474 | ::fixbug::ResultCode* temp = result_; 475 | result_ = nullptr; 476 | return temp; 477 | } 478 | inline ::fixbug::ResultCode* GetFriendsListResponse::_internal_mutable_result() { 479 | 480 | if (result_ == nullptr) { 481 | auto* p = CreateMaybeMessage<::fixbug::ResultCode>(GetArenaNoVirtual()); 482 | result_ = p; 483 | } 484 | return result_; 485 | } 486 | inline ::fixbug::ResultCode* GetFriendsListResponse::mutable_result() { 487 | // @@protoc_insertion_point(field_mutable:fixbug.GetFriendsListResponse.result) 488 | return _internal_mutable_result(); 489 | } 490 | inline void GetFriendsListResponse::set_allocated_result(::fixbug::ResultCode* result) { 491 | ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); 492 | if (message_arena == nullptr) { 493 | delete reinterpret_cast< ::PROTOBUF_NAMESPACE_ID::MessageLite*>(result_); 494 | } 495 | if (result) { 496 | ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = nullptr; 497 | if (message_arena != submessage_arena) { 498 | result = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( 499 | message_arena, result, submessage_arena); 500 | } 501 | 502 | } else { 503 | 504 | } 505 | result_ = result; 506 | // @@protoc_insertion_point(field_set_allocated:fixbug.GetFriendsListResponse.result) 507 | } 508 | 509 | // repeated bytes friends = 2; 510 | inline int GetFriendsListResponse::_internal_friends_size() const { 511 | return friends_.size(); 512 | } 513 | inline int GetFriendsListResponse::friends_size() const { 514 | return _internal_friends_size(); 515 | } 516 | inline void GetFriendsListResponse::clear_friends() { 517 | friends_.Clear(); 518 | } 519 | inline std::string* GetFriendsListResponse::add_friends() { 520 | // @@protoc_insertion_point(field_add_mutable:fixbug.GetFriendsListResponse.friends) 521 | return _internal_add_friends(); 522 | } 523 | inline const std::string& GetFriendsListResponse::_internal_friends(int index) const { 524 | return friends_.Get(index); 525 | } 526 | inline const std::string& GetFriendsListResponse::friends(int index) const { 527 | // @@protoc_insertion_point(field_get:fixbug.GetFriendsListResponse.friends) 528 | return _internal_friends(index); 529 | } 530 | inline std::string* GetFriendsListResponse::mutable_friends(int index) { 531 | // @@protoc_insertion_point(field_mutable:fixbug.GetFriendsListResponse.friends) 532 | return friends_.Mutable(index); 533 | } 534 | inline void GetFriendsListResponse::set_friends(int index, const std::string& value) { 535 | // @@protoc_insertion_point(field_set:fixbug.GetFriendsListResponse.friends) 536 | friends_.Mutable(index)->assign(value); 537 | } 538 | inline void GetFriendsListResponse::set_friends(int index, std::string&& value) { 539 | // @@protoc_insertion_point(field_set:fixbug.GetFriendsListResponse.friends) 540 | friends_.Mutable(index)->assign(std::move(value)); 541 | } 542 | inline void GetFriendsListResponse::set_friends(int index, const char* value) { 543 | GOOGLE_DCHECK(value != nullptr); 544 | friends_.Mutable(index)->assign(value); 545 | // @@protoc_insertion_point(field_set_char:fixbug.GetFriendsListResponse.friends) 546 | } 547 | inline void GetFriendsListResponse::set_friends(int index, const void* value, size_t size) { 548 | friends_.Mutable(index)->assign( 549 | reinterpret_cast(value), size); 550 | // @@protoc_insertion_point(field_set_pointer:fixbug.GetFriendsListResponse.friends) 551 | } 552 | inline std::string* GetFriendsListResponse::_internal_add_friends() { 553 | return friends_.Add(); 554 | } 555 | inline void GetFriendsListResponse::add_friends(const std::string& value) { 556 | friends_.Add()->assign(value); 557 | // @@protoc_insertion_point(field_add:fixbug.GetFriendsListResponse.friends) 558 | } 559 | inline void GetFriendsListResponse::add_friends(std::string&& value) { 560 | friends_.Add(std::move(value)); 561 | // @@protoc_insertion_point(field_add:fixbug.GetFriendsListResponse.friends) 562 | } 563 | inline void GetFriendsListResponse::add_friends(const char* value) { 564 | GOOGLE_DCHECK(value != nullptr); 565 | friends_.Add()->assign(value); 566 | // @@protoc_insertion_point(field_add_char:fixbug.GetFriendsListResponse.friends) 567 | } 568 | inline void GetFriendsListResponse::add_friends(const void* value, size_t size) { 569 | friends_.Add()->assign(reinterpret_cast(value), size); 570 | // @@protoc_insertion_point(field_add_pointer:fixbug.GetFriendsListResponse.friends) 571 | } 572 | inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& 573 | GetFriendsListResponse::friends() const { 574 | // @@protoc_insertion_point(field_list:fixbug.GetFriendsListResponse.friends) 575 | return friends_; 576 | } 577 | inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* 578 | GetFriendsListResponse::mutable_friends() { 579 | // @@protoc_insertion_point(field_mutable_list:fixbug.GetFriendsListResponse.friends) 580 | return &friends_; 581 | } 582 | 583 | #ifdef __GNUC__ 584 | #pragma GCC diagnostic pop 585 | #endif // __GNUC__ 586 | // ------------------------------------------------------------------- 587 | 588 | 589 | // @@protoc_insertion_point(namespace_scope) 590 | 591 | } // namespace fixbug 592 | 593 | // @@protoc_insertion_point(global_scope) 594 | 595 | #include 596 | #endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_friend_2eproto 597 | -------------------------------------------------------------------------------- /example/friend.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package fixbug; 4 | 5 | option cc_generic_services = true; 6 | 7 | import "publictype.proto"; 8 | 9 | message GetFriendsListRequest 10 | { 11 | uint32 userid = 1; 12 | } 13 | 14 | message GetFriendsListResponse 15 | { 16 | ResultCode result = 1; 17 | repeated bytes friends = 2; 18 | } 19 | 20 | // 好友模块 21 | service FiendServiceRpc 22 | { 23 | rpc GetFriendsList(GetFriendsListRequest) returns(GetFriendsListResponse); 24 | } 25 | -------------------------------------------------------------------------------- /example/publictype.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: publictype.proto 3 | 4 | #include "publictype.pb.h" 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | // @@protoc_insertion_point(includes) 16 | #include 17 | namespace fixbug { 18 | class ResultCodeDefaultTypeInternal { 19 | public: 20 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 21 | } _ResultCode_default_instance_; 22 | } // namespace fixbug 23 | static void InitDefaultsscc_info_ResultCode_publictype_2eproto() { 24 | GOOGLE_PROTOBUF_VERIFY_VERSION; 25 | 26 | { 27 | void* ptr = &::fixbug::_ResultCode_default_instance_; 28 | new (ptr) ::fixbug::ResultCode(); 29 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 30 | } 31 | ::fixbug::ResultCode::InitAsDefaultInstance(); 32 | } 33 | 34 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ResultCode_publictype_2eproto = 35 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_ResultCode_publictype_2eproto}, {}}; 36 | 37 | static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_publictype_2eproto[1]; 38 | static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_publictype_2eproto = nullptr; 39 | static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_publictype_2eproto = nullptr; 40 | 41 | const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_publictype_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 42 | ~0u, // no _has_bits_ 43 | PROTOBUF_FIELD_OFFSET(::fixbug::ResultCode, _internal_metadata_), 44 | ~0u, // no _extensions_ 45 | ~0u, // no _oneof_case_ 46 | ~0u, // no _weak_field_map_ 47 | PROTOBUF_FIELD_OFFSET(::fixbug::ResultCode, errcode_), 48 | PROTOBUF_FIELD_OFFSET(::fixbug::ResultCode, errmsg_), 49 | }; 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 51 | { 0, -1, sizeof(::fixbug::ResultCode)}, 52 | }; 53 | 54 | static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { 55 | reinterpret_cast(&::fixbug::_ResultCode_default_instance_), 56 | }; 57 | 58 | const char descriptor_table_protodef_publictype_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = 59 | "\n\020publictype.proto\022\006fixbug\"-\n\nResultCode" 60 | "\022\017\n\007errcode\030\001 \001(\005\022\016\n\006errmsg\030\002 \001(\014b\006proto" 61 | "3" 62 | ; 63 | static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_publictype_2eproto_deps[1] = { 64 | }; 65 | static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_publictype_2eproto_sccs[1] = { 66 | &scc_info_ResultCode_publictype_2eproto.base, 67 | }; 68 | static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_publictype_2eproto_once; 69 | static bool descriptor_table_publictype_2eproto_initialized = false; 70 | const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_publictype_2eproto = { 71 | &descriptor_table_publictype_2eproto_initialized, descriptor_table_protodef_publictype_2eproto, "publictype.proto", 81, 72 | &descriptor_table_publictype_2eproto_once, descriptor_table_publictype_2eproto_sccs, descriptor_table_publictype_2eproto_deps, 1, 0, 73 | schemas, file_default_instances, TableStruct_publictype_2eproto::offsets, 74 | file_level_metadata_publictype_2eproto, 1, file_level_enum_descriptors_publictype_2eproto, file_level_service_descriptors_publictype_2eproto, 75 | }; 76 | 77 | // Force running AddDescriptors() at dynamic initialization time. 78 | static bool dynamic_init_dummy_publictype_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_publictype_2eproto), true); 79 | namespace fixbug { 80 | 81 | // =================================================================== 82 | 83 | void ResultCode::InitAsDefaultInstance() { 84 | } 85 | class ResultCode::_Internal { 86 | public: 87 | }; 88 | 89 | ResultCode::ResultCode() 90 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 91 | SharedCtor(); 92 | // @@protoc_insertion_point(constructor:fixbug.ResultCode) 93 | } 94 | ResultCode::ResultCode(const ResultCode& from) 95 | : ::PROTOBUF_NAMESPACE_ID::Message(), 96 | _internal_metadata_(nullptr) { 97 | _internal_metadata_.MergeFrom(from._internal_metadata_); 98 | errmsg_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 99 | if (!from._internal_errmsg().empty()) { 100 | errmsg_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.errmsg_); 101 | } 102 | errcode_ = from.errcode_; 103 | // @@protoc_insertion_point(copy_constructor:fixbug.ResultCode) 104 | } 105 | 106 | void ResultCode::SharedCtor() { 107 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ResultCode_publictype_2eproto.base); 108 | errmsg_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 109 | errcode_ = 0; 110 | } 111 | 112 | ResultCode::~ResultCode() { 113 | // @@protoc_insertion_point(destructor:fixbug.ResultCode) 114 | SharedDtor(); 115 | } 116 | 117 | void ResultCode::SharedDtor() { 118 | errmsg_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 119 | } 120 | 121 | void ResultCode::SetCachedSize(int size) const { 122 | _cached_size_.Set(size); 123 | } 124 | const ResultCode& ResultCode::default_instance() { 125 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ResultCode_publictype_2eproto.base); 126 | return *internal_default_instance(); 127 | } 128 | 129 | 130 | void ResultCode::Clear() { 131 | // @@protoc_insertion_point(message_clear_start:fixbug.ResultCode) 132 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 133 | // Prevent compiler warnings about cached_has_bits being unused 134 | (void) cached_has_bits; 135 | 136 | errmsg_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 137 | errcode_ = 0; 138 | _internal_metadata_.Clear(); 139 | } 140 | 141 | const char* ResultCode::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 142 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 143 | while (!ctx->Done(&ptr)) { 144 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 145 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 146 | CHK_(ptr); 147 | switch (tag >> 3) { 148 | // int32 errcode = 1; 149 | case 1: 150 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { 151 | errcode_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); 152 | CHK_(ptr); 153 | } else goto handle_unusual; 154 | continue; 155 | // bytes errmsg = 2; 156 | case 2: 157 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { 158 | auto str = _internal_mutable_errmsg(); 159 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 160 | CHK_(ptr); 161 | } else goto handle_unusual; 162 | continue; 163 | default: { 164 | handle_unusual: 165 | if ((tag & 7) == 4 || tag == 0) { 166 | ctx->SetLastTag(tag); 167 | goto success; 168 | } 169 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 170 | CHK_(ptr != nullptr); 171 | continue; 172 | } 173 | } // switch 174 | } // while 175 | success: 176 | return ptr; 177 | failure: 178 | ptr = nullptr; 179 | goto success; 180 | #undef CHK_ 181 | } 182 | 183 | ::PROTOBUF_NAMESPACE_ID::uint8* ResultCode::_InternalSerialize( 184 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 185 | // @@protoc_insertion_point(serialize_to_array_start:fixbug.ResultCode) 186 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 187 | (void) cached_has_bits; 188 | 189 | // int32 errcode = 1; 190 | if (this->errcode() != 0) { 191 | target = stream->EnsureSpace(target); 192 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(1, this->_internal_errcode(), target); 193 | } 194 | 195 | // bytes errmsg = 2; 196 | if (this->errmsg().size() > 0) { 197 | target = stream->WriteBytesMaybeAliased( 198 | 2, this->_internal_errmsg(), target); 199 | } 200 | 201 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 202 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 203 | _internal_metadata_.unknown_fields(), target, stream); 204 | } 205 | // @@protoc_insertion_point(serialize_to_array_end:fixbug.ResultCode) 206 | return target; 207 | } 208 | 209 | size_t ResultCode::ByteSizeLong() const { 210 | // @@protoc_insertion_point(message_byte_size_start:fixbug.ResultCode) 211 | size_t total_size = 0; 212 | 213 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 214 | // Prevent compiler warnings about cached_has_bits being unused 215 | (void) cached_has_bits; 216 | 217 | // bytes errmsg = 2; 218 | if (this->errmsg().size() > 0) { 219 | total_size += 1 + 220 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( 221 | this->_internal_errmsg()); 222 | } 223 | 224 | // int32 errcode = 1; 225 | if (this->errcode() != 0) { 226 | total_size += 1 + 227 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( 228 | this->_internal_errcode()); 229 | } 230 | 231 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 232 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 233 | _internal_metadata_, total_size, &_cached_size_); 234 | } 235 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 236 | SetCachedSize(cached_size); 237 | return total_size; 238 | } 239 | 240 | void ResultCode::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 241 | // @@protoc_insertion_point(generalized_merge_from_start:fixbug.ResultCode) 242 | GOOGLE_DCHECK_NE(&from, this); 243 | const ResultCode* source = 244 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 245 | &from); 246 | if (source == nullptr) { 247 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:fixbug.ResultCode) 248 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 249 | } else { 250 | // @@protoc_insertion_point(generalized_merge_from_cast_success:fixbug.ResultCode) 251 | MergeFrom(*source); 252 | } 253 | } 254 | 255 | void ResultCode::MergeFrom(const ResultCode& from) { 256 | // @@protoc_insertion_point(class_specific_merge_from_start:fixbug.ResultCode) 257 | GOOGLE_DCHECK_NE(&from, this); 258 | _internal_metadata_.MergeFrom(from._internal_metadata_); 259 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 260 | (void) cached_has_bits; 261 | 262 | if (from.errmsg().size() > 0) { 263 | 264 | errmsg_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.errmsg_); 265 | } 266 | if (from.errcode() != 0) { 267 | _internal_set_errcode(from._internal_errcode()); 268 | } 269 | } 270 | 271 | void ResultCode::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 272 | // @@protoc_insertion_point(generalized_copy_from_start:fixbug.ResultCode) 273 | if (&from == this) return; 274 | Clear(); 275 | MergeFrom(from); 276 | } 277 | 278 | void ResultCode::CopyFrom(const ResultCode& from) { 279 | // @@protoc_insertion_point(class_specific_copy_from_start:fixbug.ResultCode) 280 | if (&from == this) return; 281 | Clear(); 282 | MergeFrom(from); 283 | } 284 | 285 | bool ResultCode::IsInitialized() const { 286 | return true; 287 | } 288 | 289 | void ResultCode::InternalSwap(ResultCode* other) { 290 | using std::swap; 291 | _internal_metadata_.Swap(&other->_internal_metadata_); 292 | errmsg_.Swap(&other->errmsg_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 293 | GetArenaNoVirtual()); 294 | swap(errcode_, other->errcode_); 295 | } 296 | 297 | ::PROTOBUF_NAMESPACE_ID::Metadata ResultCode::GetMetadata() const { 298 | return GetMetadataStatic(); 299 | } 300 | 301 | 302 | // @@protoc_insertion_point(namespace_scope) 303 | } // namespace fixbug 304 | PROTOBUF_NAMESPACE_OPEN 305 | template<> PROTOBUF_NOINLINE ::fixbug::ResultCode* Arena::CreateMaybeMessage< ::fixbug::ResultCode >(Arena* arena) { 306 | return Arena::CreateInternal< ::fixbug::ResultCode >(arena); 307 | } 308 | PROTOBUF_NAMESPACE_CLOSE 309 | 310 | // @@protoc_insertion_point(global_scope) 311 | #include 312 | -------------------------------------------------------------------------------- /example/publictype.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: publictype.proto 3 | 4 | #ifndef GOOGLE_PROTOBUF_INCLUDED_publictype_2eproto 5 | #define GOOGLE_PROTOBUF_INCLUDED_publictype_2eproto 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #if PROTOBUF_VERSION < 3011000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 3011000 < PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include // IWYU pragma: export 33 | #include // IWYU pragma: export 34 | #include 35 | // @@protoc_insertion_point(includes) 36 | #include 37 | #define PROTOBUF_INTERNAL_EXPORT_publictype_2eproto 38 | PROTOBUF_NAMESPACE_OPEN 39 | namespace internal { 40 | class AnyMetadata; 41 | } // namespace internal 42 | PROTOBUF_NAMESPACE_CLOSE 43 | 44 | // Internal implementation detail -- do not use these members. 45 | struct TableStruct_publictype_2eproto { 46 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] 47 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 48 | static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] 49 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] 51 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 52 | static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; 53 | static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; 54 | static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; 55 | }; 56 | extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_publictype_2eproto; 57 | namespace fixbug { 58 | class ResultCode; 59 | class ResultCodeDefaultTypeInternal; 60 | extern ResultCodeDefaultTypeInternal _ResultCode_default_instance_; 61 | } // namespace fixbug 62 | PROTOBUF_NAMESPACE_OPEN 63 | template<> ::fixbug::ResultCode* Arena::CreateMaybeMessage<::fixbug::ResultCode>(Arena*); 64 | PROTOBUF_NAMESPACE_CLOSE 65 | namespace fixbug { 66 | 67 | // =================================================================== 68 | 69 | class ResultCode : 70 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.ResultCode) */ { 71 | public: 72 | ResultCode(); 73 | virtual ~ResultCode(); 74 | 75 | ResultCode(const ResultCode& from); 76 | ResultCode(ResultCode&& from) noexcept 77 | : ResultCode() { 78 | *this = ::std::move(from); 79 | } 80 | 81 | inline ResultCode& operator=(const ResultCode& from) { 82 | CopyFrom(from); 83 | return *this; 84 | } 85 | inline ResultCode& operator=(ResultCode&& from) noexcept { 86 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 87 | if (this != &from) InternalSwap(&from); 88 | } else { 89 | CopyFrom(from); 90 | } 91 | return *this; 92 | } 93 | 94 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 95 | return GetDescriptor(); 96 | } 97 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 98 | return GetMetadataStatic().descriptor; 99 | } 100 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 101 | return GetMetadataStatic().reflection; 102 | } 103 | static const ResultCode& default_instance(); 104 | 105 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 106 | static inline const ResultCode* internal_default_instance() { 107 | return reinterpret_cast( 108 | &_ResultCode_default_instance_); 109 | } 110 | static constexpr int kIndexInFileMessages = 111 | 0; 112 | 113 | friend void swap(ResultCode& a, ResultCode& b) { 114 | a.Swap(&b); 115 | } 116 | inline void Swap(ResultCode* other) { 117 | if (other == this) return; 118 | InternalSwap(other); 119 | } 120 | 121 | // implements Message ---------------------------------------------- 122 | 123 | inline ResultCode* New() const final { 124 | return CreateMaybeMessage(nullptr); 125 | } 126 | 127 | ResultCode* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 128 | return CreateMaybeMessage(arena); 129 | } 130 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 131 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 132 | void CopyFrom(const ResultCode& from); 133 | void MergeFrom(const ResultCode& from); 134 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 135 | bool IsInitialized() const final; 136 | 137 | size_t ByteSizeLong() const final; 138 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 139 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 140 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 141 | int GetCachedSize() const final { return _cached_size_.Get(); } 142 | 143 | private: 144 | inline void SharedCtor(); 145 | inline void SharedDtor(); 146 | void SetCachedSize(int size) const final; 147 | void InternalSwap(ResultCode* other); 148 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 149 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 150 | return "fixbug.ResultCode"; 151 | } 152 | private: 153 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 154 | return nullptr; 155 | } 156 | inline void* MaybeArenaPtr() const { 157 | return nullptr; 158 | } 159 | public: 160 | 161 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 162 | private: 163 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 164 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_publictype_2eproto); 165 | return ::descriptor_table_publictype_2eproto.file_level_metadata[kIndexInFileMessages]; 166 | } 167 | 168 | public: 169 | 170 | // nested types ---------------------------------------------------- 171 | 172 | // accessors ------------------------------------------------------- 173 | 174 | enum : int { 175 | kErrmsgFieldNumber = 2, 176 | kErrcodeFieldNumber = 1, 177 | }; 178 | // bytes errmsg = 2; 179 | void clear_errmsg(); 180 | const std::string& errmsg() const; 181 | void set_errmsg(const std::string& value); 182 | void set_errmsg(std::string&& value); 183 | void set_errmsg(const char* value); 184 | void set_errmsg(const void* value, size_t size); 185 | std::string* mutable_errmsg(); 186 | std::string* release_errmsg(); 187 | void set_allocated_errmsg(std::string* errmsg); 188 | private: 189 | const std::string& _internal_errmsg() const; 190 | void _internal_set_errmsg(const std::string& value); 191 | std::string* _internal_mutable_errmsg(); 192 | public: 193 | 194 | // int32 errcode = 1; 195 | void clear_errcode(); 196 | ::PROTOBUF_NAMESPACE_ID::int32 errcode() const; 197 | void set_errcode(::PROTOBUF_NAMESPACE_ID::int32 value); 198 | private: 199 | ::PROTOBUF_NAMESPACE_ID::int32 _internal_errcode() const; 200 | void _internal_set_errcode(::PROTOBUF_NAMESPACE_ID::int32 value); 201 | public: 202 | 203 | // @@protoc_insertion_point(class_scope:fixbug.ResultCode) 204 | private: 205 | class _Internal; 206 | 207 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 208 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr errmsg_; 209 | ::PROTOBUF_NAMESPACE_ID::int32 errcode_; 210 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 211 | friend struct ::TableStruct_publictype_2eproto; 212 | }; 213 | // =================================================================== 214 | 215 | 216 | // =================================================================== 217 | 218 | #ifdef __GNUC__ 219 | #pragma GCC diagnostic push 220 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" 221 | #endif // __GNUC__ 222 | // ResultCode 223 | 224 | // int32 errcode = 1; 225 | inline void ResultCode::clear_errcode() { 226 | errcode_ = 0; 227 | } 228 | inline ::PROTOBUF_NAMESPACE_ID::int32 ResultCode::_internal_errcode() const { 229 | return errcode_; 230 | } 231 | inline ::PROTOBUF_NAMESPACE_ID::int32 ResultCode::errcode() const { 232 | // @@protoc_insertion_point(field_get:fixbug.ResultCode.errcode) 233 | return _internal_errcode(); 234 | } 235 | inline void ResultCode::_internal_set_errcode(::PROTOBUF_NAMESPACE_ID::int32 value) { 236 | 237 | errcode_ = value; 238 | } 239 | inline void ResultCode::set_errcode(::PROTOBUF_NAMESPACE_ID::int32 value) { 240 | _internal_set_errcode(value); 241 | // @@protoc_insertion_point(field_set:fixbug.ResultCode.errcode) 242 | } 243 | 244 | // bytes errmsg = 2; 245 | inline void ResultCode::clear_errmsg() { 246 | errmsg_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 247 | } 248 | inline const std::string& ResultCode::errmsg() const { 249 | // @@protoc_insertion_point(field_get:fixbug.ResultCode.errmsg) 250 | return _internal_errmsg(); 251 | } 252 | inline void ResultCode::set_errmsg(const std::string& value) { 253 | _internal_set_errmsg(value); 254 | // @@protoc_insertion_point(field_set:fixbug.ResultCode.errmsg) 255 | } 256 | inline std::string* ResultCode::mutable_errmsg() { 257 | // @@protoc_insertion_point(field_mutable:fixbug.ResultCode.errmsg) 258 | return _internal_mutable_errmsg(); 259 | } 260 | inline const std::string& ResultCode::_internal_errmsg() const { 261 | return errmsg_.GetNoArena(); 262 | } 263 | inline void ResultCode::_internal_set_errmsg(const std::string& value) { 264 | 265 | errmsg_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 266 | } 267 | inline void ResultCode::set_errmsg(std::string&& value) { 268 | 269 | errmsg_.SetNoArena( 270 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 271 | // @@protoc_insertion_point(field_set_rvalue:fixbug.ResultCode.errmsg) 272 | } 273 | inline void ResultCode::set_errmsg(const char* value) { 274 | GOOGLE_DCHECK(value != nullptr); 275 | 276 | errmsg_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 277 | // @@protoc_insertion_point(field_set_char:fixbug.ResultCode.errmsg) 278 | } 279 | inline void ResultCode::set_errmsg(const void* value, size_t size) { 280 | 281 | errmsg_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 282 | ::std::string(reinterpret_cast(value), size)); 283 | // @@protoc_insertion_point(field_set_pointer:fixbug.ResultCode.errmsg) 284 | } 285 | inline std::string* ResultCode::_internal_mutable_errmsg() { 286 | 287 | return errmsg_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 288 | } 289 | inline std::string* ResultCode::release_errmsg() { 290 | // @@protoc_insertion_point(field_release:fixbug.ResultCode.errmsg) 291 | 292 | return errmsg_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 293 | } 294 | inline void ResultCode::set_allocated_errmsg(std::string* errmsg) { 295 | if (errmsg != nullptr) { 296 | 297 | } else { 298 | 299 | } 300 | errmsg_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), errmsg); 301 | // @@protoc_insertion_point(field_set_allocated:fixbug.ResultCode.errmsg) 302 | } 303 | 304 | #ifdef __GNUC__ 305 | #pragma GCC diagnostic pop 306 | #endif // __GNUC__ 307 | 308 | // @@protoc_insertion_point(namespace_scope) 309 | 310 | } // namespace fixbug 311 | 312 | // @@protoc_insertion_point(global_scope) 313 | 314 | #include 315 | #endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_publictype_2eproto 316 | -------------------------------------------------------------------------------- /example/publictype.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package fixbug; 4 | 5 | message ResultCode 6 | { 7 | int32 errcode = 1; 8 | bytes errmsg = 2; 9 | } 10 | -------------------------------------------------------------------------------- /example/user.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: user.proto 3 | 4 | #ifndef GOOGLE_PROTOBUF_INCLUDED_user_2eproto 5 | #define GOOGLE_PROTOBUF_INCLUDED_user_2eproto 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #if PROTOBUF_VERSION < 3011000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 3011000 < PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include // IWYU pragma: export 33 | #include // IWYU pragma: export 34 | #include 35 | #include 36 | #include "publictype.pb.h" 37 | // @@protoc_insertion_point(includes) 38 | #include 39 | #define PROTOBUF_INTERNAL_EXPORT_user_2eproto 40 | PROTOBUF_NAMESPACE_OPEN 41 | namespace internal { 42 | class AnyMetadata; 43 | } // namespace internal 44 | PROTOBUF_NAMESPACE_CLOSE 45 | 46 | // Internal implementation detail -- do not use these members. 47 | struct TableStruct_user_2eproto { 48 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] 49 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] 51 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 52 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[4] 53 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 54 | static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; 55 | static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; 56 | static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; 57 | }; 58 | extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_user_2eproto; 59 | namespace fixbug { 60 | class LoginRequest; 61 | class LoginRequestDefaultTypeInternal; 62 | extern LoginRequestDefaultTypeInternal _LoginRequest_default_instance_; 63 | class LoginResponse; 64 | class LoginResponseDefaultTypeInternal; 65 | extern LoginResponseDefaultTypeInternal _LoginResponse_default_instance_; 66 | class RegisterRequest; 67 | class RegisterRequestDefaultTypeInternal; 68 | extern RegisterRequestDefaultTypeInternal _RegisterRequest_default_instance_; 69 | class RegisterResponse; 70 | class RegisterResponseDefaultTypeInternal; 71 | extern RegisterResponseDefaultTypeInternal _RegisterResponse_default_instance_; 72 | } // namespace fixbug 73 | PROTOBUF_NAMESPACE_OPEN 74 | template<> ::fixbug::LoginRequest* Arena::CreateMaybeMessage<::fixbug::LoginRequest>(Arena*); 75 | template<> ::fixbug::LoginResponse* Arena::CreateMaybeMessage<::fixbug::LoginResponse>(Arena*); 76 | template<> ::fixbug::RegisterRequest* Arena::CreateMaybeMessage<::fixbug::RegisterRequest>(Arena*); 77 | template<> ::fixbug::RegisterResponse* Arena::CreateMaybeMessage<::fixbug::RegisterResponse>(Arena*); 78 | PROTOBUF_NAMESPACE_CLOSE 79 | namespace fixbug { 80 | 81 | // =================================================================== 82 | 83 | class LoginRequest : 84 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.LoginRequest) */ { 85 | public: 86 | LoginRequest(); 87 | virtual ~LoginRequest(); 88 | 89 | LoginRequest(const LoginRequest& from); 90 | LoginRequest(LoginRequest&& from) noexcept 91 | : LoginRequest() { 92 | *this = ::std::move(from); 93 | } 94 | 95 | inline LoginRequest& operator=(const LoginRequest& from) { 96 | CopyFrom(from); 97 | return *this; 98 | } 99 | inline LoginRequest& operator=(LoginRequest&& from) noexcept { 100 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 101 | if (this != &from) InternalSwap(&from); 102 | } else { 103 | CopyFrom(from); 104 | } 105 | return *this; 106 | } 107 | 108 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 109 | return GetDescriptor(); 110 | } 111 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 112 | return GetMetadataStatic().descriptor; 113 | } 114 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 115 | return GetMetadataStatic().reflection; 116 | } 117 | static const LoginRequest& default_instance(); 118 | 119 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 120 | static inline const LoginRequest* internal_default_instance() { 121 | return reinterpret_cast( 122 | &_LoginRequest_default_instance_); 123 | } 124 | static constexpr int kIndexInFileMessages = 125 | 0; 126 | 127 | friend void swap(LoginRequest& a, LoginRequest& b) { 128 | a.Swap(&b); 129 | } 130 | inline void Swap(LoginRequest* other) { 131 | if (other == this) return; 132 | InternalSwap(other); 133 | } 134 | 135 | // implements Message ---------------------------------------------- 136 | 137 | inline LoginRequest* New() const final { 138 | return CreateMaybeMessage(nullptr); 139 | } 140 | 141 | LoginRequest* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 142 | return CreateMaybeMessage(arena); 143 | } 144 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 145 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 146 | void CopyFrom(const LoginRequest& from); 147 | void MergeFrom(const LoginRequest& from); 148 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 149 | bool IsInitialized() const final; 150 | 151 | size_t ByteSizeLong() const final; 152 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 153 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 154 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 155 | int GetCachedSize() const final { return _cached_size_.Get(); } 156 | 157 | private: 158 | inline void SharedCtor(); 159 | inline void SharedDtor(); 160 | void SetCachedSize(int size) const final; 161 | void InternalSwap(LoginRequest* other); 162 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 163 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 164 | return "fixbug.LoginRequest"; 165 | } 166 | private: 167 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 168 | return nullptr; 169 | } 170 | inline void* MaybeArenaPtr() const { 171 | return nullptr; 172 | } 173 | public: 174 | 175 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 176 | private: 177 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 178 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_user_2eproto); 179 | return ::descriptor_table_user_2eproto.file_level_metadata[kIndexInFileMessages]; 180 | } 181 | 182 | public: 183 | 184 | // nested types ---------------------------------------------------- 185 | 186 | // accessors ------------------------------------------------------- 187 | 188 | enum : int { 189 | kNameFieldNumber = 1, 190 | kPwdFieldNumber = 2, 191 | }; 192 | // bytes name = 1; 193 | void clear_name(); 194 | const std::string& name() const; 195 | void set_name(const std::string& value); 196 | void set_name(std::string&& value); 197 | void set_name(const char* value); 198 | void set_name(const void* value, size_t size); 199 | std::string* mutable_name(); 200 | std::string* release_name(); 201 | void set_allocated_name(std::string* name); 202 | private: 203 | const std::string& _internal_name() const; 204 | void _internal_set_name(const std::string& value); 205 | std::string* _internal_mutable_name(); 206 | public: 207 | 208 | // bytes pwd = 2; 209 | void clear_pwd(); 210 | const std::string& pwd() const; 211 | void set_pwd(const std::string& value); 212 | void set_pwd(std::string&& value); 213 | void set_pwd(const char* value); 214 | void set_pwd(const void* value, size_t size); 215 | std::string* mutable_pwd(); 216 | std::string* release_pwd(); 217 | void set_allocated_pwd(std::string* pwd); 218 | private: 219 | const std::string& _internal_pwd() const; 220 | void _internal_set_pwd(const std::string& value); 221 | std::string* _internal_mutable_pwd(); 222 | public: 223 | 224 | // @@protoc_insertion_point(class_scope:fixbug.LoginRequest) 225 | private: 226 | class _Internal; 227 | 228 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 229 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; 230 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr pwd_; 231 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 232 | friend struct ::TableStruct_user_2eproto; 233 | }; 234 | // ------------------------------------------------------------------- 235 | 236 | class LoginResponse : 237 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.LoginResponse) */ { 238 | public: 239 | LoginResponse(); 240 | virtual ~LoginResponse(); 241 | 242 | LoginResponse(const LoginResponse& from); 243 | LoginResponse(LoginResponse&& from) noexcept 244 | : LoginResponse() { 245 | *this = ::std::move(from); 246 | } 247 | 248 | inline LoginResponse& operator=(const LoginResponse& from) { 249 | CopyFrom(from); 250 | return *this; 251 | } 252 | inline LoginResponse& operator=(LoginResponse&& from) noexcept { 253 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 254 | if (this != &from) InternalSwap(&from); 255 | } else { 256 | CopyFrom(from); 257 | } 258 | return *this; 259 | } 260 | 261 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 262 | return GetDescriptor(); 263 | } 264 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 265 | return GetMetadataStatic().descriptor; 266 | } 267 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 268 | return GetMetadataStatic().reflection; 269 | } 270 | static const LoginResponse& default_instance(); 271 | 272 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 273 | static inline const LoginResponse* internal_default_instance() { 274 | return reinterpret_cast( 275 | &_LoginResponse_default_instance_); 276 | } 277 | static constexpr int kIndexInFileMessages = 278 | 1; 279 | 280 | friend void swap(LoginResponse& a, LoginResponse& b) { 281 | a.Swap(&b); 282 | } 283 | inline void Swap(LoginResponse* other) { 284 | if (other == this) return; 285 | InternalSwap(other); 286 | } 287 | 288 | // implements Message ---------------------------------------------- 289 | 290 | inline LoginResponse* New() const final { 291 | return CreateMaybeMessage(nullptr); 292 | } 293 | 294 | LoginResponse* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 295 | return CreateMaybeMessage(arena); 296 | } 297 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 298 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 299 | void CopyFrom(const LoginResponse& from); 300 | void MergeFrom(const LoginResponse& from); 301 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 302 | bool IsInitialized() const final; 303 | 304 | size_t ByteSizeLong() const final; 305 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 306 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 307 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 308 | int GetCachedSize() const final { return _cached_size_.Get(); } 309 | 310 | private: 311 | inline void SharedCtor(); 312 | inline void SharedDtor(); 313 | void SetCachedSize(int size) const final; 314 | void InternalSwap(LoginResponse* other); 315 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 316 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 317 | return "fixbug.LoginResponse"; 318 | } 319 | private: 320 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 321 | return nullptr; 322 | } 323 | inline void* MaybeArenaPtr() const { 324 | return nullptr; 325 | } 326 | public: 327 | 328 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 329 | private: 330 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 331 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_user_2eproto); 332 | return ::descriptor_table_user_2eproto.file_level_metadata[kIndexInFileMessages]; 333 | } 334 | 335 | public: 336 | 337 | // nested types ---------------------------------------------------- 338 | 339 | // accessors ------------------------------------------------------- 340 | 341 | enum : int { 342 | kResultFieldNumber = 1, 343 | kSuccessFieldNumber = 3, 344 | }; 345 | // .fixbug.ResultCode result = 1; 346 | bool has_result() const; 347 | private: 348 | bool _internal_has_result() const; 349 | public: 350 | void clear_result(); 351 | const ::fixbug::ResultCode& result() const; 352 | ::fixbug::ResultCode* release_result(); 353 | ::fixbug::ResultCode* mutable_result(); 354 | void set_allocated_result(::fixbug::ResultCode* result); 355 | private: 356 | const ::fixbug::ResultCode& _internal_result() const; 357 | ::fixbug::ResultCode* _internal_mutable_result(); 358 | public: 359 | 360 | // bool success = 3; 361 | void clear_success(); 362 | bool success() const; 363 | void set_success(bool value); 364 | private: 365 | bool _internal_success() const; 366 | void _internal_set_success(bool value); 367 | public: 368 | 369 | // @@protoc_insertion_point(class_scope:fixbug.LoginResponse) 370 | private: 371 | class _Internal; 372 | 373 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 374 | ::fixbug::ResultCode* result_; 375 | bool success_; 376 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 377 | friend struct ::TableStruct_user_2eproto; 378 | }; 379 | // ------------------------------------------------------------------- 380 | 381 | class RegisterRequest : 382 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.RegisterRequest) */ { 383 | public: 384 | RegisterRequest(); 385 | virtual ~RegisterRequest(); 386 | 387 | RegisterRequest(const RegisterRequest& from); 388 | RegisterRequest(RegisterRequest&& from) noexcept 389 | : RegisterRequest() { 390 | *this = ::std::move(from); 391 | } 392 | 393 | inline RegisterRequest& operator=(const RegisterRequest& from) { 394 | CopyFrom(from); 395 | return *this; 396 | } 397 | inline RegisterRequest& operator=(RegisterRequest&& from) noexcept { 398 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 399 | if (this != &from) InternalSwap(&from); 400 | } else { 401 | CopyFrom(from); 402 | } 403 | return *this; 404 | } 405 | 406 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 407 | return GetDescriptor(); 408 | } 409 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 410 | return GetMetadataStatic().descriptor; 411 | } 412 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 413 | return GetMetadataStatic().reflection; 414 | } 415 | static const RegisterRequest& default_instance(); 416 | 417 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 418 | static inline const RegisterRequest* internal_default_instance() { 419 | return reinterpret_cast( 420 | &_RegisterRequest_default_instance_); 421 | } 422 | static constexpr int kIndexInFileMessages = 423 | 2; 424 | 425 | friend void swap(RegisterRequest& a, RegisterRequest& b) { 426 | a.Swap(&b); 427 | } 428 | inline void Swap(RegisterRequest* other) { 429 | if (other == this) return; 430 | InternalSwap(other); 431 | } 432 | 433 | // implements Message ---------------------------------------------- 434 | 435 | inline RegisterRequest* New() const final { 436 | return CreateMaybeMessage(nullptr); 437 | } 438 | 439 | RegisterRequest* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 440 | return CreateMaybeMessage(arena); 441 | } 442 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 443 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 444 | void CopyFrom(const RegisterRequest& from); 445 | void MergeFrom(const RegisterRequest& from); 446 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 447 | bool IsInitialized() const final; 448 | 449 | size_t ByteSizeLong() const final; 450 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 451 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 452 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 453 | int GetCachedSize() const final { return _cached_size_.Get(); } 454 | 455 | private: 456 | inline void SharedCtor(); 457 | inline void SharedDtor(); 458 | void SetCachedSize(int size) const final; 459 | void InternalSwap(RegisterRequest* other); 460 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 461 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 462 | return "fixbug.RegisterRequest"; 463 | } 464 | private: 465 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 466 | return nullptr; 467 | } 468 | inline void* MaybeArenaPtr() const { 469 | return nullptr; 470 | } 471 | public: 472 | 473 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 474 | private: 475 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 476 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_user_2eproto); 477 | return ::descriptor_table_user_2eproto.file_level_metadata[kIndexInFileMessages]; 478 | } 479 | 480 | public: 481 | 482 | // nested types ---------------------------------------------------- 483 | 484 | // accessors ------------------------------------------------------- 485 | 486 | enum : int { 487 | kNameFieldNumber = 2, 488 | kPwdFieldNumber = 3, 489 | kIdFieldNumber = 1, 490 | }; 491 | // bytes name = 2; 492 | void clear_name(); 493 | const std::string& name() const; 494 | void set_name(const std::string& value); 495 | void set_name(std::string&& value); 496 | void set_name(const char* value); 497 | void set_name(const void* value, size_t size); 498 | std::string* mutable_name(); 499 | std::string* release_name(); 500 | void set_allocated_name(std::string* name); 501 | private: 502 | const std::string& _internal_name() const; 503 | void _internal_set_name(const std::string& value); 504 | std::string* _internal_mutable_name(); 505 | public: 506 | 507 | // bytes pwd = 3; 508 | void clear_pwd(); 509 | const std::string& pwd() const; 510 | void set_pwd(const std::string& value); 511 | void set_pwd(std::string&& value); 512 | void set_pwd(const char* value); 513 | void set_pwd(const void* value, size_t size); 514 | std::string* mutable_pwd(); 515 | std::string* release_pwd(); 516 | void set_allocated_pwd(std::string* pwd); 517 | private: 518 | const std::string& _internal_pwd() const; 519 | void _internal_set_pwd(const std::string& value); 520 | std::string* _internal_mutable_pwd(); 521 | public: 522 | 523 | // uint32 id = 1; 524 | void clear_id(); 525 | ::PROTOBUF_NAMESPACE_ID::uint32 id() const; 526 | void set_id(::PROTOBUF_NAMESPACE_ID::uint32 value); 527 | private: 528 | ::PROTOBUF_NAMESPACE_ID::uint32 _internal_id() const; 529 | void _internal_set_id(::PROTOBUF_NAMESPACE_ID::uint32 value); 530 | public: 531 | 532 | // @@protoc_insertion_point(class_scope:fixbug.RegisterRequest) 533 | private: 534 | class _Internal; 535 | 536 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 537 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; 538 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr pwd_; 539 | ::PROTOBUF_NAMESPACE_ID::uint32 id_; 540 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 541 | friend struct ::TableStruct_user_2eproto; 542 | }; 543 | // ------------------------------------------------------------------- 544 | 545 | class RegisterResponse : 546 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:fixbug.RegisterResponse) */ { 547 | public: 548 | RegisterResponse(); 549 | virtual ~RegisterResponse(); 550 | 551 | RegisterResponse(const RegisterResponse& from); 552 | RegisterResponse(RegisterResponse&& from) noexcept 553 | : RegisterResponse() { 554 | *this = ::std::move(from); 555 | } 556 | 557 | inline RegisterResponse& operator=(const RegisterResponse& from) { 558 | CopyFrom(from); 559 | return *this; 560 | } 561 | inline RegisterResponse& operator=(RegisterResponse&& from) noexcept { 562 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 563 | if (this != &from) InternalSwap(&from); 564 | } else { 565 | CopyFrom(from); 566 | } 567 | return *this; 568 | } 569 | 570 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 571 | return GetDescriptor(); 572 | } 573 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 574 | return GetMetadataStatic().descriptor; 575 | } 576 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 577 | return GetMetadataStatic().reflection; 578 | } 579 | static const RegisterResponse& default_instance(); 580 | 581 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 582 | static inline const RegisterResponse* internal_default_instance() { 583 | return reinterpret_cast( 584 | &_RegisterResponse_default_instance_); 585 | } 586 | static constexpr int kIndexInFileMessages = 587 | 3; 588 | 589 | friend void swap(RegisterResponse& a, RegisterResponse& b) { 590 | a.Swap(&b); 591 | } 592 | inline void Swap(RegisterResponse* other) { 593 | if (other == this) return; 594 | InternalSwap(other); 595 | } 596 | 597 | // implements Message ---------------------------------------------- 598 | 599 | inline RegisterResponse* New() const final { 600 | return CreateMaybeMessage(nullptr); 601 | } 602 | 603 | RegisterResponse* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 604 | return CreateMaybeMessage(arena); 605 | } 606 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 607 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 608 | void CopyFrom(const RegisterResponse& from); 609 | void MergeFrom(const RegisterResponse& from); 610 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 611 | bool IsInitialized() const final; 612 | 613 | size_t ByteSizeLong() const final; 614 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 615 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 616 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 617 | int GetCachedSize() const final { return _cached_size_.Get(); } 618 | 619 | private: 620 | inline void SharedCtor(); 621 | inline void SharedDtor(); 622 | void SetCachedSize(int size) const final; 623 | void InternalSwap(RegisterResponse* other); 624 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 625 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 626 | return "fixbug.RegisterResponse"; 627 | } 628 | private: 629 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 630 | return nullptr; 631 | } 632 | inline void* MaybeArenaPtr() const { 633 | return nullptr; 634 | } 635 | public: 636 | 637 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 638 | private: 639 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 640 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_user_2eproto); 641 | return ::descriptor_table_user_2eproto.file_level_metadata[kIndexInFileMessages]; 642 | } 643 | 644 | public: 645 | 646 | // nested types ---------------------------------------------------- 647 | 648 | // accessors ------------------------------------------------------- 649 | 650 | enum : int { 651 | kResultFieldNumber = 1, 652 | kSuccessFieldNumber = 2, 653 | }; 654 | // .fixbug.ResultCode result = 1; 655 | bool has_result() const; 656 | private: 657 | bool _internal_has_result() const; 658 | public: 659 | void clear_result(); 660 | const ::fixbug::ResultCode& result() const; 661 | ::fixbug::ResultCode* release_result(); 662 | ::fixbug::ResultCode* mutable_result(); 663 | void set_allocated_result(::fixbug::ResultCode* result); 664 | private: 665 | const ::fixbug::ResultCode& _internal_result() const; 666 | ::fixbug::ResultCode* _internal_mutable_result(); 667 | public: 668 | 669 | // bool success = 2; 670 | void clear_success(); 671 | bool success() const; 672 | void set_success(bool value); 673 | private: 674 | bool _internal_success() const; 675 | void _internal_set_success(bool value); 676 | public: 677 | 678 | // @@protoc_insertion_point(class_scope:fixbug.RegisterResponse) 679 | private: 680 | class _Internal; 681 | 682 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 683 | ::fixbug::ResultCode* result_; 684 | bool success_; 685 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 686 | friend struct ::TableStruct_user_2eproto; 687 | }; 688 | // =================================================================== 689 | 690 | class UserServiceRpc_Stub; 691 | 692 | class UserServiceRpc : public ::PROTOBUF_NAMESPACE_ID::Service { 693 | protected: 694 | // This class should be treated as an abstract interface. 695 | inline UserServiceRpc() {}; 696 | public: 697 | virtual ~UserServiceRpc(); 698 | 699 | typedef UserServiceRpc_Stub Stub; 700 | 701 | static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* descriptor(); 702 | 703 | virtual void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 704 | const ::fixbug::LoginRequest* request, 705 | ::fixbug::LoginResponse* response, 706 | ::google::protobuf::Closure* done); 707 | virtual void Register(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 708 | const ::fixbug::RegisterRequest* request, 709 | ::fixbug::RegisterResponse* response, 710 | ::google::protobuf::Closure* done); 711 | 712 | // implements Service ---------------------------------------------- 713 | 714 | const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* GetDescriptor(); 715 | void CallMethod(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method, 716 | ::PROTOBUF_NAMESPACE_ID::RpcController* controller, 717 | const ::PROTOBUF_NAMESPACE_ID::Message* request, 718 | ::PROTOBUF_NAMESPACE_ID::Message* response, 719 | ::google::protobuf::Closure* done); 720 | const ::PROTOBUF_NAMESPACE_ID::Message& GetRequestPrototype( 721 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const; 722 | const ::PROTOBUF_NAMESPACE_ID::Message& GetResponsePrototype( 723 | const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const; 724 | 725 | private: 726 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UserServiceRpc); 727 | }; 728 | 729 | class UserServiceRpc_Stub : public UserServiceRpc { 730 | public: 731 | UserServiceRpc_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel); 732 | UserServiceRpc_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel, 733 | ::PROTOBUF_NAMESPACE_ID::Service::ChannelOwnership ownership); 734 | ~UserServiceRpc_Stub(); 735 | 736 | inline ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel() { return channel_; } 737 | 738 | // implements UserServiceRpc ------------------------------------------ 739 | 740 | void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 741 | const ::fixbug::LoginRequest* request, 742 | ::fixbug::LoginResponse* response, 743 | ::google::protobuf::Closure* done); 744 | void Register(::PROTOBUF_NAMESPACE_ID::RpcController* controller, 745 | const ::fixbug::RegisterRequest* request, 746 | ::fixbug::RegisterResponse* response, 747 | ::google::protobuf::Closure* done); 748 | private: 749 | ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel_; 750 | bool owns_channel_; 751 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UserServiceRpc_Stub); 752 | }; 753 | 754 | 755 | // =================================================================== 756 | 757 | 758 | // =================================================================== 759 | 760 | #ifdef __GNUC__ 761 | #pragma GCC diagnostic push 762 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" 763 | #endif // __GNUC__ 764 | // LoginRequest 765 | 766 | // bytes name = 1; 767 | inline void LoginRequest::clear_name() { 768 | name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 769 | } 770 | inline const std::string& LoginRequest::name() const { 771 | // @@protoc_insertion_point(field_get:fixbug.LoginRequest.name) 772 | return _internal_name(); 773 | } 774 | inline void LoginRequest::set_name(const std::string& value) { 775 | _internal_set_name(value); 776 | // @@protoc_insertion_point(field_set:fixbug.LoginRequest.name) 777 | } 778 | inline std::string* LoginRequest::mutable_name() { 779 | // @@protoc_insertion_point(field_mutable:fixbug.LoginRequest.name) 780 | return _internal_mutable_name(); 781 | } 782 | inline const std::string& LoginRequest::_internal_name() const { 783 | return name_.GetNoArena(); 784 | } 785 | inline void LoginRequest::_internal_set_name(const std::string& value) { 786 | 787 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 788 | } 789 | inline void LoginRequest::set_name(std::string&& value) { 790 | 791 | name_.SetNoArena( 792 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 793 | // @@protoc_insertion_point(field_set_rvalue:fixbug.LoginRequest.name) 794 | } 795 | inline void LoginRequest::set_name(const char* value) { 796 | GOOGLE_DCHECK(value != nullptr); 797 | 798 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 799 | // @@protoc_insertion_point(field_set_char:fixbug.LoginRequest.name) 800 | } 801 | inline void LoginRequest::set_name(const void* value, size_t size) { 802 | 803 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 804 | ::std::string(reinterpret_cast(value), size)); 805 | // @@protoc_insertion_point(field_set_pointer:fixbug.LoginRequest.name) 806 | } 807 | inline std::string* LoginRequest::_internal_mutable_name() { 808 | 809 | return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 810 | } 811 | inline std::string* LoginRequest::release_name() { 812 | // @@protoc_insertion_point(field_release:fixbug.LoginRequest.name) 813 | 814 | return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 815 | } 816 | inline void LoginRequest::set_allocated_name(std::string* name) { 817 | if (name != nullptr) { 818 | 819 | } else { 820 | 821 | } 822 | name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); 823 | // @@protoc_insertion_point(field_set_allocated:fixbug.LoginRequest.name) 824 | } 825 | 826 | // bytes pwd = 2; 827 | inline void LoginRequest::clear_pwd() { 828 | pwd_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 829 | } 830 | inline const std::string& LoginRequest::pwd() const { 831 | // @@protoc_insertion_point(field_get:fixbug.LoginRequest.pwd) 832 | return _internal_pwd(); 833 | } 834 | inline void LoginRequest::set_pwd(const std::string& value) { 835 | _internal_set_pwd(value); 836 | // @@protoc_insertion_point(field_set:fixbug.LoginRequest.pwd) 837 | } 838 | inline std::string* LoginRequest::mutable_pwd() { 839 | // @@protoc_insertion_point(field_mutable:fixbug.LoginRequest.pwd) 840 | return _internal_mutable_pwd(); 841 | } 842 | inline const std::string& LoginRequest::_internal_pwd() const { 843 | return pwd_.GetNoArena(); 844 | } 845 | inline void LoginRequest::_internal_set_pwd(const std::string& value) { 846 | 847 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 848 | } 849 | inline void LoginRequest::set_pwd(std::string&& value) { 850 | 851 | pwd_.SetNoArena( 852 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 853 | // @@protoc_insertion_point(field_set_rvalue:fixbug.LoginRequest.pwd) 854 | } 855 | inline void LoginRequest::set_pwd(const char* value) { 856 | GOOGLE_DCHECK(value != nullptr); 857 | 858 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 859 | // @@protoc_insertion_point(field_set_char:fixbug.LoginRequest.pwd) 860 | } 861 | inline void LoginRequest::set_pwd(const void* value, size_t size) { 862 | 863 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 864 | ::std::string(reinterpret_cast(value), size)); 865 | // @@protoc_insertion_point(field_set_pointer:fixbug.LoginRequest.pwd) 866 | } 867 | inline std::string* LoginRequest::_internal_mutable_pwd() { 868 | 869 | return pwd_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 870 | } 871 | inline std::string* LoginRequest::release_pwd() { 872 | // @@protoc_insertion_point(field_release:fixbug.LoginRequest.pwd) 873 | 874 | return pwd_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 875 | } 876 | inline void LoginRequest::set_allocated_pwd(std::string* pwd) { 877 | if (pwd != nullptr) { 878 | 879 | } else { 880 | 881 | } 882 | pwd_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), pwd); 883 | // @@protoc_insertion_point(field_set_allocated:fixbug.LoginRequest.pwd) 884 | } 885 | 886 | // ------------------------------------------------------------------- 887 | 888 | // LoginResponse 889 | 890 | // .fixbug.ResultCode result = 1; 891 | inline bool LoginResponse::_internal_has_result() const { 892 | return this != internal_default_instance() && result_ != nullptr; 893 | } 894 | inline bool LoginResponse::has_result() const { 895 | return _internal_has_result(); 896 | } 897 | inline const ::fixbug::ResultCode& LoginResponse::_internal_result() const { 898 | const ::fixbug::ResultCode* p = result_; 899 | return p != nullptr ? *p : *reinterpret_cast( 900 | &::fixbug::_ResultCode_default_instance_); 901 | } 902 | inline const ::fixbug::ResultCode& LoginResponse::result() const { 903 | // @@protoc_insertion_point(field_get:fixbug.LoginResponse.result) 904 | return _internal_result(); 905 | } 906 | inline ::fixbug::ResultCode* LoginResponse::release_result() { 907 | // @@protoc_insertion_point(field_release:fixbug.LoginResponse.result) 908 | 909 | ::fixbug::ResultCode* temp = result_; 910 | result_ = nullptr; 911 | return temp; 912 | } 913 | inline ::fixbug::ResultCode* LoginResponse::_internal_mutable_result() { 914 | 915 | if (result_ == nullptr) { 916 | auto* p = CreateMaybeMessage<::fixbug::ResultCode>(GetArenaNoVirtual()); 917 | result_ = p; 918 | } 919 | return result_; 920 | } 921 | inline ::fixbug::ResultCode* LoginResponse::mutable_result() { 922 | // @@protoc_insertion_point(field_mutable:fixbug.LoginResponse.result) 923 | return _internal_mutable_result(); 924 | } 925 | inline void LoginResponse::set_allocated_result(::fixbug::ResultCode* result) { 926 | ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); 927 | if (message_arena == nullptr) { 928 | delete reinterpret_cast< ::PROTOBUF_NAMESPACE_ID::MessageLite*>(result_); 929 | } 930 | if (result) { 931 | ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = nullptr; 932 | if (message_arena != submessage_arena) { 933 | result = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( 934 | message_arena, result, submessage_arena); 935 | } 936 | 937 | } else { 938 | 939 | } 940 | result_ = result; 941 | // @@protoc_insertion_point(field_set_allocated:fixbug.LoginResponse.result) 942 | } 943 | 944 | // bool success = 3; 945 | inline void LoginResponse::clear_success() { 946 | success_ = false; 947 | } 948 | inline bool LoginResponse::_internal_success() const { 949 | return success_; 950 | } 951 | inline bool LoginResponse::success() const { 952 | // @@protoc_insertion_point(field_get:fixbug.LoginResponse.success) 953 | return _internal_success(); 954 | } 955 | inline void LoginResponse::_internal_set_success(bool value) { 956 | 957 | success_ = value; 958 | } 959 | inline void LoginResponse::set_success(bool value) { 960 | _internal_set_success(value); 961 | // @@protoc_insertion_point(field_set:fixbug.LoginResponse.success) 962 | } 963 | 964 | // ------------------------------------------------------------------- 965 | 966 | // RegisterRequest 967 | 968 | // uint32 id = 1; 969 | inline void RegisterRequest::clear_id() { 970 | id_ = 0u; 971 | } 972 | inline ::PROTOBUF_NAMESPACE_ID::uint32 RegisterRequest::_internal_id() const { 973 | return id_; 974 | } 975 | inline ::PROTOBUF_NAMESPACE_ID::uint32 RegisterRequest::id() const { 976 | // @@protoc_insertion_point(field_get:fixbug.RegisterRequest.id) 977 | return _internal_id(); 978 | } 979 | inline void RegisterRequest::_internal_set_id(::PROTOBUF_NAMESPACE_ID::uint32 value) { 980 | 981 | id_ = value; 982 | } 983 | inline void RegisterRequest::set_id(::PROTOBUF_NAMESPACE_ID::uint32 value) { 984 | _internal_set_id(value); 985 | // @@protoc_insertion_point(field_set:fixbug.RegisterRequest.id) 986 | } 987 | 988 | // bytes name = 2; 989 | inline void RegisterRequest::clear_name() { 990 | name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 991 | } 992 | inline const std::string& RegisterRequest::name() const { 993 | // @@protoc_insertion_point(field_get:fixbug.RegisterRequest.name) 994 | return _internal_name(); 995 | } 996 | inline void RegisterRequest::set_name(const std::string& value) { 997 | _internal_set_name(value); 998 | // @@protoc_insertion_point(field_set:fixbug.RegisterRequest.name) 999 | } 1000 | inline std::string* RegisterRequest::mutable_name() { 1001 | // @@protoc_insertion_point(field_mutable:fixbug.RegisterRequest.name) 1002 | return _internal_mutable_name(); 1003 | } 1004 | inline const std::string& RegisterRequest::_internal_name() const { 1005 | return name_.GetNoArena(); 1006 | } 1007 | inline void RegisterRequest::_internal_set_name(const std::string& value) { 1008 | 1009 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 1010 | } 1011 | inline void RegisterRequest::set_name(std::string&& value) { 1012 | 1013 | name_.SetNoArena( 1014 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 1015 | // @@protoc_insertion_point(field_set_rvalue:fixbug.RegisterRequest.name) 1016 | } 1017 | inline void RegisterRequest::set_name(const char* value) { 1018 | GOOGLE_DCHECK(value != nullptr); 1019 | 1020 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 1021 | // @@protoc_insertion_point(field_set_char:fixbug.RegisterRequest.name) 1022 | } 1023 | inline void RegisterRequest::set_name(const void* value, size_t size) { 1024 | 1025 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 1026 | ::std::string(reinterpret_cast(value), size)); 1027 | // @@protoc_insertion_point(field_set_pointer:fixbug.RegisterRequest.name) 1028 | } 1029 | inline std::string* RegisterRequest::_internal_mutable_name() { 1030 | 1031 | return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 1032 | } 1033 | inline std::string* RegisterRequest::release_name() { 1034 | // @@protoc_insertion_point(field_release:fixbug.RegisterRequest.name) 1035 | 1036 | return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 1037 | } 1038 | inline void RegisterRequest::set_allocated_name(std::string* name) { 1039 | if (name != nullptr) { 1040 | 1041 | } else { 1042 | 1043 | } 1044 | name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); 1045 | // @@protoc_insertion_point(field_set_allocated:fixbug.RegisterRequest.name) 1046 | } 1047 | 1048 | // bytes pwd = 3; 1049 | inline void RegisterRequest::clear_pwd() { 1050 | pwd_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 1051 | } 1052 | inline const std::string& RegisterRequest::pwd() const { 1053 | // @@protoc_insertion_point(field_get:fixbug.RegisterRequest.pwd) 1054 | return _internal_pwd(); 1055 | } 1056 | inline void RegisterRequest::set_pwd(const std::string& value) { 1057 | _internal_set_pwd(value); 1058 | // @@protoc_insertion_point(field_set:fixbug.RegisterRequest.pwd) 1059 | } 1060 | inline std::string* RegisterRequest::mutable_pwd() { 1061 | // @@protoc_insertion_point(field_mutable:fixbug.RegisterRequest.pwd) 1062 | return _internal_mutable_pwd(); 1063 | } 1064 | inline const std::string& RegisterRequest::_internal_pwd() const { 1065 | return pwd_.GetNoArena(); 1066 | } 1067 | inline void RegisterRequest::_internal_set_pwd(const std::string& value) { 1068 | 1069 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 1070 | } 1071 | inline void RegisterRequest::set_pwd(std::string&& value) { 1072 | 1073 | pwd_.SetNoArena( 1074 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 1075 | // @@protoc_insertion_point(field_set_rvalue:fixbug.RegisterRequest.pwd) 1076 | } 1077 | inline void RegisterRequest::set_pwd(const char* value) { 1078 | GOOGLE_DCHECK(value != nullptr); 1079 | 1080 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 1081 | // @@protoc_insertion_point(field_set_char:fixbug.RegisterRequest.pwd) 1082 | } 1083 | inline void RegisterRequest::set_pwd(const void* value, size_t size) { 1084 | 1085 | pwd_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 1086 | ::std::string(reinterpret_cast(value), size)); 1087 | // @@protoc_insertion_point(field_set_pointer:fixbug.RegisterRequest.pwd) 1088 | } 1089 | inline std::string* RegisterRequest::_internal_mutable_pwd() { 1090 | 1091 | return pwd_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 1092 | } 1093 | inline std::string* RegisterRequest::release_pwd() { 1094 | // @@protoc_insertion_point(field_release:fixbug.RegisterRequest.pwd) 1095 | 1096 | return pwd_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 1097 | } 1098 | inline void RegisterRequest::set_allocated_pwd(std::string* pwd) { 1099 | if (pwd != nullptr) { 1100 | 1101 | } else { 1102 | 1103 | } 1104 | pwd_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), pwd); 1105 | // @@protoc_insertion_point(field_set_allocated:fixbug.RegisterRequest.pwd) 1106 | } 1107 | 1108 | // ------------------------------------------------------------------- 1109 | 1110 | // RegisterResponse 1111 | 1112 | // .fixbug.ResultCode result = 1; 1113 | inline bool RegisterResponse::_internal_has_result() const { 1114 | return this != internal_default_instance() && result_ != nullptr; 1115 | } 1116 | inline bool RegisterResponse::has_result() const { 1117 | return _internal_has_result(); 1118 | } 1119 | inline const ::fixbug::ResultCode& RegisterResponse::_internal_result() const { 1120 | const ::fixbug::ResultCode* p = result_; 1121 | return p != nullptr ? *p : *reinterpret_cast( 1122 | &::fixbug::_ResultCode_default_instance_); 1123 | } 1124 | inline const ::fixbug::ResultCode& RegisterResponse::result() const { 1125 | // @@protoc_insertion_point(field_get:fixbug.RegisterResponse.result) 1126 | return _internal_result(); 1127 | } 1128 | inline ::fixbug::ResultCode* RegisterResponse::release_result() { 1129 | // @@protoc_insertion_point(field_release:fixbug.RegisterResponse.result) 1130 | 1131 | ::fixbug::ResultCode* temp = result_; 1132 | result_ = nullptr; 1133 | return temp; 1134 | } 1135 | inline ::fixbug::ResultCode* RegisterResponse::_internal_mutable_result() { 1136 | 1137 | if (result_ == nullptr) { 1138 | auto* p = CreateMaybeMessage<::fixbug::ResultCode>(GetArenaNoVirtual()); 1139 | result_ = p; 1140 | } 1141 | return result_; 1142 | } 1143 | inline ::fixbug::ResultCode* RegisterResponse::mutable_result() { 1144 | // @@protoc_insertion_point(field_mutable:fixbug.RegisterResponse.result) 1145 | return _internal_mutable_result(); 1146 | } 1147 | inline void RegisterResponse::set_allocated_result(::fixbug::ResultCode* result) { 1148 | ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); 1149 | if (message_arena == nullptr) { 1150 | delete reinterpret_cast< ::PROTOBUF_NAMESPACE_ID::MessageLite*>(result_); 1151 | } 1152 | if (result) { 1153 | ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = nullptr; 1154 | if (message_arena != submessage_arena) { 1155 | result = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( 1156 | message_arena, result, submessage_arena); 1157 | } 1158 | 1159 | } else { 1160 | 1161 | } 1162 | result_ = result; 1163 | // @@protoc_insertion_point(field_set_allocated:fixbug.RegisterResponse.result) 1164 | } 1165 | 1166 | // bool success = 2; 1167 | inline void RegisterResponse::clear_success() { 1168 | success_ = false; 1169 | } 1170 | inline bool RegisterResponse::_internal_success() const { 1171 | return success_; 1172 | } 1173 | inline bool RegisterResponse::success() const { 1174 | // @@protoc_insertion_point(field_get:fixbug.RegisterResponse.success) 1175 | return _internal_success(); 1176 | } 1177 | inline void RegisterResponse::_internal_set_success(bool value) { 1178 | 1179 | success_ = value; 1180 | } 1181 | inline void RegisterResponse::set_success(bool value) { 1182 | _internal_set_success(value); 1183 | // @@protoc_insertion_point(field_set:fixbug.RegisterResponse.success) 1184 | } 1185 | 1186 | #ifdef __GNUC__ 1187 | #pragma GCC diagnostic pop 1188 | #endif // __GNUC__ 1189 | // ------------------------------------------------------------------- 1190 | 1191 | // ------------------------------------------------------------------- 1192 | 1193 | // ------------------------------------------------------------------- 1194 | 1195 | 1196 | // @@protoc_insertion_point(namespace_scope) 1197 | 1198 | } // namespace fixbug 1199 | 1200 | // @@protoc_insertion_point(global_scope) 1201 | 1202 | #include 1203 | #endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_user_2eproto 1204 | -------------------------------------------------------------------------------- /example/user.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package fixbug; 4 | 5 | option cc_generic_services = true; 6 | 7 | import "publictype.proto"; 8 | 9 | message LoginRequest 10 | { 11 | bytes name = 1; 12 | bytes pwd = 2; 13 | } 14 | 15 | message LoginResponse 16 | { 17 | ResultCode result = 1; 18 | bool success = 3; 19 | } 20 | 21 | message RegisterRequest 22 | { 23 | uint32 id = 1; 24 | bytes name = 2; 25 | bytes pwd = 3; 26 | } 27 | 28 | message RegisterResponse 29 | { 30 | ResultCode result = 1; 31 | bool success = 2; 32 | } 33 | 34 | 35 | service UserServiceRpc 36 | { 37 | rpc Login(LoginRequest) returns(LoginResponse); 38 | rpc Register(RegisterRequest) returns(RegisterResponse); 39 | } -------------------------------------------------------------------------------- /lib/libmprpc.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/lib/libmprpc.a -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SRC_LIST mprpcapplication.cc 2 | rpcheader.pb.cc 3 | rpcprovider.cc 4 | mprpcconfig.cc 5 | mprpccontroller.cc 6 | mprpcchannel.cc 7 | asynclogging.cc 8 | logger.cc 9 | zookeeperutil.cc) 10 | 11 | add_library(mprpc ${SRC_LIST}) 12 | target_link_libraries(mprpc muduo_net muduo_base pthread zookeeper_mt) -------------------------------------------------------------------------------- /src/asynclogging.cc: -------------------------------------------------------------------------------- 1 | #include "asynclogging.h" 2 | 3 | #include 4 | 5 | const int G_LOGQUEU_MAX_SIZE = 8; 6 | 7 | static std::string MakeLogFilename(const char* outdir) { 8 | // 获取当前的日期,然后取日志信息,写入相应的日志文件当中 a+ 9 | time_t now = time(nullptr); 10 | tm *nowtm = localtime(&now); 11 | 12 | char file_name[128] = "\0"; 13 | sprintf(file_name, "%s/%d-%d-%d-log.txt", outdir, nowtm->tm_year+1900, nowtm->tm_mon+1, nowtm->tm_mday); 14 | return file_name; 15 | } 16 | 17 | AsyncLogging::AsyncLogging() { 18 | m_exit = false; 19 | } 20 | 21 | void AsyncLogging::Init() { 22 | // 启动专门的写日志线程 23 | std::thread writeLogTask([&](){ 24 | std::mutex mtx; 25 | for (;;) 26 | { 27 | // 日志写入线程的第二种苏醒方式:每个三秒自动起来看一眼 28 | std::unique_lock lock(mtx); 29 | while (m_lckQue.empty()) { 30 | m_cond.wait_for(lock, std::chrono::seconds(3)); 31 | if (m_exit == true) { 32 | break; 33 | } 34 | } 35 | lock.unlock(); 36 | if (m_exit == true) { 37 | break; 38 | } 39 | 40 | std::string file_name = MakeLogFilename(m_outDir.c_str()); 41 | FILE *pf = fopen(file_name.c_str(), "a+"); 42 | if (pf == nullptr) 43 | { 44 | std::cout << "logger file : " << file_name << " open error!" << std::endl; 45 | exit(EXIT_FAILURE); 46 | } 47 | 48 | /** 49 | * 如果当前缓冲区大小 < G_LOGQUEU_MAX_SIZE,则正常去一条数据写入到文件 50 | * 否则将整个缓冲区数据全部写入到文件 51 | */ 52 | if (m_lckQue.size() < G_LOGQUEU_MAX_SIZE) { 53 | std::string msg = m_lckQue.pop(); 54 | msg.append("\n"); 55 | fputs(msg.c_str(), pf); 56 | } else { 57 | std::queue q; 58 | m_lckQue.swap(q); 59 | 60 | while (!q.empty()) { 61 | std::string msg = q.front(); 62 | q.pop(); 63 | msg.append("\n"); 64 | fputs(msg.c_str(), pf); 65 | } 66 | } 67 | 68 | fclose(pf); 69 | } 70 | }); 71 | writeLogTask.detach(); 72 | } 73 | 74 | AsyncLogging::~AsyncLogging() { 75 | m_exit = true; 76 | m_cond.notify_one(); 77 | } 78 | 79 | // 前端线程往queue中写日志 80 | void AsyncLogging::Log(const std::string& msg) { 81 | m_lckQue.push(msg); 82 | m_cond.notify_one(); 83 | } 84 | 85 | void AsyncLogging::SetOutDir(const std::string& outdir) { 86 | m_outDir = outdir; 87 | } -------------------------------------------------------------------------------- /src/include/asynclogging.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "lockqueue.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | class AsyncLogging { 10 | public: 11 | AsyncLogging(); 12 | ~AsyncLogging(); 13 | void Log(const std::string& msg); 14 | 15 | void SetOutDir(const std::string& outdir); 16 | void Init(); 17 | 18 | private: 19 | bool m_exit; 20 | LockQueue m_lckQue; 21 | std::condition_variable m_cond; 22 | 23 | std::string m_outDir; 24 | 25 | AsyncLogging(const AsyncLogging&) = delete; 26 | AsyncLogging(AsyncLogging&&) = delete; 27 | }; -------------------------------------------------------------------------------- /src/include/lockqueue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | template 7 | class LockQueue { 8 | public: 9 | void push(const T& data) { 10 | std::lock_guard lock(m_mutex); 11 | m_queue.push(data); 12 | } 13 | 14 | T pop() { 15 | std::lock_guard lock(m_mutex); 16 | T data = m_queue.front(); 17 | m_queue.pop(); 18 | return data; 19 | } 20 | 21 | size_t size() { 22 | std::lock_guard lock(m_mutex); 23 | m_queue.size(); 24 | } 25 | 26 | bool empty() { 27 | std::lock_guard lock(m_mutex); 28 | return m_queue.empty(); 29 | } 30 | 31 | void swap(std::queue& q) { 32 | std::lock_guard lock(m_mutex); 33 | m_queue.swap(q); 34 | } 35 | 36 | private: 37 | std::queue m_queue; 38 | std::mutex m_mutex; 39 | }; -------------------------------------------------------------------------------- /src/include/logger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "mprpcconfig.h" 4 | #include "asynclogging.h" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | enum LogLevel 11 | { 12 | TRACE, // 调试,指出比DEBUG粒度更细的一些信息事件(开发过程中使用) 13 | DEBUG, // 调试,指出细粒度信息事件对调试应用程序是非常有帮助的。(开发过程中使用) 14 | INFO, // 信息,表明消息在粗粒度级别上突出强调应用程序的运行过程。 15 | WARN, // 警告,系统能正常运行,但可能会出现潜在错误的情形。 16 | ERROR, // 错误,指出虽然发生错误事件,但仍然不影响系统的继续运行 17 | FATAL // 崩溃,指出每个严重的错误事件将会导致应用程序的退出。 18 | }; 19 | 20 | std::string MakeLogMsgHeader(int level); 21 | 22 | // 定义宏 LOG_INFO("xxx %d %s", 20, "xxxx") 23 | #define LOG_PRINT(logmsgformat, ...) \ 24 | do \ 25 | { \ 26 | Logger &logger = Logger::GetInstance(); \ 27 | char c[1024] = {0}; \ 28 | snprintf(c, 1024, "[%s] %s [%s %d] %s", logger.GetTag().c_str(), MakeLogMsgHeader(logger.GetLogLevel()).c_str(),\ 29 | __FILE__, __LINE__, logmsgformat, ##__VA_ARGS__); \ 30 | logger.Log(c); \ 31 | } while(0); \ 32 | 33 | #define LOG_TRACE(logmsgformat, ...) \ 34 | do \ 35 | { \ 36 | if (TRACE >= Logger::GetInstance().GetLogLevel()) { \ 37 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 38 | } \ 39 | } while(0); \ 40 | 41 | #define LOG_DEBUG(logmsgformat, ...) \ 42 | do \ 43 | { \ 44 | if (DEBUG >= Logger::GetInstance().GetLogLevel()) { \ 45 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 46 | } \ 47 | } while(0); \ 48 | 49 | #define LOG_INFO(logmsgformat, ...) \ 50 | do \ 51 | { \ 52 | if (INFO >= Logger::GetInstance().GetLogLevel()) { \ 53 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 54 | } \ 55 | } while(0); \ 56 | 57 | #define LOG_WARN(logmsgformat, ...) \ 58 | do \ 59 | { \ 60 | if (WARN >= Logger::GetInstance().GetLogLevel()) { \ 61 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 62 | } \ 63 | } while(0); \ 64 | 65 | #define LOG_ERROR(logmsgformat, ...) \ 66 | do \ 67 | { \ 68 | if (ERROR >= Logger::GetInstance().GetLogLevel()) { \ 69 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 70 | } \ 71 | } while(0); \ 72 | 73 | #define LOG_FATAL(logmsgformat, ...) \ 74 | do \ 75 | { \ 76 | if (FATAL >= Logger::GetInstance().GetLogLevel()) { \ 77 | LOG_PRINT(logmsgformat, ##__VA_ARGS__); \ 78 | } \ 79 | } while(0); \ 80 | 81 | class Logger { 82 | public: 83 | void Init(const std::string& path); 84 | void SetLogLevel(LogLevel level); 85 | void SetLogLevel(const std::string& level); 86 | int GetLogLevel() const { return m_loglevel; } 87 | const std::string& GetTag() const { return m_tag; } 88 | void Log(const std::string& msg); 89 | 90 | static Logger& GetInstance(); 91 | 92 | private: 93 | int m_loglevel; 94 | std::string m_tag; 95 | MprpcConfig m_logConfig; 96 | 97 | AsyncLogging* m_asyncLogging; 98 | 99 | Logger(); 100 | ~Logger(); 101 | Logger(const Logger&) = delete; 102 | Logger(Logger&&) = delete; 103 | }; -------------------------------------------------------------------------------- /src/include/mprpcapplication.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "mprpcconfig.h" 4 | 5 | // mprpc的基础类 6 | class MprpcApplication { 7 | public: 8 | static void Init(int argc, char **argv); 9 | 10 | static MprpcApplication& GetInstance(); 11 | static MprpcConfig& GetConfig(); 12 | 13 | private: 14 | static MprpcConfig m_config; 15 | MprpcApplication() {}; 16 | MprpcApplication(const MprpcApplication&) = delete; 17 | MprpcApplication(MprpcApplication&&) = delete; 18 | }; -------------------------------------------------------------------------------- /src/include/mprpcchannel.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "zookeeperutil.h" 6 | 7 | class MprpcChannel : public google::protobuf::RpcChannel { 8 | public: 9 | 10 | void CallMethod(const google::protobuf::MethodDescriptor* method, 11 | google::protobuf::RpcController* controller, 12 | const google::protobuf::Message* request, 13 | google::protobuf::Message* response, 14 | google::protobuf::Closure* done); 15 | private: 16 | std::unique_ptr m_zkClientPtr; 17 | }; -------------------------------------------------------------------------------- /src/include/mprpcconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class MprpcConfig { 7 | public: 8 | void LoadConfigFile(const char* config_file); 9 | std::string Load(const std::string& key); 10 | private: 11 | std::unordered_map m_configMap; 12 | 13 | void Trim(std::string& src_buf); 14 | }; -------------------------------------------------------------------------------- /src/include/mprpccontroller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class MprpcController : public google::protobuf::RpcController { 7 | public: 8 | MprpcController(); 9 | ~MprpcController(){} 10 | 11 | void Reset(); 12 | bool Failed() const; 13 | std::string ErrorText() const; 14 | void SetFailed(const std::string& reason); 15 | 16 | void StartCancel(); 17 | bool IsCanceled() const; 18 | void NotifyOnCancel(google::protobuf::Closure* callback); 19 | 20 | private: 21 | bool m_failed; 22 | std::string m_errText; 23 | }; -------------------------------------------------------------------------------- /src/include/rpcheader.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: rpcheader.proto 3 | 4 | #ifndef GOOGLE_PROTOBUF_INCLUDED_rpcheader_2eproto 5 | #define GOOGLE_PROTOBUF_INCLUDED_rpcheader_2eproto 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #if PROTOBUF_VERSION < 3011000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 3011000 < PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include // IWYU pragma: export 33 | #include // IWYU pragma: export 34 | #include 35 | // @@protoc_insertion_point(includes) 36 | #include 37 | #define PROTOBUF_INTERNAL_EXPORT_rpcheader_2eproto 38 | PROTOBUF_NAMESPACE_OPEN 39 | namespace internal { 40 | class AnyMetadata; 41 | } // namespace internal 42 | PROTOBUF_NAMESPACE_CLOSE 43 | 44 | // Internal implementation detail -- do not use these members. 45 | struct TableStruct_rpcheader_2eproto { 46 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] 47 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 48 | static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] 49 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] 51 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 52 | static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; 53 | static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; 54 | static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; 55 | }; 56 | extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_rpcheader_2eproto; 57 | namespace mprpc { 58 | class RpcHeader; 59 | class RpcHeaderDefaultTypeInternal; 60 | extern RpcHeaderDefaultTypeInternal _RpcHeader_default_instance_; 61 | } // namespace mprpc 62 | PROTOBUF_NAMESPACE_OPEN 63 | template<> ::mprpc::RpcHeader* Arena::CreateMaybeMessage<::mprpc::RpcHeader>(Arena*); 64 | PROTOBUF_NAMESPACE_CLOSE 65 | namespace mprpc { 66 | 67 | // =================================================================== 68 | 69 | class RpcHeader : 70 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:mprpc.RpcHeader) */ { 71 | public: 72 | RpcHeader(); 73 | virtual ~RpcHeader(); 74 | 75 | RpcHeader(const RpcHeader& from); 76 | RpcHeader(RpcHeader&& from) noexcept 77 | : RpcHeader() { 78 | *this = ::std::move(from); 79 | } 80 | 81 | inline RpcHeader& operator=(const RpcHeader& from) { 82 | CopyFrom(from); 83 | return *this; 84 | } 85 | inline RpcHeader& operator=(RpcHeader&& from) noexcept { 86 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 87 | if (this != &from) InternalSwap(&from); 88 | } else { 89 | CopyFrom(from); 90 | } 91 | return *this; 92 | } 93 | 94 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 95 | return GetDescriptor(); 96 | } 97 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 98 | return GetMetadataStatic().descriptor; 99 | } 100 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 101 | return GetMetadataStatic().reflection; 102 | } 103 | static const RpcHeader& default_instance(); 104 | 105 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 106 | static inline const RpcHeader* internal_default_instance() { 107 | return reinterpret_cast( 108 | &_RpcHeader_default_instance_); 109 | } 110 | static constexpr int kIndexInFileMessages = 111 | 0; 112 | 113 | friend void swap(RpcHeader& a, RpcHeader& b) { 114 | a.Swap(&b); 115 | } 116 | inline void Swap(RpcHeader* other) { 117 | if (other == this) return; 118 | InternalSwap(other); 119 | } 120 | 121 | // implements Message ---------------------------------------------- 122 | 123 | inline RpcHeader* New() const final { 124 | return CreateMaybeMessage(nullptr); 125 | } 126 | 127 | RpcHeader* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 128 | return CreateMaybeMessage(arena); 129 | } 130 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 131 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 132 | void CopyFrom(const RpcHeader& from); 133 | void MergeFrom(const RpcHeader& from); 134 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 135 | bool IsInitialized() const final; 136 | 137 | size_t ByteSizeLong() const final; 138 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 139 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 140 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 141 | int GetCachedSize() const final { return _cached_size_.Get(); } 142 | 143 | private: 144 | inline void SharedCtor(); 145 | inline void SharedDtor(); 146 | void SetCachedSize(int size) const final; 147 | void InternalSwap(RpcHeader* other); 148 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 149 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 150 | return "mprpc.RpcHeader"; 151 | } 152 | private: 153 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 154 | return nullptr; 155 | } 156 | inline void* MaybeArenaPtr() const { 157 | return nullptr; 158 | } 159 | public: 160 | 161 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 162 | private: 163 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 164 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_rpcheader_2eproto); 165 | return ::descriptor_table_rpcheader_2eproto.file_level_metadata[kIndexInFileMessages]; 166 | } 167 | 168 | public: 169 | 170 | // nested types ---------------------------------------------------- 171 | 172 | // accessors ------------------------------------------------------- 173 | 174 | enum : int { 175 | kServiceNameFieldNumber = 1, 176 | kMethodNameFieldNumber = 2, 177 | kArgsSizeFieldNumber = 3, 178 | }; 179 | // bytes service_name = 1; 180 | void clear_service_name(); 181 | const std::string& service_name() const; 182 | void set_service_name(const std::string& value); 183 | void set_service_name(std::string&& value); 184 | void set_service_name(const char* value); 185 | void set_service_name(const void* value, size_t size); 186 | std::string* mutable_service_name(); 187 | std::string* release_service_name(); 188 | void set_allocated_service_name(std::string* service_name); 189 | private: 190 | const std::string& _internal_service_name() const; 191 | void _internal_set_service_name(const std::string& value); 192 | std::string* _internal_mutable_service_name(); 193 | public: 194 | 195 | // bytes method_name = 2; 196 | void clear_method_name(); 197 | const std::string& method_name() const; 198 | void set_method_name(const std::string& value); 199 | void set_method_name(std::string&& value); 200 | void set_method_name(const char* value); 201 | void set_method_name(const void* value, size_t size); 202 | std::string* mutable_method_name(); 203 | std::string* release_method_name(); 204 | void set_allocated_method_name(std::string* method_name); 205 | private: 206 | const std::string& _internal_method_name() const; 207 | void _internal_set_method_name(const std::string& value); 208 | std::string* _internal_mutable_method_name(); 209 | public: 210 | 211 | // uint32 args_size = 3; 212 | void clear_args_size(); 213 | ::PROTOBUF_NAMESPACE_ID::uint32 args_size() const; 214 | void set_args_size(::PROTOBUF_NAMESPACE_ID::uint32 value); 215 | private: 216 | ::PROTOBUF_NAMESPACE_ID::uint32 _internal_args_size() const; 217 | void _internal_set_args_size(::PROTOBUF_NAMESPACE_ID::uint32 value); 218 | public: 219 | 220 | // @@protoc_insertion_point(class_scope:mprpc.RpcHeader) 221 | private: 222 | class _Internal; 223 | 224 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 225 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr service_name_; 226 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr method_name_; 227 | ::PROTOBUF_NAMESPACE_ID::uint32 args_size_; 228 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 229 | friend struct ::TableStruct_rpcheader_2eproto; 230 | }; 231 | // =================================================================== 232 | 233 | 234 | // =================================================================== 235 | 236 | #ifdef __GNUC__ 237 | #pragma GCC diagnostic push 238 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" 239 | #endif // __GNUC__ 240 | // RpcHeader 241 | 242 | // bytes service_name = 1; 243 | inline void RpcHeader::clear_service_name() { 244 | service_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 245 | } 246 | inline const std::string& RpcHeader::service_name() const { 247 | // @@protoc_insertion_point(field_get:mprpc.RpcHeader.service_name) 248 | return _internal_service_name(); 249 | } 250 | inline void RpcHeader::set_service_name(const std::string& value) { 251 | _internal_set_service_name(value); 252 | // @@protoc_insertion_point(field_set:mprpc.RpcHeader.service_name) 253 | } 254 | inline std::string* RpcHeader::mutable_service_name() { 255 | // @@protoc_insertion_point(field_mutable:mprpc.RpcHeader.service_name) 256 | return _internal_mutable_service_name(); 257 | } 258 | inline const std::string& RpcHeader::_internal_service_name() const { 259 | return service_name_.GetNoArena(); 260 | } 261 | inline void RpcHeader::_internal_set_service_name(const std::string& value) { 262 | 263 | service_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 264 | } 265 | inline void RpcHeader::set_service_name(std::string&& value) { 266 | 267 | service_name_.SetNoArena( 268 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 269 | // @@protoc_insertion_point(field_set_rvalue:mprpc.RpcHeader.service_name) 270 | } 271 | inline void RpcHeader::set_service_name(const char* value) { 272 | GOOGLE_DCHECK(value != nullptr); 273 | 274 | service_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 275 | // @@protoc_insertion_point(field_set_char:mprpc.RpcHeader.service_name) 276 | } 277 | inline void RpcHeader::set_service_name(const void* value, size_t size) { 278 | 279 | service_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 280 | ::std::string(reinterpret_cast(value), size)); 281 | // @@protoc_insertion_point(field_set_pointer:mprpc.RpcHeader.service_name) 282 | } 283 | inline std::string* RpcHeader::_internal_mutable_service_name() { 284 | 285 | return service_name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 286 | } 287 | inline std::string* RpcHeader::release_service_name() { 288 | // @@protoc_insertion_point(field_release:mprpc.RpcHeader.service_name) 289 | 290 | return service_name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 291 | } 292 | inline void RpcHeader::set_allocated_service_name(std::string* service_name) { 293 | if (service_name != nullptr) { 294 | 295 | } else { 296 | 297 | } 298 | service_name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), service_name); 299 | // @@protoc_insertion_point(field_set_allocated:mprpc.RpcHeader.service_name) 300 | } 301 | 302 | // bytes method_name = 2; 303 | inline void RpcHeader::clear_method_name() { 304 | method_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 305 | } 306 | inline const std::string& RpcHeader::method_name() const { 307 | // @@protoc_insertion_point(field_get:mprpc.RpcHeader.method_name) 308 | return _internal_method_name(); 309 | } 310 | inline void RpcHeader::set_method_name(const std::string& value) { 311 | _internal_set_method_name(value); 312 | // @@protoc_insertion_point(field_set:mprpc.RpcHeader.method_name) 313 | } 314 | inline std::string* RpcHeader::mutable_method_name() { 315 | // @@protoc_insertion_point(field_mutable:mprpc.RpcHeader.method_name) 316 | return _internal_mutable_method_name(); 317 | } 318 | inline const std::string& RpcHeader::_internal_method_name() const { 319 | return method_name_.GetNoArena(); 320 | } 321 | inline void RpcHeader::_internal_set_method_name(const std::string& value) { 322 | 323 | method_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 324 | } 325 | inline void RpcHeader::set_method_name(std::string&& value) { 326 | 327 | method_name_.SetNoArena( 328 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 329 | // @@protoc_insertion_point(field_set_rvalue:mprpc.RpcHeader.method_name) 330 | } 331 | inline void RpcHeader::set_method_name(const char* value) { 332 | GOOGLE_DCHECK(value != nullptr); 333 | 334 | method_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 335 | // @@protoc_insertion_point(field_set_char:mprpc.RpcHeader.method_name) 336 | } 337 | inline void RpcHeader::set_method_name(const void* value, size_t size) { 338 | 339 | method_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 340 | ::std::string(reinterpret_cast(value), size)); 341 | // @@protoc_insertion_point(field_set_pointer:mprpc.RpcHeader.method_name) 342 | } 343 | inline std::string* RpcHeader::_internal_mutable_method_name() { 344 | 345 | return method_name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 346 | } 347 | inline std::string* RpcHeader::release_method_name() { 348 | // @@protoc_insertion_point(field_release:mprpc.RpcHeader.method_name) 349 | 350 | return method_name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 351 | } 352 | inline void RpcHeader::set_allocated_method_name(std::string* method_name) { 353 | if (method_name != nullptr) { 354 | 355 | } else { 356 | 357 | } 358 | method_name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), method_name); 359 | // @@protoc_insertion_point(field_set_allocated:mprpc.RpcHeader.method_name) 360 | } 361 | 362 | // uint32 args_size = 3; 363 | inline void RpcHeader::clear_args_size() { 364 | args_size_ = 0u; 365 | } 366 | inline ::PROTOBUF_NAMESPACE_ID::uint32 RpcHeader::_internal_args_size() const { 367 | return args_size_; 368 | } 369 | inline ::PROTOBUF_NAMESPACE_ID::uint32 RpcHeader::args_size() const { 370 | // @@protoc_insertion_point(field_get:mprpc.RpcHeader.args_size) 371 | return _internal_args_size(); 372 | } 373 | inline void RpcHeader::_internal_set_args_size(::PROTOBUF_NAMESPACE_ID::uint32 value) { 374 | 375 | args_size_ = value; 376 | } 377 | inline void RpcHeader::set_args_size(::PROTOBUF_NAMESPACE_ID::uint32 value) { 378 | _internal_set_args_size(value); 379 | // @@protoc_insertion_point(field_set:mprpc.RpcHeader.args_size) 380 | } 381 | 382 | #ifdef __GNUC__ 383 | #pragma GCC diagnostic pop 384 | #endif // __GNUC__ 385 | 386 | // @@protoc_insertion_point(namespace_scope) 387 | 388 | } // namespace mprpc 389 | 390 | // @@protoc_insertion_point(global_scope) 391 | 392 | #include 393 | #endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_rpcheader_2eproto 394 | -------------------------------------------------------------------------------- /src/include/rpcprovider.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "zookeeperutil.h" 16 | 17 | // 框架提供的专门发布rpc服务的网络对象类 18 | class RpcProvider { 19 | public: 20 | RpcProvider(); 21 | ~RpcProvider(); 22 | 23 | void NotifyService(google::protobuf::Service* service); 24 | 25 | // 启动rpc服务节点 26 | void Run(); 27 | 28 | private: 29 | void OnConnection(const muduo::net::TcpConnectionPtr& conn); 30 | void OnMessage(const muduo::net::TcpConnectionPtr&, muduo::net::Buffer*, muduo::Timestamp); 31 | 32 | // Closure的回调操作,用于序列化rpc的响应和网络发送 33 | void SendRpcResponse(const muduo::net::TcpConnectionPtr&, google::protobuf::Message*); 34 | 35 | private: 36 | std::unique_ptr m_zkClientPtr; 37 | 38 | std::unique_ptr m_serverPtr; 39 | muduo::net::EventLoop* m_eventLoop; 40 | 41 | // service服务类型 42 | struct ServiceInfo { 43 | google::protobuf::Service* m_service; // 保存服务对象 44 | std::unordered_map m_methodMap; // 保存服务方法 45 | }; 46 | 47 | // 存储注册成功的服务对象和其服务方法的所有信息 48 | std::unordered_map m_serviceMap; 49 | }; -------------------------------------------------------------------------------- /src/include/zookeeperutil.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // 封装的zk客户端类 8 | class ZkClient 9 | { 10 | public: 11 | ZkClient(); 12 | ~ZkClient(); 13 | // zkclient启动连接zkserver 14 | void Start(const std::string& host, const std::string& port); 15 | // 在zkserver上根据指定的path创建znode节点 16 | void Create(const char *path, const char *data, int datalen, int state=0); 17 | // 根据参数指定的znode节点路径,或者znode节点的值 18 | std::string GetData(const char *path); 19 | private: 20 | // zk的客户端句柄 21 | zhandle_t *m_zhandle; 22 | }; -------------------------------------------------------------------------------- /src/logger.cc: -------------------------------------------------------------------------------- 1 | #include "logger.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | static std::unordered_map g_logLevelMap = 8 | { 9 | {TRACE, "TRACE"}, 10 | {DEBUG, "DEBUG"}, 11 | {INFO, "INFO"}, 12 | {WARN, "WARN"}, 13 | {ERROR, "ERROR"}, 14 | {FATAL, "FATAL"} 15 | }; 16 | 17 | static std::unordered_map g_logLevelStrMap = 18 | { 19 | {"TRACE", TRACE}, 20 | {"DEBUG", DEBUG}, 21 | {"INFO", INFO}, 22 | {"WARN", WARN}, 23 | {"ERROR", ERROR}, 24 | {"FATAL", FATAL} 25 | }; 26 | 27 | std::string MakeLogMsgHeader(int level) { 28 | time_t now = time(nullptr); 29 | tm *nowtm = localtime(&now); 30 | char time_buf[128] = "\0"; 31 | sprintf(time_buf, "%d-%d-%d %d:%d:%d [%s]", 32 | nowtm->tm_year+1900, 33 | nowtm->tm_mon+1, 34 | nowtm->tm_mday, 35 | nowtm->tm_hour, 36 | nowtm->tm_min, 37 | nowtm->tm_sec, 38 | g_logLevelMap.at(level).c_str()); 39 | return time_buf; 40 | } 41 | 42 | Logger& Logger::GetInstance() { 43 | static Logger logger; 44 | return logger; 45 | } 46 | 47 | Logger::Logger() { 48 | // 默认日志等级是INFO 49 | m_loglevel = INFO; 50 | m_tag = "DEFAULT"; 51 | m_asyncLogging = nullptr; 52 | } 53 | 54 | Logger::~Logger() { 55 | if (m_asyncLogging != nullptr) { 56 | delete m_asyncLogging; 57 | } 58 | } 59 | 60 | void Logger::Init(const std::string& path) { 61 | m_logConfig.LoadConfigFile(path.c_str()); 62 | 63 | m_tag = m_logConfig.Load("logtag"); 64 | std::string out_dir = m_logConfig.Load("outdir"); 65 | if (out_dir != "") { 66 | m_asyncLogging = new AsyncLogging(); 67 | m_asyncLogging->SetOutDir(out_dir); 68 | m_asyncLogging->Init(); 69 | } 70 | } 71 | 72 | void Logger::SetLogLevel(LogLevel level) { 73 | m_loglevel = level; 74 | } 75 | 76 | void Logger::SetLogLevel(const std::string& level) { 77 | auto it = g_logLevelStrMap.find(level); 78 | if (it == g_logLevelStrMap.end()) { 79 | std::cout << "< set log level is error! > level: " 80 | << "[ INFO|ERROR ]" << std::endl; 81 | exit(EXIT_FAILURE); 82 | } 83 | } 84 | 85 | // 前端线程往queue中写日志 86 | void Logger::Log(const std::string& msg) { 87 | // 输出到标准输出 88 | printf("%s\n", msg.c_str()); 89 | if (m_asyncLogging != nullptr) { 90 | m_asyncLogging->Log(msg); 91 | } 92 | } -------------------------------------------------------------------------------- /src/mprpcapplication.cc: -------------------------------------------------------------------------------- 1 | #include "mprpcapplication.h" 2 | #include "logger.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | MprpcConfig MprpcApplication::m_config; 9 | 10 | void ShowArgHelp() { 11 | std::cout << "fomat: commid -i " << std::endl; 12 | } 13 | 14 | void MprpcApplication::Init(int argc, char **argv) { 15 | if (argc < 2) { 16 | ShowArgHelp(); 17 | exit(EXIT_FAILURE); 18 | } 19 | 20 | int c = 0; 21 | std::string config_file; 22 | while ((c = getopt(argc, argv, "i:")) != -1) { 23 | switch (c) { 24 | case 'i': 25 | config_file = optarg; 26 | break; 27 | case '?': 28 | ShowArgHelp(); 29 | exit(EXIT_FAILURE); 30 | case ':': 31 | ShowArgHelp(); 32 | exit(EXIT_FAILURE); 33 | default: 34 | break; 35 | } 36 | } 37 | 38 | // 加载配置文件 39 | m_config.LoadConfigFile(config_file.c_str()); 40 | 41 | // std::cout << "[ config info: ]" << std::endl; 42 | // std::cout << "rpcserverip: " << m_config.Load("rpcserverip") << std::endl; 43 | // std::cout << "rpcserverport: " << m_config.Load("rpcserverport") << std::endl; 44 | // std::cout << "zookeeperip: " << m_config.Load("zookeeperip") << std::endl; 45 | // std::cout << "zookeeperport: " << m_config.Load("zookeeperport") << std::endl; 46 | } 47 | 48 | MprpcApplication& MprpcApplication::GetInstance() { 49 | static MprpcApplication app; 50 | return app; 51 | } 52 | 53 | MprpcConfig& MprpcApplication::GetConfig() { 54 | return m_config; 55 | } -------------------------------------------------------------------------------- /src/mprpcchannel.cc: -------------------------------------------------------------------------------- 1 | #include "mprpcchannel.h" 2 | #include "rpcheader.pb.h" 3 | #include "mprpcapplication.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void MprpcChannel::CallMethod(const google::protobuf::MethodDescriptor* method, 14 | google::protobuf::RpcController* controller, 15 | const google::protobuf::Message* request, 16 | google::protobuf::Message* response, 17 | google::protobuf::Closure* done) 18 | { 19 | std::string service_name = method->service()->name(); 20 | std::string method_name = method->name(); 21 | 22 | uint32_t args_size = 0; 23 | std::string args_str; 24 | if (request->SerializeToString(&args_str)) { 25 | args_size = args_str.size(); 26 | } else { 27 | std::cout << "serialize request error" << std::endl; 28 | return; 29 | } 30 | 31 | mprpc::RpcHeader rpcHeader; 32 | rpcHeader.set_service_name(service_name); 33 | rpcHeader.set_method_name(method_name); 34 | rpcHeader.set_args_size(args_size); 35 | 36 | uint32_t header_size = 0; 37 | std::string rpc_header_str; 38 | if (rpcHeader.SerializeToString(&rpc_header_str)) { 39 | header_size = rpc_header_str.size(); 40 | } else { 41 | controller->SetFailed("serialize rpc header error"); 42 | return; 43 | } 44 | 45 | // 组织带发送的rpc请求字符串 46 | std::string send_rpc_str; 47 | send_rpc_str.insert(0, std::string((char*)&header_size, 4)); // header_size 48 | send_rpc_str += rpc_header_str; // rpcheader 49 | send_rpc_str += args_str; // args // args_str 50 | 51 | // tcp编程, 创建socket 52 | 53 | int clientfd = socket(AF_INET, SOCK_STREAM, 0); 54 | if (-1 == clientfd) { 55 | controller->SetFailed("create socket error"); 56 | return; 57 | } 58 | 59 | // std::string ip = MprpcApplication::GetInstance().GetConfig().Load("rpcserverip"); 60 | // uint16_t port = atoi(MprpcApplication::GetInstance().GetConfig().Load("rpcserverport").c_str()); 61 | std::string zk_host = MprpcApplication::GetInstance().GetConfig().Load("zookeeperip"); 62 | std::string zk_port = MprpcApplication::GetInstance().GetConfig().Load("zookeeperport"); 63 | m_zkClientPtr = std::make_unique(); 64 | m_zkClientPtr->Start(zk_host, zk_port); 65 | // /UserServiceRpc/Login 66 | std::string method_path = "/" + service_name + "/" + method_name; 67 | // 127.0.0.1:8000 68 | std::string host_data = m_zkClientPtr->GetData(method_path.c_str()); 69 | if (host_data == "") 70 | { 71 | controller->SetFailed(method_path + " is not exist!"); 72 | return; 73 | } 74 | int idx = host_data.find(":"); 75 | if (idx == -1) 76 | { 77 | controller->SetFailed(method_path + " address is invalid!"); 78 | return; 79 | } 80 | std::string ip = host_data.substr(0, idx); 81 | uint16_t port = atoi(host_data.substr(idx+1, host_data.size()-idx).c_str()); 82 | 83 | struct sockaddr_in server_addr; 84 | server_addr.sin_family = AF_INET; 85 | server_addr.sin_port = htons(port); 86 | server_addr.sin_addr.s_addr = inet_addr(ip.c_str()); 87 | 88 | if (-1 == connect(clientfd, (struct sockaddr*)&server_addr, sizeof(server_addr))) { 89 | char errtxt[512] = "\0"; 90 | sprintf(errtxt, "connet error! error: %d", errno); 91 | controller->SetFailed(errtxt); 92 | close(clientfd); 93 | return; 94 | } 95 | 96 | // 发送rpc请求 97 | if (-1 == send(clientfd, send_rpc_str.c_str(), send_rpc_str.size(), 0)) { 98 | char errtxt[512] = "\0"; 99 | sprintf(errtxt, "send error! error: %d", errno); 100 | controller->SetFailed(errtxt); 101 | close(clientfd); 102 | return; 103 | } 104 | 105 | // 接收rpc请求的响应值 106 | char recv_buf[1024] = "\0"; 107 | int recv_size = 0; 108 | if (-1 == (recv_size = recv(clientfd, recv_buf, 1024, 0))) { 109 | char errtxt[512] = "\0"; 110 | sprintf(errtxt, "recv error! error: %d", errno); 111 | controller->SetFailed(errtxt); 112 | close(clientfd); 113 | return; 114 | } 115 | 116 | std::string response_str(recv_buf, 0, recv_size); 117 | if (!response->ParseFromArray(recv_buf, recv_size)) { 118 | char errtxt[512] = "\0"; 119 | sprintf(errtxt, "parse error! recv_buf: %s", recv_buf); 120 | controller->SetFailed(errtxt); 121 | } 122 | close(clientfd); 123 | return; 124 | } -------------------------------------------------------------------------------- /src/mprpcconfig.cc: -------------------------------------------------------------------------------- 1 | #include "mprpcconfig.h" 2 | 3 | #include 4 | 5 | void MprpcConfig::Trim(std::string& src_buf) { 6 | int idx = src_buf.find_first_not_of(' '); 7 | if (idx != -1) { 8 | src_buf = src_buf.substr(idx, src_buf.size() - idx); 9 | } 10 | idx = src_buf.find_last_not_of(' '); 11 | if (idx != -1) { 12 | src_buf = src_buf.substr(0, idx + 1); 13 | } 14 | } 15 | 16 | // 加载解析配置文件 17 | void MprpcConfig::LoadConfigFile(const char* config_file) { 18 | FILE* pf = fopen(config_file, "r"); 19 | if (nullptr == pf) { 20 | std::cout << config_file << "is note exist!" << std::endl; 21 | exit(EXIT_FAILURE); 22 | } 23 | 24 | while (!feof(pf)) { 25 | char buf[512] = "\0"; 26 | fgets(buf, 512, pf); 27 | 28 | std::string src_buf(buf); 29 | 30 | Trim(src_buf); 31 | 32 | // 判断# 33 | if (src_buf[0] == '#' || src_buf.empty()) { 34 | continue; 35 | } 36 | 37 | // 解析配置项 38 | int idx = src_buf.find('='); 39 | if (idx == -1) { 40 | continue; 41 | } 42 | 43 | std::string key; 44 | std::string value; 45 | key = src_buf.substr(0, idx); 46 | Trim(key); 47 | int endidx = src_buf.find('\n', idx); 48 | value = src_buf.substr(idx + 1, endidx - idx - 1); 49 | Trim(value); 50 | m_configMap.insert({key, value}); 51 | } 52 | fclose(pf); 53 | } 54 | 55 | std::string MprpcConfig::Load(const std::string& key) { 56 | auto it = m_configMap.find(key); 57 | if (it == m_configMap.end()) { 58 | return ""; 59 | } 60 | 61 | return it->second; 62 | } -------------------------------------------------------------------------------- /src/mprpccontroller.cc: -------------------------------------------------------------------------------- 1 | #include "mprpccontroller.h" 2 | 3 | MprpcController::MprpcController() { 4 | m_failed = false; 5 | m_errText = ""; 6 | } 7 | 8 | void MprpcController::Reset() { 9 | m_failed = false; 10 | m_errText = ""; 11 | } 12 | bool MprpcController::Failed() const { 13 | return m_failed; 14 | } 15 | std::string MprpcController::ErrorText() const { 16 | return m_errText; 17 | } 18 | void MprpcController::SetFailed(const std::string& reason) { 19 | m_failed = true; 20 | m_errText = reason; 21 | } 22 | 23 | // 目前未实现的功能 24 | void MprpcController::StartCancel() {} 25 | bool MprpcController::IsCanceled() const { return false; } 26 | void MprpcController::NotifyOnCancel(google::protobuf::Closure* callback) {} 27 | -------------------------------------------------------------------------------- /src/rpcheader.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: rpcheader.proto 3 | 4 | #include "rpcheader.pb.h" 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | // @@protoc_insertion_point(includes) 16 | #include 17 | namespace mprpc { 18 | class RpcHeaderDefaultTypeInternal { 19 | public: 20 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 21 | } _RpcHeader_default_instance_; 22 | } // namespace mprpc 23 | static void InitDefaultsscc_info_RpcHeader_rpcheader_2eproto() { 24 | GOOGLE_PROTOBUF_VERIFY_VERSION; 25 | 26 | { 27 | void* ptr = &::mprpc::_RpcHeader_default_instance_; 28 | new (ptr) ::mprpc::RpcHeader(); 29 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 30 | } 31 | ::mprpc::RpcHeader::InitAsDefaultInstance(); 32 | } 33 | 34 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_RpcHeader_rpcheader_2eproto = 35 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_RpcHeader_rpcheader_2eproto}, {}}; 36 | 37 | static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_rpcheader_2eproto[1]; 38 | static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_rpcheader_2eproto = nullptr; 39 | static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_rpcheader_2eproto = nullptr; 40 | 41 | const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_rpcheader_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 42 | ~0u, // no _has_bits_ 43 | PROTOBUF_FIELD_OFFSET(::mprpc::RpcHeader, _internal_metadata_), 44 | ~0u, // no _extensions_ 45 | ~0u, // no _oneof_case_ 46 | ~0u, // no _weak_field_map_ 47 | PROTOBUF_FIELD_OFFSET(::mprpc::RpcHeader, service_name_), 48 | PROTOBUF_FIELD_OFFSET(::mprpc::RpcHeader, method_name_), 49 | PROTOBUF_FIELD_OFFSET(::mprpc::RpcHeader, args_size_), 50 | }; 51 | static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 52 | { 0, -1, sizeof(::mprpc::RpcHeader)}, 53 | }; 54 | 55 | static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { 56 | reinterpret_cast(&::mprpc::_RpcHeader_default_instance_), 57 | }; 58 | 59 | const char descriptor_table_protodef_rpcheader_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = 60 | "\n\017rpcheader.proto\022\005mprpc\"I\n\tRpcHeader\022\024\n" 61 | "\014service_name\030\001 \001(\014\022\023\n\013method_name\030\002 \001(\014" 62 | "\022\021\n\targs_size\030\003 \001(\rb\006proto3" 63 | ; 64 | static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_rpcheader_2eproto_deps[1] = { 65 | }; 66 | static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_rpcheader_2eproto_sccs[1] = { 67 | &scc_info_RpcHeader_rpcheader_2eproto.base, 68 | }; 69 | static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_rpcheader_2eproto_once; 70 | static bool descriptor_table_rpcheader_2eproto_initialized = false; 71 | const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_rpcheader_2eproto = { 72 | &descriptor_table_rpcheader_2eproto_initialized, descriptor_table_protodef_rpcheader_2eproto, "rpcheader.proto", 107, 73 | &descriptor_table_rpcheader_2eproto_once, descriptor_table_rpcheader_2eproto_sccs, descriptor_table_rpcheader_2eproto_deps, 1, 0, 74 | schemas, file_default_instances, TableStruct_rpcheader_2eproto::offsets, 75 | file_level_metadata_rpcheader_2eproto, 1, file_level_enum_descriptors_rpcheader_2eproto, file_level_service_descriptors_rpcheader_2eproto, 76 | }; 77 | 78 | // Force running AddDescriptors() at dynamic initialization time. 79 | static bool dynamic_init_dummy_rpcheader_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_rpcheader_2eproto), true); 80 | namespace mprpc { 81 | 82 | // =================================================================== 83 | 84 | void RpcHeader::InitAsDefaultInstance() { 85 | } 86 | class RpcHeader::_Internal { 87 | public: 88 | }; 89 | 90 | RpcHeader::RpcHeader() 91 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 92 | SharedCtor(); 93 | // @@protoc_insertion_point(constructor:mprpc.RpcHeader) 94 | } 95 | RpcHeader::RpcHeader(const RpcHeader& from) 96 | : ::PROTOBUF_NAMESPACE_ID::Message(), 97 | _internal_metadata_(nullptr) { 98 | _internal_metadata_.MergeFrom(from._internal_metadata_); 99 | service_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 100 | if (!from._internal_service_name().empty()) { 101 | service_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.service_name_); 102 | } 103 | method_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 104 | if (!from._internal_method_name().empty()) { 105 | method_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.method_name_); 106 | } 107 | args_size_ = from.args_size_; 108 | // @@protoc_insertion_point(copy_constructor:mprpc.RpcHeader) 109 | } 110 | 111 | void RpcHeader::SharedCtor() { 112 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_RpcHeader_rpcheader_2eproto.base); 113 | service_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 114 | method_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 115 | args_size_ = 0u; 116 | } 117 | 118 | RpcHeader::~RpcHeader() { 119 | // @@protoc_insertion_point(destructor:mprpc.RpcHeader) 120 | SharedDtor(); 121 | } 122 | 123 | void RpcHeader::SharedDtor() { 124 | service_name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 125 | method_name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 126 | } 127 | 128 | void RpcHeader::SetCachedSize(int size) const { 129 | _cached_size_.Set(size); 130 | } 131 | const RpcHeader& RpcHeader::default_instance() { 132 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_RpcHeader_rpcheader_2eproto.base); 133 | return *internal_default_instance(); 134 | } 135 | 136 | 137 | void RpcHeader::Clear() { 138 | // @@protoc_insertion_point(message_clear_start:mprpc.RpcHeader) 139 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 140 | // Prevent compiler warnings about cached_has_bits being unused 141 | (void) cached_has_bits; 142 | 143 | service_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 144 | method_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 145 | args_size_ = 0u; 146 | _internal_metadata_.Clear(); 147 | } 148 | 149 | const char* RpcHeader::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 150 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 151 | while (!ctx->Done(&ptr)) { 152 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 153 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 154 | CHK_(ptr); 155 | switch (tag >> 3) { 156 | // bytes service_name = 1; 157 | case 1: 158 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { 159 | auto str = _internal_mutable_service_name(); 160 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 161 | CHK_(ptr); 162 | } else goto handle_unusual; 163 | continue; 164 | // bytes method_name = 2; 165 | case 2: 166 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { 167 | auto str = _internal_mutable_method_name(); 168 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 169 | CHK_(ptr); 170 | } else goto handle_unusual; 171 | continue; 172 | // uint32 args_size = 3; 173 | case 3: 174 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { 175 | args_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); 176 | CHK_(ptr); 177 | } else goto handle_unusual; 178 | continue; 179 | default: { 180 | handle_unusual: 181 | if ((tag & 7) == 4 || tag == 0) { 182 | ctx->SetLastTag(tag); 183 | goto success; 184 | } 185 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 186 | CHK_(ptr != nullptr); 187 | continue; 188 | } 189 | } // switch 190 | } // while 191 | success: 192 | return ptr; 193 | failure: 194 | ptr = nullptr; 195 | goto success; 196 | #undef CHK_ 197 | } 198 | 199 | ::PROTOBUF_NAMESPACE_ID::uint8* RpcHeader::_InternalSerialize( 200 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 201 | // @@protoc_insertion_point(serialize_to_array_start:mprpc.RpcHeader) 202 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 203 | (void) cached_has_bits; 204 | 205 | // bytes service_name = 1; 206 | if (this->service_name().size() > 0) { 207 | target = stream->WriteBytesMaybeAliased( 208 | 1, this->_internal_service_name(), target); 209 | } 210 | 211 | // bytes method_name = 2; 212 | if (this->method_name().size() > 0) { 213 | target = stream->WriteBytesMaybeAliased( 214 | 2, this->_internal_method_name(), target); 215 | } 216 | 217 | // uint32 args_size = 3; 218 | if (this->args_size() != 0) { 219 | target = stream->EnsureSpace(target); 220 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt32ToArray(3, this->_internal_args_size(), target); 221 | } 222 | 223 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 224 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 225 | _internal_metadata_.unknown_fields(), target, stream); 226 | } 227 | // @@protoc_insertion_point(serialize_to_array_end:mprpc.RpcHeader) 228 | return target; 229 | } 230 | 231 | size_t RpcHeader::ByteSizeLong() const { 232 | // @@protoc_insertion_point(message_byte_size_start:mprpc.RpcHeader) 233 | size_t total_size = 0; 234 | 235 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 236 | // Prevent compiler warnings about cached_has_bits being unused 237 | (void) cached_has_bits; 238 | 239 | // bytes service_name = 1; 240 | if (this->service_name().size() > 0) { 241 | total_size += 1 + 242 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( 243 | this->_internal_service_name()); 244 | } 245 | 246 | // bytes method_name = 2; 247 | if (this->method_name().size() > 0) { 248 | total_size += 1 + 249 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( 250 | this->_internal_method_name()); 251 | } 252 | 253 | // uint32 args_size = 3; 254 | if (this->args_size() != 0) { 255 | total_size += 1 + 256 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt32Size( 257 | this->_internal_args_size()); 258 | } 259 | 260 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 261 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 262 | _internal_metadata_, total_size, &_cached_size_); 263 | } 264 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 265 | SetCachedSize(cached_size); 266 | return total_size; 267 | } 268 | 269 | void RpcHeader::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 270 | // @@protoc_insertion_point(generalized_merge_from_start:mprpc.RpcHeader) 271 | GOOGLE_DCHECK_NE(&from, this); 272 | const RpcHeader* source = 273 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 274 | &from); 275 | if (source == nullptr) { 276 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:mprpc.RpcHeader) 277 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 278 | } else { 279 | // @@protoc_insertion_point(generalized_merge_from_cast_success:mprpc.RpcHeader) 280 | MergeFrom(*source); 281 | } 282 | } 283 | 284 | void RpcHeader::MergeFrom(const RpcHeader& from) { 285 | // @@protoc_insertion_point(class_specific_merge_from_start:mprpc.RpcHeader) 286 | GOOGLE_DCHECK_NE(&from, this); 287 | _internal_metadata_.MergeFrom(from._internal_metadata_); 288 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 289 | (void) cached_has_bits; 290 | 291 | if (from.service_name().size() > 0) { 292 | 293 | service_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.service_name_); 294 | } 295 | if (from.method_name().size() > 0) { 296 | 297 | method_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.method_name_); 298 | } 299 | if (from.args_size() != 0) { 300 | _internal_set_args_size(from._internal_args_size()); 301 | } 302 | } 303 | 304 | void RpcHeader::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 305 | // @@protoc_insertion_point(generalized_copy_from_start:mprpc.RpcHeader) 306 | if (&from == this) return; 307 | Clear(); 308 | MergeFrom(from); 309 | } 310 | 311 | void RpcHeader::CopyFrom(const RpcHeader& from) { 312 | // @@protoc_insertion_point(class_specific_copy_from_start:mprpc.RpcHeader) 313 | if (&from == this) return; 314 | Clear(); 315 | MergeFrom(from); 316 | } 317 | 318 | bool RpcHeader::IsInitialized() const { 319 | return true; 320 | } 321 | 322 | void RpcHeader::InternalSwap(RpcHeader* other) { 323 | using std::swap; 324 | _internal_metadata_.Swap(&other->_internal_metadata_); 325 | service_name_.Swap(&other->service_name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 326 | GetArenaNoVirtual()); 327 | method_name_.Swap(&other->method_name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 328 | GetArenaNoVirtual()); 329 | swap(args_size_, other->args_size_); 330 | } 331 | 332 | ::PROTOBUF_NAMESPACE_ID::Metadata RpcHeader::GetMetadata() const { 333 | return GetMetadataStatic(); 334 | } 335 | 336 | 337 | // @@protoc_insertion_point(namespace_scope) 338 | } // namespace mprpc 339 | PROTOBUF_NAMESPACE_OPEN 340 | template<> PROTOBUF_NOINLINE ::mprpc::RpcHeader* Arena::CreateMaybeMessage< ::mprpc::RpcHeader >(Arena* arena) { 341 | return Arena::CreateInternal< ::mprpc::RpcHeader >(arena); 342 | } 343 | PROTOBUF_NAMESPACE_CLOSE 344 | 345 | // @@protoc_insertion_point(global_scope) 346 | #include 347 | -------------------------------------------------------------------------------- /src/rpcheader.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package mprpc; 4 | 5 | message RpcHeader 6 | { 7 | bytes service_name = 1; 8 | bytes method_name = 2; 9 | uint32 args_size = 3; 10 | } -------------------------------------------------------------------------------- /src/rpcprovider.cc: -------------------------------------------------------------------------------- 1 | #include "rpcprovider.h" 2 | #include "mprpcapplication.h" 3 | #include "rpcheader.pb.h" 4 | 5 | RpcProvider::RpcProvider() 6 | : m_eventLoop(nullptr) 7 | { 8 | } 9 | 10 | RpcProvider::~RpcProvider() { 11 | if (m_eventLoop) { 12 | delete m_eventLoop; 13 | } 14 | } 15 | 16 | void RpcProvider::NotifyService(google::protobuf::Service* service) { 17 | 18 | ServiceInfo service_info; 19 | // 获取服务对象的描述信息 20 | const google::protobuf::ServiceDescriptor* pserviceDesc = service->GetDescriptor(); 21 | // 获取服务名字 22 | std::string service_name = pserviceDesc->name(); 23 | // 获取服务对象方法的数量 24 | int methodCot = pserviceDesc->method_count(); 25 | 26 | for (int i = 0; i < methodCot; i++) { 27 | const google::protobuf::MethodDescriptor* pmethodDesc = pserviceDesc->method(i); 28 | std::string method_name = pmethodDesc->name(); 29 | service_info.m_methodMap.insert({method_name, pmethodDesc}); 30 | } 31 | service_info.m_service = service; 32 | m_serviceMap.insert({service_name, service_info}); 33 | } 34 | 35 | // 启动rpc服务节点 36 | void RpcProvider::Run() { 37 | 38 | m_eventLoop = new muduo::net::EventLoop(); 39 | if (m_eventLoop == nullptr) { 40 | exit(EXIT_FAILURE); 41 | } 42 | 43 | std::string ip = MprpcApplication::GetInstance().GetConfig().Load("rpcserverip"); 44 | uint16_t port = atoi(MprpcApplication::GetInstance().GetConfig().Load("rpcserverport").c_str()); 45 | std::string zk_host = MprpcApplication::GetInstance().GetConfig().Load("zookeeperip"); 46 | std::string zk_port = MprpcApplication::GetInstance().GetConfig().Load("zookeeperport"); 47 | muduo::net::InetAddress address(ip, port); 48 | 49 | m_serverPtr = std::make_unique(m_eventLoop, address, "RpcProvider"); 50 | 51 | //绑定连接回调和消息读写回调 52 | m_serverPtr->setConnectionCallback(std::bind(&RpcProvider::OnConnection, this, std::placeholders::_1)); 53 | m_serverPtr->setMessageCallback(std::bind(&RpcProvider::OnMessage, this, 54 | std::placeholders::_1, 55 | std::placeholders::_2, 56 | std::placeholders::_3)); 57 | 58 | // 把当前rpc节点上要发布的服务全部注册到zk上面,让rpc client可以从zk上发现服务 59 | // session timeout 30s zkclient 网络I/O线程 1/3 * timeout 时间发送ping消息 60 | m_zkClientPtr = std::make_unique(); 61 | m_zkClientPtr->Start(zk_host, zk_port); 62 | // service_name为永久性节点 method_name为临时性节点 63 | for (auto &sp : m_serviceMap) 64 | { 65 | // /service_name /UserServiceRpc 66 | std::string service_path = "/" + sp.first; 67 | m_zkClientPtr->Create(service_path.c_str(), nullptr, 0); 68 | for (auto &mp : sp.second.m_methodMap) 69 | { 70 | // /service_name/method_name /UserServiceRpc/Login 存储当前这个rpc服务节点主机的ip和port 71 | std::string method_path = service_path + "/" + mp.first; 72 | char method_path_data[128] = {0}; 73 | sprintf(method_path_data, "%s:%d", ip.c_str(), port); 74 | // ZOO_EPHEMERAL表示znode是一个临时性节点 75 | m_zkClientPtr->Create(method_path.c_str(), method_path_data, strlen(method_path_data), ZOO_EPHEMERAL); 76 | } 77 | } 78 | 79 | // 设置线程数量 80 | m_serverPtr->setThreadNum(4); 81 | 82 | std::cout << "RpcProvider start service at ip: " << ip << ", port: " << port << std::endl; 83 | 84 | // 启动网络服务 85 | m_serverPtr->start(); 86 | m_eventLoop->loop(); 87 | } 88 | 89 | // new socket连接回调 90 | void RpcProvider::OnConnection(const muduo::net::TcpConnectionPtr& conn) { 91 | 92 | if (!conn->connected()) { 93 | conn->shutdown(); 94 | } 95 | } 96 | 97 | // 读写事件回调 98 | void RpcProvider::OnMessage(const muduo::net::TcpConnectionPtr& conn, 99 | muduo::net::Buffer* buffer, 100 | muduo::Timestamp) { 101 | 102 | std::string recv_buf = buffer->retrieveAllAsString(); 103 | 104 | // 读取4字节的头部大小 105 | uint32_t header_size = 0; 106 | recv_buf.copy((char*)&header_size, 4, 0); 107 | 108 | // 根据头部大小获取头部内容 109 | std::string rpc_header_str = recv_buf.substr(4, header_size); 110 | mprpc::RpcHeader rpcHeader; 111 | 112 | std::string service_name; 113 | std::string method_name; 114 | uint32_t args_size; 115 | if (rpcHeader.ParseFromString(rpc_header_str)) { 116 | // 反序列化成功 117 | service_name = rpcHeader.service_name(); 118 | method_name = rpcHeader.method_name(); 119 | args_size = rpcHeader.args_size(); 120 | } else { 121 | std::cout << "rpc_header_str: " << rpc_header_str << ", parse error!" << std::endl; 122 | return; 123 | } 124 | 125 | std::string args_str = recv_buf.substr(4 + header_size, args_size); 126 | 127 | 128 | // 获取service对象和method对象 129 | auto it = m_serviceMap.find(service_name); 130 | if (it == m_serviceMap.end()) { 131 | std::cout << service_name << " is not exist! " << std::endl; 132 | return; 133 | } 134 | 135 | auto mit = it->second.m_methodMap.find(method_name); 136 | if (mit == it->second.m_methodMap.end()) { 137 | std::cout << service_name << ":" << method_name << " is not exist! " << std::endl; 138 | return; 139 | } 140 | 141 | google::protobuf::Service* service = it->second.m_service; 142 | const google::protobuf::MethodDescriptor* method = mit->second; 143 | 144 | // 生成rpc方法调用请求request和响应response参数 145 | google::protobuf::Message* request = service->GetRequestPrototype(method).New(); 146 | if (!request->ParseFromString(args_str)) { 147 | std::cout << "request parse error, content: " << args_str << std::endl; 148 | return; 149 | } 150 | google::protobuf::Message* response = service->GetResponsePrototype(method).New(); 151 | 152 | // 给下面的method方法的调用。绑定一个Closure的回调函数 153 | google::protobuf::Closure* done = google::protobuf::NewCallback 156 | (this, &RpcProvider::SendRpcResponse, conn, response); 157 | 158 | // 根据远端rpc请求,调用rpc节点发布服务的方法 159 | service->CallMethod(method, nullptr, request, response, done); 160 | } 161 | 162 | // Closure的回调操作,用于序列化rpc的响应和网络发送 163 | void RpcProvider::SendRpcResponse(const muduo::net::TcpConnectionPtr& conn, google::protobuf::Message* response) { 164 | 165 | std::string response_str; 166 | if (response->SerializeToString(&response_str)) { 167 | conn->send(response_str); 168 | // 模拟http的短链接服务,由rpcprovider主动断开链接 169 | conn->shutdown(); 170 | } else { 171 | std::cout << "serialize response_str error!" << std::endl; 172 | } 173 | 174 | conn->shutdown(); 175 | } -------------------------------------------------------------------------------- /src/zookeeperutil.cc: -------------------------------------------------------------------------------- 1 | #include "zookeeperutil.h" 2 | #include "mprpcapplication.h" 3 | #include 4 | #include 5 | 6 | // 全局的watcher观察器 zkserver给zkclient的通知 7 | void global_watcher(zhandle_t *zh, int type, 8 | int state, const char *path, void *watcherCtx) 9 | { 10 | if (type == ZOO_SESSION_EVENT) // 回调的消息类型是和会话相关的消息类型 11 | { 12 | if (state == ZOO_CONNECTED_STATE) // zkclient和zkserver连接成功 13 | { 14 | sem_t *sem = (sem_t*)zoo_get_context(zh); 15 | sem_post(sem); 16 | } 17 | } 18 | } 19 | 20 | ZkClient::ZkClient() : m_zhandle(nullptr) 21 | { 22 | } 23 | 24 | ZkClient::~ZkClient() 25 | { 26 | if (m_zhandle != nullptr) 27 | { 28 | zookeeper_close(m_zhandle); // 关闭句柄,释放资源 MySQL_Conn 29 | } 30 | } 31 | 32 | // 连接zkserver 33 | void ZkClient::Start(const std::string& host, const std::string& port) 34 | { 35 | std::string connstr = host + ":" + port; 36 | 37 | /* 38 | zookeeper_mt:多线程版本 39 | zookeeper的API客户端程序提供了三个线程 40 | API调用线程 41 | 网络I/O线程 pthread_create poll 42 | watcher回调线程 pthread_create 43 | */ 44 | m_zhandle = zookeeper_init(connstr.c_str(), global_watcher, 30000, nullptr, nullptr, 0); 45 | if (nullptr == m_zhandle) 46 | { 47 | std::cout << "zookeeper_init error!" << std::endl; 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | sem_t sem; 52 | sem_init(&sem, 0, 0); 53 | zoo_set_context(m_zhandle, &sem); 54 | 55 | sem_wait(&sem); 56 | std::cout << "zookeeper_init success!" << std::endl; 57 | } 58 | 59 | void ZkClient::Create(const char *path, const char *data, int datalen, int state) 60 | { 61 | char path_buffer[128]; 62 | int bufferlen = sizeof(path_buffer); 63 | int flag; 64 | // 先判断path表示的znode节点是否存在,如果存在,就不再重复创建了 65 | flag = zoo_exists(m_zhandle, path, 0, nullptr); 66 | if (ZNONODE == flag) // 表示path的znode节点不存在 67 | { 68 | // 创建指定path的znode节点了 69 | flag = zoo_create(m_zhandle, path, data, datalen, 70 | &ZOO_OPEN_ACL_UNSAFE, state, path_buffer, bufferlen); 71 | if (flag == ZOK) 72 | { 73 | std::cout << "znode create success... path:" << path << std::endl; 74 | } 75 | else 76 | { 77 | std::cout << "flag:" << flag << std::endl; 78 | std::cout << "znode create error... path:" << path << std::endl; 79 | exit(EXIT_FAILURE); 80 | } 81 | } 82 | } 83 | 84 | // 根据指定的path,获取znode节点的值 85 | std::string ZkClient::GetData(const char *path) 86 | { 87 | char buffer[64]; 88 | int bufferlen = sizeof(buffer); 89 | int flag = zoo_get(m_zhandle, path, 0, buffer, &bufferlen, nullptr); 90 | if (flag != ZOK) 91 | { 92 | std::cout << "get znode error... path:" << path << std::endl; 93 | return ""; 94 | } 95 | else 96 | { 97 | return buffer; 98 | } 99 | } -------------------------------------------------------------------------------- /test/protobuf/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NfhLion/mprpc/161c8e91d3ebceb19763d2aca904147493129e15/test/protobuf/main -------------------------------------------------------------------------------- /test/protobuf/main.cc: -------------------------------------------------------------------------------- 1 | #include "test.pb.h" 2 | #include 3 | 4 | using namespace fixbug; 5 | 6 | void testLoginReuest() { 7 | // 封装了login请求对象的数据 8 | LoginReuest req; 9 | req.set_name("zhang san"); 10 | req.set_pwd("123456"); 11 | 12 | // 对象数据序列化 -> char* 13 | std::string send_str; 14 | if (req.SerializeToString(&send_str)) { 15 | std::cout << send_str << std::endl; 16 | } 17 | 18 | // 从send_str反序列化一个login请求对象 19 | LoginReuest reqb; 20 | if (reqb.ParseFromString(send_str)) { 21 | std::cout << reqb.name() << std::endl; 22 | std::cout << reqb.pwd() << std::endl; 23 | } 24 | } 25 | 26 | void testLink() { 27 | GetFriendListsResponse rsp; 28 | ResultCode* rc = rsp.mutable_result(); 29 | rc->set_errcode(0); 30 | 31 | for (int i = 0; i < 10; ++i) { 32 | user* user1 = rsp.add_friend_lists(); 33 | user1->set_name("zhang san"); 34 | user1->set_age(21 + i); 35 | user1->set_sex(user::MAN); 36 | } 37 | 38 | std::cout << rsp.friend_lists_size() << std::endl; 39 | } 40 | 41 | int main() { 42 | 43 | // LoginResponse rsp; 44 | // ResultCode* rc = rsp.mutable_result(); 45 | // rc->set_errcode(1); 46 | // rc->set_errmsg("Login Failed"); 47 | 48 | 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /test/protobuf/test.proto: -------------------------------------------------------------------------------- 1 | // 生成.cc文件方法: protoc test.proto --cpp_out= 存放.cc的目录 2 | 3 | syntax = "proto3"; 4 | 5 | package fixbug; 6 | 7 | // 定义下面的选项,表示生成service服务类和rpc方法描述,默认不生成 8 | option cc_generic_services = true; 9 | 10 | //protobuf存储的数据类型: 数据 列表 映射表 11 | 12 | message ResultCode 13 | { 14 | int32 errcode = 1; 15 | bytes errmsg = 2; 16 | } 17 | 18 | message LoginReuest 19 | { 20 | bytes name = 1; 21 | bytes pwd = 2; 22 | // map test = 3; 23 | } 24 | 25 | message LoginResponse 26 | { 27 | ResultCode result = 1; 28 | bool success = 3; 29 | } 30 | 31 | message GetFriendListsRequest 32 | { 33 | uint32 userid = 1; 34 | } 35 | 36 | message user 37 | { 38 | bytes name = 1; 39 | uint32 age = 2; 40 | enum Sex 41 | { 42 | MAN = 0; 43 | WOMAN = 1; 44 | } 45 | Sex sex = 3; 46 | } 47 | 48 | message GetFriendListsResponse 49 | { 50 | ResultCode result = 1; 51 | repeated user friend_lists = 2; // 定义一个列表类型 52 | } 53 | 54 | // 在protobuf中如何定义描述rpc方法的类型 55 | service UserServiceRpc 56 | { 57 | rpc Login(LoginReuest) returns(LoginResponse); 58 | rpc GetFriendLists(GetFriendListsRequest) returns(GetFriendListsResponse); 59 | } 60 | 61 | // message定义的类型主要用于rpc方法参数的一个序列化和反序列化 62 | // rpc服务提供者(服务方可以理解为Client): 63 | // rpc服务消费者(调用方 _Stub,可以理解为Server):继承RpcChannel,重写CallMethod在这个函数里面进行rpc方法的序列化 --------------------------------------------------------------------------------