Coding style changes.

This commit is contained in:
hiker 2015-10-22 16:03:11 +11:00
parent b8837d5d59
commit 1f4f2bce86
6 changed files with 116 additions and 88 deletions

View File

@ -62,7 +62,7 @@ Event::Event(ENetEvent* event)
m_peer = NULL; m_peer = NULL;
for (unsigned int i = 0; i < peers.size(); i++) for (unsigned int i = 0; i < peers.size(); i++)
{ {
if (peers[i]->m_peer == event->peer) if (peers[i]->m_enet_peer == event->peer)
{ {
m_peer = peers[i]; m_peer = peers[i];
Log::verbose("Event", "The peer you sought has been found on %p", Log::verbose("Event", "The peer you sought has been found on %p",
@ -73,7 +73,7 @@ Event::Event(ENetEvent* event)
if (m_peer == NULL) // peer does not exist, create him if (m_peer == NULL) // peer does not exist, create him
{ {
STKPeer* new_peer = new STKPeer(); STKPeer* new_peer = new STKPeer();
new_peer->m_peer = event->peer; new_peer->m_enet_peer = event->peer;
m_peer = new_peer; m_peer = new_peer;
Log::debug("Event", Log::debug("Event",
"Creating a new peer, address are STKPeer:%p, Peer:%p", "Creating a new peer, address are STKPeer:%p, Peer:%p",

View File

@ -96,12 +96,12 @@ void NetworkManager::abort()
* \param peer : The transport address which you want to connect to. * \param peer : The transport address which you want to connect to.
* \return True if we're successfully connected. False elseway. * \return True if we're successfully connected. False elseway.
*/ */
bool NetworkManager::connect(const TransportAddress& peer) bool NetworkManager::connect(const TransportAddress& address)
{ {
if (peerExists(peer)) if (peerExists(address))
return isConnectedTo(peer); return isConnectedTo(address);
return STKPeer::connectToHost(m_localhost, peer, 2, 0); return STKPeer::connectToHost(m_localhost, address, 2, 0);
} // connect } // connect
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -4,6 +4,7 @@
#include "karts/abstract_kart.hpp" #include "karts/abstract_kart.hpp"
#include "karts/controller/controller.hpp" #include "karts/controller/controller.hpp"
#include "network/event.hpp" #include "network/event.hpp"
#include "network/game_setup.hpp"
#include "network/network_manager.hpp" #include "network/network_manager.hpp"
#include "network/network_world.hpp" #include "network/network_world.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"

View File

@ -7,6 +7,7 @@
#include "items/powerup.hpp" #include "items/powerup.hpp"
#include "modes/world.hpp" #include "modes/world.hpp"
#include "network/event.hpp" #include "network/event.hpp"
#include "network/game_setup.hpp"
#include "network/network_manager.hpp" #include "network/network_manager.hpp"
#include <stdint.h> #include <stdint.h>

View File

@ -18,54 +18,53 @@
#include "network/stk_peer.hpp" #include "network/stk_peer.hpp"
#include "network/network_manager.hpp" #include "network/network_manager.hpp"
#include "network/game_setup.hpp"
#include "network/network_string.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include <string.h> #include <string.h>
/** Constructor for an empty peer.
*/
STKPeer::STKPeer() STKPeer::STKPeer()
{ {
m_peer = NULL; m_enet_peer = NULL;
m_player_profile = new NetworkPlayerProfile*; m_player_profile = NULL;
*m_player_profile = NULL; m_client_server_token = 0;
m_client_server_token = new uint32_t; m_token_set = false;
*m_client_server_token = 0; } // STKPeer
m_token_set = new bool;
*m_token_set = false;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Copy constructor.
*/
STKPeer::STKPeer(const STKPeer& peer) STKPeer::STKPeer(const STKPeer& peer)
{ {
m_peer = peer.m_peer; m_enet_peer = peer.m_enet_peer;
m_player_profile = peer.m_player_profile; m_player_profile = peer.m_player_profile;
m_client_server_token = peer.m_client_server_token; m_client_server_token = peer.m_client_server_token;
m_token_set = peer.m_token_set; m_token_set = peer.m_token_set;
} } // STKPeer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Destructor.
*/
STKPeer::~STKPeer() STKPeer::~STKPeer()
{ {
if (m_peer) if (m_enet_peer)
m_peer = NULL; m_enet_peer = NULL;
if (m_player_profile) if (m_player_profile)
delete m_player_profile; delete m_player_profile;
m_player_profile = NULL; m_player_profile = NULL;
if (m_client_server_token) m_client_server_token = 0;
delete m_client_server_token; } // ~STKPeer
m_client_server_token = NULL;
if (m_token_set)
delete m_token_set;
m_token_set = NULL;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Connect to the specified host.
*/
bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host, bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host,
uint32_t channel_count, uint32_t data) uint32_t channel_count, uint32_t data)
{ {
const ENetAddress address = host.toEnetAddress(); const ENetAddress address = host.toEnetAddress();
ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0); ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0);
if (peer == NULL) if (peer == NULL)
@ -76,70 +75,70 @@ bool STKPeer::connectToHost(STKHost* localhost, const TransportAddress &host,
TransportAddress a(peer->address); TransportAddress a(peer->address);
Log::verbose("STKPeer", "Connecting to %s", a.toString().c_str()); Log::verbose("STKPeer", "Connecting to %s", a.toString().c_str());
return true; return true;
} } // connectToHost
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Disconnect from the server.
*/
void STKPeer::disconnect() void STKPeer::disconnect()
{ {
enet_peer_disconnect(m_peer, 0); enet_peer_disconnect(m_enet_peer, 0);
} } // disconnect
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Sends a packet to this host.
* \param data The data to send.
* \param reliable If the data is sent reliable or not.
*/
void STKPeer::sendPacket(NetworkString const& data, bool reliable) void STKPeer::sendPacket(NetworkString const& data, bool reliable)
{ {
TransportAddress a(m_peer->address); TransportAddress a(m_enet_peer->address);
Log::verbose("STKPeer", "sending packet of size %d to %s", Log::verbose("STKPeer", "sending packet of size %d to %s",
a.toString().c_str()); a.toString().c_str());
ENetPacket* packet = enet_packet_create(data.getBytes(), data.size() + 1, ENetPacket* packet = enet_packet_create(data.getBytes(), data.size() + 1,
(reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED)); (reliable ? ENET_PACKET_FLAG_RELIABLE
/* to debug the packet output : ENET_PACKET_FLAG_UNSEQUENCED));
printf("STKPeer: "); enet_peer_send(m_enet_peer, 0, packet);
for (unsigned int i = 0; i < data.size(); i++) } // sendPacket
{
printf("%d ", (uint8_t)(data[i]));
}
printf("\n");
*/
enet_peer_send(m_peer, 0, packet);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the IP address (in host format) of this client.
*/
uint32_t STKPeer::getAddress() const uint32_t STKPeer::getAddress() const
{ {
return ntohl(m_peer->address.host); return ntohl(m_enet_peer->address.host);
} } // getAddress
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the port of this peer.
*/
uint16_t STKPeer::getPort() const uint16_t STKPeer::getPort() const
{ {
return m_peer->address.port; return m_enet_peer->address.port;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns if the peer is connected or not.
*/
bool STKPeer::isConnected() const bool STKPeer::isConnected() const
{ {
Log::info("STKPeer", "The peer state is %i", m_peer->state); Log::info("STKPeer", "The peer state is %i", m_enet_peer->state);
return (m_peer->state == ENET_PEER_STATE_CONNECTED); return (m_enet_peer->state == ENET_PEER_STATE_CONNECTED);
} } // isConnected
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool STKPeer::exists() const bool STKPeer::exists() const
{ {
return (m_peer != NULL); // assert that the peer exists return (m_enet_peer != NULL); // assert that the peer exists
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool STKPeer::isSamePeer(const STKPeer* peer) const bool STKPeer::isSamePeer(const STKPeer* peer) const
{ {
return peer->m_peer==m_peer; return peer->m_enet_peer==m_enet_peer;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -23,11 +23,15 @@
#ifndef STK_PEER_HPP #ifndef STK_PEER_HPP
#define STK_PEER_HPP #define STK_PEER_HPP
#include "network/stk_host.hpp" #include "utils/types.hpp"
#include "network/network_string.hpp"
#include "network/game_setup.hpp"
#include <enet/enet.h> #include <enet/enet.h>
class NetworkPlayerProfile;
class NetworkString;
class STKHost;
class TransportAddress;
/*! \class STKPeer /*! \class STKPeer
* \brief Represents a peer. * \brief Represents a peer.
* This class is used to interface the ENetPeer structure. * This class is used to interface the ENetPeer structure.
@ -36,35 +40,58 @@ class STKPeer
{ {
friend class Event; friend class Event;
protected: protected:
ENetPeer* m_peer; /** Pointer to the corresponding ENet peer data structure. */
NetworkPlayerProfile** m_player_profile; ENetPeer* m_enet_peer;
uint32_t *m_client_server_token;
bool *m_token_set; NetworkPlayerProfile* m_player_profile;
/** The token of this client. */
uint32_t m_client_server_token;
/** True if the token for this peer has been set. */
bool m_token_set;
public: public:
STKPeer(); STKPeer();
STKPeer(const STKPeer& peer); STKPeer(const STKPeer& peer);
virtual ~STKPeer(); virtual ~STKPeer();
virtual void sendPacket(const NetworkString& data, bool reliable = true); virtual void sendPacket(const NetworkString& data, bool reliable = true);
static bool connectToHost(STKHost* localhost, const TransportAddress& host, static bool connectToHost(STKHost* localhost, const TransportAddress& host,
uint32_t channel_count, uint32_t data); uint32_t channel_count, uint32_t data);
void disconnect(); void disconnect();
bool isConnected() const;
bool exists() const;
uint32_t getAddress() const;
uint16_t getPort() const;
bool isSamePeer(const STKPeer* peer) const;
void setClientServerToken(const uint32_t& token) { *m_client_server_token = token; *m_token_set = true; } // ------------------------------------------------------------------------
void unsetClientServerToken() { *m_token_set = false; } /** Sets the token for this client. */
void setPlayerProfile(NetworkPlayerProfile* profile) { *m_player_profile = profile; } void setClientServerToken(const uint32_t& token)
void setPlayerProfilePtr(NetworkPlayerProfile** profile) { m_player_profile = profile; } {
m_client_server_token = token;
bool isConnected() const; m_token_set = true;
bool exists() const; } // setClientServerToken
uint32_t getAddress() const; // ------------------------------------------------------------------------
uint16_t getPort() const; void unsetClientServerToken() { m_token_set = false; }
NetworkPlayerProfile* getPlayerProfile() { return (m_player_profile)?(*m_player_profile):NULL; } // ------------------------------------------------------------------------
uint32_t getClientServerToken() const { return *m_client_server_token; } void setPlayerProfile(NetworkPlayerProfile* profile)
bool isClientServerTokenSet() const { return *m_token_set; } {
m_player_profile = profile;
bool isSamePeer(const STKPeer* peer) const; } // setPlayerProfile
// ------------------------------------------------------------------------
/** Returns the player profile of this peer. */
NetworkPlayerProfile* getPlayerProfile()
{
return m_player_profile;
} // getPlayerProfile
// ------------------------------------------------------------------------
/** Returns the token of this client. */
uint32_t getClientServerToken() const { return m_client_server_token; }
// ------------------------------------------------------------------------
/** Returns if the token for this client is known. */
bool isClientServerTokenSet() const { return m_token_set; }
}; // STKPeer }; // STKPeer