From 2bcaf4781113fa00b7119b823f9558f9c959ec6c Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 10 Aug 2015 18:37:25 +0200 Subject: [PATCH] Use unique_ptr for RLPXFrameCoder. --- libp2p/Host.cpp | 4 ++-- libp2p/Host.h | 2 +- libp2p/RLPxHandshake.cpp | 7 +++---- libp2p/RLPxHandshake.h | 4 ++-- libp2p/Session.cpp | 5 ++--- libp2p/Session.h | 4 ++-- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 64d10b172..786ed77bf 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -225,7 +225,7 @@ void Host::doneWorking() m_sessions.clear(); } -void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* _io, std::shared_ptr const& _s) +void Host::startPeerSession(Public const& _id, RLP const& _rlp, unique_ptr&& _io, std::shared_ptr const& _s) { // session maybe ingress or egress so m_peers and node table entries may not exist shared_ptr p; @@ -264,7 +264,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* clog(NetMessageSummary) << "Hello: " << clientVersion << "V[" << protocolVersion << "]" << _id << showbase << capslog.str() << dec << listenPort; // create session so disconnects are managed - auto ps = make_shared(this, _io, _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet(), 0, map(), protocolVersion})); + auto ps = make_shared(this, move(_io), _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet(), 0, map(), protocolVersion})); if (protocolVersion < dev::p2p::c_protocolVersion - 1) { ps->disconnect(IncompatibleProtocol); diff --git a/libp2p/Host.h b/libp2p/Host.h index 863149899..18fde758f 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -225,7 +225,7 @@ public: bool haveNetwork() const { return m_run && !!m_nodeTable; } /// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. - void startPeerSession(Public const& _id, RLP const& _hello, RLPXFrameCoder* _io, std::shared_ptr const& _s); + void startPeerSession(Public const& _id, RLP const& _hello, std::unique_ptr&& _io, std::shared_ptr const& _s); /// Get session by id std::shared_ptr peerSession(NodeId const& _id) { RecursiveGuard l(x_sessions); return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr(); } diff --git a/libp2p/RLPxHandshake.cpp b/libp2p/RLPxHandshake.cpp index 4fee9b42b..5f7e527c4 100644 --- a/libp2p/RLPxHandshake.cpp +++ b/libp2p/RLPxHandshake.cpp @@ -143,8 +143,7 @@ void RLPXHandshake::error() clog(NetP2PConnect) << "Handshake Failed (Connection reset by peer)"; m_socket->close(); - if (m_io != nullptr) - delete m_io; + m_io.reset(); } void RLPXHandshake::transition(boost::system::error_code _ech) @@ -194,7 +193,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech) /// This pointer will be freed if there is an error otherwise /// it will be passed to Host which will take ownership. - m_io = new RLPXFrameCoder(*this); + m_io.reset(new RLPXFrameCoder(*this)); // old packet format // 5 arguments, HelloPacket @@ -286,7 +285,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech) try { RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall); - m_host->startPeerSession(m_remote, rlp, m_io, m_socket); + m_host->startPeerSession(m_remote, rlp, move(m_io), m_socket); } catch (std::exception const& _e) { diff --git a/libp2p/RLPxHandshake.h b/libp2p/RLPxHandshake.h index 82d2a6f44..a11f26f0a 100644 --- a/libp2p/RLPxHandshake.h +++ b/libp2p/RLPxHandshake.h @@ -123,11 +123,11 @@ protected: /// Used to read and write RLPx encrypted frames for last step of handshake authentication. /// Passed onto Host which will take ownership. - RLPXFrameCoder* m_io = nullptr; + std::unique_ptr m_io; std::shared_ptr m_socket; ///< Socket. boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout. }; } -} \ No newline at end of file +} diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index 0d90ea6ea..ac38e813d 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -33,9 +33,9 @@ using namespace std; using namespace dev; using namespace dev::p2p; -Session::Session(Host* _h, RLPXFrameCoder* _io, std::shared_ptr const& _s, std::shared_ptr const& _n, PeerSessionInfo _info): +Session::Session(Host* _h, unique_ptr&& _io, std::shared_ptr const& _s, std::shared_ptr const& _n, PeerSessionInfo _info): m_server(_h), - m_io(_io), + m_io(move(_io)), m_socket(_s), m_peer(_n), m_info(_info), @@ -69,7 +69,6 @@ Session::~Session() } } catch (...){} - delete m_io; } ReputationManager& Session::repMan() const diff --git a/libp2p/Session.h b/libp2p/Session.h index 8d8c3ea1e..4d9a12a6c 100644 --- a/libp2p/Session.h +++ b/libp2p/Session.h @@ -55,7 +55,7 @@ class Session: public std::enable_shared_from_this friend class HostCapabilityFace; public: - Session(Host* _server, RLPXFrameCoder* _io, std::shared_ptr const& _s, std::shared_ptr const& _n, PeerSessionInfo _info); + Session(Host* _server, std::unique_ptr&& _io, std::shared_ptr const& _s, std::shared_ptr const& _n, PeerSessionInfo _info); virtual ~Session(); void start(); @@ -113,7 +113,7 @@ private: Host* m_server; ///< The host that owns us. Never null. - RLPXFrameCoder* m_io; ///< Transport over which packets are sent. + std::unique_ptr m_io; ///< Transport over which packets are sent. std::shared_ptr m_socket; ///< Socket of peer's connection. Mutex x_writeQueue; ///< Mutex for the write queue. std::deque m_writeQueue; ///< The write queue.