diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index ade09a6c9..015a8a497 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -86,7 +86,7 @@ void NetworkManager::abort() //----------------------------------------------------------------------------- -bool NetworkManager::connect(TransportAddress peer) +bool NetworkManager::connect(const TransportAddress& peer) { if (peerExists(peer)) return isConnectedTo(peer); @@ -193,9 +193,9 @@ void NetworkManager::setLogin(std::string username, std::string password) //----------------------------------------------------------------------------- -void NetworkManager::setPublicAddress(TransportAddress addr) +void NetworkManager::setPublicAddress(const TransportAddress& addr) { - m_public_address = addr; + m_public_address.copy(addr); } //----------------------------------------------------------------------------- @@ -233,14 +233,14 @@ void NetworkManager::removePeer(STKPeer* peer) //----------------------------------------------------------------------------- -bool NetworkManager::peerExists(TransportAddress peer) +bool NetworkManager::peerExists(const TransportAddress& peer) { return m_localhost->peerExists(peer); } //----------------------------------------------------------------------------- -bool NetworkManager::isConnectedTo(TransportAddress peer) +bool NetworkManager::isConnectedTo(const TransportAddress& peer) { return m_localhost->isConnectedTo(peer); } diff --git a/src/network/network_manager.hpp b/src/network/network_manager.hpp index ada420fab..c943b8630 100644 --- a/src/network/network_manager.hpp +++ b/src/network/network_manager.hpp @@ -64,7 +64,7 @@ class NetworkManager : public AbstractSingleton * \param peer : The transport address which you want to connect to. * \return True if we're successfully connected. False elseway. */ - virtual bool connect(TransportAddress peer); + virtual bool connect(const TransportAddress& peer); /** \brief Changes the socket working mode. * Sockets can be in two modes : The ENet mode and a mode we will call * the 'Raw' mode. In the ENet mode, the socket will be read as @@ -90,12 +90,12 @@ class NetworkManager : public AbstractSingleton // raw data management void setLogin(std::string username, std::string password); - void setPublicAddress(TransportAddress addr); + void setPublicAddress(const TransportAddress& addr); void removePeer(STKPeer* peer); // getters - virtual bool peerExists(TransportAddress peer); - virtual bool isConnectedTo(TransportAddress peer); + virtual bool peerExists(const TransportAddress& peer); + virtual bool isConnectedTo(const TransportAddress& peer); virtual bool isServer() = 0; inline bool isClient() { return !isServer(); } @@ -103,7 +103,7 @@ class NetworkManager : public AbstractSingleton STKHost* getHost() { return m_localhost; } std::vector getPeers() { return m_peers; } unsigned int getPeerCount() { return (int)m_peers.size(); } - TransportAddress getPublicAddress() { return m_public_address; } + const TransportAddress& getPublicAddress() { return m_public_address; } GameSetup* getGameSetup() { return m_game_setup; } protected: diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index ff3ba69bb..71cedd08f 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -31,7 +31,7 @@ ClientLobbyRoomProtocol::ClientLobbyRoomProtocol(const TransportAddress& server_address) : LobbyRoomProtocol(NULL) { - m_server_address = server_address; + m_server_address.copy(server_address); m_server = NULL; } // ClientLobbyRoomProtocol diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 08de8cf6c..75a401ee8 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -233,7 +233,7 @@ void ConnectToServer::asynchronousUpdate() delete[] table; #endif - m_server_address = sender; + m_server_address.copy(sender); m_state = CONNECTING; } } diff --git a/src/network/protocols/get_public_address.cpp b/src/network/protocols/get_public_address.cpp index 5f92e94fb..76e55710c 100644 --- a/src/network/protocols/get_public_address.cpp +++ b/src/network/protocols/get_public_address.cpp @@ -182,7 +182,7 @@ std::string GetPublicAddress::parseStunResponse() Log::debug("GetPublicAddress", "The public address has been found: %s", address.toString().c_str()); TransportAddress* addr = static_cast(m_callback_object); - *addr = address; + addr->copy(address); // The address and the port are known, so the connection can be closed m_state = EXITING; diff --git a/src/network/protocols/ping_protocol.cpp b/src/network/protocols/ping_protocol.cpp index 045062668..c287adaba 100644 --- a/src/network/protocols/ping_protocol.cpp +++ b/src/network/protocols/ping_protocol.cpp @@ -21,9 +21,10 @@ #include "network/network_manager.hpp" #include "utils/time.hpp" -PingProtocol::PingProtocol(const TransportAddress& ping_dst, double delay_between_pings) : Protocol(NULL, PROTOCOL_SILENT) +PingProtocol::PingProtocol(const TransportAddress& ping_dst, double delay_between_pings) + : Protocol(NULL, PROTOCOL_SILENT) { - m_ping_dst = ping_dst; + m_ping_dst.copy(ping_dst); m_delay_between_pings = delay_between_pings; } diff --git a/src/network/protocols/quick_join_protocol.cpp b/src/network/protocols/quick_join_protocol.cpp index 0f20fa540..988f7443d 100644 --- a/src/network/protocols/quick_join_protocol.cpp +++ b/src/network/protocols/quick_join_protocol.cpp @@ -42,7 +42,7 @@ void QuickJoinProtocol::asynchronousUpdate() { if (m_state == NONE) { - TransportAddress addr = NetworkManager::getInstance()->getPublicAddress(); + const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress(); m_request = new Online::XMLRequest(); PlayerManager::setUserDetails(m_request, "quick-join", Online::API::SERVER_PATH); diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index 63b4f1d08..16448ee49 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -180,7 +180,7 @@ void ServerLobbyRoomProtocol::checkIncomingConnectionRequests() if (StkTime::getRealTime() > last_poll_time+10.0) { last_poll_time = StkTime::getRealTime(); - TransportAddress addr = NetworkManager::getInstance()->getPublicAddress(); + const TransportAddress &addr = NetworkManager::getInstance()->getPublicAddress(); Online::XMLRequest* request = new Online::XMLRequest(); PlayerManager::setUserDetails(request, "poll-connection-requests", Online::API::SERVER_PATH); diff --git a/src/network/protocols/show_public_address.cpp b/src/network/protocols/show_public_address.cpp index 6a7362469..f48ee9467 100644 --- a/src/network/protocols/show_public_address.cpp +++ b/src/network/protocols/show_public_address.cpp @@ -41,7 +41,7 @@ void ShowPublicAddress::asynchronousUpdate() { if (m_state == NONE) { - TransportAddress addr = NetworkManager::getInstance()->getPublicAddress(); + const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress(); m_request = new Online::XMLRequest(); PlayerManager::setUserDetails(m_request, "set", Online::API::SERVER_PATH); diff --git a/src/network/protocols/start_server.cpp b/src/network/protocols/start_server.cpp index f7321fd8d..620470246 100644 --- a/src/network/protocols/start_server.cpp +++ b/src/network/protocols/start_server.cpp @@ -40,7 +40,7 @@ void StartServer::asynchronousUpdate() { if (m_state == NONE) { - TransportAddress addr = NetworkManager::getInstance()->getPublicAddress(); + const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress(); m_request = new Online::XMLRequest(); PlayerManager::setUserDetails(m_request, "start", Online::API::SERVER_PATH); diff --git a/src/network/protocols/stop_server.cpp b/src/network/protocols/stop_server.cpp index 559f14ece..ba2c9e67f 100644 --- a/src/network/protocols/stop_server.cpp +++ b/src/network/protocols/stop_server.cpp @@ -45,7 +45,7 @@ void StopServer::asynchronousUpdate() { if (m_state == NONE) { - TransportAddress addr = NetworkManager::getInstance()->getPublicAddress(); + const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress(); m_request = new Online::XMLRequest(); PlayerManager::setUserDetails(m_request, "stop", Online::API::SERVER_PATH); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index aef9eeafb..71345be07 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -201,7 +201,7 @@ void STKHost::stopListening() // ---------------------------------------------------------------------------- -void STKHost::sendRawPacket(uint8_t* data, int length, TransportAddress dst) +void STKHost::sendRawPacket(uint8_t* data, int length, const TransportAddress& dst) { struct sockaddr_in to; int to_len = sizeof(to); @@ -278,7 +278,7 @@ uint8_t* STKHost::receiveRawPacket(TransportAddress* sender) // ---------------------------------------------------------------------------- -uint8_t* STKHost::receiveRawPacket(TransportAddress sender, int max_tries) +uint8_t* STKHost::receiveRawPacket(const TransportAddress& sender, int max_tries) { uint8_t* buffer; // max size needed normally (only used for stun) buffer = (uint8_t*)(malloc(sizeof(uint8_t)*2048)); @@ -333,7 +333,7 @@ void STKHost::broadcastPacket(const NetworkString& data, bool reliable) // ---------------------------------------------------------------------------- -bool STKHost::peerExists(TransportAddress peer) +bool STKHost::peerExists(const TransportAddress& peer) { for (unsigned int i = 0; i < m_host->peerCount; i++) { @@ -348,7 +348,7 @@ bool STKHost::peerExists(TransportAddress peer) // ---------------------------------------------------------------------------- -bool STKHost::isConnectedTo(TransportAddress peer) +bool STKHost::isConnectedTo(const TransportAddress& peer) { for (unsigned int i = 0; i < m_host->peerCount; i++) { diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index d293a603e..8f9b38d8f 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -118,7 +118,7 @@ class STKHost * \param dst : Destination of the packet. */ void sendRawPacket(uint8_t* data, int length, - TransportAddress dst); + const TransportAddress& dst); /*! \brief Receives a packet directly from the network interface. * Receive a packet whithout ENet processing it. * \return A string containing the data of the received packet. @@ -138,7 +138,7 @@ class STKHost * \return A string containing the data of the received packet * matching the sender's ip address. */ - uint8_t* receiveRawPacket(TransportAddress sender, int max_tries = -1); + uint8_t* receiveRawPacket(const TransportAddress& sender, int max_tries = -1); /*! \brief Broadcasts a packet to all peers. * \param data : Data to send. */ @@ -147,11 +147,11 @@ class STKHost /*! \brief Tells if a peer is known. * \return True if the peer is known, false elseway. */ - bool peerExists(TransportAddress peer_address); + bool peerExists(const TransportAddress& peer_address); /*! \brief Tells if a peer is known and connected. * \return True if the peer is known and connected, false elseway. */ - bool isConnectedTo(TransportAddress peer_address); + bool isConnectedTo(const TransportAddress& peer_address); /*! \brief Returns true when the thread should stop listening. */ int mustStopListening(); diff --git a/src/network/types.hpp b/src/network/types.hpp index 34edf0f9a..3ed6ddc2a 100644 --- a/src/network/types.hpp +++ b/src/network/types.hpp @@ -22,6 +22,7 @@ #ifndef TYPES_HPP #define TYPES_HPP +#include "utils/no_copy.hpp" #include "utils/string_utils.hpp" #include "utils/types.hpp" @@ -46,7 +47,7 @@ class CallbackObject * \brief Describes a transport-layer address. * For IP networks, a transport address is the couple ip:port. */ -class TransportAddress : public CallbackObject +class TransportAddress : public CallbackObject, public NoCopy { private: uint32_t m_ip; //!< The IPv4 address @@ -70,6 +71,15 @@ public: // ------------------------------------------------------------------------ ~TransportAddress() {} + // ------------------------------------------------------------------------ + /** A copy function (to replace the copy constructor which is disabled + * using NoCopy). */ + void copy(const TransportAddress &other) + { + m_ip = other.m_ip; + m_port = other.m_port; + } // copy + // ------------------------------------------------------------------------ /** Resets ip and port to 0. */ void clear()