summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-10-30 13:07:48 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-10-30 13:07:48 +0900
commite9b86f2f4354dc147ce9d63fe3f576eb4f2a17dd (patch)
treed9f334c3bde48857e6d431faf93701d309cdffcf
parentd1667ea246d492ab0280624cdcc6a454aff7ba0d (diff)
Use SegList<int> instead of IntSequence in PeerListenCommand::bindPort()
-rw-r--r--src/BtSetup.cc15
-rw-r--r--src/PeerListenCommand.cc19
-rw-r--r--src/PeerListenCommand.h4
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<Command*>& commands,
bool ret;
uint16_t port;
if(btReg->getTcpPort()) {
- IntSequence seq = util::parseIntRange(util::uitos(btReg->getTcpPort()));
- ret = command->bindPort(port, seq);
+ SegList<int> 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<int> 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<int>& sgl)
{
socket_.reset(new SocketCore());
-
- std::vector<int32_t> randPorts = seq.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());
const int ipv = (family_ == AF_INET) ? 4 : 6;
- 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;
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<int>& seq);
// Returns binded port
uint16_t getPort() const;