Removed ShowPublicAddress protocol, instead submit blocking

request in ConnectToServer.
This commit is contained in:
hiker
2015-10-30 16:45:43 +11:00
parent dd289dc1cd
commit 94bc18df52
9 changed files with 103 additions and 211 deletions

View File

@@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@@ -30,18 +30,6 @@
#include <iostream>
#ifdef XX
#include "network/protocols/connect_to_server.hpp"
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/hide_public_address.hpp"
#include "network/protocols/show_public_address.hpp"
#include <enet/enet.h>
#include <pthread.h>
#include <string>
#include <stdlib.h>
#endif
NetworkConsole::NetworkConsole()
{

View File

@@ -21,7 +21,6 @@
#include "network/event.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/show_public_address.hpp"
#include "network/protocols/hide_public_address.hpp"
#include "network/protocols/request_connection.hpp"
#include "network/protocols/ping_protocol.hpp"

View File

@@ -22,7 +22,6 @@
#include "network/event.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/show_public_address.hpp"
#include "network/protocols/hide_public_address.hpp"
#include "network/protocols/request_connection.hpp"
#include "network/protocols/ping_protocol.hpp"
@@ -104,62 +103,59 @@ void ConnectToServer::asynchronousUpdate()
case NONE:
{
Log::info("ConnectToServer", "Protocol starting");
m_current_protocol = new GetPublicAddress();
// This protocol will write the public address of this
// instance to STKHost.
m_current_protocol = new GetPublicAddress(this);
m_current_protocol->requestStart();
// This protocol will be unpaused in the callback from
// GetPublicAddress
ProtocolManager::getInstance()->pauseProtocol(this);
m_state = GETTING_SELF_ADDRESS;
break;
}
case GETTING_SELF_ADDRESS:
// Wait till we know the public address
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
m_state = SHOWING_SELF_ADDRESS;
delete m_current_protocol;
m_current_protocol = new ShowPublicAddress();
m_current_protocol->requestStart();
Log::info("ConnectToServer", "Public address known");
}
break;
case SHOWING_SELF_ADDRESS:
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
delete m_current_protocol;
{
delete m_current_protocol;
m_current_protocol = NULL;
m_current_protocol = NULL;
// now our public address is in the database
Log::info("ConnectToServer", "Public address shown");
if (m_quick_join)
{
handleQuickConnect();
}
else
{
m_current_protocol = new GetPeerAddress(m_host_id, this);
m_current_protocol->requestStart();
m_state = GETTING_SERVER_ADDRESS;
// Pause this protocol till GetPeerAddress finishes.
// The callback then will unpause this protocol/
ProtocolManager::getInstance()->pauseProtocol(this);
}
}
break;
case GETTING_SERVER_ADDRESS:
registerWithSTKServer();
if (m_quick_join)
{
if(m_current_protocol)
delete m_current_protocol;
Log::info("ConnectToServer", "Server's address known");
// we're in the same lan (same public ip address) !!
if (m_server_address.getIP() ==
STKHost::get()->getPublicAddress().getIP())
{
Log::info("ConnectToServer",
"Server appears to be in the same LAN.");
}
handleQuickConnect();
m_state = REQUESTING_CONNECTION;
m_current_protocol = new RequestConnection(m_server_id);
m_current_protocol->requestStart();
}
else
{
// No quick connect, so we have a server to connect to.
// Find its address
m_current_protocol = new GetPeerAddress(m_host_id, this);
m_current_protocol->requestStart();
m_state = GETTING_SERVER_ADDRESS;
// Pause this protocol till GetPeerAddress finishes.
// The callback then will unpause this protocol/
ProtocolManager::getInstance()->pauseProtocol(this);
}
}
break;
case GETTING_SERVER_ADDRESS:
{
assert(!m_quick_join);
delete m_current_protocol;
m_current_protocol = NULL;
Log::info("ConnectToServer", "Server's address known");
// we're in the same lan (same public ip address) !!
if (m_server_address.getIP() ==
STKHost::get()->getPublicAddress().getIP())
{
Log::info("ConnectToServer",
"Server appears to be in the same LAN.");
}
m_state = REQUESTING_CONNECTION;
m_current_protocol = new RequestConnection(m_server_id);
m_current_protocol->requestStart();
break;
}
case REQUESTING_CONNECTION:
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
@@ -240,12 +236,61 @@ void ConnectToServer::asynchronousUpdate()
// ----------------------------------------------------------------------------
/** Called when the GetPeerAddress protocol terminates.
*/
*/
void ConnectToServer::callback(Protocol *protocol)
{
ProtocolManager::getInstance()->unpauseProtocol(this);
// Callbacks are only expected when getting either its owb public address
// or the server address.
if(m_state == GETTING_SELF_ADDRESS ||
m_state == GETTING_SERVER_ADDRESS )
{
ProtocolManager::getInstance()->unpauseProtocol(this);
}
else
{
Log::error("ConnectToServer",
"Received unexpected callback while in state %d.", m_state);
}
} // callback
// ----------------------------------------------------------------------------
/** Register this client with the STK server.
*/
void ConnectToServer::registerWithSTKServer()
{
// Our public address is now known, register details with
// STK server.
const TransportAddress& addr = STKHost::get()->getPublicAddress();
Online::XMLRequest *request = new Online::XMLRequest();
PlayerManager::setUserDetails(request, "set",
Online::API::SERVER_PATH);
request->addParameter("address", addr.getIP());
request->addParameter("port", addr.getPort());
request->addParameter("private_port", STKHost::get()->getPort());
Log::info("ConnectToServer", "Registering addr %s",
addr.toString().c_str());
// This can be done blocking: till we are registered with the
// stk server, there is no need to to react to any other
// network requests
request->executeNow();
const XMLNode * result = request->getXMLData();
delete request;
std::string success;
if(result->get("success", &success) && success == "yes")
{
Log::debug("ConnectToServer", "Address registered successfully.");
}
else
{
Log::error("ConnectToServer", "Failed to register address.");
}
} // registerWithSTKServer
// ----------------------------------------------------------------------------
/** Called to request a quick connect from the STK server.
*/
@@ -279,7 +324,7 @@ void ConnectToServer::handleQuickConnect()
}
else
{
Log::error("GetPeerAddress", "Fail to get address.");
Log::error("GetPeerAddress", "Failed to get address.");
}
} // handleQuickConnect

