windows 版本6.1 sp1 win7旗舰版
VS2010 SP1
下载安装cygwin
http://thrift.apache.org/docs/install/
make/g++ 4.2+/gcc/boost 1.40.0+/lex/yacc/flex/autoconf/automake/libtool/zlib/看还需要啥搜索啥
thrift 版本:0.9.0 http://thrift.apache.org/download/
代码修改:
\thrift-0.9.0\lib\cpp\src\thrift\concurrency\Mutex.cpp 246行 Mutex::RECURSIVE_INITIALIZER:
void Mutex::RECURSIVE_INITIALIZER(void* arg) {
//init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
// modified by uniqs 20140217
init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE);
}
\thrift-0.9.0\lib\cpp\src\thrift\concurrency\ThreadManager.h 24 行 :
// modified by uniqs 20140217
#include <boost/tr1/functional.hpp>
//#include <tr1/functional>
\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 154行 TServerSocket::listen:
void TServerSocket::listen() {
// modified by uniqs 20140217
#ifdef _WIN32
WSADATA kData;
WSAStartup(MAKEWORD(2, 2), &kData);
#endif
\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 175行 TServerSocket::listen:
// modified by uniqs 20140217
#ifdef _WIN32
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
#else
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
#endif
// hints.ai_family = PF_UNSPEC;
// hints.ai_socktype = SOCK_STREAM;
// hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 206行 TServerSocket::listen:
if (! path_.empty()) {
// modified by uniqs 20140217
#ifdef _WIN32
serverSocket_ = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
#else
serverSocket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
#endif
} else {
serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
}
编译:
http://thrift.apache.org/docs/BuildingFromSource/
http://thrift.apache.org/docs/install/windows/
cd thrift-0.9.0
./configure
make
make install
测试是否成功:thrift -version
VS2010新建工程,thrift_cpp_server 同时新建同名项目,引入\thrift-0.9.0\lib\cpp中的libthrift
定义文件 User.thrift :
struct User {
1: i32 id,
2: string name,
}
service UserStorage {
void set_user(1: string name)
User get_user(1: i32 id)
}
thrift -r --gen cpp User.thrift
在thrift_cpp_server项目中导入所有生成的文件,并修改 UserStorage_server.skeleton.cpp 为 UserStorage_server.cpp
简单修改如下:
class UserStorageHandler : virtual public UserStorageIf {
private:
typedef map<int, User> ARRUSERS;
ARRUSERS m_mapUsers;
static unsigned int m_uUserIdIndex;
......
~UserStorageHandler()
{
m_mapUsers.clear();
}
void set_user(const std::string& name) {
// Your implementation goes here
User kUser;
kUser.__set_name(name);
kUser.__set_id(++m_uUserIdIndex);
m_mapUsers[kUser.id] = kUser;
printf("set_user\n");
}
void get_user(User& _return, const int32_t id) {
// Your implementation goes here
ARRUSERS::iterator it = m_mapUsers.find(id);
if (it != m_mapUsers.end())
{
_return = it->second;
}
printf("get_user\n");
}
......
unsigned int UserStorageHandler::m_uUserIdIndex = 0;
编译链接
thrift --gen csharp User.thrift
再新建一个csharp工程test_csharp_client,注意.netframework的版本一定要与后导入的Thrift项目的.netframework版本一致。
新建项目thrift_csharp_client。并导入 \thrift-0.9.0\lib\csharp\src 中的 Thrift 项目
在thrift_csharp_client的自动生成的Program.cs中加入如下代码 :
......
using Thrift;
using Thrift.Transport;
using Thrift.Protocol;
......
static void Main(string[] args)
{
TTransport transport = new TSocket("localhost", 9090);
TProtocol protocol = new TBinaryProtocol(transport);
UserStorage.Client client = new UserStorage.Client(protocol);
transport.Open();
client.set_user("aaaaa");
User u = client.get_user(1);
Console.WriteLine(u.Name);
transport.Flush();
transport.Close();
......
在工程中添加Program.cs/UserStorage.cs文件,添加Thrift项目的引用。