From 07e490c038aaa17f477d1c4cc388296a7025d8f6 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 14 May 2019 00:25:01 +0800 Subject: [PATCH] Make sure protocol only started once --- src/network/protocol_manager.cpp | 26 ++++++++++++++++++++------ src/network/protocol_manager.hpp | 5 +---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/network/protocol_manager.cpp b/src/network/protocol_manager.cpp index 445bc645c..8182d962c 100644 --- a/src/network/protocol_manager.cpp +++ b/src/network/protocol_manager.cpp @@ -247,14 +247,28 @@ void ProtocolManager::startProtocol(std::shared_ptr 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 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. diff --git a/src/network/protocol_manager.hpp b/src/network/protocol_manager.hpp index 9f149669e..24d3eed83 100644 --- a/src/network/protocol_manager.hpp +++ b/src/network/protocol_manager.hpp @@ -173,10 +173,7 @@ private: m_protocols[0]->handleDisconnects(); } // handleDisconnects // -------------------------------------------------------------------- - void addProtocol(std::shared_ptr p) - { - m_protocols.push_back(p); - } // addProtocol + void addProtocol(std::shared_ptr p); // -------------------------------------------------------------------- /** Returns if there are no protocols of this type registered. */ bool isEmpty() const { return m_protocols.empty(); }