now network GUI interacts properly with the server, can go up to the selection (still cannot ask for selection)

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13385 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius
2013-07-29 23:43:28 +00:00
parent 840faec44b
commit bcd15a8851
14 changed files with 106 additions and 14 deletions

View File

@@ -111,6 +111,17 @@ void ClientNetworkManager::run()
Log::info("ClientNetworkManager", "Ready !");
}
void ClientNetworkManager::reset()
{
NetworkManager::reset();
m_connected = false;
m_localhost = new STKHost();
m_localhost->setupClient(1, 2, 0, 0);
m_localhost->startListening();
}
void ClientNetworkManager::sendPacket(const NetworkString& data, bool reliable)
{
if (m_peers.size() > 1)

View File

@@ -31,6 +31,7 @@ class ClientNetworkManager : public NetworkManager
}
virtual void run();
virtual void reset();
virtual void sendPacket(const NetworkString& data, bool reliable = true);
STKPeer* getPeer();

View File

@@ -30,6 +30,12 @@ GameSetup::GameSetup()
GameSetup::~GameSetup()
{
// remove all players
for (unsigned int i = 0; i < m_players.size(); i++)
{
delete m_players[i];
};
m_players.clear();
}
//-----------------------------------------------------------------------------
@@ -87,7 +93,7 @@ void GameSetup::setPlayerKart(uint8_t id, std::string kart_name)
if (m_players[i]->race_id == id)
{
m_players[i]->kart_name = kart_name;
Log::info("GameSetup::setPlayerKart", "Player %d took kart %s",
Log::info("GameSetup::setPlayerKart", "Player %d took kart %s",
id, kart_name.c_str());
found = true;
}

View File

@@ -64,6 +64,18 @@ void NetworkManager::run()
ProtocolManager::getInstance<ProtocolManager>();
}
void NetworkManager::reset()
{
if (m_localhost)
delete m_localhost;
m_localhost = NULL;
while(!m_peers.empty())
{
delete m_peers.back();
m_peers.pop_back();
}
}
//-----------------------------------------------------------------------------
bool NetworkManager::connect(TransportAddress peer)
@@ -140,12 +152,30 @@ GameSetup* NetworkManager::setupNewGame()
{
if (m_game_setup)
delete m_game_setup;
m_game_setup = NULL;
m_game_setup = new GameSetup();
return m_game_setup;
}
//-----------------------------------------------------------------------------
void NetworkManager::disconnected()
{
// delete the game setup
if (m_game_setup)
delete m_game_setup;
m_game_setup = NULL;
// remove all peers
for (unsigned int i = 0; i < m_peers.size(); i++)
{
delete m_peers[i];
m_peers[i] = NULL;
}
m_peers.clear();
}
//-----------------------------------------------------------------------------
void NetworkManager::setLogin(std::string username, std::string password)
{
@@ -163,6 +193,8 @@ void NetworkManager::setPublicAddress(TransportAddress addr)
void NetworkManager::removePeer(STKPeer* peer)
{
if (!peer || !peer->exists()) // peer does not exist (already removed)
return;
Log::debug("NetworkManager", "Disconnected host: %i.%i.%i.%i:%i",
peer->getAddress()>>24&0xff,
peer->getAddress()>>16&0xff,

View File

@@ -35,6 +35,7 @@ class NetworkManager : public Singleton<NetworkManager>
friend class Singleton<NetworkManager>;
public:
virtual void run();
virtual void reset();
// network management functions
virtual bool connect(TransportAddress peer);
@@ -48,6 +49,7 @@ class NetworkManager : public Singleton<NetworkManager>
// Game related functions
virtual GameSetup* setupNewGame(); //!< Creates a new game setup and returns it
virtual void disconnected(); //!< Called when you leave a server
// raw data management
void setLogin(std::string username, std::string password);

View File

@@ -60,8 +60,9 @@ void ClientLobbyRoomProtocol::requestKartSelection(std::string kart_name)
void ClientLobbyRoomProtocol::leave()
{
assert(NetworkManager::getInstance()->getPeerCount() == 1);
NetworkManager::getInstance()->getPeers()[0]->disconnect(); // just dc
m_server->disconnect();
m_server_address.ip = 0;
m_server_address.port = 0;
}
//-----------------------------------------------------------------------------
@@ -96,8 +97,14 @@ void ClientLobbyRoomProtocol::notifyEvent(Event* event)
else if (event->type == EVENT_TYPE_CONNECTED)
{
} // connection
else if (event->type == EVENT_TYPE_DISCONNECTED)
else if (event->type == EVENT_TYPE_DISCONNECTED) // means we left essentially
{
NetworkManager::getInstance()->removePeer(m_server);
m_server = NULL;
NetworkManager::getInstance()->disconnected();
m_listener->requestTerminate(this);
NetworkManager::getInstance()->reset();
NetworkManager::getInstance()->removePeer(*event->peer); // prolly the same as m_server
} // disconnection
}

View File

@@ -159,6 +159,7 @@ void ConnectToServer::asynchronousUpdate()
{
timer = Time::getRealTime();
NetworkManager::getInstance()->connect(m_server_address);
Log::info("ConnectToServer", "Trying to connect");
}
break;
}

View File

@@ -47,5 +47,6 @@ void PingProtocol::asynchronousUpdate()
m_last_ping_time = Time::getRealTime();
uint8_t data = 0;
NetworkManager::getInstance()->getHost()->sendRawPacket(&data, 1, m_ping_dst);
Log::info("PingProtocol", "Ping message sent");
}
}

