From b1f823ea7192cdadb075308d449ecc5f6f6838f3 Mon Sep 17 00:00:00 2001 From: hilnius Date: Mon, 8 Jul 2013 22:06:57 +0000 Subject: [PATCH] hmmm that's so good ! working connection, better protocols, fixed ALL the endianness problems (sure this time :p) git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/networking@13131 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/network/protocols/connect_to_server.cpp | 2 ++ src/network/protocols/lobby_room_protocol.cpp | 30 +++++++++++++++---- src/network/protocols/lobby_room_protocol.hpp | 5 +++- src/network/stk_host.cpp | 4 +-- src/network/stk_peer.cpp | 2 +- src/network/types.cpp | 9 ++++++ src/network/types.hpp | 2 ++ 7 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 src/network/types.cpp diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index a386d4246..4f562026a 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -25,6 +25,7 @@ #include "network/protocols/hide_public_address.hpp" #include "network/protocols/request_connection.hpp" #include "network/protocols/ping_protocol.hpp" +#include "network/protocols/lobby_room_protocol.hpp" #include "online/current_online_user.hpp" #include "utils/time.hpp" #include "utils/log.hpp" @@ -133,6 +134,7 @@ void ConnectToServer::update() == PROTOCOL_STATE_TERMINATED) // we have hidden our address { m_state = DONE; + m_listener->requestStart(new ClientLobbyRoomProtocol(m_server_address)); } break; case DONE: diff --git a/src/network/protocols/lobby_room_protocol.cpp b/src/network/protocols/lobby_room_protocol.cpp index 3adf82b0e..3693de62b 100644 --- a/src/network/protocols/lobby_room_protocol.cpp +++ b/src/network/protocols/lobby_room_protocol.cpp @@ -51,6 +51,7 @@ void ClientLobbyRoomProtocol::setup() { m_setup = NetworkManager::getInstance()->setupNewGame(); // create a new setup m_state = NONE; + Log::info("ClientLobbyRoomProtocol", "Starting the protocol."); } //----------------------------------------------------------------------------- @@ -121,6 +122,7 @@ void ClientLobbyRoomProtocol::notifyEvent(Event* event) profile.user_profile = CurrentOnlineUser::get(); m_setup->addPlayer(profile); event->peer->setClientServerToken(token); + m_state = CONNECTED; } } // connection accepted else if (message_type == 0b10000000) // connection refused @@ -164,7 +166,8 @@ void ServerLobbyRoomProtocol::notifyEvent(Event* event) { assert(event->data.size()); // message not empty uint8_t message_type; - event->data.gui8(&message_type); + message_type = event->data.getAndRemoveUInt8(); + Log::info("ServerLobbyRoomProtocol", "Message received with type %d.", message_type); if (message_type == 1) // player requesting connection { if (event->data.size() != 5 || event->data[0] != 4) @@ -231,6 +234,21 @@ void ClientLobbyRoomProtocol::update() switch (m_state) { case NONE: + if (NetworkManager::getInstance()->isConnectedTo(m_server_address)) + { + m_state = LINKED; + } + break; + case LINKED: + { + NetworkString ns; + // 1 (connection request), 4 (size of id), global id + ns.ai8(1).ai8(4).ai32(CurrentOnlineUser::get()->getUserID()); + m_listener->sendMessage(this, ns); + m_state = REQUESTING_CONNECTION; + break; + } + case REQUESTING_CONNECTION: break; case CONNECTED: break; @@ -247,20 +265,20 @@ void ServerLobbyRoomProtocol::update() switch (m_state) { case NONE: - m_current_protocol_id = ProtocolManager::getInstance()->requestStart(new GetPublicAddress(&m_public_address)); + m_current_protocol_id = m_listener->requestStart(new GetPublicAddress(&m_public_address)); m_state = GETTING_PUBLIC_ADDRESS; break; case GETTING_PUBLIC_ADDRESS: - if (ProtocolManager::getInstance()->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) + if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) { NetworkManager::getInstance()->setPublicAddress(m_public_address); - m_current_protocol_id = ProtocolManager::getInstance()->requestStart(new StartServer()); + m_current_protocol_id = m_listener->requestStart(new StartServer()); m_state = LAUNCHING_SERVER; Log::info("ServerLobbyRoomProtocol", "Public address known."); } break; case LAUNCHING_SERVER: - if (ProtocolManager::getInstance()->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) + if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) { m_state = WORKING; Log::info("ServerLobbyRoomProtocol", "Server setup"); @@ -299,7 +317,7 @@ void ServerLobbyRoomProtocol::update() // now for (int i = 0; i < m_incoming_peers_ids.size(); i++) { - ProtocolManager::getInstance()->requestStart(new ConnectToPeer(m_incoming_peers_ids[i])); + m_listener->requestStart(new ConnectToPeer(m_incoming_peers_ids[i])); } m_incoming_peers_ids.clear(); diff --git a/src/network/protocols/lobby_room_protocol.hpp b/src/network/protocols/lobby_room_protocol.hpp index 0866dada2..72ba74a1f 100644 --- a/src/network/protocols/lobby_room_protocol.hpp +++ b/src/network/protocols/lobby_room_protocol.hpp @@ -47,7 +47,8 @@ class LobbyRoomProtocol : public Protocol class ClientLobbyRoomProtocol : public LobbyRoomProtocol { public: - ClientLobbyRoomProtocol() : LobbyRoomProtocol(NULL) {} + ClientLobbyRoomProtocol(const TransportAddress& server_address) : LobbyRoomProtocol(NULL) + { m_server_address = server_address;} virtual ~ClientLobbyRoomProtocol() {} virtual void notifyEvent(Event* event); @@ -62,6 +63,8 @@ class ClientLobbyRoomProtocol : public LobbyRoomProtocol enum STATE { NONE, + LINKED, + REQUESTING_CONNECTION, CONNECTED, DONE }; diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index e4dc12fb9..8da311ae1 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -207,7 +207,7 @@ bool STKHost::peerExists(TransportAddress peer) { for (unsigned int i = 0; i < m_host->peerCount; i++) { - if (m_host->peers[i].address.host == peer.ip && + if (m_host->peers[i].address.host == turnEndianness(peer.ip) && m_host->peers[i].address.port == peer.port) { return true; @@ -222,7 +222,7 @@ bool STKHost::isConnectedTo(TransportAddress peer) { for (unsigned int i = 0; i < m_host->peerCount; i++) { - if (m_host->peers[i].address.host == peer.ip && + if (m_host->peers[i].address.host == turnEndianness(peer.ip) && m_host->peers[i].address.port == peer.port && m_host->peers[i].state == ENET_PEER_STATE_CONNECTED) { diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 2ed22c691..90bcc2dd9 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -43,7 +43,7 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, uint32_t ((host.ip & 0xff000000) >> 24) + ((host.ip & 0x00ff0000) >> 8) + ((host.ip & 0x0000ff00) << 8) - + ((host.ip & 0x000000ff) << 24); + + ((host.ip & 0x000000ff) << 24); // because ENet wants little endian address.port = host.port; ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0); diff --git a/src/network/types.cpp b/src/network/types.cpp new file mode 100644 index 000000000..deb244c32 --- /dev/null +++ b/src/network/types.cpp @@ -0,0 +1,9 @@ +#include "network/types.hpp" + +uint32_t turnEndianness(uint32_t val) +{ + return ((val&0xff000000)>>24) + +((val&0x00ff0000)>>8) + +((val&0x0000ff00)<<8) + +((val&0x000000ff)<<24); +} diff --git a/src/network/types.hpp b/src/network/types.hpp index dd5b62709..891af7c86 100644 --- a/src/network/types.hpp +++ b/src/network/types.hpp @@ -64,5 +64,7 @@ class PlayerLogin : public CallbackObject std::string password; //!< Password of the player }; +uint32_t turnEndianness(uint32_t val); + #endif // TYPES_HPP