1、参考信息
安装参考:Apache Thrift 在Windows下的安装与开发:
https://blog.csdn.net/colouroo/article/details/38588297
我的环境:
OS:Win7
VS:VS2012
libevent:libevent-2.0.22-stable(http://libevent.org/)
boost:boost_1_64_0
注:我尝试了thrift-0.12.0.tar但问题较多,所有还是先使用thrift-0.9.1.tar
2、按照参考安装时遇到的问题
1、libthrift工程属性->包含目录中的$(THIRD_PARTY)\openssl\OpenSSL-Win32\include\可以不管,openssl貌似没用到
2、按照自己的boost安装目录和libevent目录修改包含目录和库目录
3、编译遇到boost智能指针和std智能指针混淆无法转换的问题,修改\lib\cpp\src\thrift\windows\config.h中相关信息如下:
// use std::thread in MSVC11 (2012) or newer
//#if _MSC_VER >= 1700
//# define USE_STD_THREAD 1
// otherwise use boost threads
//#else
# define USE_BOOST_THREAD 1
//#endif
4、参考中的例子中hello.thrift文件内容修改如下带参数的形式
service hello {
string func1(1:i32 key, 2:string value)
}
5、参考中第6步的例子代码client.cpp修改为
#include "boost/shared_ptr.hpp"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <transport/TSocket.h>
#include <string>
#include "gen-cpp/hello.h"
#pragma comment(lib, "libthrift.lib")
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
int main(int argc, char** argv) {
int port = 9090;
shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 9090));
shared_ptr<TBufferedTransport> transport(new TBufferedTransport(socket));
shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
helloClient client(protocol);
try{
transport->open();
std::string outStr;
std::string inStr("cuixy");
int32_t inInt = 32;
client.func1(outStr,inInt, inStr);
printf("Client: ServerReturn:%s\n", outStr.c_str());
transport->close();
}catch(TException& tx){
printf("ERROR:%s\n",tx.what());
}
getchar();
return 0;
}
6、参考中第6步的例子代码hello_server.skeleton.cpp修改为
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "hello.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;
#pragma comment(lib, "libthrift.lib")
using boost::shared_ptr;
class helloHandler : virtual public helloIf {
public:
helloHandler() {
// Your initialization goes here
}
void func1(std::string& _return, const int32_t key, const std::string& value) {
// Your implementation goes here
_return = std::string("wangby");
printf("Server :ClientInput:%s\n",value.c_str());
}
};
int main(int argc, char **argv) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested =MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
int port = 9090;
shared_ptr<helloHandler> handler(new helloHandler());
shared_ptr<TProcessor> processor(new helloProcessor(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;
}