节俭服务器:检测客户端断开连接(c++库)
Thrift server: detect client disconnections (C++ library)
在运行Thrift服务器时,有必要处理客户端意外断开连接的情况。这可能在服务器处理RPC时发生。如果服务器有阻塞调用,这种情况并不少见,阻塞调用通常用于挂起操作,以便通知客户端异步事件。在任何情况下,这都是一个角落的情况,可能而且确实发生在任何服务器上,清理通常是必要的。
幸运的是,Thrift提供了类TServerEventHandler来挂钩连接/断开回调。这在以前使用c++库和命名管道传输的Thrift版本(我相信是0.8)中是可以工作的。然而,在Thrift 0.9.1中,当客户端连接时,createContext()和deleteContext()回调都会立即触发。在客户端断开连接时都不会触发。是否有一种检测客户端断开连接的新方法?
代码片段:
//============================================================================
//Code snippet where the server is instantiated and started. This may
//or may not be syntactically correct.
//The event handler class is derived from TServerEventHandler.
//
{
boost::shared_ptr<MyHandler> handler(new MyHandler());
boost::shared_ptr<TProcessor> processor(new MyProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TPipeServer("MyPipeName"));
boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
boost::shared_ptr<TServer> server(new TSimpleServer(processor, transport, tfactory, pfactory));
boost::shared_ptr<SampleEventHandler> EventHandler(new SampleEventHandler());
server->setServerEventHandler(EventHandler);
server->serve();
}
//============================================================================
//Sample event callbacks triggered by the server when something interesting
//happens with the client.
//Create an overload of TServerEventHandler specific to your needs and
//implement the necessary methods.
//
class SampleEventHandler : public server::TServerEventHandler {
public:
SampleEventHandler() :
NumClients_(0) //Initialize example member
{}
//Called before the server begins -
//virtual void preServe() {}
//createContext may return a user-defined context to aid in cleaning
//up client connections upon disconnection. This example dispenses
//with contextual information and returns NULL.
virtual void* createContext(boost::shared_ptr<protocol::TProtocol> input, boost::shared_ptr<protocol::TProtocol> output)
{
printf("SampleEventHandler callback: Client connected (total %d)n", ++NumClients_);
return NULL;
}
//Called when a client has disconnected, either naturally or by error.
virtual void deleteContext(void* serverContext, boost::shared_ptr<protocol::TProtocol>input, boost::shared_ptr<protocol::TProtocol>output)
{
printf("SampleEventHandler callback: Client disconnected (total %d)n", --NumClients_);
}
//Called when a client is about to call the processor -
//virtual void processContext(void* serverContext,
boost::shared_ptr<TTransport> transport) {}
protected:
uint32_t NumClients_; //Example member
};
如果createContext()和deleteContext()都在客户端连接时调用,而客户端没有断开连接,这是一个错误,应该在Thrift jira中创建一个问题。
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- WinSock2:connect() 提供"连接被拒绝"
- 升压信号2将插槽传递到成员功能以断开连接