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;
}