- 编写thrift文件
student.thrift
定义自定义类,和接口函数
struct Student{
1: i32 sno,
2: string sname,
3: bool ssex,
4: i16 sage,
}
service Serv{
void put(1: Student s),
}
- 生成开发库
thrift -r --gen cpp student.thrift
生成基类和服务端cpp
Serv.cpp //接口基类
Serv.h
Serv_server.skeleton.cpp //简单的server端代码,可以修改,一般都参照它来写serve程序
student_constants.cpp
student_constants.h
student_types.cpp
student_types.h
- 服务端
重编写服务端函数,继承基类
#include "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;
using boost::shared_ptr;
class ServHandler : virtual public ServIf { //继承基类Serv.cpp的基类ServIf
public:
ServHandler() {
// Your initialization goes here
}
void put(const Student& s) {//接口函数
// Your implementation goes here
printf("put\n");
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<ServHandler> handler(new ServHandler());
shared_ptr<TProcessor> processor(new ServProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();//服务
return 0;
}
shared_ptr<ServHandler> handler(new ServHandler());//ServHandler()自定义派生类
//自己编写的派生类中的接口功能
class ServHandler : virtual public ServIf { //继承基类Serv.cpp的基类ServIf
public:
ServHandler() {
// Your initialization goes here
}
void put(const Student& s) {//接口函数
...
}
};
- 客户端
#include </gen-cpp/Serv.h>
#include </usr/local/include/thrift/transport/TSocket.h>
#include </usr/local/include/thrift/transport/TBufferTransports.h>
#include </usr/local/include/thrift/protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using boost::shared_ptr;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090)); //注意此处的ip和端口
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
// 我们的代码写在这里
Student s;
s.sno = 123;
s.sname = "xiaoshe";
s.ssex = 1;
s.sage = 30;
ServClient client(protocol);
client.put(s);
transport->close();
return 0;
}
ServClient client(protocol);
client.put(s);
//Serv.h ServClient继承ServIf
class ServClient : virtual public ServIf {
public:
ServClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) {
setProtocol(prot);
}
ServClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) {
setProtocol(iprot,oprot);
}
private:
void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) {
setProtocol(prot,prot);
}
void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) {
piprot_=iprot;
poprot_=oprot;
iprot_ = iprot.get();
oprot_ = oprot.get();
}
public:
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() {
return piprot_;
}
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() {
return poprot_;
}
void put(const Student& s);//客户端调用函数
void send_put(const Student& s);
void recv_put();
protected:
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot_;
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot_;
::apache::thrift::protocol::TProtocol* iprot_;
::apache::thrift::protocol::TProtocol* oprot_;
};