From e9b86f2f4354dc147ce9d63fe3f576eb4f2a17dd Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 30 Oct 2011 13:07:48 +0900 Subject: [PATCH] Use SegList instead of IntSequence in PeerListenCommand::bindPort() --- src/BtSetup.cc | 15 +++++++++------ src/PeerListenCommand.cc | 19 +++++++++---------- src/PeerListenCommand.h | 4 ++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/BtSetup.cc b/src/BtSetup.cc index 36e57a9e..7ae588b4 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -53,7 +53,7 @@ #include "LogFactory.h" #include "Logger.h" #include "util.h" -#include "IntSequence.h" +#include "SegList.h" #include "DHTGetPeersCommand.h" #include "DHTPeerAnnounceStorage.h" #include "DHTSetup.h" @@ -193,12 +193,15 @@ void BtSetup::setup(std::vector& commands, bool ret; uint16_t port; if(btReg->getTcpPort()) { - IntSequence seq = util::parseIntRange(util::uitos(btReg->getTcpPort())); - ret = command->bindPort(port, seq); + SegList sgl; + int usedPort = btReg->getTcpPort(); + sgl.add(usedPort, usedPort+1); + ret = command->bindPort(port, sgl); } else { - IntSequence seq = - util::parseIntRange(e->getOption()->get(PREF_LISTEN_PORT)); - ret = command->bindPort(port, seq); + SegList sgl; + util::parseIntSegments(sgl, e->getOption()->get(PREF_LISTEN_PORT)); + sgl.normalize(); + ret = command->bindPort(port, sgl); } if(ret) { btReg->setTcpPort(port); diff --git a/src/PeerListenCommand.cc b/src/PeerListenCommand.cc index d2600f16..f19e4daa 100644 --- a/src/PeerListenCommand.cc +++ b/src/PeerListenCommand.cc @@ -63,20 +63,19 @@ PeerListenCommand::PeerListenCommand PeerListenCommand::~PeerListenCommand() {} -bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq) +bool PeerListenCommand::bindPort(uint16_t& port, SegList& sgl) { socket_.reset(new SocketCore()); - - std::vector randPorts = seq.flush(); - std::random_shuffle(randPorts.begin(), randPorts.end(), + std::vector ports; + while(sgl.hasNext()) { + ports.push_back(sgl.next()); + } + std::random_shuffle(ports.begin(), ports.end(), *SimpleRandomizer::getInstance().get()); const int ipv = (family_ == AF_INET) ? 4 : 6; - for(std::vector::const_iterator portItr = randPorts.begin(), - eoi = randPorts.end(); portItr != eoi; ++portItr) { - if(!(0 < (*portItr) && (*portItr) <= 65535)) { - continue; - } - port = (*portItr); + for(std::vector::const_iterator i = ports.begin(), + eoi = ports.end(); i != eoi; ++i) { + port = *i; try { socket_->bind(A2STR::NIL, port, family_); socket_->beginListen(); diff --git a/src/PeerListenCommand.h b/src/PeerListenCommand.h index 8d607c3d..3a9d348d 100644 --- a/src/PeerListenCommand.h +++ b/src/PeerListenCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "IntSequence.h" +#include "SegList.h" namespace aria2 { @@ -60,7 +60,7 @@ public: * Binds port. If successful, the binded port number is assinged to port and * returns true, otherwise port is undefined and returns false. */ - bool bindPort(uint16_t& port, IntSequence& seq); + bool bindPort(uint16_t& port, SegList& seq); // Returns binded port uint16_t getPort() const;