Browse Source

Merge pull request #2781 from chriseth/uniquePtrFrameCoder

Use unique_ptr for RLPXFrameCoder.
cl-refactor
Gav Wood 10 years ago
parent
commit
f23e7b1d41
  1. 4
      libp2p/Host.cpp
  2. 2
      libp2p/Host.h
  3. 7
      libp2p/RLPxHandshake.cpp
  4. 2
      libp2p/RLPxHandshake.h
  5. 5
      libp2p/Session.cpp
  6. 4
      libp2p/Session.h

4
libp2p/Host.cpp

@ -225,7 +225,7 @@ void Host::doneWorking()
m_sessions.clear(); m_sessions.clear();
} }
void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s) void Host::startPeerSession(Public const& _id, RLP const& _rlp, unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s)
{ {
// session maybe ingress or egress so m_peers and node table entries may not exist // session maybe ingress or egress so m_peers and node table entries may not exist
shared_ptr<Peer> p; shared_ptr<Peer> 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; clog(NetMessageSummary) << "Hello: " << clientVersion << "V[" << protocolVersion << "]" << _id << showbase << capslog.str() << dec << listenPort;
// create session so disconnects are managed // create session so disconnects are managed
auto ps = make_shared<Session>(this, _io, _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>(), protocolVersion})); auto ps = make_shared<Session>(this, move(_io), _s, p, PeerSessionInfo({_id, clientVersion, p->endpoint.address.to_string(), listenPort, chrono::steady_clock::duration(), _rlp[2].toSet<CapDesc>(), 0, map<string, string>(), protocolVersion}));
if (protocolVersion < dev::p2p::c_protocolVersion - 1) if (protocolVersion < dev::p2p::c_protocolVersion - 1)
{ {
ps->disconnect(IncompatibleProtocol); ps->disconnect(IncompatibleProtocol);

2
libp2p/Host.h

@ -225,7 +225,7 @@ public:
bool haveNetwork() const { return m_run && !!m_nodeTable; } bool haveNetwork() const { return m_run && !!m_nodeTable; }
/// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. /// 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<RLPXSocket> const& _s); void startPeerSession(Public const& _id, RLP const& _hello, std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s);
/// Get session by id /// Get session by id
std::shared_ptr<Session> peerSession(NodeId const& _id) { RecursiveGuard l(x_sessions); return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr<Session>(); } std::shared_ptr<Session> peerSession(NodeId const& _id) { RecursiveGuard l(x_sessions); return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr<Session>(); }

7
libp2p/RLPxHandshake.cpp

@ -143,8 +143,7 @@ void RLPXHandshake::error()
clog(NetP2PConnect) << "Handshake Failed (Connection reset by peer)"; clog(NetP2PConnect) << "Handshake Failed (Connection reset by peer)";
m_socket->close(); m_socket->close();
if (m_io != nullptr) m_io.reset();
delete m_io;
} }
void RLPXHandshake::transition(boost::system::error_code _ech) 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 /// This pointer will be freed if there is an error otherwise
/// it will be passed to Host which will take ownership. /// it will be passed to Host which will take ownership.
m_io = new RLPXFrameCoder(*this); m_io.reset(new RLPXFrameCoder(*this));
// old packet format // old packet format
// 5 arguments, HelloPacket // 5 arguments, HelloPacket
@ -286,7 +285,7 @@ void RLPXHandshake::transition(boost::system::error_code _ech)
try try
{ {
RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall); 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) catch (std::exception const& _e)
{ {

2
libp2p/RLPxHandshake.h

@ -123,7 +123,7 @@ protected:
/// Used to read and write RLPx encrypted frames for last step of handshake authentication. /// Used to read and write RLPx encrypted frames for last step of handshake authentication.
/// Passed onto Host which will take ownership. /// Passed onto Host which will take ownership.
RLPXFrameCoder* m_io = nullptr; std::unique_ptr<RLPXFrameCoder> m_io;
std::shared_ptr<RLPXSocket> m_socket; ///< Socket. std::shared_ptr<RLPXSocket> m_socket; ///< Socket.
boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout. boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout.

5
libp2p/Session.cpp

@ -33,9 +33,9 @@ using namespace std;
using namespace dev; using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
Session::Session(Host* _h, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info): Session::Session(Host* _h, unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info):
m_server(_h), m_server(_h),
m_io(_io), m_io(move(_io)),
m_socket(_s), m_socket(_s),
m_peer(_n), m_peer(_n),
m_info(_info), m_info(_info),
@ -69,7 +69,6 @@ Session::~Session()
} }
} }
catch (...){} catch (...){}
delete m_io;
} }
ReputationManager& Session::repMan() const ReputationManager& Session::repMan() const

4
libp2p/Session.h

@ -55,7 +55,7 @@ class Session: public std::enable_shared_from_this<Session>
friend class HostCapabilityFace; friend class HostCapabilityFace;
public: public:
Session(Host* _server, RLPXFrameCoder* _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info); Session(Host* _server, std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info);
virtual ~Session(); virtual ~Session();
void start(); void start();
@ -113,7 +113,7 @@ private:
Host* m_server; ///< The host that owns us. Never null. Host* m_server; ///< The host that owns us. Never null.
RLPXFrameCoder* m_io; ///< Transport over which packets are sent. std::unique_ptr<RLPXFrameCoder> m_io; ///< Transport over which packets are sent.
std::shared_ptr<RLPXSocket> m_socket; ///< Socket of peer's connection. std::shared_ptr<RLPXSocket> m_socket; ///< Socket of peer's connection.
Mutex x_writeQueue; ///< Mutex for the write queue. Mutex x_writeQueue; ///< Mutex for the write queue.
std::deque<bytes> m_writeQueue; ///< The write queue. std::deque<bytes> m_writeQueue; ///< The write queue.

Loading…
Cancel
Save