View File

@@ -35,7 +35,8 @@ private:
Protocol *m_current_protocol;
bool m_quick_join;
enum State
/** State for finite state machine. */
enum
{
NONE,
GETTING_SELF_ADDRESS,
@@ -48,11 +49,9 @@ private:
HIDING_ADDRESS,
DONE,
EXITING
};
/** State for finite state machine. */
State m_state;
} m_state;
void registerWithSTKServer();
void handleQuickConnect();
void handleSameLAN();

View File

@@ -56,16 +56,16 @@ void HidePublicAddress::asynchronousUpdate()
{
if(rec_success == "yes")
{
Log::debug("ShowPublicAddress", "Address hidden successfully.");
Log::debug("HidePublicAddress", "Address hidden successfully.");
}
else
{
Log::error("ShowPublicAddress", "Fail to hide address.");
Log::error("HidePublicAddress", "Fail to hide address.");
}
}
else
{
Log::error("ShowPublicAddress", "Fail to hide address.");
Log::error("HidePublicAddress", "Fail to hide address.");
}
m_state = DONE;
}

View File

@@ -24,7 +24,6 @@
#include "network/event.hpp"
#include "network/network_world.hpp"
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/show_public_address.hpp"
#include "network/protocols/connect_to_peer.hpp"
#include "network/protocols/start_game_protocol.hpp"
#include "network/protocol_manager.hpp"

View File

@@ -1,86 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/show_public_address.hpp"
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "network/stk_host.hpp"
#include "online/request_manager.hpp"
#include "utils/log.hpp"
ShowPublicAddress::ShowPublicAddress() : Protocol(PROTOCOL_SILENT)
{
}
ShowPublicAddress::~ShowPublicAddress()
{
}
void ShowPublicAddress::setup()
{
m_state = NONE;
}
void ShowPublicAddress::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "set", Online::API::SERVER_PATH);
m_request->addParameter("address", addr.getIP());
m_request->addParameter("port", addr.getPort());
m_request->addParameter("private_port", STKHost::get()->getPort());
Log::info("ShowPublicAddress", "Showing addr %s", addr.toString().c_str());
Online::RequestManager::get()->addRequest(m_request);
m_state = REQUEST_PENDING;
}
else if (m_state == REQUEST_PENDING && m_request->isDone())
{
const XMLNode * result = m_request->getXMLData();
std::string rec_success;
if(result->get("success", &rec_success))
{
if(rec_success == "yes")
{
Log::debug("ShowPublicAddress", "Address shown successfully.");
}
else
{
Log::error("ShowPublicAddress", "Fail to show address.");
}
}
else
{
Log::error("ShowPublicAddress", "Fail to show address.");
}
m_state = DONE;
}
else if (m_state == DONE)
{
m_state = EXITING;
delete m_request;
m_request = NULL;
requestTerminate();
}
} // asynchronousUpdate

View File

@@ -1,52 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef SHOW_PUBLIC_ADDRESS_HPP
#define SHOW_PUBLIC_ADDRESS_HPP
#include "network/protocol.hpp"
#include <string>
namespace Online { class XMLRequest; }
class ShowPublicAddress : public Protocol
{
public:
ShowPublicAddress();
virtual ~ShowPublicAddress();
virtual bool notifyEvent(Event* event) { return true; }
virtual bool notifyEventAsynchronous(Event* event) { return true; }
virtual void setup();
virtual void update() {}
virtual void asynchronousUpdate();
protected:
Online::XMLRequest* m_request;
enum STATE
{
NONE,
REQUEST_PENDING,
DONE,
EXITING
};
STATE m_state;
};
#endif // HIDE_PUBLIC_ADDRESS_HPP