我在尋找修改Boost Asio HTTP Server 3 example以維護當前連接客戶端列表的最佳方法。boost asio:維護連接客戶端列表
如果我從例如作爲修改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()
使用它。
任何人都有一個好建議&安全的方法來做到這一點?
感謝, PaulH