TServer类中提供了EventHandler机制,支持以下四种情况
1. 服务运行前
2. 创建客户端上下文前
3. 处理客户端请求前
4. 销毁客户端上下文前
源代码如下:thrift-0.9.0\lib\cpp\src\thrift\server\TServer.h
/**
* Virtual interface class that can handle events from the server core. To
* use this you should subclass it and implement the methods that you care
* about. Your subclass can also store local data that you may care about,
* such as additional "arguments" to these methods (stored in the object
* instance's state).
*/
class TServerEventHandler {
public:
virtual ~TServerEventHandler() {}
/**
* Called before the server begins.
*/
virtual void preServe() {}
/**
* Called when a new client has connected and is about to being processing.
*/
virtual void* createContext(boost::shared_ptr<TProtocol> input,
boost::shared_ptr<TProtocol> output) {
(void)input;
(void)output;
return NULL;
}
/**
* Called when a client has finished request-handling to delete server
* context.
*/
virtual void deleteContext(void* serverContext,
boost::shared_ptr<TProtocol>input,
boost::shared_ptr<TProtocol>output) {
(void)serverContext;
(void)input;
(void)output;
}
/**
* Called when a client is about to call the processor.
*/
virtual void processContext(void* serverContext,
boost::shared_ptr<TTransport> transport) {
(void)serverContext;
(void)transport;
}
protected:
/**
* Prevent direct instantiation.
*/
TServerEventHandler() {}
};