Prechádzať zdrojové kódy

Use constexpr for macros defined in BtConstants.h

Tatsuhiro Tsujikawa 10 rokov pred
rodič
commit
f5ff5da9da

+ 14 - 15
src/BtConstants.h

@@ -36,33 +36,32 @@
 #define D_BT_CONSTANTS_H
 
 #include "common.h"
-#include <vector>
 
-#define INFO_HASH_LENGTH 20
+namespace aria2 {
 
-#define PIECE_HASH_LENGTH 20
+constexpr size_t INFO_HASH_LENGTH = 20;
 
-#define PEER_ID_LENGTH 20
+constexpr size_t PIECE_HASH_LENGTH = 20;
 
-#define INFO_HASH_LENGTH 20
+constexpr size_t PEER_ID_LENGTH = 20;
 
-#define MAX_BLOCK_LENGTH (16*1024)
+constexpr size_t MAX_BLOCK_LENGTH = 16 * 1024;
 
-#define DEFAULT_MAX_OUTSTANDING_REQUEST 6
-
-#define OUTSTANDING_REQUEST_STEP 6
+constexpr size_t DEFAULT_MAX_OUTSTANDING_REQUEST = 6;
 
 // Upper Bound of the number of outstanding request
-#define UB_MAX_OUTSTANDING_REQUEST 256
+constexpr size_t UB_MAX_OUTSTANDING_REQUEST = 256;
+
+constexpr size_t METADATA_PIECE_SIZE = 16 * 1024;
 
-#define METADATA_PIECE_SIZE (16*1024)
+constexpr const char LPD_MULTICAST_ADDR[] = "239.192.152.143";
 
-#define LPD_MULTICAST_ADDR "239.192.152.143"
+constexpr uint16_t LPD_MULTICAST_PORT = 6771;
 
-#define LPD_MULTICAST_PORT 6771
+constexpr size_t COMPACT_LEN_IPV4 = 6;
 
-#define COMPACT_LEN_IPV4 6
+constexpr size_t COMPACT_LEN_IPV6 = 18;
 
-#define COMPACT_LEN_IPV6 18
+} // namespace aria2
 
 #endif // D_BT_CONSTANTS_H

+ 2 - 2
src/bittorrent_helper.cc

@@ -794,7 +794,7 @@ void checkLength(int32_t length)
 {
   if(length > MAX_BLOCK_LENGTH) {
     throw DL_ABORT_EX
-      (fmt("Length too long: %d > %dKB", length, MAX_BLOCK_LENGTH/1024));
+      (fmt("Length too long: %d > %luKB", length, MAX_BLOCK_LENGTH/1024));
   }
   if(length == 0) {
     throw DL_ABORT_EX(fmt("Invalid length: %d", length));
@@ -855,7 +855,7 @@ void createPeerMessageString
   msg[4] = messageId;
 }
 
-int packcompact
+size_t packcompact
 (unsigned char* compact, const std::string& addr, uint16_t port)
 {
   size_t len = net::getBinAddr(compact, addr);

+ 1 - 1
src/bittorrent_helper.h

@@ -213,7 +213,7 @@ void createPeerMessageString
  * pre-allocated.  Returns the number of written bytes; for IPv4
  * address, it is 6 and for IPv6, it is 18. On failure, returns 0.
  */
-int packcompact
+size_t packcompact
 (unsigned char* compact, const std::string& addr, uint16_t port);
 
 /**

+ 3 - 3
test/BittorrentHelperTest.cc

@@ -886,17 +886,17 @@ void BittorrentHelperTest::testExtract2PeersFromList()
 void BittorrentHelperTest::testPackcompact()
 {
   unsigned char compact[COMPACT_LEN_IPV6];
-  CPPUNIT_ASSERT_EQUAL(18,
+  CPPUNIT_ASSERT_EQUAL((size_t)18,
                        packcompact(compact,
                                    "1002:1035:4527:3546:7854:1237:3247:3217",
                                    6881));
   CPPUNIT_ASSERT_EQUAL(std::string("100210354527354678541237324732171ae1"),
                        util::toHex(compact, 18));
 
-  CPPUNIT_ASSERT_EQUAL(6, packcompact(compact, "192.168.0.1", 6881));
+  CPPUNIT_ASSERT_EQUAL((size_t)6, packcompact(compact, "192.168.0.1", 6881));
   CPPUNIT_ASSERT_EQUAL(std::string("c0a800011ae1"), util::toHex(compact, 6));
 
-  CPPUNIT_ASSERT_EQUAL(0, packcompact(compact, "badaddr", 6881));
+  CPPUNIT_ASSERT_EQUAL((size_t)0, packcompact(compact, "badaddr", 6881));
 }
 
 void BittorrentHelperTest::testUnpackcompact()