------------------------------------------------------------ revno: 3295 committer: poy <p...@123gen.com> branch nick: trunk timestamp: Wed 2013-05-15 19:17:56 +0200 message: plugin API: add UDP hooks modified: dcpp/ClientManager.cpp dcpp/ClientManager.h dcpp/NmdcHub.cpp dcpp/PluginApiImpl.cpp dcpp/PluginDefs.h dcpp/PluginEntity.h dcpp/PluginManager.cpp dcpp/PluginManager.h dcpp/SearchManager.cpp dcpp/SearchManager.h
-- lp:dcplusplus https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk Your team Dcplusplus-team is subscribed to branch lp:dcplusplus. To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/ClientManager.cpp' --- dcpp/ClientManager.cpp 2013-04-12 21:10:13 +0000 +++ dcpp/ClientManager.cpp 2013-05-15 17:17:56 +0000 @@ -441,11 +441,18 @@ cmd.setTo(user.getIdentity().getSID()); const_cast<Client&>(user.getClient()).send(cmd); } else { - try { - udp.writeTo(user.getIdentity().getIp(), user.getIdentity().getUdpPort(), cmd.toString(getMe()->getCID())); - } catch(const SocketException&) { - dcdebug("Socket exception sending ADC UDP command\n"); - } + sendUDP(user.getIdentity().getIp(), user.getIdentity().getUdpPort(), cmd.toString(getMe()->getCID())); + } +} + +void ClientManager::sendUDP(const string& ip, const string& port, const string& data) { + if(PluginManager::getInstance()->onUDP(true, ip, port, data)) + return; + + try { + udp.writeTo(ip, port, data); + } catch(const SocketException&) { + dcdebug("Socket exception when sending UDP data to %s:%s\n", ip.c_str(), port.c_str()); } } @@ -487,20 +494,18 @@ aClient->send(str); } else { - try { - string ip, port, file, proto, query, fragment; - - Util::decodeUrl(aSeeker, proto, ip, port, file, query, fragment); - ip = Socket::resolve(ip, AF_INET); - if(static_cast<NmdcHub*>(aClient)->isProtectedIP(ip)) - return; - if(port.empty()) - port = "412"; - for(const auto& sr: l) { - udp.writeTo(ip, port, sr->toSR(*aClient)); - } - } catch(const SocketException& /* e */) { - dcdebug("Search caught error\n"); + string ip, port, file, proto, query, fragment; + Util::decodeUrl(aSeeker, proto, ip, port, file, query, fragment); + + ip = Socket::resolve(ip, AF_INET); + if(static_cast<NmdcHub*>(aClient)->isProtectedIP(ip)) + return; + + if(port.empty()) + port = "412"; + + for(const auto& sr: l) { + sendUDP(ip, port, sr->toSR(*aClient)); } } } === modified file 'dcpp/ClientManager.h' --- dcpp/ClientManager.h 2013-02-03 22:26:48 +0000 +++ dcpp/ClientManager.h 2013-05-15 17:17:56 +0000 @@ -183,6 +183,8 @@ */ OnlineUser* findOnlineUserHint(const CID& cid, const string& hintUrl, OnlinePairC& p) const; + void sendUDP(const string& ip, const string& port, const string& data); + string getUsersFile() const { return Util::getPath(Util::PATH_USER_LOCAL) + "Users.xml"; } // ClientListener === modified file 'dcpp/NmdcHub.cpp' --- dcpp/NmdcHub.cpp 2013-01-24 21:51:00 +0000 +++ dcpp/NmdcHub.cpp 2013-05-15 17:17:56 +0000 @@ -463,7 +463,7 @@ } } } else if(cmd == "$SR") { - SearchManager::getInstance()->onSearchResult(aLine); + SearchManager::getInstance()->onData(aLine); } else if(cmd == "$HubName") { // If " - " found, the first part goes to hub name, rest to description // If no " - " found, first word goes to hub name, rest to description === modified file 'dcpp/PluginApiImpl.cpp' --- dcpp/PluginApiImpl.cpp 2013-04-23 17:41:14 +0000 +++ dcpp/PluginApiImpl.cpp 2013-05-15 17:17:56 +0000 @@ -42,7 +42,7 @@ namespace dcpp { -#define IMPL_HOOKS_COUNT 21 +#define IMPL_HOOKS_COUNT 23 static const char* hookGuids[IMPL_HOOKS_COUNT] = { HOOK_CHAT_IN, @@ -60,6 +60,8 @@ HOOK_NETWORK_HUB_OUT, HOOK_NETWORK_CONN_IN, HOOK_NETWORK_CONN_OUT, + HOOK_NETWORK_UDP_IN, + HOOK_NETWORK_UDP_OUT, HOOK_QUEUE_ADDED, HOOK_QUEUE_MOVED, === modified file 'dcpp/PluginDefs.h' --- dcpp/PluginDefs.h 2013-05-13 18:22:52 +0000 +++ dcpp/PluginDefs.h 2013-05-15 17:17:56 +0000 @@ -100,6 +100,8 @@ #define HOOK_NETWORK_HUB_OUT "dcpp.network.onHubDataOut" /* Outgoing protocol message to hub (obj: HubData) */ #define HOOK_NETWORK_CONN_IN "dcpp.network.onClientDataIn" /* Incoming client<->client protocol message (obj: ConnectionData) */ #define HOOK_NETWORK_CONN_OUT "dcpp.network.onClientDataOut" /* Outgoing client<->client protocol message (obj: ConnectionData) */ +#define HOOK_NETWORK_UDP_IN "dcpp.network.onUDPDataIn" /* Incoming UDP data (obj: UDPData) */ +#define HOOK_NETWORK_UDP_OUT "dcpp.network.onUDPDataOut" /* Outgoing UDP data (obj: UDPData) */ #define HOOK_QUEUE_ADDED "dcpp.queue.onAdded" /* (New) item has been added to download queue (obj: QueueData) */ #define HOOK_QUEUE_MOVED "dcpp.queue.onMoved" /* Download queue item has been moved to new location (obj: QueueData) */ @@ -246,6 +248,12 @@ Bool isManaged; /* Always True (Plugins can not lookup, or track the scope of, a specific instance) */ } ConnectionData, *ConnectionDataPtr; +/* UDP */ +typedef struct tagUDPData { + const char* ip; /* The ip address (remote) for this connection */ + uint16_t port; /* The port for this connection */ +} UDPData, *UDPDataPtr; + /* Queue items and files */ typedef struct tagQueueData { const char* target; /* The *final* location for the file */ === modified file 'dcpp/PluginEntity.h' --- dcpp/PluginEntity.h 2013-01-18 21:28:38 +0000 +++ dcpp/PluginEntity.h 2013-05-15 17:17:56 +0000 @@ -35,7 +35,7 @@ PluginEntity() : pod() { pod.isManaged = True; } - virtual ~PluginEntity() { psCache.clear(); } + virtual ~PluginEntity() { } PluginType* copyPluginObject() { Lock l(cs); === modified file 'dcpp/PluginManager.cpp' --- dcpp/PluginManager.cpp 2013-05-13 18:22:52 +0000 +++ dcpp/PluginManager.cpp 2013-05-15 17:17:56 +0000 @@ -296,6 +296,12 @@ } // Functions that call the plugin +bool PluginManager::onUDP(bool out, const string& ip, const string& port, const string& data) { + UDPData udp = { ip.c_str(), Util::toInt(port) }; + return runHook(out ? HOOK_NETWORK_UDP_OUT : HOOK_NETWORK_UDP_IN, &udp, + reinterpret_cast<dcptr_t>(const_cast<char*>(data.c_str()))); +} + bool PluginManager::onChatTags(Tagger& tagger, OnlineUser* from) { TagData data = { reinterpret_cast<dcptr_t>(&tagger), True }; return runHook(HOOK_UI_CHAT_TAGS, from, &data); === modified file 'dcpp/PluginManager.h' --- dcpp/PluginManager.h 2013-05-13 18:22:52 +0000 +++ dcpp/PluginManager.h 2013-05-15 17:17:56 +0000 @@ -125,6 +125,7 @@ DCCorePtr getCore() { return &dcCore; } // Functions that call the plugin + bool onUDP(bool out, const string& ip, const string& port, const string& data); bool onChatTags(Tagger& tagger, OnlineUser* from = nullptr); bool onChatDisplay(string& htmlMessage, OnlineUser* from = nullptr); bool onChatCommand(Client* client, const string& line); @@ -149,7 +150,7 @@ template<class T> bool runHook(const string& guid, PluginEntity<T>* entity, const string& data) { - return runHook<T>(guid, entity, (dcptr_t)data.c_str()); + return runHook<T>(guid, entity, reinterpret_cast<dcptr_t>(const_cast<char*>(data.c_str()))); } // Plugin interface registry === modified file 'dcpp/SearchManager.cpp' --- dcpp/SearchManager.cpp 2013-02-03 22:26:48 +0000 +++ dcpp/SearchManager.cpp 2013-05-15 17:17:56 +0000 @@ -26,7 +26,7 @@ #include "ConnectivityManager.h" #include "format.h" #include "LogManager.h" -#include "UploadManager.h" +#include "PluginManager.h" #include "SearchResult.h" #include "ShareManager.h" @@ -130,9 +130,15 @@ } if((len = socket->read(&buf[0], BUFSIZE, remoteAddr)) > 0) { - onData(&buf[0], len, remoteAddr); + string data(reinterpret_cast<char*>(&buf[0]), len); + + if(PluginManager::getInstance()->onUDP(false, remoteAddr, port, data)) + continue; + + onData(data, remoteAddr); continue; } + } catch(const SocketException& e) { dcdebug("SearchManager::run Error: %s\n", e.getError().c_str()); } @@ -165,8 +171,7 @@ return 0; } -void SearchManager::onData(const uint8_t* buf, size_t aLen, const string& remoteIp) { - string x((char*)buf, aLen); +void SearchManager::onData(const string& x, const string& remoteIp) { if(x.compare(0, 4, "$SR ") == 0) { string::size_type i, j; // Directories: $SR <nick><0x20><directory><0x20><free slots>/<total slots><0x05><Hubname><0x20>(<Hubip:port>) === modified file 'dcpp/SearchManager.h' --- dcpp/SearchManager.h 2013-01-28 19:41:11 +0000 +++ dcpp/SearchManager.h 2013-05-15 17:17:56 +0000 @@ -75,10 +75,8 @@ void listen(); void disconnect() noexcept; - void onSearchResult(const string& aLine) { - onData((const uint8_t*)aLine.data(), aLine.length(), Util::emptyString); - } + void onData(const string& data, const string& remoteIp = Util::emptyString); void onRES(const AdcCommand& cmd, const UserPtr& from, const string& removeIp = Util::emptyString); int32_t timeToSearch() { @@ -103,7 +101,6 @@ virtual int run(); virtual ~SearchManager(); - void onData(const uint8_t* buf, size_t aLen, const string& address); }; } // namespace dcpp
_______________________________________________ Mailing list: https://launchpad.net/~linuxdcpp-team Post to : linuxdcpp-team@lists.launchpad.net Unsubscribe : https://launchpad.net/~linuxdcpp-team More help : https://help.launchpad.net/ListHelp