diff --git a/src/network/event.cpp b/src/network/event.cpp index c20cd581a..e507f3766 100644 --- a/src/network/event.cpp +++ b/src/network/event.cpp @@ -62,7 +62,7 @@ Event::Event(ENetEvent* event) m_peer = NULL; for (unsigned int i = 0; i < peers.size(); i++) { - if (peers[i]->m_peer == event->peer) + if (peers[i]->m_enet_peer == event->peer) { m_peer = peers[i]; Log::verbose("Event", "The peer you sought has been found on %p", @@ -73,7 +73,7 @@ Event::Event(ENetEvent* event) if (m_peer == NULL) // peer does not exist, create him { STKPeer* new_peer = new STKPeer(); - new_peer->m_peer = event->peer; + new_peer->m_enet_peer = event->peer; m_peer = new_peer; Log::debug("Event", "Creating a new peer, address are STKPeer:%p, Peer:%p", diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index 3dd33e1dc..c2825a0c8 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -96,12 +96,12 @@ void NetworkManager::abort() * \param peer : The transport address which you want to connect to. * \return True if we're successfully connected. False elseway. */ -bool NetworkManager::connect(const TransportAddress& peer) +bool NetworkManager::connect(const TransportAddress& address) { - if (peerExists(peer)) - return isConnectedTo(peer); + if (peerExists(address)) + return isConnectedTo(address); - return STKPeer::connectToHost(m_localhost, peer, 2, 0); + return STKPeer::connectToHost(m_localhost, address, 2, 0); } // connect //----------------------------------------------------------------------------- diff --git a/src/network/protocols/controller_events_protocol.cpp b/src/network/protocols/controller_events_protocol.cpp index 748c758c2..711daa96b 100644 --- a/src/network/protocols/controller_events_protocol.cpp +++ b/src/network/protocols/controller_events_protocol.cpp @@ -4,6 +4,7 @@ #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" #include "network/event.hpp" +#include "network/game_setup.hpp" #include "network/network_manager.hpp" #include "network/network_world.hpp" #include "utils/log.hpp" diff --git a/src/network/protocols/game_events_protocol.cpp b/src/network/protocols/game_events_protocol.cpp index 1dfc8e8e3..095451b77 100644 --- a/src/network/protocols/game_events_protocol.cpp +++ b/src/network/protocols/game_events_protocol.cpp @@ -7,6 +7,7 @@ #include "items/powerup.hpp" #include "modes/world.hpp" #include "network/event.hpp" +#include "network/game_setup.hpp" #include "network/network_manager.hpp" #include diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 23e76624d..73b545dca 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -18,54 +18,53 @@ #include "network/stk_peer.hpp" #include "network/network_manager.hpp" +#include "network/game_setup.hpp" +#include "network/network_string.hpp" #include "utils/log.hpp" #include +/** Constructor for an empty peer. + */ STKPeer::STKPeer() { - m_peer = NULL; - m_player_profile = new NetworkPlayerProfile*; - *m_player_profile = NULL; - m_client_server_token = new uint32_t; - *m_client_server_token = 0; - m_token_set = new bool; - *m_token_set = false; -} + m_enet_peer = NULL; + m_player_profile = NULL; + m_client_server_token = 0; + m_token_set = false; +} // STKPeer //----------------------------------------------------------------------------- - +/** Copy constructor. + */ STKPeer::STKPeer(const STKPeer& peer) { - m_peer = peer.m_peer; - m_player_profile = peer.m_player_profile; + m_enet_peer = peer.m_enet_peer; + m_player_profile = peer.m_player_profile; m_client_server_token = peer.m_client_server_token; - m_token_set = peer.m_token_set; -} + m_token_set = peer.m_token_set; +} // STKPeer //----------------------------------------------------------------------------- - +/** Destructor. + */ STKPeer::~STKPeer() { - if (m_peer) - m_peer = NULL; + if (m_enet_peer) + m_enet_peer = NULL; if (m_player_profile) delete m_player_profile; m_player_profile = NULL; - if (m_client_server_token) - delete m_client_server_token; - m_client_server_token = NULL; - if (m_token_set) - delete m_token_set; - m_token_set = NULL; -} + m_client_server_token = 0; +} // ~STKPeer //----------------------------------------------------------------------------- - +/** Connect to the specified host. + */ bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host, - uint32_t channel_count, uint32_t data) + uint32_t channel_count, uint32_t data) { - const ENetAddress address = host.toEnetAddress(); + const ENetAddress address = host.toEnetAddress(); ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0); if (peer == NULL) @@ -76,70 +75,70 @@ bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host, TransportAddress a(peer->address); Log::verbose("STKPeer", "Connecting to %s", a.toString().c_str()); return true; -} +} // connectToHost //----------------------------------------------------------------------------- - +/** Disconnect from the server. + */ void STKPeer::disconnect() { - enet_peer_disconnect(m_peer, 0); -} + enet_peer_disconnect(m_enet_peer, 0); +} // disconnect //----------------------------------------------------------------------------- - +/** Sends a packet to this host. + * \param data The data to send. + * \param reliable If the data is sent reliable or not. + */ void STKPeer::sendPacket(NetworkString const& data, bool reliable) { - TransportAddress a(m_peer->address); + TransportAddress a(m_enet_peer->address); Log::verbose("STKPeer", "sending packet of size %d to %s", a.toString().c_str()); ENetPacket* packet = enet_packet_create(data.getBytes(), data.size() + 1, - (reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED)); - /* to debug the packet output - printf("STKPeer: "); - for (unsigned int i = 0; i < data.size(); i++) - { - printf("%d ", (uint8_t)(data[i])); - } - printf("\n"); - */ - enet_peer_send(m_peer, 0, packet); -} + (reliable ? ENET_PACKET_FLAG_RELIABLE + : ENET_PACKET_FLAG_UNSEQUENCED)); + enet_peer_send(m_enet_peer, 0, packet); +} // sendPacket //----------------------------------------------------------------------------- - +/** Returns the IP address (in host format) of this client. + */ uint32_t STKPeer::getAddress() const { - return ntohl(m_peer->address.host); -} + return ntohl(m_enet_peer->address.host); +} // getAddress //----------------------------------------------------------------------------- - +/** Returns the port of this peer. + */ uint16_t STKPeer::getPort() const { - return m_peer->address.port; + return m_enet_peer->address.port; } //----------------------------------------------------------------------------- - +/** Returns if the peer is connected or not. + */ bool STKPeer::isConnected() const { - Log::info("STKPeer", "The peer state is %i", m_peer->state); - return (m_peer->state == ENET_PEER_STATE_CONNECTED); -} + Log::info("STKPeer", "The peer state is %i", m_enet_peer->state); + return (m_enet_peer->state == ENET_PEER_STATE_CONNECTED); +} // isConnected //----------------------------------------------------------------------------- bool STKPeer::exists() const { - return (m_peer != NULL); // assert that the peer exists + return (m_enet_peer != NULL); // assert that the peer exists } //----------------------------------------------------------------------------- bool STKPeer::isSamePeer(const STKPeer* peer) const { - return peer->m_peer==m_peer; + return peer->m_enet_peer==m_enet_peer; } //----------------------------------------------------------------------------- diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index 25263ee46..555189d70 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -23,11 +23,15 @@ #ifndef STK_PEER_HPP #define STK_PEER_HPP -#include "network/stk_host.hpp" -#include "network/network_string.hpp" -#include "network/game_setup.hpp" +#include "utils/types.hpp" + #include +class NetworkPlayerProfile; +class NetworkString; +class STKHost; +class TransportAddress; + /*! \class STKPeer * \brief Represents a peer. * This class is used to interface the ENetPeer structure. @@ -36,35 +40,58 @@ class STKPeer { friend class Event; protected: - ENetPeer* m_peer; - NetworkPlayerProfile** m_player_profile; - uint32_t *m_client_server_token; - bool *m_token_set; + /** Pointer to the corresponding ENet peer data structure. */ + ENetPeer* m_enet_peer; + + NetworkPlayerProfile* m_player_profile; + + /** The token of this client. */ + uint32_t m_client_server_token; + + /** True if the token for this peer has been set. */ + bool m_token_set; public: - STKPeer(); - STKPeer(const STKPeer& peer); - virtual ~STKPeer(); + STKPeer(); + STKPeer(const STKPeer& peer); + virtual ~STKPeer(); - virtual void sendPacket(const NetworkString& data, bool reliable = true); - static bool connectToHost(STKHost* localhost, const TransportAddress& host, - uint32_t channel_count, uint32_t data); - void disconnect(); + virtual void sendPacket(const NetworkString& data, bool reliable = true); + static bool connectToHost(STKHost* localhost, const TransportAddress& host, + uint32_t channel_count, uint32_t data); + void disconnect(); + bool isConnected() const; + bool exists() const; + uint32_t getAddress() const; + uint16_t getPort() const; + bool isSamePeer(const STKPeer* peer) const; - void setClientServerToken(const uint32_t& token) { *m_client_server_token = token; *m_token_set = true; } - void unsetClientServerToken() { *m_token_set = false; } - void setPlayerProfile(NetworkPlayerProfile* profile) { *m_player_profile = profile; } - void setPlayerProfilePtr(NetworkPlayerProfile** profile) { m_player_profile = profile; } - - bool isConnected() const; - bool exists() const; - uint32_t getAddress() const; - uint16_t getPort() const; - NetworkPlayerProfile* getPlayerProfile() { return (m_player_profile)?(*m_player_profile):NULL; } - uint32_t getClientServerToken() const { return *m_client_server_token; } - bool isClientServerTokenSet() const { return *m_token_set; } - - bool isSamePeer(const STKPeer* peer) const; + // ------------------------------------------------------------------------ + /** Sets the token for this client. */ + void setClientServerToken(const uint32_t& token) + { + m_client_server_token = token; + m_token_set = true; + } // setClientServerToken + // ------------------------------------------------------------------------ + void unsetClientServerToken() { m_token_set = false; } + // ------------------------------------------------------------------------ + void setPlayerProfile(NetworkPlayerProfile* profile) + { + m_player_profile = profile; + } // setPlayerProfile + // ------------------------------------------------------------------------ + /** Returns the player profile of this peer. */ + NetworkPlayerProfile* getPlayerProfile() + { + return m_player_profile; + } // getPlayerProfile + // ------------------------------------------------------------------------ + /** Returns the token of this client. */ + uint32_t getClientServerToken() const { return m_client_server_token; } + // ------------------------------------------------------------------------ + /** Returns if the token for this client is known. */ + bool isClientServerTokenSet() const { return m_token_set; } }; // STKPeer