Removed friend declaration; fixed crash caused by deleting the

ENetPeer when deleting an (STK) Event.
This commit is contained in:
hiker 2015-11-17 08:11:07 +11:00
parent 0ed22d0d6e
commit 29e0b50e14
5 changed files with 42 additions and 30 deletions

View File

@ -60,27 +60,7 @@ Event::Event(ENetEvent* event)
m_packet = NULL;
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
m_peer = NULL;
for (unsigned int i = 0; i < peers.size(); i++)
{
if (peers[i]->m_enet_peer == event->peer)
{
m_peer = peers[i];
Log::verbose("Event", "The peer you sought has been found on %p",
m_peer);
return;
}
}
if (m_peer == NULL) // peer does not exist, create him
{
STKPeer* new_peer = new STKPeer();
new_peer->m_enet_peer = event->peer;
m_peer = new_peer;
Log::debug("Event",
"Creating a new peer, address are STKPeer:%p, Peer:%p",
new_peer, event->peer);
}
m_peer = STKHost::get()->getPeer(event->peer);
} // Event(ENetEvent)
// ----------------------------------------------------------------------------
@ -88,7 +68,8 @@ Event::Event(ENetEvent* event)
*/
Event::~Event()
{
delete m_peer;
// Do not delete m_peer, it's a pointer to the enet data structure
// which is persistent.
m_peer = NULL;
m_packet = NULL;
} // ~Event

View File

@ -533,6 +533,28 @@ bool STKHost::peerExists(const TransportAddress& peer)
return false;
} // peerExists
// ----------------------------------------------------------------------------
/** Returns the STK peer belonging to the given enet_peer. If no STKPeer
* exists, create a new STKPeer.
* \param enet_peer The EnetPeer.
*/
STKPeer* STKHost::getPeer(ENetPeer *enet_peer)
{
for(unsigned int i=0; i<m_peers.size(); i++)
{
if(m_peers[i]->isSamePeer(enet_peer))
return m_peers[i];
}
//FIXME Should we check #clients here? It might be easier to only
// handle this at connect time, not in all getPeer calls.
STKPeer *peer = new STKPeer(enet_peer);
Log::debug("getPeer",
"Creating a new peer, address are STKPeer:%p, Peer:%p",
peer, enet_peer);
m_peers.push_back(peer);
return peer;
} // getPeer
// ----------------------------------------------------------------------------
/** \brief Tells if a peer is known and connected.
* \return True if the peer is known and connected, false elseway.

View File

@ -142,6 +142,7 @@ public:
bool peerExists(const TransportAddress& peer_address);
void removePeer(const STKPeer* peer);
bool isConnectedTo(const TransportAddress& peer_address);
STKPeer *getPeer(ENetPeer *enet_peer);
int mustStopListening();
uint16_t getPort() const;
void setErrorMessage(const irr::core::stringw &message);

View File

@ -26,9 +26,9 @@
/** Constructor for an empty peer.
*/
STKPeer::STKPeer()
STKPeer::STKPeer(ENetPeer *enet_peer)
{
m_enet_peer = NULL;
m_enet_peer = enet_peer;
m_player_profile = NULL;
m_client_server_token = 0;
m_token_set = false;
@ -39,8 +39,7 @@ STKPeer::STKPeer()
*/
STKPeer::~STKPeer()
{
if (m_enet_peer)
m_enet_peer = NULL;
m_enet_peer = NULL;
if (m_player_profile)
delete m_player_profile;
m_player_profile = NULL;
@ -105,11 +104,20 @@ bool STKPeer::exists() const
}
//-----------------------------------------------------------------------------
/** Returns if this STKPeer is the same as the given peer.
*/
bool STKPeer::isSamePeer(const STKPeer* peer) const
{
return peer->m_enet_peer==m_enet_peer;
}
} // isSamePeer
//-----------------------------------------------------------------------------
/** Returns if this STKPeer is the same as the given peer.
*/
bool STKPeer::isSamePeer(const ENetPeer* peer) const
{
return peer==m_enet_peer;
} // isSamePeer
//-----------------------------------------------------------------------------

View File

@ -38,7 +38,6 @@ class TransportAddress;
*/
class STKPeer : public NoCopy
{
friend class Event;
protected:
/** Pointer to the corresponding ENet peer data structure. */
ENetPeer* m_enet_peer;
@ -52,7 +51,7 @@ protected:
bool m_token_set;
public:
STKPeer();
STKPeer(ENetPeer *enet_peer);
virtual ~STKPeer();
virtual void sendPacket(const NetworkString& data, bool reliable = true);
@ -62,6 +61,7 @@ public:
uint32_t getAddress() const;
uint16_t getPort() const;
bool isSamePeer(const STKPeer* peer) const;
bool isSamePeer(const ENetPeer* peer) const;
// ------------------------------------------------------------------------
/** Sets the token for this client. */