Coding style changes.
This commit is contained in:
parent
9b9fc2ee35
commit
b8837d5d59
@ -19,6 +19,7 @@
|
|||||||
#include "network/network_manager.hpp"
|
#include "network/network_manager.hpp"
|
||||||
|
|
||||||
#include "network/event.hpp"
|
#include "network/event.hpp"
|
||||||
|
#include "network/game_setup.hpp"
|
||||||
#include "network/protocol_manager.hpp"
|
#include "network/protocol_manager.hpp"
|
||||||
#include "utils/log.hpp"
|
#include "utils/log.hpp"
|
||||||
|
|
||||||
@ -52,6 +53,8 @@ NetworkManager::~NetworkManager()
|
|||||||
} // ~Networkmanager
|
} // ~Networkmanager
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
/** \brief Function to start the Network Manager (start threads).
|
||||||
|
*/
|
||||||
void NetworkManager::run()
|
void NetworkManager::run()
|
||||||
{
|
{
|
||||||
// create the protocol manager
|
// create the protocol manager
|
||||||
@ -59,6 +62,9 @@ void NetworkManager::run()
|
|||||||
} // run
|
} // run
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** \brief Function to reset the Network Manager.
|
||||||
|
* This function resets the peers and the listening host.
|
||||||
|
*/
|
||||||
void NetworkManager::reset()
|
void NetworkManager::reset()
|
||||||
{
|
{
|
||||||
if (m_localhost)
|
if (m_localhost)
|
||||||
@ -72,6 +78,10 @@ void NetworkManager::reset()
|
|||||||
} // reset
|
} // reset
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** \brief Function that aborts the NetworkManager.
|
||||||
|
* This function will stop the listening, delete the host and stop
|
||||||
|
* threads that are related to networking.
|
||||||
|
*/
|
||||||
void NetworkManager::abort()
|
void NetworkManager::abort()
|
||||||
{
|
{
|
||||||
m_localhost->stopListening();
|
m_localhost->stopListening();
|
||||||
@ -82,7 +92,10 @@ void NetworkManager::abort()
|
|||||||
} // abort
|
} // abort
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** \brief Try to establish a connection to a given transport address.
|
||||||
|
* \param peer : The transport address which you want to connect to.
|
||||||
|
* \return True if we're successfully connected. False elseway.
|
||||||
|
*/
|
||||||
bool NetworkManager::connect(const TransportAddress& peer)
|
bool NetworkManager::connect(const TransportAddress& peer)
|
||||||
{
|
{
|
||||||
if (peerExists(peer))
|
if (peerExists(peer))
|
||||||
@ -92,7 +105,12 @@ bool NetworkManager::connect(const TransportAddress& peer)
|
|||||||
} // connect
|
} // connect
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** \brief Changes the socket working mode.
|
||||||
|
* Sockets can be in two modes : The ENet mode and a mode we will call
|
||||||
|
* the 'Raw' mode. In the ENet mode, the socket will be read as
|
||||||
|
* \param peer : The transport address which you want to connect to.
|
||||||
|
* \return True if we're successfully connected. False elseway.
|
||||||
|
*/
|
||||||
void NetworkManager::setManualSocketsMode(bool manual)
|
void NetworkManager::setManualSocketsMode(bool manual)
|
||||||
{
|
{
|
||||||
if (manual)
|
if (manual)
|
||||||
@ -163,18 +181,20 @@ void NetworkManager::sendPacketExcept(STKPeer* peer, const NetworkString& data,
|
|||||||
} // sendPacketExcept
|
} // sendPacketExcept
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** A previous GameSetup is deletea and a new one is created.
|
||||||
|
* \return Newly create GameSetup object.
|
||||||
|
*/
|
||||||
GameSetup* NetworkManager::setupNewGame()
|
GameSetup* NetworkManager::setupNewGame()
|
||||||
{
|
{
|
||||||
if (m_game_setup)
|
if (m_game_setup)
|
||||||
delete m_game_setup;
|
delete m_game_setup;
|
||||||
m_game_setup = NULL;
|
|
||||||
m_game_setup = new GameSetup();
|
m_game_setup = new GameSetup();
|
||||||
return m_game_setup;
|
return m_game_setup;
|
||||||
} // setupNewGame
|
} // setupNewGame
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
/** Called when you leave a server.
|
||||||
|
*/
|
||||||
void NetworkManager::disconnected()
|
void NetworkManager::disconnected()
|
||||||
{
|
{
|
||||||
// delete the game setup
|
// delete the game setup
|
||||||
@ -248,15 +268,3 @@ void NetworkManager::removePeer(STKPeer* peer)
|
|||||||
} // removePeer
|
} // removePeer
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
bool NetworkManager::peerExists(const TransportAddress& peer)
|
|
||||||
{
|
|
||||||
return m_localhost->peerExists(peer);
|
|
||||||
} // peerExists
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
bool NetworkManager::isConnectedTo(const TransportAddress& peer)
|
|
||||||
{
|
|
||||||
return m_localhost->isConnectedTo(peer);
|
|
||||||
} // isConnectedTo
|
|
||||||
|
@ -28,13 +28,13 @@
|
|||||||
|
|
||||||
#include "network/protocol_manager.hpp"
|
#include "network/protocol_manager.hpp"
|
||||||
#include "network/types.hpp"
|
#include "network/types.hpp"
|
||||||
#include "network/game_setup.hpp"
|
|
||||||
#include "utils/singleton.hpp"
|
#include "utils/singleton.hpp"
|
||||||
#include "utils/synchronised.hpp"
|
#include "utils/synchronised.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Event;
|
class Event;
|
||||||
|
class GameSetup;
|
||||||
|
|
||||||
/** \class NetworkManager
|
/** \class NetworkManager
|
||||||
* \brief Gives the general functions to use network communication.
|
* \brief Gives the general functions to use network communication.
|
||||||
@ -47,98 +47,93 @@ class Event;
|
|||||||
*/
|
*/
|
||||||
class NetworkManager : public AbstractSingleton<NetworkManager>
|
class NetworkManager : public AbstractSingleton<NetworkManager>
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
NetworkManager();
|
||||||
|
virtual ~NetworkManager();
|
||||||
|
|
||||||
|
/** Pointer to the one stk host instance, which is used to do all
|
||||||
|
* network communication. */
|
||||||
|
STKHost* m_localhost;
|
||||||
|
|
||||||
|
/** The list of peers connected to this instance. */
|
||||||
|
std::vector<STKPeer*> m_peers;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
friend class AbstractSingleton<NetworkManager>;
|
friend class AbstractSingleton<NetworkManager>;
|
||||||
public:
|
public:
|
||||||
/** \brief Function to start the Network Manager (start threads) */
|
virtual void run();
|
||||||
virtual void run();
|
virtual void reset();
|
||||||
/** \brief Function to reset the Network Manager.
|
virtual void abort();
|
||||||
* This function resets the peers and the listening host.
|
virtual bool connect(const TransportAddress& peer);
|
||||||
*/
|
virtual void setManualSocketsMode(bool manual);
|
||||||
virtual void reset();
|
virtual void propagateEvent(Event* event);
|
||||||
/** \brief Function that aborts the NetworkManager.
|
virtual void sendPacket(const NetworkString& data,
|
||||||
* This function will stop the listening, delete the host and stop
|
bool reliable = true) = 0;
|
||||||
* threads that are related to networking.
|
virtual void sendPacket(STKPeer* peer,
|
||||||
*/
|
const NetworkString& data,
|
||||||
virtual void abort();
|
bool reliable = true);
|
||||||
|
virtual void sendPacketExcept(STKPeer* peer,
|
||||||
|
const NetworkString& data,
|
||||||
|
bool reliable = true);
|
||||||
|
|
||||||
// network management functions
|
// Game related functions
|
||||||
/** \brief Try to establish a connection to a given transport address.
|
virtual GameSetup* setupNewGame();
|
||||||
* \param peer : The transport address which you want to connect to.
|
virtual void disconnected();
|
||||||
* \return True if we're successfully connected. False elseway.
|
virtual bool isServer() = 0;
|
||||||
*/
|
|
||||||
virtual bool connect(const TransportAddress& peer);
|
|
||||||
/** \brief Changes the socket working mode.
|
|
||||||
* Sockets can be in two modes : The ENet mode and a mode we will call
|
|
||||||
* the 'Raw' mode. In the ENet mode, the socket will be read as
|
|
||||||
* \param peer : The transport address which you want to connect to.
|
|
||||||
* \return True if we're successfully connected. False elseway.
|
|
||||||
*/
|
|
||||||
virtual void setManualSocketsMode(bool manual);
|
|
||||||
|
|
||||||
// message/packets related functions
|
// raw data management
|
||||||
virtual void propagateEvent(Event* event);
|
void setLogin(std::string username, std::string password);
|
||||||
virtual void sendPacket(const NetworkString& data,
|
void setPublicAddress(const TransportAddress& addr);
|
||||||
bool reliable = true) = 0;
|
void removePeer(STKPeer* peer);
|
||||||
virtual void sendPacket(STKPeer* peer,
|
|
||||||
const NetworkString& data,
|
|
||||||
bool reliable = true);
|
|
||||||
virtual void sendPacketExcept(STKPeer* peer,
|
|
||||||
const NetworkString& data,
|
|
||||||
bool reliable = true);
|
|
||||||
|
|
||||||
// Game related functions
|
// getters
|
||||||
virtual GameSetup* setupNewGame(); //!< Creates a new game setup and returns it
|
// ------------------------------------------------------------------------
|
||||||
virtual void disconnected(); //!< Called when you leave a server
|
/** Returns if a peer from the specified IP:port address
|
||||||
|
* already exists. */
|
||||||
|
virtual bool peerExists(const TransportAddress& peer)
|
||||||
|
{
|
||||||
|
return m_localhost->peerExists(peer);
|
||||||
|
} // peerExists
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
virtual bool isConnectedTo(const TransportAddress& peer)
|
||||||
|
{
|
||||||
|
return m_localhost->isConnectedTo(peer);
|
||||||
|
} // isConnectedTo
|
||||||
|
|
||||||
// raw data management
|
// --------------------------------------------------------------------
|
||||||
void setLogin(std::string username, std::string password);
|
inline bool isClient() { return !isServer(); }
|
||||||
void setPublicAddress(const TransportAddress& addr);
|
// --------------------------------------------------------------------
|
||||||
void removePeer(STKPeer* peer);
|
STKHost* getHost() { return m_localhost; }
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
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
|
||||||
|
|
||||||
// getters
|
// --------------------------------------------------------------------
|
||||||
virtual bool peerExists(const TransportAddress& peer);
|
/** Returns the current game setup. */
|
||||||
virtual bool isConnectedTo(const TransportAddress& peer);
|
GameSetup* getGameSetup() { return m_game_setup; }
|
||||||
|
|
||||||
virtual bool isServer() = 0;
|
}; // class NetworkManager
|
||||||
// --------------------------------------------------------------------
|
|
||||||
inline bool isClient() { return !isServer(); }
|
|
||||||
// --------------------------------------------------------------------
|
|
||||||
bool isPlayingOnline() { return m_playing_online; }
|
|
||||||
// --------------------------------------------------------------------
|
|
||||||
STKHost* getHost() { return m_localhost; }
|
|
||||||
// --------------------------------------------------------------------
|
|
||||||
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
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
|
||||||
GameSetup* getGameSetup() { return m_game_setup; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
NetworkManager();
|
|
||||||
virtual ~NetworkManager();
|
|
||||||
|
|
||||||
// protected members
|
|
||||||
std::vector<STKPeer*> m_peers;
|
|
||||||
STKHost* m_localhost;
|
|
||||||
bool m_playing_online;
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // NETWORKMANAGER_HPP
|
#endif // NETWORKMANAGER_HPP
|
||||||
|
@ -34,7 +34,6 @@ class ServerLobbyRoomProtocol : public LobbyRoomProtocol
|
|||||||
void playerLapsVote(Event* event);
|
void playerLapsVote(Event* event);
|
||||||
|
|
||||||
uint8_t m_next_id; //!< Next id to assign to a peer.
|
uint8_t m_next_id; //!< Next id to assign to a peer.
|
||||||
std::vector<TransportAddress> m_peers;
|
|
||||||
std::vector<uint32_t> m_incoming_peers_ids;
|
std::vector<uint32_t> m_incoming_peers_ids;
|
||||||
uint32_t m_current_protocol_id;
|
uint32_t m_current_protocol_id;
|
||||||
bool m_selection_enabled;
|
bool m_selection_enabled;
|
||||||
|
Loading…
Reference in New Issue
Block a user