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
This commit is contained in:
hilnius 2013-07-08 22:06:57 +00:00
parent 0ee94ed9ed
commit b1f823ea71
7 changed files with 44 additions and 10 deletions

View File

@ -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:

View File

@ -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();

View File

@ -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
};

View File

@ -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)
{

View File

@ -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);

9
src/network/types.cpp Normal file
View File

@ -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);
}

View File

@ -64,5 +64,7 @@ class PlayerLogin : public CallbackObject
std::string password; //!< Password of the player
};
uint32_t turnEndianness(uint32_t val);
#endif // TYPES_HPP