Moved public address data from NetworkManager to STKHost.

This commit is contained in:
hiker 2015-10-24 23:30:34 +11:00
parent 350f434c37
commit 34f0d1b349
14 changed files with 60 additions and 54 deletions

View File

@ -30,9 +30,6 @@
NetworkManager::NetworkManager()
{
m_public_address.lock();
m_public_address.getData().clear();
m_public_address.unlock();
m_game_setup = NULL;
} // NetworkManager
@ -190,14 +187,6 @@ void NetworkManager::disconnected()
m_peers.clear();
} // disconnected
//-----------------------------------------------------------------------------
void NetworkManager::setPublicAddress(const TransportAddress& addr)
{
m_public_address.lock();
m_public_address.getData().copy(addr);
m_public_address.unlock();
} // setPublicAddress
//-----------------------------------------------------------------------------
void NetworkManager::removePeer(STKPeer* peer)

View File

@ -57,10 +57,6 @@ protected:
private:
GameSetup* m_game_setup;
/** This computer's public IP address. With lock since it can
* be updated from a separate thread. */
Synchronised<TransportAddress> m_public_address;
PlayerLogin m_player_login;
@ -86,7 +82,6 @@ public:
virtual bool isServer() = 0;
// raw data management
void setPublicAddress(const TransportAddress& addr);
void removePeer(STKPeer* peer);
// getters
@ -109,18 +104,6 @@ public:
std::vector<STKPeer*> getPeers() { return m_peers; }
// --------------------------------------------------------------------
unsigned int getPeerCount() { return (int)m_peers.size(); }
// --------------------------------------------------------------------
/** Returns the public IP address (thread safe). The network manager
* is a friend of TransportAddress and so has access to the copy
* constructor, which is otherwise declared private. */
const TransportAddress getPublicAddress()
{
m_public_address.lock();
TransportAddress a;
a.copy(m_public_address.getData());
m_public_address.unlock();
return a;
} // getPublicAddress
// --------------------------------------------------------------------
/** Returns the current game setup. */

View File

@ -86,7 +86,7 @@ void ConnectToPeer::asynchronousUpdate()
if (m_peer_address.getIP() != 0 && m_peer_address.getPort() != 0)
{
// we're in the same lan (same public ip address) !!
if (m_peer_address.getIP() == NetworkManager::getInstance()->getPublicAddress().getIP())
if (m_peer_address.getIP() == STKHost::get()->getPublicAddress().getIP())
{
// just send a broadcast packet with the string aloha_stk inside, the client will know our ip address and will connect
TransportAddress broadcast_address;
@ -120,7 +120,7 @@ void ConnectToPeer::asynchronousUpdate()
case CONNECTED:
{
// the ping protocol is there for NAT traversal (disabled when connecting to LAN peer)
if (m_peer_address != NetworkManager::getInstance()->getPublicAddress())
if (m_peer_address != STKHost::get()->getPublicAddress())
{
// Kill the ping protocol because we're connected
ProtocolManager::getInstance()

View File

@ -100,7 +100,7 @@ void ConnectToServer::asynchronousUpdate()
{
Log::info("ConnectToServer", "Protocol starting");
m_current_protocol = new GetPublicAddress();
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
m_state = GETTING_SELF_ADDRESS;
break;
}
@ -110,7 +110,7 @@ void ConnectToServer::asynchronousUpdate()
{
m_state = SHOWING_SELF_ADDRESS;
m_current_protocol = new ShowPublicAddress();
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
Log::info("ConnectToServer", "Public address known");
}
break;
@ -123,14 +123,14 @@ void ConnectToServer::asynchronousUpdate()
{
m_current_protocol = new QuickJoinProtocol(&m_server_address,
&m_server_id);
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
m_state = REQUESTING_CONNECTION;
}
else
{
m_current_protocol = new GetPeerAddress(m_host_id,
&m_server_address);
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
m_state = GETTING_SERVER_ADDRESS;
}
}
@ -142,14 +142,14 @@ void ConnectToServer::asynchronousUpdate()
Log::info("ConnectToServer", "Server's address known");
// we're in the same lan (same public ip address) !!
if (m_server_address.getIP() ==
NetworkManager::getInstance()->getPublicAddress().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);
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
}
break;
case REQUESTING_CONNECTION:
@ -165,11 +165,11 @@ void ConnectToServer::asynchronousUpdate()
Log::error("ConnectToServer", "Server address is %s",
m_server_address.toString().c_str());
m_current_protocol = new HidePublicAddress();
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
return;
}
if (m_server_address.getIP() == NetworkManager::getInstance()
->getPublicAddress().getIP())
if (m_server_address.getIP()
== STKHost::get()->getPublicAddress().getIP())
{
// we're in the same lan (same public ip address) !!
handleSameLAN();
@ -178,7 +178,7 @@ void ConnectToServer::asynchronousUpdate()
{
m_state = CONNECTING;
m_current_protocol = new PingProtocol(m_server_address, 2.0);
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
}
}
break;
@ -198,9 +198,10 @@ void ConnectToServer::asynchronousUpdate()
{
Log::info("ConnectToServer", "Connected");
// Kill the ping protocol because we're connected
ProtocolManager::getInstance()->requestTerminate(m_current_protocol);
m_current_protocol->requestTerminate();
m_current_protocol = new HidePublicAddress();
ProtocolManager::getInstance()->requestStart(m_current_protocol);
m_current_protocol->requestStart();
ClientNetworkManager::getInstance()->setConnected(true);
m_state = HIDING_ADDRESS;
break;
@ -214,13 +215,13 @@ void ConnectToServer::asynchronousUpdate()
// lobby room protocol if we're connected only
if (ClientNetworkManager::getInstance()->isConnected())
{
ProtocolManager::getInstance()->requestStart(
new ClientLobbyRoomProtocol(m_server_address));
Protocol *p = new ClientLobbyRoomProtocol(m_server_address);
p->requestStart();
}
}
break;
case DONE:
ProtocolManager::getInstance()->requestTerminate(this);
requestTerminate();
m_state = EXITING;
break;
case EXITING:

View File

@ -66,8 +66,8 @@ void GetPeerAddress::asynchronousUpdate()
addr->setIP(ip);
uint16_t port;
if (addr->getIP() ==
NetworkManager::getInstance()->getPublicAddress().getIP())
if (addr->getIP() ==
STKHost::get()->getPublicAddress().getIP())
result->get("private_port", &port);
else
result->get("port", &port);

View File

@ -180,7 +180,7 @@ std::string GetPublicAddress::parseStunResponse()
Log::debug("GetPublicAddress",
"The public address has been found: %s",
address.toString().c_str());
NetworkManager::getInstance()->setPublicAddress(address);
STKHost::get()->setPublicAddress(address);
break;
} // type = 0 or 1
pos += 4 + size;

