From: <arn...@us...> - 2007-11-17 15:16:16
|
Revision: 90 http://adchpp.svn.sourceforge.net/adchpp/?rev=90&view=rev Author: arnetheduck Date: 2007-11-17 07:16:11 -0800 (Sat, 17 Nov 2007) Log Message: ----------- update feature variable Modified Paths: -------------- adchpp/trunk/adchpp/Client.cpp Modified: adchpp/trunk/adchpp/Client.cpp =================================================================== --- adchpp/trunk/adchpp/Client.cpp 2007-11-17 14:47:54 UTC (rev 89) +++ adchpp/trunk/adchpp/Client.cpp 2007-11-17 15:16:11 UTC (rev 90) @@ -27,229 +27,228 @@ namespace adchpp { using namespace std::tr1::placeholders; - + Client* Client::create(const ManagedSocketPtr& ms) throw() { Client* c = new Client(); c->setSocket(ms); return c; } -Client::Client() throw() : - sid(0), state(STATE_PROTOCOL), disconnecting(false), socket(0), dataBytes(0), floodTimer(0) { +Client::Client() throw() : sid(0), state(STATE_PROTOCOL), disconnecting(false), socket(0), dataBytes(0), floodTimer(0) { } namespace { -// Lightweight call forwarders, instead of tr1::bind -struct Handler { - Handler(void (Client::*f)(), Client* c_) : - c(c_), f0(f) { - } - Handler(void (Client::*f)(const ByteVector&), Client* c_) : - c(c_), f1(f) { - } - - void operator()() throw() { - (c->*f0)(); - } - void operator()(const ByteVector& bv) throw() { - (c->*f1)(bv); - } - - Client* c; - union { - void (Client::*f0)(); - void (Client::*f1)(const ByteVector&); + // Lightweight call forwarders, instead of tr1::bind + struct Handler { + Handler(void (Client::*f)(), Client* c_) : c(c_), f0(f) { } + Handler(void (Client::*f)(const ByteVector&), Client* c_) : c(c_), f1(f) { } + + void operator()() throw() { + (c->*f0)(); + } + void operator()(const ByteVector& bv) throw() { + (c->*f1)(bv); + } + + Client* c; + union { + void (Client::*f0)(); + void (Client::*f1)(const ByteVector&); + }; }; -}; } void Client::setSocket(const ManagedSocketPtr& aSocket) throw() { -dcassert(!socket); -socket = aSocket; -socket->setConnectedHandler(Handler(&Client::onConnected, this)); -socket->setDataHandler(Handler(&Client::onData, this)); -socket->setFailedHandler(Handler(&Client::onFailed, this)); + dcassert(!socket); + socket = aSocket; + socket->setConnectedHandler(Handler(&Client::onConnected, this)); + socket->setDataHandler(Handler(&Client::onData, this)); + socket->setFailedHandler(Handler(&Client::onFailed, this)); } void Client::onConnected() throw() { -dcdebug("Client::onConnected\n"); -ClientManager::getInstance()->onConnected(*this); + dcdebug("Client::onConnected\n"); + ClientManager::getInstance()->onConnected(*this); } void* Client::setPSD(int id, void* data) throw() { -PSDIter i = find_if(psd.begin(), psd.end(), CompareFirst<int, void*>(id)); -if(i != psd.end()) { - void* old = i->second; - i->second = data; - return old; -} else { - psd.push_back(make_pair(id, data)); - return 0; + PSDIter i = find_if(psd.begin(), psd.end(), CompareFirst<int, void*>(id)); + if(i != psd.end()) { + void* old = i->second; + i->second = data; + return old; + } else { + psd.push_back(make_pair(id, data)); + return 0; + } } -} void* Client::getPSD(int id) throw() { -PSDIter i = find_if(psd.begin(), psd.end(), CompareFirst<int, void*>(id)); -if(i != psd.end()) -return i->second; -else -return 0; + PSDIter i = find_if(psd.begin(), psd.end(), CompareFirst<int, void*>(id)); + return (i != psd.end()) ? i->second : 0; } void Client::onData(const vector<uint8_t>& data) throw() { -dcdebug("In (%d): %.*s\n", data.size(), data.size(), &data[0]); + dcdebug("In (%d): %.*s\n", data.size(), data.size(), &data[0]); -size_t done = 0; -size_t len = data.size(); -while(!disconnecting && done < len) { - if(dataBytes > 0) { - size_t n = (size_t)min(dataBytes, (int64_t)(len - done)); - dataHandler(*this, &data[done], n); - dataBytes -= n; - done += n; - } else { - size_t j = done; - while(j < len && data[j] != '\n') - ++j; + size_t done = 0; + size_t len = data.size(); + while(!disconnecting && done < len) { + if(dataBytes > 0) { + size_t n = (size_t)min(dataBytes, (int64_t)(len - done)); + dataHandler(*this, &data[done], n); + dataBytes -= n; + done += n; + } else { + size_t j = done; + while(j < len && data[j] != '\n') + ++j; + + if(j == len) { + line.append((char*)&data[done], j - done); + return; + } + line.append((char*)&data[done], j - done + 1); // include LF + + done = j + 1; - if(j == len) { - line.append((char*)&data[done], j - done); - return; + if(SETTING(MAX_COMMAND_SIZE) > 0 && line.size() > (size_t)SETTING(MAX_COMMAND_SIZE)) { + send(AdcCommand(AdcCommand::SEV_FATAL, AdcCommand::ERROR_PROTOCOL_GENERIC, "Command too long")); + disconnect(Util::REASON_MAX_COMMAND_SIZE); + return; + } + + if(line.size() == 1) { + line.clear(); + continue; + } + + try { + AdcCommand cmd(line); + + if(cmd.getType() == 'H') { + cmd.setFrom(getSID()); + } else if(cmd.getFrom() != getSID()) { + disconnect(Util::REASON_INVALID_SID); + return; + } + ClientManager::getInstance()->onReceive(*this, cmd); + } catch(const ParseException&) { + ClientManager::getInstance()->onBadLine(*this, line); + } + line.clear(); } - line.append((char*)&data[done], j - done + 1); // include LF + } +} - done = j + 1; - - if(SETTING(MAX_COMMAND_SIZE) > 0 && line.size() > (size_t)SETTING(MAX_COMMAND_SIZE)) { - send(AdcCommand(AdcCommand::SEV_FATAL, AdcCommand::ERROR_PROTOCOL_GENERIC, "Command too long")); - disconnect(Util::REASON_MAX_COMMAND_SIZE); - return; - } - - if(line.size() == 1) { - line.clear(); - continue; - } - - try { - AdcCommand cmd(line); - - if(cmd.getType() == 'H') { - cmd.setFrom(getSID()); - } else if(cmd.getFrom() != getSID()) { - disconnect(Util::REASON_INVALID_SID); - return; - } - ClientManager::getInstance()->onReceive(*this, cmd); - } catch(const ParseException&) { - ClientManager::getInstance()->onBadLine(*this, line); - } - line.clear(); - } - } -} - void Client::setField(const char* name, const string& value) throw() { + uint16_t code = AdcCommand::toCode(name); + + if(code == AdcCommand::toCode("SU")) { + filters.clear(); + Util::tokenize(filters, value, ','); + } + if(value.empty()) { - info.erase(AdcCommand::toCode(name)); + info.erase(code); } else { - info[AdcCommand::toCode(name)] = value; + info[code] = value; } - changed[AdcCommand::toCode(name)] = value; + changed[code] = value; INF.clear(); } bool Client::getChangedFields(AdcCommand& cmd) const throw() { - for(InfMap::const_iterator i = changed.begin(); i != changed.end(); ++i) - cmd.addParam(string((char*)&i->first, 2)); - return !changed.empty(); + for(InfMap::const_iterator i = changed.begin(); i != changed.end(); ++i) + cmd.addParam(string((char*)&i->first, 2)); + return !changed.empty(); } bool Client::getAllFields(AdcCommand& cmd) const throw() { - for(InfMap::const_iterator i = info.begin(); i != info.end(); ++i) - cmd.addParam(string((char*)&i->first, 2), i->second); - return !info.empty(); + for(InfMap::const_iterator i = info.begin(); i != info.end(); ++i) + cmd.addParam(string((char*)&i->first, 2), i->second); + return !info.empty(); } const string& Client::getINF() const throw() { - if(INF.empty()) { - AdcCommand cmd(AdcCommand::CMD_INF, AdcCommand::TYPE_BROADCAST, getSID()); - getAllFields(cmd); - INF = cmd.toString(); - } - return INF; + if(INF.empty()) { + AdcCommand cmd(AdcCommand::CMD_INF, AdcCommand::TYPE_BROADCAST, getSID()); + getAllFields(cmd); + INF = cmd.toString(); + } + return INF; } void Client::updateFields(const AdcCommand& cmd) throw() { - dcassert(cmd.getCommand() == AdcCommand::CMD_INF); - for(StringIterC j = cmd.getParameters().begin(); j != cmd.getParameters().end(); ++j) { - if(j->size() < 2) - continue; - setField(j->substr(0, 2).c_str(), j->substr(2)); - } + dcassert(cmd.getCommand() == AdcCommand::CMD_INF); + for(StringIterC j = cmd.getParameters().begin(); j != cmd.getParameters().end(); ++j) { + if(j->size() < 2) + continue; + setField(j->substr(0, 2).c_str(), j->substr(2)); + } } bool Client::isFiltered(const string& features) const { - if(filters.empty()) { - return true; - } - - for(size_t i = 0; i < features.size(); i += 5) { - if(features[i] == '-') { - if(std::find(filters.begin(), filters.end(), features.substr(i+1, 4)) != filters.end()) { - return true; - } - } else if(features[i] == '+') { - if(std::find(filters.begin(), filters.end(), features.substr(i+1, 4)) == filters.end()) { - return true; - } - } - } - return false; + if(filters.empty()) { + return true; + } + + for(size_t i = 0; i < features.size(); i += 5) { + if(features[i] == '-') { + if(std::find(filters.begin(), filters.end(), features.substr(i+1, 4)) != filters.end()) { + return true; + } + } else if(features[i] == '+') { + if(std::find(filters.begin(), filters.end(), features.substr(i+1, 4)) == filters.end()) { + return true; + } + } + } + return false; } void Client::updateSupports(const AdcCommand& cmd) throw() { - for(StringIterC i = cmd.getParameters().begin(); i != cmd.getParameters().end(); ++i) { - const string& str = *i; - if(str.size() != 6) { - continue; - } - if(str.compare(0, 2, "AD") == 0) { - supportList.push_back(str.substr(2)); - } else if(str.compare(0, 2, "RM") == 0) { - supportList.erase(std::remove(supportList.begin(), supportList.end(), str.substr(2)), supportList.end()); - } else { - continue; - } - } + for(StringIterC i = cmd.getParameters().begin(); i != cmd.getParameters().end(); ++i) { + const string& str = *i; + if(str.size() != 6) { + continue; + } + if(str.compare(0, 2, "AD") == 0) { + supportList.push_back(str.substr(2)); + } else if(str.compare(0, 2, "RM") == 0) { + supportList.erase(std::remove(supportList.begin(), supportList.end(), str.substr(2)), supportList.end()); + } else { + continue; + } + } } bool Client::isFlooding(time_t addSeconds) { - time_t now = GET_TIME(); - if(floodTimer < now) { - floodTimer = now; - } - - floodTimer += addSeconds; - - if(floodTimer > now + SETTING(FLOOD_THRESHOLD)) { - return true; - } - - return false; + time_t now = GET_TIME(); + if(floodTimer < now) { + floodTimer = now; + } + + floodTimer += addSeconds; + + if(floodTimer > now + SETTING(FLOOD_THRESHOLD)) { + return true; + } + + return false; } void Client::disconnect(Util::Reason reason) throw() { - if(socket && !disconnecting) { - disconnecting = true; - line.clear(); - socket->disconnect(reason); - } + if(socket && !disconnecting) { + disconnecting = true; + line.clear(); + socket->disconnect(reason); + } } void Client::onFailed() throw() { - ClientManager::getInstance()->onFailed(*this); - delete this; + ClientManager::getInstance()->onFailed(*this); + delete this; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |