Changed Event's m_peer from STKPeer** to STKPeer *. Fixes some minor memory leaks.

This commit is contained in:
hiker 2015-10-21 08:11:35 +11:00
parent 9cdf7058a7
commit 40629d9816
10 changed files with 26 additions and 26 deletions

View File

@ -59,23 +59,22 @@ Event::Event(ENetEvent* event)
m_packet = NULL; m_packet = NULL;
std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers(); std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
m_peer = new STKPeer*; m_peer = NULL;
*m_peer = NULL;
for (unsigned int i = 0; i < peers.size(); i++) for (unsigned int i = 0; i < peers.size(); i++)
{ {
if (peers[i]->m_peer == event->peer) if (peers[i]->m_peer == event->peer)
{ {
*m_peer = peers[i]; m_peer = peers[i];
Log::verbose("Event", "The peer you sought has been found on %p", Log::verbose("Event", "The peer you sought has been found on %p",
m_peer); m_peer);
return; return;
} }
} }
if (*m_peer == NULL) // peer does not exist, create him if (m_peer == NULL) // peer does not exist, create him
{ {
STKPeer* new_peer = new STKPeer(); STKPeer* new_peer = new STKPeer();
new_peer->m_peer = event->peer; new_peer->m_peer = event->peer;
*m_peer = new_peer; m_peer = new_peer;
Log::debug("Event", Log::debug("Event",
"Creating a new peer, address are STKPeer:%p, Peer:%p", "Creating a new peer, address are STKPeer:%p, Peer:%p",
new_peer, event->peer); new_peer, event->peer);

View File

@ -62,7 +62,7 @@ private:
EVENT_TYPE m_type; EVENT_TYPE m_type;
/** Pointer to the peer that triggered that event. */ /** Pointer to the peer that triggered that event. */
STKPeer** m_peer; STKPeer* m_peer;
public: public:
Event(ENetEvent* event); Event(ENetEvent* event);
@ -76,7 +76,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the peer of this event. */ /** Returns the peer of this event. */
STKPeer ** getPeer() const { return m_peer; } STKPeer* getPeer() const { return m_peer; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** \brief Get a copy of the data. /** \brief Get a copy of the data.
* \return A copy of the message data. This is empty for events like * \return A copy of the message data. This is empty for events like

View File

@ -112,7 +112,7 @@ void NetworkManager::notifyEvent(Event* event)
{ {
Log::verbose("NetworkManager", "EVENT received of type %d", Log::verbose("NetworkManager", "EVENT received of type %d",
(int)(event->getType())); (int)(event->getType()));
STKPeer* peer = *event->getPeer(); STKPeer* peer = event->getPeer();
if (event->getType() == EVENT_TYPE_CONNECTED) if (event->getType() == EVENT_TYPE_CONNECTED)
{ {
Log::info("NetworkManager", "A client has just connected. There are " Log::info("NetworkManager", "A client has just connected. There are "

View File

@ -64,7 +64,7 @@ bool Protocol::checkDataSizeAndToken(Event* event, int minimum_size)
data.size(), data[0]); data.size(), data[0]);
return false; return false;
} }
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
uint32_t token = data.gui32(1); uint32_t token = data.gui32(1);
if (token != peer->getClientServerToken()) if (token != peer->getClientServerToken())
{ {

View File

@ -210,7 +210,8 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
NetworkManager::getInstance()->disconnected(); NetworkManager::getInstance()->disconnected();
m_listener->requestTerminate(this); m_listener->requestTerminate(this);
NetworkManager::getInstance()->reset(); NetworkManager::getInstance()->reset();
NetworkManager::getInstance()->removePeer(*event->getPeer()); // prolly the same as m_server // probably the same as m_server
NetworkManager::getInstance()->removePeer(event->getPeer());
return true; return true;
} // disconnection } // disconnection
return false; return false;
@ -364,7 +365,7 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
Log::error("ClientLobbyRoomProtocol", "A message notifying an accepted connection wasn't formated as expected."); Log::error("ClientLobbyRoomProtocol", "A message notifying an accepted connection wasn't formated as expected.");
return; return;
} }
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
uint32_t global_id = data.gui32(8); uint32_t global_id = data.gui32(8);
if (global_id == PlayerManager::getCurrentOnlineId()) if (global_id == PlayerManager::getCurrentOnlineId())
@ -406,7 +407,7 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
} }
// add self // add self
m_server = *(event->getPeer()); m_server = event->getPeer();
m_state = CONNECTED; m_state = CONNECTED;
} }
else else
@ -604,7 +605,7 @@ void ClientLobbyRoomProtocol::raceFinished(Event* event)
return; return;
} }
NetworkString data = event->data(); NetworkString data = event->data();
if ((*event->getPeer())->getClientServerToken() != data.gui32(1)) if (event->getPeer()->getClientServerToken() != data.gui32(1))
{ {
Log::error("ClientLobbyRoomProtocol", "Bad token"); Log::error("ClientLobbyRoomProtocol", "Bad token");
return; return;

View File

@ -72,7 +72,7 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
uint32_t token = data.gui32(); uint32_t token = data.gui32();
NetworkString pure_message = data; NetworkString pure_message = data;
pure_message.removeFront(4); pure_message.removeFront(4);
if (token != (*event->getPeer())->getClientServerToken()) if (token != event->getPeer()->getClientServerToken())
{ {
Log::error("ControllerEventsProtocol", "Bad token from peer."); Log::error("ControllerEventsProtocol", "Bad token from peer.");
return true; return true;

View File

@ -27,7 +27,7 @@ bool GameEventsProtocol::notifyEvent(Event* event)
Log::warn("GameEventsProtocol", "Too short message."); Log::warn("GameEventsProtocol", "Too short message.");
return true; return true;
} }
if ( (*event->getPeer())->getClientServerToken() != data.gui32()) if ( event->getPeer()->getClientServerToken() != data.gui32())
{ {
Log::warn("GameEventsProtocol", "Bad token."); Log::warn("GameEventsProtocol", "Bad token.");
return true; return true;

View File

@ -305,7 +305,7 @@ void ServerLobbyRoomProtocol::checkRaceFinished()
void ServerLobbyRoomProtocol::kartDisconnected(Event* event) void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
{ {
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (peer->getPlayerProfile() != NULL) // others knew him if (peer->getPlayerProfile() != NULL) // others knew him
{ {
NetworkString msg(3); NetworkString msg(3);
@ -334,7 +334,7 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
*/ */
void ServerLobbyRoomProtocol::connectionRequested(Event* event) void ServerLobbyRoomProtocol::connectionRequested(Event* event)
{ {
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
NetworkString data = event->data(); NetworkString data = event->data();
if (data.size() != 5 || data[0] != 4) if (data.size() != 5 || data[0] != 4)
{ {
@ -415,7 +415,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event) void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 6)) if (!checkDataSizeAndToken(event, 6))
return; return;
@ -476,7 +476,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
void ServerLobbyRoomProtocol::playerMajorVote(Event* event) void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7)) if (!checkDataSizeAndToken(event, 7))
return; return;
if (!isByteCorrect(event, 5, 1)) if (!isByteCorrect(event, 5, 1))
@ -507,7 +507,7 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event) void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer)(); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7)) if (!checkDataSizeAndToken(event, 7))
return; return;
if (!isByteCorrect(event, 5, 1)) if (!isByteCorrect(event, 5, 1))
@ -538,7 +538,7 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
void ServerLobbyRoomProtocol::playerMinorVote(Event* event) void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 7)) if (!checkDataSizeAndToken(event, 7))
return; return;
if (!isByteCorrect(event, 5, 1)) if (!isByteCorrect(event, 5, 1))
@ -569,7 +569,7 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
void ServerLobbyRoomProtocol::playerTrackVote(Event* event) void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 8)) if (!checkDataSizeAndToken(event, 8))
return; return;
int N = data[5]; int N = data[5];
@ -602,7 +602,7 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
void ServerLobbyRoomProtocol::playerReversedVote(Event* event) void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 9)) if (!checkDataSizeAndToken(event, 9))
return; return;
if (!isByteCorrect(event, 5, 1)) if (!isByteCorrect(event, 5, 1))
@ -635,7 +635,7 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
void ServerLobbyRoomProtocol::playerLapsVote(Event* event) void ServerLobbyRoomProtocol::playerLapsVote(Event* event)
{ {
NetworkString data = event->data(); NetworkString data = event->data();
STKPeer* peer = *(event->getPeer()); STKPeer* peer = event->getPeer();
if (!checkDataSizeAndToken(event, 9)) if (!checkDataSizeAndToken(event, 9))
return; return;
if (!isByteCorrect(event, 5, 1)) if (!isByteCorrect(event, 5, 1))

View File

@ -44,7 +44,7 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event)
} }
uint32_t token = data.gui32(); uint32_t token = data.gui32();
uint8_t ready = data.gui8(4); uint8_t ready = data.gui8(4);
STKPeer* peer = (*(event->getPeer())); STKPeer* peer = event->getPeer();
if (peer->getClientServerToken() != token) if (peer->getClientServerToken() != token)
{ {
Log::error("StartGameProtocol", "Bad token received."); Log::error("StartGameProtocol", "Bad token received.");

View File

@ -61,7 +61,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
uint8_t peer_id = 0; uint8_t peer_id = 0;
for (unsigned int i = 0; i < peers.size(); i++) for (unsigned int i = 0; i < peers.size(); i++)
{ {
if (peers[i]->isSamePeer(*event->getPeer())) if (peers[i]->isSamePeer(event->getPeer()))
{ {
peer_id = i; peer_id = i;
} }