diff options
author | Yohann Ferreira <bertram@cegetel.net> | 2005-12-18 00:38:30 +0000 |
---|---|---|
committer | Yohann Ferreira <bertram@cegetel.net> | 2005-12-18 00:38:30 +0000 |
commit | ca9f3f98a9d8bd718f11806a33e7a9a2ab284de5 (patch) | |
tree | 9625123c35209b2d42d9b85dac225b90b3be990a /src | |
parent | b40a31f6c21ffc2f2e3acdbcd56b544ddfef6096 (diff) | |
download | manaserv-ca9f3f98a9d8bd718f11806a33e7a9a2ab284de5.tar.gz manaserv-ca9f3f98a9d8bd718f11806a33e7a9a2ab284de5.tar.bz2 manaserv-ca9f3f98a9d8bd718f11806a33e7a9a2ab284de5.tar.xz manaserv-ca9f3f98a9d8bd718f11806a33e7a9a2ab284de5.zip |
Made the handlers use the countedPtr for Beings as they should. Fixed a segfault when removing a being from the world.
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 4 | ||||
-rw-r--r-- | src/chathandler.cpp | 2 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 4 | ||||
-rw-r--r-- | src/connectionhandler.h | 2 | ||||
-rw-r--r-- | src/gamehandler.cpp | 2 | ||||
-rw-r--r-- | src/netcomputer.cpp | 12 | ||||
-rw-r--r-- | src/netcomputer.h | 6 | ||||
-rw-r--r-- | src/state.cpp | 28 | ||||
-rw-r--r-- | src/state.h | 10 |
9 files changed, 36 insertions, 34 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 4fe91e38..aaac1118 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -346,7 +346,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) } // set character - computer.setCharacter(chars[charNum].get()); + computer.setCharacter(chars[charNum]); result.writeByte(SELECT_OK); std::cout << "Selected Character " << int(charNum) @@ -386,7 +386,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) // Delete the character // if the character to delete is the current character, get off of it in // memory. - if ( computer.getCharacter() != NULL ) + if ( computer.getCharacter().get() != NULL ) { if ( computer.getCharacter()->getName() == chars[charNum].get()->getName() ) { diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 33f0eb33..1a25ce57 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -27,7 +27,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - if (computer.getCharacter() == NULL) + if (computer.getCharacter().get() == NULL) return; // character not selected switch (message.getId()) diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index 9ff50619..52186187 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -248,12 +248,12 @@ void ConnectionHandler::registerHandler( handlers[msgId] = handler; } -void ConnectionHandler::sendTo(tmwserv::Being *being, MessageOut &msg) +void ConnectionHandler::sendTo(tmwserv::BeingPtr beingPtr, MessageOut &msg) { for (NetComputers::iterator i = clients.begin(); i != clients.end(); i++) { - if ((*i)->getCharacter() == being) { + if ((*i)->getCharacter().get() == beingPtr.get()) { (*i)->send(msg.getPacket()); break; } diff --git a/src/connectionhandler.h b/src/connectionhandler.h index 943c2d12..0d3bfa32 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -106,7 +106,7 @@ class ConnectionHandler /** * Send packet to client with matching Being* */ - void sendTo(tmwserv::Being*, MessageOut &); + void sendTo(tmwserv::BeingPtr, MessageOut &); private: std::map<unsigned int, MessageHandler*> handlers; diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 7d525b5f..4c7ae662 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -27,7 +27,7 @@ void GameHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - if (computer.getCharacter() == NULL) + if (computer.getCharacter().get() == NULL) return; MessageOut result; diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp index ff1c93a1..c00061fc 100644 --- a/src/netcomputer.cpp +++ b/src/netcomputer.cpp @@ -54,13 +54,13 @@ void NetComputer::setAccount(tmwserv::Account *acc) account = acc; } -void NetComputer::setCharacter(tmwserv::Being *ch) +void NetComputer::setCharacter(tmwserv::BeingPtr ch) { tmwserv::State &state = tmwserv::State::instance(); - if (character != NULL) + if (character.get() != NULL) { // Remove being from the world : This is buggy for now. - //state.removeBeing(character); + state.removeBeing(character); } character = ch; state.addBeing(character, character->getMap()); @@ -75,8 +75,8 @@ void NetComputer::unsetAccount() void NetComputer::unsetCharacter() { // remove being from world - //tmwserv::State &state = tmwserv::State::instance(); - //state.removeBeing(character); - character = NULL; + tmwserv::State &state = tmwserv::State::instance(); + state.removeBeing(character); + character = tmwserv::BeingPtr(NULL); } diff --git a/src/netcomputer.h b/src/netcomputer.h index 8dace0d8..d1ac11f2 100644 --- a/src/netcomputer.h +++ b/src/netcomputer.h @@ -96,7 +96,7 @@ class NetComputer /** * Set the selected character associated with connection */ - void setCharacter(tmwserv::Being *ch); + void setCharacter(tmwserv::BeingPtr ch); /** * Deselect the character associated with connection @@ -107,7 +107,7 @@ class NetComputer /** * Get character associated with the connection */ - tmwserv::Being *getCharacter() { return character; } + tmwserv::BeingPtr getCharacter() { return character; } private: ConnectionHandler *handler; @@ -116,7 +116,7 @@ class NetComputer TCPsocket socket; /**< Client socket */ tmwserv::Account *account; /**< Account associated with connection */ - tmwserv::Being *character; /**< Selected character */ + tmwserv::BeingPtr character; /**< Selected character */ }; #endif diff --git a/src/state.cpp b/src/state.cpp index ec027531..3177131d 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -75,31 +75,33 @@ void State::update(ConnectionHandler &connectionHandler) msg.writeLong(b2->get()->getX());// x msg.writeLong(b2->get()->getY());// y - connectionHandler.sendTo(b->get(), msg); + connectionHandler.sendTo(*b, msg); } } } } } -void State::addBeing(Being *being, const std::string &map) { - if (!beingExists(being)) { +void State::addBeing(BeingPtr beingPtr, const std::string &map) { + if (!beingExists(beingPtr)) { if (!mapExists(map)) if (!loadMap(map)) return; - - maps[map].beings.push_back(tmwserv::BeingPtr(being)); + // WARNING: We should pass a copy there. + // Otherwise, remove being will erase one character + // from the account object. + maps[map].beings.push_back(beingPtr); } } -void State::removeBeing(Being *being) { +void State::removeBeing(BeingPtr beingPtr) { for (std::map<std::string, MapComposite>::iterator i = maps.begin(); i != maps.end(); i++) { for (Beings::iterator b = i->second.beings.begin(); b != i->second.beings.end(); b++) { - if (b->get() == being) { + if (b->get() == beingPtr.get()) { i->second.beings.erase(b); return; } @@ -114,14 +116,14 @@ bool State::mapExists(const std::string &map) { return true; } -bool State::beingExists(Being *being) { +bool State::beingExists(BeingPtr beingPtr) { for (std::map<std::string, MapComposite>::iterator i = maps.begin(); i != maps.end(); i++) { for (Beings::iterator b = i->second.beings.begin(); b != i->second.beings.end(); b++) { - if (b->get() == being) + if (b->get() == beingPtr.get()) return true; } } @@ -139,7 +141,7 @@ bool State::loadMap(const std::string &map) { // will need to load extra map related resources here also - return true; + return true; // We let true for testing on beings } void State::addObject(Object *object, const std::string &map) { @@ -166,7 +168,7 @@ void State::removeObject(Object *object) { } } -bool State::objectExists(Object *object) { +bool State::objectExists(const Object *object) { for (std::map<std::string, MapComposite>::iterator i = maps.begin(); i != maps.end(); i++) { @@ -180,14 +182,14 @@ bool State::objectExists(Object *object) { return false; } -const std::string State::findPlayer(Being *being) { +const std::string State::findPlayer(BeingPtr beingPtr) { for (std::map<std::string, MapComposite>::iterator i = maps.begin(); i != maps.end(); i++) { for (Beings::iterator b = i->second.beings.begin(); b != i->second.beings.end(); b++) { - if (b->get() == being) + if (b->get() == beingPtr.get()) return i->first; } } diff --git a/src/state.h b/src/state.h index 5599e36e..2fda7f50 100644 --- a/src/state.h +++ b/src/state.h @@ -85,12 +85,12 @@ class State : public utils::Singleton<State> /** * Add being to game world at specified map */ - void addBeing(Being *being, const std::string &map); + void addBeing(BeingPtr beingPtr, const std::string &map); /** * Remove being from game world */ - void removeBeing(Being *being); + void removeBeing(BeingPtr beingPtr); /** * Check to see if a map exists in game world @@ -100,7 +100,7 @@ class State : public utils::Singleton<State> /** * Check if being exists in game world already */ - bool beingExists(Being *being); + bool beingExists(BeingPtr beingPtr); /** * Load map into game world @@ -120,12 +120,12 @@ class State : public utils::Singleton<State> /** * Find out whether an object exists in the game world or not */ - bool objectExists(Object *object); + bool objectExists(const Object *object); /** * Find map player in world is on */ - const std::string findPlayer(Being *being); + const std::string findPlayer(BeingPtr being); /** * Find map object in world is on |