2013-06-29 18:57:18 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2013-2015 SuperTuxKart-Team
|
2013-06-29 18:57:18 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2013-07-04 09:19:32 -04:00
|
|
|
#include "network/stk_peer.hpp"
|
2015-10-22 01:03:11 -04:00
|
|
|
#include "network/game_setup.hpp"
|
|
|
|
#include "network/network_string.hpp"
|
2015-11-25 15:41:37 -05:00
|
|
|
#include "network/network_player_profile.hpp"
|
2016-03-11 00:42:35 -05:00
|
|
|
#include "network/stk_host.hpp"
|
2015-10-29 03:39:01 -04:00
|
|
|
#include "network/transport_address.hpp"
|
2013-07-04 12:59:30 -04:00
|
|
|
#include "utils/log.hpp"
|
2018-03-04 12:54:44 -05:00
|
|
|
#include "utils/time.hpp"
|
2013-07-04 12:59:30 -04:00
|
|
|
|
2013-06-29 18:57:18 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-22 01:03:11 -04:00
|
|
|
/** Constructor for an empty peer.
|
|
|
|
*/
|
2018-03-09 10:00:47 -05:00
|
|
|
STKPeer::STKPeer(ENetPeer *enet_peer, Network* network)
|
|
|
|
: m_peer_address(enet_peer->address), m_network(network)
|
2013-06-29 18:57:18 -04:00
|
|
|
{
|
2015-11-16 16:11:07 -05:00
|
|
|
m_enet_peer = enet_peer;
|
2016-02-10 17:03:51 -05:00
|
|
|
m_is_authorised = false;
|
2015-10-22 01:03:11 -04:00
|
|
|
m_client_server_token = 0;
|
2016-02-09 16:28:15 -05:00
|
|
|
m_host_id = 0;
|
2015-10-22 01:03:11 -04:00
|
|
|
m_token_set = false;
|
|
|
|
} // STKPeer
|
2013-07-11 08:03:28 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-10-22 01:03:11 -04:00
|
|
|
/** Destructor.
|
|
|
|
*/
|
2013-06-29 18:57:18 -04:00
|
|
|
STKPeer::~STKPeer()
|
|
|
|
{
|
2018-03-09 05:33:19 -05:00
|
|
|
enet_peer_disconnect(m_enet_peer, 0);
|
2015-10-22 01:03:11 -04:00
|
|
|
m_client_server_token = 0;
|
|
|
|
} // ~STKPeer
|
2013-06-29 18:57:18 -04:00
|
|
|
|
2013-07-11 08:03:28 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-10-22 01:03:11 -04:00
|
|
|
/** Disconnect from the server.
|
|
|
|
*/
|
2013-07-09 19:35:53 -04:00
|
|
|
void STKPeer::disconnect()
|
|
|
|
{
|
2015-10-22 01:03:11 -04:00
|
|
|
enet_peer_disconnect(m_enet_peer, 0);
|
|
|
|
} // disconnect
|
2013-07-09 19:35:53 -04:00
|
|
|
|
2013-07-11 08:03:28 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
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.
|
|
|
|
*/
|
2016-02-27 23:54:43 -05:00
|
|
|
void STKPeer::sendPacket(NetworkString *data, bool reliable)
|
2013-06-29 18:57:18 -04:00
|
|
|
{
|
2015-10-22 01:03:11 -04:00
|
|
|
TransportAddress a(m_enet_peer->address);
|
2018-03-09 12:07:23 -05:00
|
|
|
// 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());
|
2018-03-09 05:33:19 -05:00
|
|
|
|
2016-03-09 05:21:50 -05:00
|
|
|
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));
|
2018-03-09 10:00:47 -05:00
|
|
|
auto lock = m_network->acquireEnetLock();
|
2015-10-22 01:03:11 -04:00
|
|
|
enet_peer_send(m_enet_peer, 0, packet);
|
|
|
|
} // sendPacket
|
2013-06-29 18:57:18 -04:00
|
|
|
|
2013-07-11 08:03:28 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-10-22 01:03:11 -04:00
|
|
|
/** Returns if the peer is connected or not.
|
|
|
|
*/
|
2013-07-07 15:49:51 -04:00
|
|
|
bool STKPeer::isConnected() const
|
2013-06-29 18:57:18 -04:00
|
|
|
{
|
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
|
2013-07-11 08:03:28 -04:00
|
|
|
|
2013-07-29 19:43:28 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-11-16 16:11:07 -05:00
|
|
|
/** Returns if this STKPeer is the same as the given peer.
|
|
|
|
*/
|
2013-07-10 20:03:47 -04:00
|
|
|
bool STKPeer::isSamePeer(const STKPeer* peer) const
|
2013-07-10 19:03:45 -04:00
|
|
|
{
|
2015-10-22 01:03:11 -04:00
|
|
|
return peer->m_enet_peer==m_enet_peer;
|
2015-11-16 16:11:07 -05:00
|
|
|
} // 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
|
2013-07-10 19:03:45 -04:00
|
|
|
|
2013-07-11 08:03:28 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-03-11 00:42:35 -05:00
|
|
|
/** 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
|
2013-07-11 08:03:28 -04:00
|
|
|
|