View File

@@ -85,7 +85,6 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host,
void STKPeer::disconnect()
{
enet_peer_disconnect(m_peer, 0);
NetworkManager::getInstance()->removePeer(this);
}
//-----------------------------------------------------------------------------
@@ -133,6 +132,13 @@ bool STKPeer::isConnected() const
//-----------------------------------------------------------------------------
bool STKPeer::exists() const
{
return (m_peer != NULL); // assert that the peer exists
}
//-----------------------------------------------------------------------------
bool STKPeer::isSamePeer(const STKPeer* peer) const
{
return peer->m_peer==m_peer;

View File

@@ -42,6 +42,7 @@ class STKPeer
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; }

View File

@@ -46,18 +46,23 @@ void NetworkKartSelectionScreen::eventCallback(GUIEngine::Widget* widget, const
}
else if (name == "back")
{
// first do the back action
KartSelectionScreen::eventCallback(widget, name, playerID);
// then remove the lobby screen (you left the server)
StateManager::get()->popMenu();
// and notify the server that you left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
if (protocol)
protocol->leave();
}
else // name != karts
{
KartSelectionScreen::eventCallback(widget, name, playerID);
}
} // eventCallback
bool NetworkKartSelectionScreen::onEscapePressed()
{
// then remove the lobby screen (you left the server)
StateManager::get()->popMenu();
// notify the server that we left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
if (protocol)
protocol->leave();
return true; // remove the screen
}

View File

@@ -12,9 +12,11 @@ protected:
virtual ~NetworkKartSelectionScreen();
public:
virtual void init();
virtual void init() OVERRIDE;
virtual void eventCallback(GUIEngine::Widget* widget, const std::string& name,
const int playerID) OVERRIDE;
virtual bool onEscapePressed() OVERRIDE;
};
#endif // NETWORK_KART_SELECTION_HPP

View File

@@ -33,6 +33,8 @@
#include "states_screens/online_screen.hpp"
#include "states_screens/state_manager.hpp"
#include "states_screens/dialogs/message_dialog.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp"
#include "modes/demo_world.hpp"
#include "utils/translation.hpp"
#include "online/servers_manager.hpp"
@@ -121,6 +123,18 @@ void NetworkingLobby::tearDown()
{
} // tearDown
// ----------------------------------------------------------------------------
bool NetworkingLobby::onEscapePressed()
{
// notify the server that we left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
if (protocol)
protocol->leave();
return true; // close the screen
}
// ----------------------------------------------------------------------------
void NetworkingLobby::onDisabledItemClicked(const std::string& item)
{

View File

@@ -72,6 +72,9 @@ public:
/** \brief implement callback from parent class GUIEngine::Screen */
virtual void tearDown() OVERRIDE;
/** \brief implement callback from parent class GUIEngine::Screen */
virtual bool onEscapePressed() OVERRIDE;
/** \brief implement callback from parent class GUIEngine::Screen */
virtual void onDisabledItemClicked(const std::string& item) OVERRIDE;