cd match_systm
mkdir src 存储源文件
cd src
thrift -r --gen cpp ../../thrift/match.thrift 根据match.thrift内定义的接口生成c++版本的服务器代码 gen-cpp/Match_server.skeleton.cpp
cd gen-cpp
vim Match_server.skeleton.cpp
thrift 好用:定义完接口match.thrift后,c++代码不用自己去实现,thrift会帮我们实现好
cd ..
mv gen-cpp match_server 改名字
mv match_server/Match_server.skeleton.cpp main.cpp 把实现好的Match_server.skeleton.cpp 移动到文件src中改名为main.cpp
vim main.cpp
修改main.cpp添加业务逻辑(server端)
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "match_server/Match.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include<iostream>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::match_service;
using namespace std;
接口函数
class MatchHandler : virtual public MatchIf {
public:
MatchHandler() {
// Your initialization goes here
}
int32_t add_user(const User& user, const std::string& info) {
// Your implementation goes here
printf("add_user\n");
return 0;
}
int32_t remove_user(const User& user, const std::string& info) {
// Your implementation goes here
printf("remove_user\n");
return 0;
}
};
int main(int argc, char **argv) {
int port = 9090;
::std::shared_ptr<MatchHandler> handler(new MatchHandler());
::std::shared_ptr<TProcessor> processor(new MatchProcessor(handler));
::std::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
::std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
::std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
cout<<"Start Match Server"<<endl;
server.serve();
return 0;
}
cd game
mkdir src
cd src
thrift -r --gen py ../../thrift/match.thrift 在thrift官网生成python代码
mv gen-py match_client
cd match_client
cd match
Match-remote 可执行文件是利用python代码实现服务端而不是客户端,因此可以删掉
rm Match-remote
cd ../.. 回到src文件夹下
客户端client在thrift官网有例子,直接复制回来修改
vim client.py