summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Common/RequestManager.cpp20
-rw-r--r--src/Common/RequestManager.h42
-rw-r--r--src/Core/ConnectionManager.cpp2
-rw-r--r--src/madc.cpp2
4 files changed, 47 insertions, 19 deletions
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<Net::Connection*,RequestMap>::iterator it = requestMap.find(connection);
+ std::map<Net::Connection*,RequestMap*>::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<Net::Connection*,RequestMap>::iterator it = requestMap.find(connection);
+ std::map<Net::Connection*,RequestMap*>::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<unsigned short,Request::Request*> {
+ 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<Net::Connection*,RequestMap> requestMap;
+ // Prevent shallow copy
+ RequestManager(const RequestManager &o);
+ RequestManager& operator=(const RequestManager &o);
+
+ std::map<Net::Connection*,RequestMap*> 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<Net::Connection*,RequestMap*>::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<Net::Connection*,RequestMap*>::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<Net::IPAddress> &listenerAddresses) {
+ConnectionManager::ConnectionManager(const std::vector<Net::IPAddress> &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 {