stk-code_catmod/src/network/stk_peer.cpp

119 lines
4.2 KiB
C++
Raw Normal View History

//
// SuperTuxKart - a fun racing game with go-kart
2015-03-29 20:31:42 -04:00
// 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/stk_peer.hpp"
2015-10-22 01:03:11 -04:00
#include "network/game_setup.hpp"
#include "network/network_string.hpp"
#include "network/network_player_profile.hpp"
#include "network/stk_host.hpp"
#include "network/transport_address.hpp"
#include "utils/log.hpp"
#include "utils/time.hpp"
#include <string.h>
2015-10-22 01:03:11 -04:00
/** Constructor for an empty peer.
*/
STKPeer::STKPeer(ENetPeer *enet_peer, Network* network)
: m_peer_address(enet_peer->address), m_network(network)
{
m_enet_peer = enet_peer;
m_is_authorised = false;
2015-10-22 01:03:11 -04:00
m_client_server_token = 0;
m_host_id = 0;
2015-10-22 01:03:11 -04:00
m_token_set = false;
} // STKPeer
//-----------------------------------------------------------------------------
2015-10-22 01:03:11 -04:00
/** Destructor.
*/
STKPeer::~STKPeer()
{
enet_peer_disconnect(m_enet_peer, 0);
2015-10-22 01:03:11 -04:00
m_client_server_token = 0;
} // ~STKPeer
//-----------------------------------------------------------------------------
2015-10-22 01:03:11 -04:00
/** Disconnect from the server.
*/
void STKPeer::disconnect()
{
2015-10-22 01:03:11 -04:00
enet_peer_disconnect(m_enet_peer, 0);
} // disconnect
//-----------------------------------------------------------------------------
2015-10-22 01:03:11 -04:00
/** 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 *data, bool reliable)
{
2015-10-22 01:03:11 -04:00
TransportAddress a(m_enet_peer->address);
// Enet will reuse a disconnected peer so we check here to avoid sending
// to wrong peer
if (m_enet_peer->state != ENET_PEER_STATE_CONNECTED ||
a != m_peer_address)
return;
data->setToken(m_client_server_token);
2017-02-14 17:50:00 -05:00
Log::verbose("STKPeer", "sending packet of size %d to %s at %f",
data->size(), a.toString().c_str(),StkTime::getRealTime());
ENetPacket* packet = enet_packet_create(data->getData(),
data->getTotalSize(),
2015-10-22 01:03:11 -04:00
(reliable ? ENET_PACKET_FLAG_RELIABLE
: ENET_PACKET_FLAG_UNSEQUENCED));
auto lock = m_network->acquireEnetLock();
2015-10-22 01:03:11 -04:00
enet_peer_send(m_enet_peer, 0, packet);
} // sendPacket
//-----------------------------------------------------------------------------
2015-10-22 01:03:11 -04:00
/** Returns if the peer is connected or not.
*/
bool STKPeer::isConnected() const
{
2015-10-22 01:03:11 -04:00
Log::info("STKPeer", "The peer state is %i", m_enet_peer->state);
return (m_enet_peer->state == ENET_PEER_STATE_CONNECTED);
} // isConnected
//-----------------------------------------------------------------------------
/** Returns if this STKPeer is the same as the given peer.
*/
bool STKPeer::isSamePeer(const STKPeer* peer) const
{
2015-10-22 01:03:11 -04:00
return peer->m_enet_peer==m_enet_peer;
} // isSamePeer
//-----------------------------------------------------------------------------
/** Returns if this STKPeer is the same as the given peer.
*/
bool STKPeer::isSamePeer(const ENetPeer* peer) const
{
return peer==m_enet_peer;
} // isSamePeer
//-----------------------------------------------------------------------------
/** Returns the list of all player profiles connected to this peer. Note that
* this function is somewhat expensive (it loops over all network profiles
* to find the ones with the same host id as this peer.
*/
std::vector<NetworkPlayerProfile*> STKPeer::getAllPlayerProfiles()
{
return STKHost::get()->getGameSetup()->getAllPlayersOnHost(getHostId());
} // getAllPlayerProfiles