我正在寻找修改 Boost Asio HTTP Server 3 示例以维护当前连接的客户端列表的最佳方法。
如果我将示例中的 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()
.
有人对这样做的好和安全的方法有什么建议吗?
谢谢,保罗