版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luoyexuge/article/details/81784458
我使用的是mac下安装的,首先是安装的是thrift,我直接用brew命令安装的,不过可以直接去官网上下载thrift安装,下载thrift之后可以查看 thrift在哪个位置 which thrift, 主要是为了方便后面CMakeLists.txt中配置,先放出来:
include_directories(/usr/local/homebrew/include)
link_directories(/usr/local/homebrew/lib)
关于shared_ptr,老版本是用boost下面的,新版本改为标准库下面的,这点要主要了,目前网上的代码基本上都是boost的写法
使用thrift生成文件,首先看student.thrift的内容:
struct Student{
1: i32 sno,
2: string sname,
3: bool ssex,
4: i16 sage,
}
service Serv{
void put(1: Student s),
i32 icall(1: Student s),
string scall(1: Student s),
Student stcall(1: Student s),
}
用命令 thrift -r –gen cpp student.thrift 生成,把生成的文件拷贝到 项目下面的student目录下面,首先sercer.cpp client.cpp
server.cpp
#include "student/Serv.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
class ServHandler : virtual public ServIf {
public:
ServHandler() {
// Your initialization goes here
}
void put(const Student& s) {
// Your implementation goes here
printf("put\n");
printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
}
int32_t icall(const Student& s) {
// Your implementation goes here
printf("icall\n");
printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
return s.sage;
}
void scall(std::string& _return, const Student& s) {
// Your implementation goes here
printf("scall\n");
printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
_return = s.sname;
}
void stcall(Student& stu, const Student& s) {
// Your implementation goes here
printf("stcall\n");
printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
stu.sno = s.sno + 1;
stu.sname = s.sname + "123";
stu.ssex = s.ssex;
stu.sage = s.sage + 10;
}
};
int main(int argc, char **argv) {
int port = 9090;
stdcxx::shared_ptr<ServHandler> handler(new ServHandler());
stdcxx::shared_ptr<TProcessor> processor(new ServProcessor(handler));
stdcxx::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}
client.cpp:
#include "student/Serv.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
int main(int argc, char **argv)
{
stdcxx::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
stdcxx::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
//调用server服务
Student s;
s.sno = 123;
s.sname = "hao973";
s.ssex = 1;
s.sage = 30;
ServClient client(protocol);
printf("sno=%d sname=%s ssex=%d sage=%d\n", s.sno, s.sname.c_str(), s.ssex, s.sage);
//put
client.put(s);
//icall scall
std::string strname = "";
client.scall(strname, s);
printf("icall=%d, scall=%s\n", client.icall(s), strname.c_str());
//stcall
Student stu;
s.sno = 123;
s.sname = "hao973";
s.ssex = 1;
s.sage = 30;
client.stcall(stu, s);
printf("student sno=%d sname=%s ssex=%d sage=%d\n", stu.sno, stu.sname.c_str(), stu.ssex, stu.sage);
transport->close();
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(credit_excise)
set(CMAKE_CXX_STANDARD 11)
include_directories(/usr/local/homebrew/include)
link_directories(/usr/local/homebrew/lib)
set(student_SOURCES
student/Serv.cpp
student/student_constants.cpp
student/student_types.cpp
server.cpp
)
# 生成静态库目标
add_library(student STATIC ${student_SOURCES})
target_link_libraries(student thrift)
# 同下
add_executable(server server.cpp)
target_link_libraries(server student thrift)
# 生成 demo_client 可执行程序,要求链接 demo 静态库, thrift XX库
add_executable(client client.cpp)
target_link_libraries(client student thrift)
使用cmake 和make命令编译,
cd target
cmake ..
make
生成server、client可执行文件,
./server
./client既可以运行