From a0f9826c9f02909bc87e1a1d92eea1dca85f2ebc Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 9 Jul 2008 01:27:56 +0200 Subject: Kern und D?monen/Clients benutzen jetzt unterscheidbare Request IDs; au?erdem ein paar Bugfixes am Request-Code --- src/Common/RequestManager.cpp | 20 ++++++++++---------- src/Common/RequestManager.h | 42 +++++++++++++++++++++++++++++++++++------- src/Core/ConnectionManager.cpp | 2 +- src/madc.cpp | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 2d79b25..86d3529 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -25,23 +25,23 @@ namespace Mad { namespace Common { void RequestManager::receiveHandler(Net::Connection *connection, const Net::Packet &packet) { - std::map::iterator it = requestMap.find(connection); + std::map::iterator it = requestMaps.find(connection); - if(it == requestMap.end()) { + if(it == requestMaps.end()) { // TODO: Error std::cerr << "Received a packet from a unregistered connection." << std::endl; return; } - RequestMap &requestInfo = it->second; - Request::Request *request = requestInfo.findRequest(packet.getRequestId()); + RequestMap *requestMap = it->second; + Request::Request *request = requestMap->findRequest(packet.getRequestId()); if(request) { request->handlePacket(connection, packet); if(request->isFinished()) - requestInfo.deleteRequest(packet.getRequestId()); + requestMap->deleteRequest(packet.getRequestId()); return; } @@ -63,22 +63,22 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack } bool RequestManager::sendRequest(Net::Connection *connection, Request::Request *request) { - std::map::iterator it = requestMap.find(connection); + std::map::iterator it = requestMaps.find(connection); - if(it == requestMap.end()) + if(it == requestMaps.end()) return false; - RequestMap &requestInfo = it->second; + RequestMap *requestMap = it->second; unsigned short id; do { id = getRequestId(); - } while(requestInfo.findRequest(id)); + } while(requestMap->findRequest(id)); if(!request->sendRequest(connection, id)) return false; - requestInfo.addRequest(id, request); + requestMap->addRequest(id, request); return true; } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index fde8f3e..543fb83 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -30,7 +30,14 @@ namespace Common { class RequestManager { private: class RequestMap : private std::map { + private: + // Prevent shallow copy + RequestMap(const RequestMap &o); + RequestMap& operator=(const RequestMap &o); + public: + RequestMap() {} + ~RequestMap() { for(iterator it = begin(); it != end(); ++it) delete it->second; @@ -49,35 +56,56 @@ class RequestManager { } bool deleteRequest(unsigned short id) { - return erase(id); + iterator it = find(id); + if(it == end()) + return false; + + delete it->second; + + erase(it); + return true; } }; - std::map requestMap; + // Prevent shallow copy + RequestManager(const RequestManager &o); + RequestManager& operator=(const RequestManager &o); + + std::map requestMaps; unsigned short requestId; unsigned short getRequestId() { - return requestId++; + return requestId+=2; } void receiveHandler(Net::Connection *connection, const Net::Packet &packet); public: void registerConnection(Net::Connection *connection) { - requestMap.insert(std::make_pair(connection, RequestMap())); + requestMaps.insert(std::make_pair(connection, new RequestMap())); connection->signalReceive().connect(sigc::mem_fun(this, &RequestManager::receiveHandler)); } void unregisterConnection(Net::Connection *connection) { - requestMap.erase(connection); + std::map::iterator it = requestMaps.find(connection); + + if(it == requestMaps.end()) + return; + + delete it->second; + + requestMaps.erase(it); } bool sendRequest(Net::Connection *connection, Request::Request *request); - RequestManager() : requestId(0) {} + RequestManager(bool core) : requestId(core ? -2 : -1) {} - virtual ~RequestManager() {} + virtual ~RequestManager() { + for(std::map::iterator it = requestMaps.begin(); it != requestMaps.end(); ++it) + delete it->second; + } }; } diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index b9903c9..35850c0 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -50,7 +50,7 @@ void ConnectionManager::refreshPollfds() { } } -ConnectionManager::ConnectionManager(const std::vector &listenerAddresses) { +ConnectionManager::ConnectionManager(const std::vector &listenerAddresses) : requestManager(true) { if(listenerAddresses.empty()) { try { listeners.push_back(new Net::Listener()); diff --git a/src/madc.cpp b/src/madc.cpp index d6a06e3..f987a9d 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -28,7 +28,7 @@ int main() { Mad::Net::Connection::init(); - Mad::Common::RequestManager requestManager; + Mad::Common::RequestManager requestManager(false); Mad::Net::ClientConnection *connection = new Mad::Net::ClientConnection; try { -- cgit v1.2.3