Selaa lähdekoodia

Use SegList<int> instead of IntSequence.

Tatsuhiro Tsujikawa 14 vuotta sitten
vanhempi
commit
4be395117d
4 muutettua tiedostoa jossa 26 lisäystä ja 22 poistoa
  1. 9 10
      src/DHTConnectionImpl.cc
  2. 7 6
      src/DHTConnectionImpl.h
  3. 4 3
      src/DHTSetup.cc
  4. 6 3
      src/OptionHandlerImpl.cc

+ 9 - 10
src/DHTConnectionImpl.cc

@@ -55,18 +55,17 @@ DHTConnectionImpl::DHTConnectionImpl(int family)
 DHTConnectionImpl::~DHTConnectionImpl() {}
 
 bool DHTConnectionImpl::bind
-(uint16_t& port, const std::string& addr, IntSequence& ports)
+(uint16_t& port, const std::string& addr, SegList<int>& sgl)
 {
-  std::vector<int32_t> randPorts = ports.flush();
-  std::random_shuffle(randPorts.begin(), randPorts.end(),
+  std::vector<uint16_t> ports;
+  while(sgl.hasNext()) {
+    ports.push_back(sgl.next());
+  }
+  std::random_shuffle(ports.begin(), ports.end(),
                       *SimpleRandomizer::getInstance().get());
-  
-  for(std::vector<int32_t>::const_iterator portItr = randPorts.begin(),
-        eoi = randPorts.end(); portItr != eoi; ++portItr) {
-    if(!(0 < (*portItr) && (*portItr) <= 65535)) {
-      continue;
-    }
-    port = (*portItr);
+  for(std::vector<uint16_t>::const_iterator i = ports.begin(),
+        eoi = ports.end(); i != eoi; ++i) {
+    port = *i;
     if(bind(port, addr)) {
       return true;
     }

+ 7 - 6
src/DHTConnectionImpl.h

@@ -37,7 +37,7 @@
 
 #include "DHTConnection.h"
 #include "SharedHandle.h"
-#include "IntSequence.h"
+#include "SegList.h"
 
 namespace aria2 {
 
@@ -54,12 +54,13 @@ public:
   virtual ~DHTConnectionImpl();
 
   /**
-   * Binds port. All number in ports are tried.  If successful, the
-   * binded port is assigned to port and returns true.  Otherwise
-   * return false and port is undefined in this case.  If non-empty
-   * string addr is given, the socket is associated to the address.
+   * Binds port. All number in sgl are tried. All numbers in sgl must
+   * be in range [1, 65535], inclusive. If successful, the binded port
+   * is assigned to port and returns true.  Otherwise return false and
+   * port is undefined in this case.  If non-empty string addr is
+   * given, the socket is associated to the address.
    */
-  bool bind(uint16_t& port, const std::string& addr, IntSequence& ports);
+  bool bind(uint16_t& port, const std::string& addr, SegList<int>& sgl);
   
   /**
    * Binds port. The port number specified by port is used to bind.

+ 4 - 3
src/DHTSetup.cc

@@ -112,13 +112,14 @@ void DHTSetup::setup
 
     SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl(family));
     {
-      IntSequence seq =
-        util::parseIntRange(e->getOption()->get(PREF_DHT_LISTEN_PORT));
+      SegList<int> sgl;
+      util::parseIntSegments(sgl, e->getOption()->get(PREF_DHT_LISTEN_PORT));
+      sgl.normalize();
       uint16_t port;
       const std::string& addr =
         e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR:
                             PREF_DHT_LISTEN_ADDR6);
-      if(!connection->bind(port, addr, seq)) {
+      if(!connection->bind(port, addr, sgl)) {
         throw DL_ABORT_EX("Error occurred while binding port for DHT");
       }
       localNode->setPort(port);

+ 6 - 3
src/OptionHandlerImpl.cc

@@ -57,6 +57,7 @@
 #include "a2io.h"
 #include "LogFactory.h"
 #include "uri.h"
+#include "SegList.h"
 #ifdef ENABLE_MESSAGE_DIGEST
 # include "MessageDigest.h"
 #endif // ENABLE_MESSAGE_DIGEST
@@ -113,9 +114,11 @@ IntegerRangeOptionHandler::~IntegerRangeOptionHandler() {}
 void IntegerRangeOptionHandler::parseArg
 (Option& option, const std::string& optarg)
 {
-  IntSequence seq = util::parseIntRange(optarg);
-  while(seq.hasNext()) {
-    int32_t v = seq.next();
+  SegList<int> sgl;
+  util::parseIntSegments(sgl, optarg);
+  sgl.normalize();
+  while(sgl.hasNext()) {
+    int v = sgl.next();
     if(v < min_ || max_ < v) {
       std::string msg = pref_->k;
       strappend(msg, " ", _("must be between %s and %s."));