Browse Source

code review fixes. remove std::, fix improper naming of class members, camelCase ivars for readability.

cl-refactor
subtly 10 years ago
parent
commit
4ba309d9ac
  1. 18
      libp2p/Host.cpp
  2. 38
      libp2p/NodeTable.cpp
  3. 28
      libp2p/UDP.h

18
libp2p/Host.cpp

@ -272,15 +272,15 @@ void Host::determinePublic(string const& _publicAddress, bool _upnp)
// if user supplied address is a public address then we use it // if user supplied address is a public address then we use it
// if user supplied address is private, and localnetworking is enabled, we use it // if user supplied address is private, and localnetworking is enabled, we use it
bi::address reqpublicaddr(bi::address(_publicAddress.empty() ? bi::address() : bi::address::from_string(_publicAddress))); bi::address reqPublicAddr(bi::address(_publicAddress.empty() ? bi::address() : bi::address::from_string(_publicAddress)));
bi::tcp::endpoint reqpublic(reqpublicaddr, m_listenPort); bi::tcp::endpoint reqPublic(reqPublicAddr, m_listenPort);
bool isprivate = isPrivateAddress(reqpublicaddr); bool isprivate = isPrivateAddress(reqPublicAddr);
bool ispublic = !isprivate && !isLocalHostAddress(reqpublicaddr); bool ispublic = !isprivate && !isLocalHostAddress(reqPublicAddr);
if (!reqpublicaddr.is_unspecified() && (ispublic || (isprivate && m_netPrefs.localNetworking))) if (!reqPublicAddr.is_unspecified() && (ispublic || (isprivate && m_netPrefs.localNetworking)))
{ {
if (!m_peerAddresses.count(reqpublicaddr)) if (!m_peerAddresses.count(reqPublicAddr))
m_peerAddresses.insert(reqpublicaddr); m_peerAddresses.insert(reqPublicAddr);
m_tcpPublic = reqpublic; m_tcpPublic = reqPublic;
return; return;
} }
@ -307,7 +307,7 @@ void Host::determinePublic(string const& _publicAddress, bool _upnp)
} }
// or if no address provided, use private ipv4 address if local networking is enabled // or if no address provided, use private ipv4 address if local networking is enabled
if (reqpublicaddr.is_unspecified()) if (reqPublicAddr.is_unspecified())
if (m_netPrefs.localNetworking) if (m_netPrefs.localNetworking)
for (auto addr: m_peerAddresses) for (auto addr: m_peerAddresses)
if (addr.is_v4() && isPrivateAddress(addr)) if (addr.is_v4() && isPrivateAddress(addr))

38
libp2p/NodeTable.cpp

