summaryrefslogtreecommitdiff
path: root/src/UTPexExtensionMessage.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-03-26 17:12:52 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-03-26 17:20:27 +0900
commitd64089632e82a2548752c7a6baaa68365a795a74 (patch)
tree57fc7b85a8c1290975d58fa4761b04a7695bf7e3 /src/UTPexExtensionMessage.cc
parentc81b1321122e8509b55c94c0cbca0d8d6d319f0c (diff)
parenta7cd943a06b1a5ff4515b57060b84037f8d01f9f (diff)
Merge branch 'master' into random-webseeding
Diffstat (limited to 'src/UTPexExtensionMessage.cc')
-rw-r--r--src/UTPexExtensionMessage.cc92
1 files changed, 47 insertions, 45 deletions
diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc
index 6d6b7a0f..2929ea7e 100644
--- a/src/UTPexExtensionMessage.cc
+++ b/src/UTPexExtensionMessage.cc
@@ -60,30 +60,31 @@ const char UTPexExtensionMessage::EXTENSION_NAME[] = "ut_pex";
constexpr std::chrono::minutes UTPexExtensionMessage::DEFAULT_INTERVAL;
UTPexExtensionMessage::UTPexExtensionMessage(uint8_t extensionMessageID)
- : extensionMessageID_{extensionMessageID},
- peerStorage_{nullptr},
- interval_{DEFAULT_INTERVAL},
- maxFreshPeer_{DEFAULT_MAX_FRESH_PEER},
- maxDroppedPeer_{DEFAULT_MAX_DROPPED_PEER}
- {}
+ : extensionMessageID_{extensionMessageID},
+ peerStorage_{nullptr},
+ interval_{DEFAULT_INTERVAL},
+ maxFreshPeer_{DEFAULT_MAX_FRESH_PEER},
+ maxDroppedPeer_{DEFAULT_MAX_DROPPED_PEER}
+{
+}
std::string UTPexExtensionMessage::getPayload()
{
auto freshPeerPair = createCompactPeerListAndFlag(freshPeers_);
auto droppedPeerPair = createCompactPeerListAndFlag(droppedPeers_);
Dict dict;
- if(!freshPeerPair.first.first.empty()) {
+ if (!freshPeerPair.first.first.empty()) {
dict.put("added", freshPeerPair.first.first);
dict.put("added.f", freshPeerPair.first.second);
}
- if(!droppedPeerPair.first.first.empty()) {
+ if (!droppedPeerPair.first.first.empty()) {
dict.put("dropped", droppedPeerPair.first.first);
}
- if(!freshPeerPair.second.first.empty()) {
+ if (!freshPeerPair.second.first.empty()) {
dict.put("added6", freshPeerPair.second.first);
dict.put("added6.f", freshPeerPair.second.second);
}
- if(!droppedPeerPair.second.first.empty()) {
+ if (!droppedPeerPair.second.first.empty()) {
dict.put("dropped6", droppedPeerPair.second.first);
}
@@ -91,30 +92,30 @@ std::string UTPexExtensionMessage::getPayload()
}
std::pair<std::pair<std::string, std::string>,
- std::pair<std::string, std::string> >
-UTPexExtensionMessage::createCompactPeerListAndFlag
-(const std::vector<std::shared_ptr<Peer> >& peers)
+ std::pair<std::string, std::string>>
+UTPexExtensionMessage::createCompactPeerListAndFlag(
+ const std::vector<std::shared_ptr<Peer>>& peers)
{
std::string addrstring;
std::string flagstring;
std::string addrstring6;
std::string flagstring6;
- for(auto itr = std::begin(peers), eoi = std::end(peers); itr != eoi; ++itr) {
+ for (auto itr = std::begin(peers), eoi = std::end(peers); itr != eoi; ++itr) {
unsigned char compact[COMPACT_LEN_IPV6];
- int compactlen = bittorrent::packcompact
- (compact, (*itr)->getIPAddress(), (*itr)->getPort());
- if(compactlen == COMPACT_LEN_IPV4) {
+ int compactlen = bittorrent::packcompact(compact, (*itr)->getIPAddress(),
+ (*itr)->getPort());
+ if (compactlen == COMPACT_LEN_IPV4) {
addrstring.append(&compact[0], &compact[compactlen]);
flagstring += (*itr)->isSeeder() ? 0x02u : 0x00u;
- } else if(compactlen == COMPACT_LEN_IPV6) {
+ }
+ else if (compactlen == COMPACT_LEN_IPV6) {
addrstring6.append(&compact[0], &compact[compactlen]);
flagstring6 += (*itr)->isSeeder() ? 0x02u : 0x00u;
}
}
- return std::make_pair(std::make_pair(std::move(addrstring),
- std::move(flagstring)),
- std::make_pair(std::move(addrstring6),
- std::move(flagstring6)));
+ return std::make_pair(
+ std::make_pair(std::move(addrstring), std::move(flagstring)),
+ std::make_pair(std::move(addrstring6), std::move(flagstring6)));
}
std::string UTPexExtensionMessage::toString() const
@@ -132,11 +133,12 @@ void UTPexExtensionMessage::doReceivedAction()
bool UTPexExtensionMessage::addFreshPeer(const std::shared_ptr<Peer>& peer)
{
- if(!peer->isIncomingPeer() &&
- peer->getFirstContactTime().difference(global::wallclock()) < interval_) {
+ if (!peer->isIncomingPeer() &&
+ peer->getFirstContactTime().difference(global::wallclock()) < interval_) {
freshPeers_.push_back(peer);
return true;
- } else {
+ }
+ else {
return false;
}
}
@@ -154,11 +156,12 @@ bool UTPexExtensionMessage::freshPeersAreFull() const
bool UTPexExtensionMessage::addDroppedPeer(const std::shared_ptr<Peer>& peer)
{
- if(!peer->isIncomingPeer() &&
- peer->getDropStartTime().difference(global::wallclock()) < interval_) {
+ if (!peer->isIncomingPeer() &&
+ peer->getDropStartTime().difference(global::wallclock()) < interval_) {
droppedPeers_.push_back(peer);
return true;
- } else {
+ }
+ else {
return false;
}
}
@@ -192,35 +195,34 @@ void UTPexExtensionMessage::setPeerStorage(PeerStorage* peerStorage)
std::unique_ptr<UTPexExtensionMessage>
UTPexExtensionMessage::create(const unsigned char* data, size_t len)
{
- if(len < 1) {
- throw DL_ABORT_EX(fmt(MSG_TOO_SMALL_PAYLOAD_SIZE,
- EXTENSION_NAME,
+ if (len < 1) {
+ throw DL_ABORT_EX(fmt(MSG_TOO_SMALL_PAYLOAD_SIZE, EXTENSION_NAME,
static_cast<unsigned long>(len)));
}
auto msg = make_unique<UTPexExtensionMessage>(*data);
- auto decoded = bencode2::decode(data+1, len - 1);
+ auto decoded = bencode2::decode(data + 1, len - 1);
const Dict* dict = downcast<Dict>(decoded);
- if(dict) {
+ if (dict) {
const String* added = downcast<String>(dict->get("added"));
- if(added) {
- bittorrent::extractPeer
- (added, AF_INET, std::back_inserter(msg->freshPeers_));
+ if (added) {
+ bittorrent::extractPeer(added, AF_INET,
+ std::back_inserter(msg->freshPeers_));
}
const String* dropped = downcast<String>(dict->get("dropped"));
- if(dropped) {
- bittorrent::extractPeer
- (dropped, AF_INET, std::back_inserter(msg->droppedPeers_));
+ if (dropped) {
+ bittorrent::extractPeer(dropped, AF_INET,
+ std::back_inserter(msg->droppedPeers_));
}
const String* added6 = downcast<String>(dict->get("added6"));
- if(added6) {
- bittorrent::extractPeer
- (added6, AF_INET6, std::back_inserter(msg->freshPeers_));
+ if (added6) {
+ bittorrent::extractPeer(added6, AF_INET6,
+ std::back_inserter(msg->freshPeers_));
}
const String* dropped6 = downcast<String>(dict->get("dropped6"));
- if(dropped6) {
- bittorrent::extractPeer
- (dropped6, AF_INET6, std::back_inserter(msg->droppedPeers_));
+ if (dropped6) {
+ bittorrent::extractPeer(dropped6, AF_INET6,
+ std::back_inserter(msg->droppedPeers_));
}
}
return msg;