【发布时间】:2014-01-13 03:58:53
【问题描述】:
我正在寻找修改 Boost Asio HTTP Server 3 example 以维护当前连接的客户端列表的最佳方法。
如果我将示例中的 server.hpp 修改为:
class server : private boost::noncopyable
{
public:
typedef std::vector< connection_ptr > ConnectionList;
// ...
ConnectionList::const_iterator GetClientList() const
{
return connection_list_.begin();
};
void handle_accept(const boost::system::error_code& e)
{
if (!e)
{
connection_list_.push_back( new_connection_ );
new_connection_->start();
// ...
}
}
private:
ConnectionList connection_list_;
};
然后我弄乱了连接对象的生命周期,这样它就不会超出范围并与客户端断开连接,因为它仍然在 ConnectionList 中维护了一个引用。
如果我的 ConnectionList 被定义为 typedef std::vector< boost::weak_ptr< connection > > ConnectionList;,那么当有人从 GetClientList() 使用它时,我冒着客户端断开连接并使其指针无效的风险。
任何人有一个好的和安全的方式来做这件事的建议?
谢谢, 保罗H
【问题讨论】:
标签: c++ boost boost-asio