Use SegList<int> instead of IntSequence in PeerListenCommand::bindPort()

pull/2/head
Tatsuhiro Tsujikawa 2011-10-30 13:07:48 +09:00
parent d1667ea246
commit e9b86f2f43
3 changed files with 20 additions and 18 deletions

View File

@ -53,7 +53,7 @@
#include "LogFactory.h" #include "LogFactory.h"
#include "Logger.h" #include "Logger.h"
#include "util.h" #include "util.h"
#include "IntSequence.h" #include "SegList.h"
#include "DHTGetPeersCommand.h" #include "DHTGetPeersCommand.h"
#include "DHTPeerAnnounceStorage.h" #include "DHTPeerAnnounceStorage.h"
#include "DHTSetup.h" #include "DHTSetup.h"
@ -193,12 +193,15 @@ void BtSetup::setup(std::vector<Command*>& commands,
bool ret; bool ret;
uint16_t port; uint16_t port;
if(btReg->getTcpPort()) { if(btReg->getTcpPort()) {
IntSequence seq = util::parseIntRange(util::uitos(btReg->getTcpPort())); SegList<int> sgl;
ret = command->bindPort(port, seq); int usedPort = btReg->getTcpPort();
sgl.add(usedPort, usedPort+1);
ret = command->bindPort(port, sgl);
} else { } else {
IntSequence seq = SegList<int> sgl;
util::parseIntRange(e->getOption()->get(PREF_LISTEN_PORT)); util::parseIntSegments(sgl, e->getOption()->get(PREF_LISTEN_PORT));
ret = command->bindPort(port, seq); sgl.normalize();
ret = command->bindPort(port, sgl);
} }
if(ret) { if(ret) {
btReg->setTcpPort(port); btReg->setTcpPort(port);

View File

@ -63,20 +63,19 @@ PeerListenCommand::PeerListenCommand
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()); socket_.reset(new SocketCore());
std::vector<uint16_t> ports;
std::vector<int32_t> randPorts = seq.flush(); while(sgl.hasNext()) {
std::random_shuffle(randPorts.begin(), randPorts.end(), ports.push_back(sgl.next());
}
std::random_shuffle(ports.begin(), ports.end(),
*SimpleRandomizer::getInstance().get()); *SimpleRandomizer::getInstance().get());
const int ipv = (family_ == AF_INET) ? 4 : 6; const int ipv = (family_ == AF_INET) ? 4 : 6;
for(std::vector<int32_t>::const_iterator portItr = randPorts.begin(), for(std::vector<uint16_t>::const_iterator i = ports.begin(),
eoi = randPorts.end(); portItr != eoi; ++portItr) { eoi = ports.end(); i != eoi; ++i) {
if(!(0 < (*portItr) && (*portItr) <= 65535)) { port = *i;
continue;
}
port = (*portItr);
try { try {
socket_->bind(A2STR::NIL, port, family_); socket_->bind(A2STR::NIL, port, family_);
socket_->beginListen(); socket_->beginListen();

View File

@ -37,7 +37,7 @@
#include "Command.h" #include "Command.h"
#include "SharedHandle.h" #include "SharedHandle.h"
#include "IntSequence.h" #include "SegList.h"
namespace aria2 { namespace aria2 {
@ -60,7 +60,7 @@ public:
* Binds port. If successful, the binded port number is assinged to port and * Binds port. If successful, the binded port number is assinged to port and
* returns true, otherwise port is undefined and returns false. * 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 // Returns binded port
uint16_t getPort() const; uint16_t getPort() const;