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/event.hpp"
|
|
|
|
#include "network/network_manager.hpp"
|
2013-06-29 18:57:18 -04:00
|
|
|
|
2013-07-08 18:47:24 -04:00
|
|
|
#include "utils/log.hpp"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-20 16:51:53 -04:00
|
|
|
/** \brief Constructor
|
|
|
|
* \param event : The event that needs to be translated.
|
|
|
|
*/
|
2013-06-29 18:57:18 -04:00
|
|
|
Event::Event(ENetEvent* event)
|
|
|
|
{
|
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case ENET_EVENT_TYPE_CONNECT:
|
2015-10-20 17:07:00 -04:00
|
|
|
m_type = EVENT_TYPE_CONNECTED;
|
2013-06-29 18:57:18 -04:00
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
2015-10-20 17:07:00 -04:00
|
|
|
m_type = EVENT_TYPE_DISCONNECTED;
|
2013-06-29 18:57:18 -04:00
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
2015-10-20 17:07:00 -04:00
|
|
|
m_type = EVENT_TYPE_MESSAGE;
|
2013-06-29 18:57:18 -04:00
|
|
|
break;
|
2013-07-11 18:07:38 -04:00
|
|
|
case ENET_EVENT_TYPE_NONE:
|
|
|
|
return;
|
2013-06-29 18:57:18 -04:00
|
|
|
break;
|
|
|
|
}
|
2015-10-20 17:07:00 -04:00
|
|
|
if (m_type == EVENT_TYPE_MESSAGE)
|
2013-07-08 18:47:24 -04:00
|
|
|
{
|
2015-10-20 16:51:53 -04:00
|
|
|
m_data = NetworkString(std::string((char*)(event->packet->data),
|
|
|
|
event->packet->dataLength-1));
|
2013-07-05 12:16:02 -04:00
|
|
|
}
|
2013-07-10 18:46:23 -04:00
|
|
|
|
2013-07-09 18:05:06 -04:00
|
|
|
m_packet = NULL;
|
2013-06-29 18:57:18 -04:00
|
|
|
if (event->packet)
|
2015-10-20 16:51:53 -04:00
|
|
|
{
|
2013-06-29 18:57:18 -04:00
|
|
|
m_packet = event->packet;
|
2015-10-20 16:51:53 -04:00
|
|
|
// we got all we need, just remove the data.
|
|
|
|
enet_packet_destroy(m_packet);
|
|
|
|
}
|
2013-07-12 10:08:33 -04:00
|
|
|
m_packet = NULL;
|
2013-06-29 18:57:18 -04:00
|
|
|
|
2015-10-25 17:33:07 -04:00
|
|
|
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
|
2015-10-20 17:11:35 -04:00
|
|
|
m_peer = NULL;
|
2013-06-29 18:57:18 -04:00
|
|
|
for (unsigned int i = 0; i < peers.size(); i++)
|
|
|
|
{
|
2015-10-22 01:03:11 -04:00
|
|
|
if (peers[i]->m_enet_peer == event->peer)
|
2013-06-29 18:57:18 -04:00
|
|
|
{
|
2015-10-20 17:11:35 -04:00
|
|
|
m_peer = peers[i];
|
2015-10-20 16:51:53 -04:00
|
|
|
Log::verbose("Event", "The peer you sought has been found on %p",
|
2015-10-20 17:07:00 -04:00
|
|
|
m_peer);
|
2013-06-29 18:57:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-10-25 17:33:07 -04:00
|
|
|
|
2015-10-20 17:11:35 -04:00
|
|
|
if (m_peer == NULL) // peer does not exist, create him
|
2013-06-29 18:57:18 -04:00
|
|
|
{
|
|
|
|
STKPeer* new_peer = new STKPeer();
|
2015-10-22 01:03:11 -04:00
|
|
|
new_peer->m_enet_peer = event->peer;
|
2015-10-20 17:11:35 -04:00
|
|
|
m_peer = new_peer;
|
2015-10-20 16:51:53 -04:00
|
|
|
Log::debug("Event",
|
|
|
|
"Creating a new peer, address are STKPeer:%p, Peer:%p",
|
|
|
|
new_peer, event->peer);
|
2013-06-29 18:57:18 -04:00
|
|
|
}
|
2015-10-20 16:51:53 -04:00
|
|
|
} // Event(ENetEvent)
|
2013-07-10 18:46:23 -04:00
|
|
|
|
2015-10-20 16:51:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** \brief Destructor that frees the memory of the package.
|
|
|
|
*/
|
2013-06-29 18:57:18 -04:00
|
|
|
Event::~Event()
|
|
|
|
{
|
2015-10-20 17:07:00 -04:00
|
|
|
delete m_peer;
|
|
|
|
m_peer = NULL;
|
2013-07-10 20:38:33 -04:00
|
|
|
m_packet = NULL;
|
2015-10-20 16:51:53 -04:00
|
|
|
} // ~Event
|
2013-07-04 09:19:32 -04:00
|
|
|
|
2015-10-20 16:51:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-10-20 17:07:00 -04:00
|
|
|
/** \brief Remove bytes at the beginning of data.
|
|
|
|
* \param size : The number of bytes to remove.
|
|
|
|
*/
|
2013-07-04 09:19:32 -04:00
|
|
|
void Event::removeFront(int size)
|
|
|
|
{
|
2013-08-01 15:49:57 -04:00
|
|
|
m_data.removeFront(size);
|
2015-10-20 16:51:53 -04:00
|
|
|
} // removeFront
|
2013-07-04 09:19:32 -04:00
|
|
|
|