View File

@ -42,7 +42,7 @@ void QuickJoinProtocol::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress();
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "quick-join", Online::API::SERVER_PATH);

View File

@ -186,7 +186,7 @@ void ServerLobbyRoomProtocol::checkIncomingConnectionRequests()
if (StkTime::getRealTime() > last_poll_time+10.0)
{
last_poll_time = StkTime::getRealTime();
const TransportAddress &addr = NetworkManager::getInstance()->getPublicAddress();
const TransportAddress &addr = STKHost::get()->getPublicAddress();
Online::XMLRequest* request = new Online::XMLRequest();
PlayerManager::setUserDetails(request, "poll-connection-requests", Online::API::SERVER_PATH);

View File

@ -41,7 +41,7 @@ void ShowPublicAddress::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress();
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "set", Online::API::SERVER_PATH);

View File

@ -43,7 +43,7 @@ void StartServer::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress();
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "start", Online::API::SERVER_PATH);

View File

@ -45,7 +45,7 @@ void StopServer::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = NetworkManager::getInstance()->getPublicAddress();
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "stop", Online::API::SERVER_PATH);

View File

@ -51,6 +51,11 @@ STKHost::STKHost()
{
m_network = NULL;
m_listening_thread = NULL;
m_public_address.lock();
m_public_address.getData().clear();
m_public_address.unlock();
pthread_mutex_init(&m_exit_mutex, NULL);
Network::openLog();
@ -77,6 +82,14 @@ STKHost::~STKHost()
delete m_network;
} // ~STKHost
//-----------------------------------------------------------------------------
void STKHost::setPublicAddress(const TransportAddress& addr)
{
m_public_address.lock();
m_public_address.getData().copy(addr);
m_public_address.unlock();
} // setPublicAddress
// ----------------------------------------------------------------------------
/** \brief Starts the listening of events from ENet.
* Starts a thread for receiveData that updates it as often as possible.

View File

@ -71,6 +71,10 @@ private:
/** ENet host interfacing sockets. */
Network* m_network;
/** This computer's public IP address. With lock since it can
* be updated from a separate thread. */
Synchronised<TransportAddress> m_public_address;
/** Id of thread listening to enet events. */
pthread_t* m_listening_thread;
@ -110,6 +114,8 @@ public:
static void* mainLoop(void* self);
void setPublicAddress(const TransportAddress& addr);
void setupServer(uint32_t address, uint16_t port,
int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
@ -159,9 +165,23 @@ public:
return m_network->getENetHost()->address.host;
} // getAddress
// --------------------------------------------------------------------
/** Returns the public IP address (thread safe). The network manager
* is a friend of TransportAddress and so has access to the copy
* constructor, which is otherwise declared private. */
const TransportAddress getPublicAddress()
{
m_public_address.lock();
TransportAddress a;
a.copy(m_public_address.getData());
m_public_address.unlock();
return a;
} // getPublicAddress
// --------------------------------------------------------------------
/** Sets the maximum number of players for this server. */
static void setMaxPlayers(int n) { m_max_players = n; }
// --------------------------------------------------------------------
/** Returns the maximum number of players for this server. */
static int getMaxPlayers() { return m_max_players; }

View File

@ -73,7 +73,7 @@ public:
~TransportAddress() {}
// ------------------------------------------------------------------------
private:
friend class NetworkManager;
friend class STKHost;
/** The copy constructor is private, so that the friend class
* NetworkManager can access it to create a copy, but no other
* class can. */