Make sure protocol only started once

This commit is contained in:
Benau 2019-05-14 00:25:01 +08:00
parent 8027df6373
commit 07e490c038
2 changed files with 21 additions and 10 deletions

View File

@ -247,14 +247,28 @@ void ProtocolManager::startProtocol(std::shared_ptr<Protocol> protocol)
assert(std::this_thread::get_id() == m_asynchronous_update_thread.get_id());
OneProtocolType &opt = m_all_protocols[protocol->getProtocolType()];
opt.addProtocol(protocol);
protocol->setup();
Protocol* protocol_ptr = protocol.get();
Log::info("ProtocolManager",
"A %s protocol has been started.", typeid(*protocol_ptr).name());
// setup the protocol and notify it that it's started
} // startProtocol
// ----------------------------------------------------------------------------
void ProtocolManager::OneProtocolType::addProtocol(std::shared_ptr<Protocol> p)
{
auto i = std::find(m_protocols.begin(), m_protocols.end(), p);
Protocol* protocol_ptr = p.get();
if (i == m_protocols.end())
{
// setup the protocol and notify it that it's started
p->setup();
Log::info("ProtocolManager",
"A %s protocol has been started.", typeid(*protocol_ptr).name());
m_protocols.push_back(p);
}
else
{
Log::warn("ProtocolManager", "A %s protocol has already started.",
typeid(*protocol_ptr).name());
}
} // addProtocol
// ----------------------------------------------------------------------------
/** Removes a protocol from the list of protocols of a certain type.
* Note that the protocol is not deleted.

View File

@ -173,10 +173,7 @@ private:
m_protocols[0]->handleDisconnects();
} // handleDisconnects
// --------------------------------------------------------------------
void addProtocol(std::shared_ptr<Protocol> p)
{
m_protocols.push_back(p);
} // addProtocol
void addProtocol(std::shared_ptr<Protocol> p);
// --------------------------------------------------------------------
/** Returns if there are no protocols of this type registered. */
bool isEmpty() const { return m_protocols.empty(); }