Hi,
Can someone give me a entire example of C++ Asynchronous client , the thrift
doc writen rarely about this topic.
I have try the cobClient and it failed to call the java Server, but the sync
call is success;
So i try to seperate the sync process, invoke send_XX function first and start
a thread to call recv_xx function and wait the data retun.
When I set recv timeout for the TSocket, there is always a socket error 10060
or 10053, why?
Code:
class ThriftRequest{
public:
ThriftRequest(boost::shared_ptr<TSocket> socket){
transport = boost::shared_ptr<TTransport>(new TFramedTransport(socket));
protocol = boost::shared_ptr<TProtocol>(new TCompactProtocol(transport));
client = boost::shared_ptr<UserServiceClient>(new UserServiceClient(protocol));
}
~ThriftRequest(){
}
void open(){
if(!transport->isOpen()){
transport->open();
}
}
void close(){
if(transport->isOpen()){
transport->close();
}
}
public:
boost::shared_ptr<UserServiceClient> client;
boost::shared_ptr<TTransport> transport;
boost::shared_ptr<TProtocol> protocol;
};
#define ASYNC_REQUEST(socket, func, callback, ...) \
{ \
boost::shared_ptr<ThriftRequest> request(new ThriftRequest(socket));\
request->open();\
request->client->send_##func(##__VA_ARGS__); \
boost::thread recvThread(boost::bind(callback,request));\
}
#define ASYNC_RECV(func,request,rtn){\
request->client->recv_##func(##rtn);\
request->close();\
}
void asyncCallback(boost::shared_ptr<ThriftRequest> request){
printf("callback invoked\n");
User user;
ASYNC_RECV(getUserById,request, user);
std::cout << "User info : " << user.id << "," << user.userName << std::endl;
printf("callback finished\n");
}
int main(){
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9000));
socket->setRecvTimeout(3000);
ASYNC_REQUEST(socket,getUserById,asyncCallback,5);
getchar();
}
[email protected]