2013-06-29 18:57:18 -04:00
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013 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.
2013-07-04 09:19:32 -04:00
# include "network/stk_peer.hpp"
2013-06-29 18:57:18 -04:00
2013-07-04 12:59:30 -04:00
# include "utils/log.hpp"
2013-06-29 18:57:18 -04:00
# include <string.h>
STKPeer : : STKPeer ( )
{
m_peer = NULL ;
2013-07-10 18:46:23 -04:00
m_player_profile = NULL ;
2013-06-29 18:57:18 -04:00
}
2013-07-10 20:03:47 -04:00
STKPeer : : STKPeer ( const STKPeer & peer )
{
Log : : verbose ( " STKPeer " , " Construction a copy of a STKPeer. " ) ;
m_peer = peer . m_peer ;
m_player_profile = peer . m_player_profile ;
m_client_server_token = peer . m_client_server_token ;
m_token_set = peer . m_token_set ;
}
2013-06-29 18:57:18 -04:00
STKPeer : : ~ STKPeer ( )
{
if ( m_peer )
2013-07-04 12:59:30 -04:00
{
m_peer = NULL ;
}
2013-06-29 18:57:18 -04:00
}
bool STKPeer : : connectToHost ( STKHost * localhost , TransportAddress host , uint32_t channel_count , uint32_t data )
{
ENetAddress address ;
2013-07-10 18:46:23 -04:00
address . host =
2013-07-08 12:50:20 -04:00
( ( host . ip & 0xff000000 ) > > 24 )
+ ( ( host . ip & 0x00ff0000 ) > > 8 )
+ ( ( host . ip & 0x0000ff00 ) < < 8 )
2013-07-08 18:06:57 -04:00
+ ( ( host . ip & 0x000000ff ) < < 24 ) ; // because ENet wants little endian
2013-06-29 18:57:18 -04:00
address . port = host . port ;
2013-07-10 18:46:23 -04:00
2013-06-29 18:57:18 -04:00
ENetPeer * peer = enet_host_connect ( localhost - > m_host , & address , 2 , 0 ) ;
2013-07-10 18:46:23 -04:00
if ( peer = = NULL )
2013-06-29 18:57:18 -04:00
{
2013-07-04 12:59:30 -04:00
Log : : error ( " STKPeer " , " Could not try to connect to server. \n " ) ;
2013-06-29 18:57:18 -04:00
return false ;
}
2013-07-09 19:46:09 -04:00
Log : : verbose ( " STKPeer " , " Connecting to %i.%i.%i.%i:%i. \n ENetPeer address is %ld " , ( peer - > address . host > > 0 ) & 0xff , ( peer - > address . host > > 8 ) & 0xff , ( peer - > address . host > > 16 ) & 0xff , ( peer - > address . host > > 24 ) & 0xff , peer - > address . port , ( long int ) ( peer ) ) ;
2013-06-29 18:57:18 -04:00
return true ;
}
2013-07-09 19:35:53 -04:00
void STKPeer : : disconnect ( )
{
enet_peer_disconnect ( m_peer , 0 ) ;
}
2013-07-05 12:16:02 -04:00
void STKPeer : : sendPacket ( NetworkString const & data )
2013-06-29 18:57:18 -04:00
{
2013-07-09 19:46:09 -04:00
Log : : verbose ( " STKPeer " , " sending packet of size %d to %i.%i.%i.%i:%i " , data . size ( ) , ( m_peer - > address . host > > 0 ) & 0xff , ( m_peer - > address . host > > 8 ) & 0xff , ( m_peer - > address . host > > 16 ) & 0xff , ( m_peer - > address . host > > 24 ) & 0xff , m_peer - > address . port ) ;
2013-07-05 12:16:02 -04:00
ENetPacket * packet = enet_packet_create ( data . c_str ( ) , data . size ( ) + 1 , ENET_PACKET_FLAG_RELIABLE ) ;
2013-07-10 18:46:23 -04:00
2013-06-29 18:57:18 -04:00
enet_peer_send ( m_peer , 0 , packet ) ;
}
2013-07-07 15:49:51 -04:00
uint32_t STKPeer : : getAddress ( ) const
2013-06-29 18:57:18 -04:00
{
2013-07-09 18:05:06 -04:00
return turnEndianness ( m_peer - > address . host ) ;
2013-06-29 18:57:18 -04:00
}
2013-07-07 15:49:51 -04:00
uint16_t STKPeer : : getPort ( ) const
2013-06-29 18:57:18 -04:00
{
return m_peer - > address . port ;
}
2013-07-07 15:49:51 -04:00
bool STKPeer : : isConnected ( ) const
2013-06-29 18:57:18 -04:00
{
2013-07-04 12:59:30 -04:00
Log : : info ( " STKPeer " , " The peer state is %i \n " , m_peer - > state ) ;
2013-06-29 18:57:18 -04:00
return ( m_peer - > state = = ENET_PEER_STATE_CONNECTED ) ;
}
2013-07-10 20:03:47 -04:00
bool STKPeer : : isSamePeer ( const STKPeer * peer ) const
2013-07-10 19:03:45 -04:00
{
return peer - > m_peer = = m_peer ;
}