@ -55,13 +55,13 @@ void NodeTable::join()
doFindNode(m_node.id); doFindNode(m_node.id);
} }
std::list<NodeId> NodeTable::nodes() const list<NodeId> NodeTable::nodes() const
{ {
std::list<NodeId> nodes; list<NodeId> nodes;
Guard l(x_nodes); Guard l(x_nodes);
for (auto& i: m_nodes) for (auto& i: m_nodes)
nodes.push_back(i.second->id); nodes.push_back(i.second->id);
return std::move(nodes); return move(nodes);
} }
list<NodeTable::NodeEntry> NodeTable::state() const list<NodeTable::NodeEntry> NodeTable::state() const
@ -87,7 +87,7 @@ void NodeTable::requestNeighbors(NodeEntry const& _node, NodeId _target) const
m_socketPtr->send(p); m_socketPtr->send(p);
} }
void NodeTable::doFindNode(NodeId _node, unsigned _round, std::shared_ptr<std::set<std::shared_ptr<NodeEntry>>> _tried) void NodeTable::doFindNode(NodeId _node, unsigned _round, shared_ptr<set<shared_ptr<NodeEntry>>> _tried)
{ {
if (!m_socketPtr->isOpen() || _round == s_maxSteps) if (!m_socketPtr->isOpen() || _round == s_maxSteps)
return; return;
@ -99,10 +99,10 @@ void NodeTable::doFindNode(NodeId _node, unsigned _round, std::shared_ptr<std::s
} }
else if(!_round && !_tried) else if(!_round && !_tried)
// initialized _tried on first round // initialized _tried on first round
_tried.reset(new std::set<std::shared_ptr<NodeEntry>>()); _tried.reset(new set<shared_ptr<NodeEntry>>());
auto nearest = findNearest(_node); auto nearest = findNearest(_node);
std::list<std::shared_ptr<NodeEntry>> tried; list<shared_ptr<NodeEntry>> tried;
for (unsigned i = 0; i < nearest.size() && tried.size() < s_alpha; i++) for (unsigned i = 0; i < nearest.size() && tried.size() < s_alpha; i++)
if (!_tried->count(nearest[i])) if (!_tried->count(nearest[i]))
{ {
@ -135,14 +135,14 @@ void NodeTable::doFindNode(NodeId _node, unsigned _round, std::shared_ptr<std::s
}); });
} }
std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(NodeId _target) vector<shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(NodeId _target)
{ {
// send s_alpha FindNode packets to nodes we know, closest to target // send s_alpha FindNode packets to nodes we know, closest to target
static unsigned lastBin = s_bins - 1; static unsigned lastBin = s_bins - 1;
unsigned head = dist(m_node.id, _target); unsigned head = dist(m_node.id, _target);
unsigned tail = head == 0 ? lastBin : (head - 1) % s_bins; unsigned tail = head == 0 ? lastBin : (head - 1) % s_bins;
std::map<unsigned, std::list<std::shared_ptr<NodeEntry>>> found; map<unsigned, list<shared_ptr<NodeEntry>>> found;
unsigned count = 0; unsigned count = 0;
// if d is 0, then we roll look forward, if last, we reverse, else, spread from d // if d is 0, then we roll look forward, if last, we reverse, else, spread from d
@ -202,11 +202,11 @@ std::vector<std::shared_ptr<NodeTable::NodeEntry>> NodeTable::findNearest(NodeId
tail--; tail--;
} }
std::vector<std::shared_ptr<NodeEntry>> ret; vector<shared_ptr<NodeEntry>> ret;
for (auto& nodes: found) for (auto& nodes: found)
for (auto n: nodes.second) for (auto n: nodes.second)
ret.push_back(n); ret.push_back(n);
return std::move(ret); return move(ret);
} }
void NodeTable::ping(bi::udp::endpoint _to) const void NodeTable::ping(bi::udp::endpoint _to) const
@ -222,7 +222,7 @@ void NodeTable::ping(NodeEntry* _n) const
ping(_n->endpoint.udp); ping(_n->endpoint.udp);
} }
void NodeTable::evict(std::shared_ptr<NodeEntry> _leastSeen, std::shared_ptr<NodeEntry> _new) void NodeTable::evict(shared_ptr<NodeEntry> _leastSeen, shared_ptr<NodeEntry> _new)
{ {
if (!m_socketPtr->isOpen()) if (!m_socketPtr->isOpen())
return; return;
@ -242,7 +242,7 @@ void NodeTable::noteNode(Public const& _pubk, bi::udp::endpoint const& _endpoint
if (_pubk == m_node.address()) if (_pubk == m_node.address())
return; return;
std::shared_ptr<NodeEntry> node; shared_ptr<NodeEntry> node;
{ {
Guard l(x_nodes); Guard l(x_nodes);
auto n = m_nodes.find(_pubk); auto n = m_nodes.find(_pubk);
@ -264,13 +264,13 @@ void NodeTable::noteNode(Public const& _pubk, bi::udp::endpoint const& _endpoint
noteNode(node); noteNode(node);
} }
void NodeTable::noteNode(std::shared_ptr<NodeEntry> _n) void NodeTable::noteNode(shared_ptr<NodeEntry> _n)
{ {
std::shared_ptr<NodeEntry> contested; shared_ptr<NodeEntry> contested;
{ {
NodeBucket& s = bucket(_n.get()); NodeBucket& s = bucket(_n.get());
Guard l(x_state); Guard l(x_state);
s.nodes.remove_if([&_n](std::weak_ptr<NodeEntry> n) s.nodes.remove_if([&_n](weak_ptr<NodeEntry> n)
{ {
if (n.lock() == _n) if (n.lock() == _n)
return true; return true;
@ -294,12 +294,12 @@ void NodeTable::noteNode(std::shared_ptr<NodeEntry> _n)
evict(contested, _n); evict(contested, _n);
} }
void NodeTable::dropNode(std::shared_ptr<NodeEntry> _n) void NodeTable::dropNode(shared_ptr<NodeEntry> _n)
{ {
NodeBucket &s = bucket(_n.get()); NodeBucket &s = bucket(_n.get());
{ {
Guard l(x_state); Guard l(x_state);
s.nodes.remove_if([&_n](std::weak_ptr<NodeEntry> n) { return n.lock() == _n; }); s.nodes.remove_if([&_n](weak_ptr<NodeEntry> n) { return n.lock() == _n; });
} }
Guard l(x_nodes); Guard l(x_nodes);
m_nodes.erase(_n->id); m_nodes.erase(_n->id);
@ -365,7 +365,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes
// clog(NodeTableMessageSummary) << "Received FindNode from " << _from.address().to_string() << ":" << _from.port(); // clog(NodeTableMessageSummary) << "Received FindNode from " << _from.address().to_string() << ":" << _from.port();
FindNode in = FindNode::fromBytesConstRef(_from, rlpBytes); FindNode in = FindNode::fromBytesConstRef(_from, rlpBytes);
std::vector<std::shared_ptr<NodeTable::NodeEntry>> nearest = findNearest(in.target); vector<shared_ptr<NodeTable::NodeEntry>> nearest = findNearest(in.target);
static unsigned const nlimit = (m_socketPtr->maxDatagramSize - 11) / 86; static unsigned const nlimit = (m_socketPtr->maxDatagramSize - 11) / 86;
for (unsigned offset = 0; offset < nearest.size(); offset += nlimit) for (unsigned offset = 0; offset < nearest.size(); offset += nlimit)
{ {
@ -412,7 +412,7 @@ void NodeTable::doCheckEvictions(boost::system::error_code const& _ec)
return; return;
bool evictionsRemain = false; bool evictionsRemain = false;
std::list<shared_ptr<NodeEntry>> drop; list<shared_ptr<NodeEntry>> drop;
{ {
Guard le(x_evictions); Guard le(x_evictions);
Guard ln(x_nodes); Guard ln(x_nodes);

28
libp2p/UDP.h

@ -142,13 +142,13 @@ protected:
bi::udp::endpoint m_endpoint; ///< Endpoint which we listen to. bi::udp::endpoint m_endpoint; ///< Endpoint which we listen to.
Mutex x_sendQ; Mutex x_sendQ;
std::deque<UDPDatagram> sendQ; ///< Queue for egress data. std::deque<UDPDatagram> m_sendQ; ///< Queue for egress data.
std::array<byte, maxDatagramSize> recvData; ///< Buffer for ingress data. std::array<byte, maxDatagramSize> m_recvData; ///< Buffer for ingress data.
bi::udp::endpoint recvEndpoint; ///< Endpoint data was received from. bi::udp::endpoint m_recvEndpoint; ///< Endpoint data was received from.
bi::udp::socket m_socket; ///< Boost asio udp socket. bi::udp::socket m_socket; ///< Boost asio udp socket.
Mutex x_socketError; ///< Mutex for error which can be set from host or IO thread. Mutex x_socketError; ///< Mutex for error which can be set from host or IO thread.
boost::system::error_code socketError; ///< Set when shut down due to error. boost::system::error_code m_socketError; ///< Set when shut down due to error.
}; };
template <typename Handler, unsigned MaxDatagramSize> template <typename Handler, unsigned MaxDatagramSize>
@ -163,7 +163,7 @@ void UDPSocket<Handler,MaxDatagramSize>::connect()
// clear write queue so reconnect doesn't send stale messages // clear write queue so reconnect doesn't send stale messages
Guard l(x_sendQ); Guard l(x_sendQ);
sendQ.clear(); m_sendQ.clear();
m_closed = false; m_closed = false;
doRead(); doRead();
@ -176,8 +176,8 @@ bool UDPSocket<Handler,MaxDatagramSize>::send(UDPDatagram const& _datagram)
return false; return false;
Guard l(x_sendQ); Guard l(x_sendQ);
sendQ.push_back(_datagram); m_sendQ.push_back(_datagram);
if (sendQ.size() == 1) if (m_sendQ.size() == 1)
doWrite(); doWrite();
return true; return true;
@ -190,13 +190,13 @@ void UDPSocket<Handler,MaxDatagramSize>::doRead()
return; return;
auto self(UDPSocket<Handler, MaxDatagramSize>::shared_from_this()); auto self(UDPSocket<Handler, MaxDatagramSize>::shared_from_this());
m_socket.async_receive_from(boost::asio::buffer(recvData), recvEndpoint, [this, self](boost::system::error_code _ec, size_t _len) m_socket.async_receive_from(boost::asio::buffer(m_recvData), m_recvEndpoint, [this, self](boost::system::error_code _ec, size_t _len)
{ {
if (_ec) if (_ec)
return disconnectWithError(_ec); return disconnectWithError(_ec);
assert(_len); assert(_len);
m_host.onReceived(this, recvEndpoint, bytesConstRef(recvData.data(), _len)); m_host.onReceived(this, m_recvEndpoint, bytesConstRef(m_recvData.data(), _len));
doRead(); doRead();
}); });
} }
@ -207,7 +207,7 @@ void UDPSocket<Handler,MaxDatagramSize>::doWrite()
if (m_closed) if (m_closed)
return; return;
const UDPDatagram& datagram = sendQ[0]; const UDPDatagram& datagram = m_sendQ[0];
auto self(UDPSocket<Handler, MaxDatagramSize>::shared_from_this()); auto self(UDPSocket<Handler, MaxDatagramSize>::shared_from_this());
m_socket.async_send_to(boost::asio::buffer(datagram.data), datagram.endpoint(), [this, self](boost::system::error_code _ec, std::size_t) m_socket.async_send_to(boost::asio::buffer(datagram.data), datagram.endpoint(), [this, self](boost::system::error_code _ec, std::size_t)
{ {
@ -216,8 +216,8 @@ void UDPSocket<Handler,MaxDatagramSize>::doWrite()
else else
{ {
Guard l(x_sendQ); Guard l(x_sendQ);
sendQ.pop_front(); m_sendQ.pop_front();
if (sendQ.empty()) if (m_sendQ.empty())
return; return;
} }
doWrite(); doWrite();
@ -235,9 +235,9 @@ void UDPSocket<Handler,MaxDatagramSize>::disconnectWithError(boost::system::erro
{ {
// disconnect-operation following prior non-zero errors are ignored // disconnect-operation following prior non-zero errors are ignored
Guard l(x_socketError); Guard l(x_socketError);
if (socketError != boost::system::error_code()) if (m_socketError != boost::system::error_code())
return; return;
socketError = _ec; m_socketError = _ec;
} }
// TODO: (if non-zero error) schedule high-priority writes // TODO: (if non-zero error) schedule high-priority writes

Loading…
Cancel
Save