From 3443349fc3ea9b99a796bf4f2835950f75de729a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 14 May 2016 19:50:58 +0900 Subject: [PATCH] Refactor --- src/DHTGetPeersReplyMessage.cc | 43 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc index 80464810..2ae92f3b 100644 --- a/src/DHTGetPeersReplyMessage.cc +++ b/src/DHTGetPeersReplyMessage.cc @@ -35,6 +35,7 @@ #include "DHTGetPeersReplyMessage.h" #include +#include #include "DHTNode.h" #include "DHTBucket.h" @@ -81,24 +82,24 @@ std::unique_ptr DHTGetPeersReplyMessage::getResponse() rDict->put(TOKEN, token_); // TODO want parameter if (!closestKNodes_.empty()) { - unsigned char buffer[DHTBucket::K * 38]; - const int clen = bittorrent::getCompactLength(family_); - const int unit = clen + 20; - size_t offset = 0; + std::array buffer; + const auto clen = bittorrent::getCompactLength(family_); + const auto unit = clen + DHT_ID_LENGTH; + auto last = std::begin(buffer); size_t k = 0; - for (auto i = std::begin(closestKNodes_), eoi = std::end(closestKNodes_); - i != eoi && k < DHTBucket::K; ++i) { - memcpy(buffer + offset, (*i)->getID(), DHT_ID_LENGTH); - unsigned char compact[COMPACT_LEN_IPV6]; - int compactlen = bittorrent::packcompact(compact, (*i)->getIPAddress(), - (*i)->getPort()); + for (auto i = std::begin(closestKNodes_); + i != std::end(closestKNodes_) && k < DHTBucket::K; ++i) { + std::array compact; + auto compactlen = bittorrent::packcompact( + compact.data(), (*i)->getIPAddress(), (*i)->getPort()); if (compactlen == clen) { - memcpy(buffer + 20 + offset, compact, compactlen); - offset += unit; + last = std::copy_n((*i)->getID(), DHT_ID_LENGTH, last); + last = std::copy_n(std::begin(compact), compactlen, last); ++k; } } - rDict->put(family_ == AF_INET ? NODES : NODES6, String::g(buffer, offset)); + rDict->put(family_ == AF_INET ? NODES : NODES6, + String::g(std::begin(buffer), last)); } if (!values_.empty()) { // Limit the size of values list. The maximum size of UDP datagram @@ -123,16 +124,16 @@ std::unique_ptr DHTGetPeersReplyMessage::getResponse() // doesn't specify the maximum size of token, reply message // template may get bigger than 395 bytes. So we use 25 as maximum // number of peer info that a message can carry. - static const size_t MAX_VALUES_SIZE = 25; + constexpr size_t MAX_VALUES_SIZE = 25; auto valuesList = List::g(); - for (auto i = std::begin(values_), eoi = std::end(values_); - i != eoi && valuesList->size() < MAX_VALUES_SIZE; ++i) { - unsigned char compact[COMPACT_LEN_IPV6]; - const int clen = bittorrent::getCompactLength(family_); - int compactlen = bittorrent::packcompact(compact, (*i)->getIPAddress(), - (*i)->getPort()); + for (auto i = std::begin(values_); + i != std::end(values_) && valuesList->size() < MAX_VALUES_SIZE; ++i) { + std::array compact; + const auto clen = bittorrent::getCompactLength(family_); + auto compactlen = bittorrent::packcompact( + compact.data(), (*i)->getIPAddress(), (*i)->getPort()); if (compactlen == clen) { - valuesList->append(String::g(compact, compactlen)); + valuesList->append(String::g(compact.data(), compactlen)); } } rDict->put(VALUES, std::move(valuesList));