IDL定义:
service PackageManagerService
{
}
服务端使用示例:
CThriftServerHelper
return _thrift_server_helper.serve(FLAGS_package_port, rpc_threads);
客户端使用示例:
CThriftClientHelper
thrift_client_helper.connect(); // 注意需要处理异常TTransportException/TApplicationException/TException
#include
#include
#include
#include
#include
#include
#include
#include
// 1.从未连接过,也就是还未打开过连接
// 2.连接被对端关闭了
inline bool thrift_not_connected(thrift::transport::TTransportException::TTransportExceptionType type)
{return (thrift::transport::TTransportException::NOT_OPEN == type)|| (thrift::transport::TTransportException::END_OF_FILE == type);
}// 封装对thrift服务端的公共操作
template
class CThriftServerHelper
{
public:// 启动rpc服务,请注意该调用是同步阻塞的,所以需放最后调用bool serve(uint16_t port);bool serve(uint16_t port, uint8_t num_threads);bool serve(const std::string& ip, uint16_t port, uint8_t num_threads);void stop();private:boost::shared_ptr
};// 封装对thrift客户端的公共操作
template
class CThriftClientHelper
{
public:CThriftClientHelper(const std::string& host, uint16_t port, int timeout = RPC_TIMEOUT);~CThriftClientHelper();void connect();void close();ThriftClient* operator ->() const{return _container_client.get();}ThriftClient* get(){return _container_client.get();}private:std::string _host;uint16_t _port;int _timeout;boost::shared_ptr
};///template
bool CThriftServerHelper
{return serve("0.0.0.0", port, 1);
}template
bool CThriftServerHelper
{return serve("0.0.0.0", port, num_threads);
}template
bool CThriftServerHelper
{try{_handler.reset(new ThriftHandler);_processor.reset(new ServiceProcessor(_handler));_protocol_factory.reset(new thrift::protocol::TBinaryProtocolFactory());_thread_manager &#61; thrift::server::ThreadManager::newSimpleThreadManager(num_threads);_thread_factory.reset(new thrift::concurrency::PosixThreadFactory());_thread_manager->threadFactory(_thread_factory);_thread_manager->start();_server.reset(new thrift::server::TNonblockingServer(_processor, _protocol_factory, port, _thread_manager));_server->serve();}catch (thrift::TException& tx){LOG(ERROR) <<"start thrift error: " <
void CThriftServerHelper
{_server->stop();
}///template
CThriftClientHelper
{_sock_pool.reset(new thrift::transport::TSocketPool());_sock_pool->addServer(host, port);_sock_pool->setConnTimeout(timeout);_sock_pool->setRecvTimeout(timeout);_sock_pool->setSendTimeout(timeout);_socket &#61; _sock_pool;_transport.reset(new thrift::transport::TFramedTransport(_socket));_protocol.reset(new thrift::protocol::TBinaryProtocol(_transport));_container_client.reset(new ThriftClient(_protocol));
}template
CThriftClientHelper
{close();
}template
void CThriftClientHelper
{if (!_transport->isOpen()){_transport->open();}
}template
void CThriftClientHelper
{if (_transport->isOpen()){_transport->close();}
}