diff --git a/src/network/event.cpp b/src/network/event.cpp index 3db7c6955..2c3d5658f 100644 --- a/src/network/event.cpp +++ b/src/network/event.cpp @@ -60,27 +60,7 @@ Event::Event(ENetEvent* event) m_packet = NULL; const std::vector &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 diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 5b3390985..730df65df 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -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; iisSamePeer(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. diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 78ac4fb03..3cb590c32 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -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); diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index bb5e3c495..fc6dd975b 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -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 //----------------------------------------------------------------------------- diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index 6a71f2de8..3c9bc7b30 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -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. */