doxygen commenting a lot of code, improved thread-safety, improved coding standards

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12979 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius
2013-06-26 00:05:17 +00:00
parent f52469159b
commit 36673a2112
38 changed files with 1596 additions and 347 deletions

View File

@@ -44,6 +44,8 @@
<Unit filename="event.hpp" />
<Unit filename="http_functions.cpp" />
<Unit filename="http_functions.hpp" />
<Unit filename="lobby_room_protocol.cpp" />
<Unit filename="lobby_room_protocol.hpp" />
<Unit filename="main.cpp" />
<Unit filename="network_interface.cpp" />
<Unit filename="network_interface.hpp" />

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "client_network_manager.hpp"
#include "protocols/get_public_address.hpp"
@@ -34,19 +52,20 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
{
printf("_NetworkInterface>Starting the connection to host protocol\n");
// step 1 : retreive public address
int id = ProtocolManager::getInstance()->startProtocol(new GetPublicAddress(&m_publicAddress));
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
Protocol* protocol = new GetPublicAddress(&m_public_address);
ProtocolManager::getInstance()->requestStart(protocol);
while (ProtocolManager::getInstance()->getProtocolState(protocol) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is known.\n");
// step 2 : show the public address for others (here, the server)
ShowPublicAddress* spa = new ShowPublicAddress(NULL);
spa->setPassword(m_playerLogin.password);
spa->setUsername(m_playerLogin.username);
spa->setPublicAddress(m_publicAddress.ip, m_publicAddress.port);
id = ProtocolManager::getInstance()->startProtocol(spa);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
spa->setPassword(m_player_login.password);
spa->setUsername(m_player_login.username);
spa->setPublicAddress(m_public_address.ip, m_public_address.port);
ProtocolManager::getInstance()->requestStart(spa);
while (ProtocolManager::getInstance()->getProtocolState(spa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is being shown online.\n");
@@ -55,8 +74,8 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
TransportAddress addr;
GetPeerAddress* gpa = new GetPeerAddress(&addr);
gpa->setPeerName(serverNickname);
id = ProtocolManager::getInstance()->startProtocol(gpa);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
ProtocolManager::getInstance()->requestStart(gpa);
while (ProtocolManager::getInstance()->getProtocolState(gpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address of the server is known.\n");
@@ -64,12 +83,12 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
// step 4 : connect to the server
ConnectToServer* cts = new ConnectToServer(NULL);
cts->setServerAddress(addr.ip, addr.port);
id = ProtocolManager::getInstance()->startProtocol(cts);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
ProtocolManager::getInstance()->requestStart(cts);
while (ProtocolManager::getInstance()->getProtocolState(cts) != PROTOCOL_STATE_TERMINATED )
{
}
bool success = false;
if (m_localhost->isConnectedTo(addr.ip, addr.port))
if (m_localhost->isConnectedTo(TransportAddress(addr.ip, addr.port)))
{
success = true;
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A SERVER.\n");
@@ -80,10 +99,10 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
}
// step 5 : hide our public address
HidePublicAddress* hpa = new HidePublicAddress(NULL);
hpa->setPassword(m_playerLogin.password);
hpa->setUsername(m_playerLogin.username);
id = ProtocolManager::getInstance()->startProtocol(hpa);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
hpa->setPassword(m_player_login.password);
hpa->setUsername(m_player_login.username);
ProtocolManager::getInstance()->requestStart(hpa);
while (ProtocolManager::getInstance()->getProtocolState(hpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is now hidden online.\n");

View File

@@ -1,9 +1,26 @@
//
// 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.
#ifndef CLIENT_NETWORK_MANAGER_HPP
#define CLIENT_NETWORK_MANAGER_HPP
#include "network_manager.hpp"
class ClientNetworkManager : public NetworkManager
{
friend class Singleton<NetworkManager>;

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "event.hpp"
#include "network_manager.hpp"
@@ -41,7 +59,7 @@ Event::Event(ENetEvent* event)
}
if (peer == NULL)
{
printf("The peer still does not exist in %u peers\n", peers.size());
printf("The peer still does not exist in %lu peers\n", peers.size());
STKPeer* newPeer = new STKPeer();
newPeer->m_peer = event->peer;
peer = newPeer;

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef EVENT_HPP
#define EVENT_HPP
@@ -5,25 +23,43 @@
#include <stdint.h>
#include <string>
/*!
* \enum EVENT_TYPE
* \brief Represents a network event type.
*/
enum EVENT_TYPE
{
EVENT_TYPE_CONNECTED,
EVENT_TYPE_DISCONNECTED,
EVENT_TYPE_MESSAGE
EVENT_TYPE_CONNECTED, //!< A peer is connected
EVENT_TYPE_DISCONNECTED,//!< A peer is disconnected
EVENT_TYPE_MESSAGE //!< A message between server and client protocols
};
/*!
* \class Event
* \brief Class representing an event that need to pass trough the system.
* This is used to remove ENet dependency in the network.
* It interfaces the ENetEvent structure.
*/
class Event
{
public:
/*!
* \brief Constructor
* \param event : The event that needs to be translated.
*/
Event(ENetEvent* event);
/*!
* \brief Destructor
* frees the memory of the ENetPacket.
*/
~Event();
EVENT_TYPE type;
std::string data;
STKPeer* peer;
EVENT_TYPE type; //!< Type of the event
std::string data; //!< Copy of the data passed by the event
STKPeer* peer; //!< Pointer to the peer that triggered that event
private:
ENetPacket* m_packet;
ENetPacket* m_packet; //!< A pointer on the ENetPacket to be deleted.
};
#endif // EVENT_HPP

View File

@@ -1,5 +1,22 @@
#include "http_functions.hpp"
//
// 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.
#include "http_functions.hpp"
#include <iostream>
#include <string>

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef HTTP_FUNCTIONS_HPP
#define HTTP_FUNCTIONS_HPP

View File

@@ -1,3 +1,21 @@
//
// 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.
#include <iostream>
#include <enet/enet.h>
@@ -106,7 +124,7 @@ int main()
std::cin >> port;
ServerNetworkManager::getInstance()->setManualSocketsMode(true);
char msg[] = "test";
ServerNetworkManager::getInstance()->getHost()->sendRawPacket((uint8_t*)(msg), sizeof(msg), peer, port);
ServerNetworkManager::getInstance()->getHost()->sendRawPacket((uint8_t*)(msg), sizeof(msg), TransportAddress(peer, port));
ServerNetworkManager::getInstance()->setManualSocketsMode(false);
continue;
}

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "network_interface.hpp"

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef NETWORK_INTERFACE_H
#define NETWORK_INTERFACE_H

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "network_manager.hpp"
#include "protocols/hide_public_address.hpp"
@@ -23,10 +41,9 @@ void* protocolManagerUpdate(void* data)
NetworkManager::NetworkManager()
{
m_publicAddress.ip = 0;
m_publicAddress.port = 0;
m_networkManager = NULL;
m_protocolManagerUpdateThread = NULL;
m_public_address.ip = 0;
m_public_address.port = 0;
m_protocol_manager_update_thread = NULL;
}
NetworkManager::~NetworkManager()
@@ -36,16 +53,16 @@ NetworkManager::~NetworkManager()
void NetworkManager::run()
{
ProtocolManager::getInstance<ProtocolManager>();
m_protocolManagerUpdateThread = (pthread_t*)(malloc(sizeof(pthread_t)));
pthread_create(m_protocolManagerUpdateThread, NULL, protocolManagerUpdate, ProtocolManager::getInstance());
m_protocol_manager_update_thread = (pthread_t*)(malloc(sizeof(pthread_t)));
pthread_create(m_protocol_manager_update_thread, NULL, protocolManagerUpdate, ProtocolManager::getInstance());
}
bool NetworkManager::connect(uint32_t ip, uint16_t port)
bool NetworkManager::connect(TransportAddress peer)
{
if (peerExists(ip, port))
return isConnectedTo(ip, port);
if (peerExists(peer))
return isConnectedTo(peer);
return STKPeer::connectToHost(m_localhost, ip, port, 2, 0);
return STKPeer::connectToHost(m_localhost, peer, 2, 0);
}
void NetworkManager::setManualSocketsMode(bool manual)
@@ -90,24 +107,23 @@ void NetworkManager::notifyEvent(Event* event)
void NetworkManager::setLogin(std::string username, std::string password)
{
m_playerLogin.username = username;
m_playerLogin.password = password;
m_player_login.username = username;
m_player_login.password = password;
}
void NetworkManager::setPublicAddress(uint32_t ip, uint16_t port)
void NetworkManager::setPublicAddress(TransportAddress addr)
{
m_publicAddress.ip = ip;
m_publicAddress.port = port;
m_public_address = addr;
}
bool NetworkManager::peerExists(uint32_t ip, uint16_t port)
bool NetworkManager::peerExists(TransportAddress peer)
{
return m_localhost->peerExists(ip, port);
return m_localhost->peerExists(peer);
}
bool NetworkManager::isConnectedTo(uint32_t ip, uint16_t port)
bool NetworkManager::isConnectedTo(TransportAddress peer)
{
return m_localhost->isConnectedTo(ip, port);
return m_localhost->isConnectedTo(peer);
}
STKHost* NetworkManager::getHost()

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef NETWORKMANAGER_HPP
#define NETWORKMANAGER_HPP
@@ -17,18 +35,18 @@ class NetworkManager : public Singleton<NetworkManager>
virtual void run();
// network management functions
virtual bool connect(uint32_t ip, uint16_t port);
virtual bool connect(TransportAddress peer);
virtual void setManualSocketsMode(bool manual);
virtual void notifyEvent(Event* event);
virtual void packetReceived(char* data) = 0;
// raw data management
void setLogin(std::string username, std::string password);
void setPublicAddress(uint32_t ip, uint16_t port);
void setPublicAddress(TransportAddress addr);
// getters
virtual bool peerExists(uint32_t ip, uint16_t port);
virtual bool isConnectedTo(uint32_t ip, uint16_t port);
virtual bool peerExists(TransportAddress peer);
virtual bool isConnectedTo(TransportAddress peer);
STKHost* getHost();
std::vector<STKPeer*> getPeers();
protected:
@@ -39,11 +57,10 @@ class NetworkManager : public Singleton<NetworkManager>
std::vector<STKPeer*> m_peers;
STKHost* m_localhost;
TransportAddress m_publicAddress;
PlayerLogin m_playerLogin;
TransportAddress m_public_address;
PlayerLogin m_player_login;
NetworkManager* m_networkManager;
pthread_t* m_protocolManagerUpdateThread;
pthread_t* m_protocol_manager_update_thread;
};
#endif // NETWORKMANAGER_HPP

View File

@@ -1,8 +1,26 @@
//
// 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.
#include "protocol.hpp"
Protocol::Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type)
Protocol::Protocol(CallbackObject* callback_object, PROTOCOL_TYPE type)
{
m_callbackObject = callbackObject;
m_callback_object = callback_object;
m_type = type;
}
@@ -12,11 +30,11 @@ Protocol::~Protocol()
void Protocol::pause()
{
m_listener->pauseProtocol(this);
m_listener->requestPause(this);
}
void Protocol::unpause()
{
m_listener->unpauseProtocol(this);
m_listener->requestUnpause(this);
}

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef PROTOCOL_HPP
#define PROTOCOL_HPP
@@ -6,33 +24,82 @@
#include <stdint.h>
/** \enum PROTOCOL_TYPE
* \brief The types that protocols can have. This is used to select which protocol receives which event.
* \ingroup network
*/
enum PROTOCOL_TYPE
{
PROTOCOL_NONE = 0,
PROTOCOL_CONNECTION = 1,
PROTOCOL_SILENT = 0xffff // used for protocols that do not subscribe to any network event.
PROTOCOL_NONE = 0, //!< No protocol type assigned.
PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection.
PROTOCOL_LOBBY_ROOM = 2, //!< Protocol that is used during the lobby room phase.
PROTOCOL_SILENT = 0xffff //!< Used for protocols that do not subscribe to any network event.
};
/** \class Protocol
* \brief Abstract class used to define the global protocol functions.
* A protocol is an entity that is started at a point, and that is updated by a thread.
* A protocol can be terminated by an other class, or it can terminate itself if has fulfilled its role.
* This class must be inherited to make any network job.
* \ingroup network
*/
class Protocol
{
public:
Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type);
/*!
* \brief Constructor
*
* Sets the basic protocol parameters, as the callback object and the protocol type.
*
* \param callback_object The callback object that will be used by the protocol. Protocols that do not use callback objects must set it to NULL.
* \param type The type of the protocol.
*/
Protocol(CallbackObject* callback_object, PROTOCOL_TYPE type);
/*!
* \brief Destructor
*/
virtual ~Protocol();
/*!
* \brief Notify a protocol matching the Event type of that event.
* \param event : Pointer to the event.
*/
virtual void notifyEvent(Event* event) = 0;
/*!
* \brief Set the protocol listener.
* \param listener : Pointer to the listener.
*/
void setListener(ProtocolManager* listener);
/*!
* \brief Called when the protocol is going to start. Must be re-defined by subclasses.
*/
virtual void setup() = 0;
/*!
* \brief Called when the protocol is paused (by an other entity or by itself).
* This function must be called by the subclasse's pause function if re-defined.
*/
virtual void pause();
/*!
* \brief Called when the protocol is unpaused.
* This function must be called by the subclasse's unpause function if re-defined.
*/
virtual void unpause();
/*!
* \brief Called by the protocol listener as often as possible. Must be re-defined.
*/
virtual void update() = 0;
/*!
* \brief Method to get a protocol's type.
* \return The protocol type.
*/
PROTOCOL_TYPE getProtocolType();
protected:
ProtocolManager* m_listener;
PROTOCOL_TYPE m_type;
CallbackObject* m_callbackObject;
ProtocolManager* m_listener; //!< The protocol listener
PROTOCOL_TYPE m_type; //!< The type of the protocol
CallbackObject* m_callback_object; //!< The callback object, if needed
};
#endif // PROTOCOL_HPP

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "protocol_manager.hpp"
#include "protocol.hpp"
@@ -10,8 +28,11 @@
ProtocolManager::ProtocolManager()
{
pthread_mutex_init(&m_eventsMutex, NULL);
pthread_mutex_init(&m_protocolsMutex, NULL);
pthread_mutex_init(&m_events_mutex, NULL);
pthread_mutex_init(&m_protocols_mutex, NULL);
pthread_mutex_init(&m_requests_mutex, NULL);
pthread_mutex_init(&m_id_mutex, NULL);
m_next_protocol_id = 0;
}
ProtocolManager::~ProtocolManager()
@@ -20,9 +41,9 @@ ProtocolManager::~ProtocolManager()
void ProtocolManager::notifyEvent(Event* event)
{
pthread_mutex_lock(&m_eventsMutex);
m_eventsToProcess.push_back(event);
pthread_mutex_unlock(&m_eventsMutex);
pthread_mutex_lock(&m_events_mutex);
m_events_to_process.push_back(event); // add the event to the queue
pthread_mutex_unlock(&m_events_mutex);
}
void ProtocolManager::sendMessage(std::string message)
@@ -31,60 +52,116 @@ void ProtocolManager::sendMessage(std::string message)
newMessage[0] = (char)(0);
}
int ProtocolManager::startProtocol(Protocol* protocol)
int ProtocolManager::requestStart(Protocol* protocol)
{
ProtocolInfo protocolInfo;
protocolInfo.state = PROTOCOL_STATE_RUNNING;
assignProtocolId(protocolInfo);
protocolInfo.protocol = protocol;
printf("__ProtocolManager> A new protocol with id=%u has been started. There are %ld protocols running.\n", protocolInfo.id, m_protocols.size()+1);
// create the request
ProtocolRequest req;
ProtocolInfo info;
info.protocol = protocol;
info.state = PROTOCOL_STATE_RUNNING;
assignProtocolId(&info); // assign a unique id to the protocol.
req.protocol_info = info;
req.type = PROTOCOL_REQUEST_START;
// add it to the request stack
pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex);
pthread_mutex_lock(&m_protocolsMutex);
m_protocols.push_back(protocolInfo);
pthread_mutex_unlock(&m_protocolsMutex);
protocol->setListener(this);
protocol->setup();
return protocolInfo.id;
return info.id;
}
void ProtocolManager::stopProtocol(Protocol* protocol)
void ProtocolManager::requestStop(Protocol* protocol)
{
// create the request
ProtocolRequest req;
req.protocol_info.protocol = protocol;
req.type = PROTOCOL_REQUEST_STOP;
// add it to the request stack
pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex);
}
void ProtocolManager::requestPause(Protocol* protocol)
{
// create the request
ProtocolRequest req;
req.protocol_info.protocol = protocol;
req.type = PROTOCOL_REQUEST_PAUSE;
// add it to the request stack
pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex);
}
void ProtocolManager::requestUnpause(Protocol* protocol)
{
// create the request
ProtocolRequest req;
req.protocol_info.protocol = protocol;
req.type = PROTOCOL_REQUEST_UNPAUSE;
// add it to the request stack
pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex);
}
void ProtocolManager::requestTerminate(Protocol* protocol)
{
// create the request
ProtocolRequest req;
req.protocol_info.protocol = protocol;
req.type = PROTOCOL_REQUEST_TERMINATE;
// add it to the request stack
pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex);
}
void ProtocolManager::startProtocol(ProtocolInfo protocol)
{
printf("__ProtocolManager> A new protocol with id=%u has been started. There are %ld protocols running.\n", protocol.id, m_protocols.size()+1);
// add the protocol to the protocol vector so that it's updated
pthread_mutex_lock(&m_protocols_mutex);
m_protocols.push_back(protocol);
pthread_mutex_unlock(&m_protocols_mutex);
// setup the protocol and notify it that it's started
protocol.protocol->setListener(this);
protocol.protocol->setup();
}
void ProtocolManager::stopProtocol(ProtocolInfo protocol)
{
}
void ProtocolManager::pauseProtocol(Protocol* protocol)
void ProtocolManager::pauseProtocol(ProtocolInfo protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_RUNNING)
if (m_protocols[i].protocol == protocol.protocol && m_protocols[i].state == PROTOCOL_STATE_RUNNING)
{
pthread_mutex_lock(&m_protocolsMutex);
m_protocols[i].state = PROTOCOL_STATE_PAUSED;
m_protocols[i].protocol->pause();
pthread_mutex_unlock(&m_protocolsMutex);
}
}
}
void ProtocolManager::unpauseProtocol(Protocol* protocol)
void ProtocolManager::unpauseProtocol(ProtocolInfo protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_PAUSED)
if (m_protocols[i].protocol == protocol.protocol && m_protocols[i].state == PROTOCOL_STATE_PAUSED)
{
pthread_mutex_lock(&m_protocolsMutex);
m_protocols[i].state = PROTOCOL_STATE_RUNNING;
m_protocols[i].protocol->unpause();
pthread_mutex_unlock(&m_protocolsMutex);
}
}
}
void ProtocolManager::protocolTerminated(Protocol* protocol)
void ProtocolManager::protocolTerminated(ProtocolInfo protocol)
{
pthread_mutex_lock(&m_protocolsMutex);
pthread_mutex_lock(&m_protocols_mutex); // be sure that noone accesses the protocols vector while we erase a protocol
int offset = 0;
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i-offset].protocol == protocol)
if (m_protocols[i-offset].protocol == protocol.protocol)
{
delete m_protocols[i].protocol;
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
@@ -92,17 +169,17 @@ void ProtocolManager::protocolTerminated(Protocol* protocol)
}
}
printf("__ProtocolManager> A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
pthread_mutex_unlock(&m_protocolsMutex);
pthread_mutex_unlock(&m_protocols_mutex);
}
void ProtocolManager::update()
{
// before updating, notice protocols that they have received information
pthread_mutex_lock(&m_eventsMutex); // secure threads
int size = m_eventsToProcess.size();
pthread_mutex_lock(&m_events_mutex); // secure threads
int size = m_events_to_process.size();
for (int i = 0; i < size; i++)
{
Event* event = m_eventsToProcess.back();
Event* event = m_events_to_process.back();
PROTOCOL_TYPE searchedProtocol = PROTOCOL_NONE;
if (event->data.size() > 0)
@@ -113,15 +190,44 @@ void ProtocolManager::update()
m_protocols[i].protocol->notifyEvent(event);
}
delete event;
m_eventsToProcess.pop_back();
m_events_to_process.pop_back();
}
pthread_mutex_unlock(&m_eventsMutex); // release the mutex
pthread_mutex_unlock(&m_events_mutex); // release the mutex
// now update all protocols
pthread_mutex_lock(&m_protocols_mutex);
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].state == PROTOCOL_STATE_RUNNING)
m_protocols[i].protocol->update();
}
pthread_mutex_unlock(&m_protocols_mutex);
// process queued events for protocols
pthread_mutex_lock(&m_requests_mutex);
for (unsigned int i = 0; i < m_requests.size(); i++)
{
switch (m_requests[i].type)
{
case PROTOCOL_REQUEST_START:
startProtocol(m_requests[i].protocol_info);
break;
case PROTOCOL_REQUEST_STOP:
stopProtocol(m_requests[i].protocol_info);
break;
case PROTOCOL_REQUEST_PAUSE:
pauseProtocol(m_requests[i].protocol_info);
break;
case PROTOCOL_REQUEST_UNPAUSE:
unpauseProtocol(m_requests[i].protocol_info);
break;
case PROTOCOL_REQUEST_TERMINATE:
protocolTerminated(m_requests[i].protocol_info);
break;
}
}
m_requests.clear();
pthread_mutex_unlock(&m_requests_mutex);
}
int ProtocolManager::runningProtocolsCount()
@@ -133,30 +239,49 @@ PROTOCOL_STATE ProtocolManager::getProtocolState(uint32_t id)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].id == id)
{
PROTOCOL_STATE state = m_protocols[i].state;
return state;
}
if (m_protocols[i].id == id) // we know a protocol with that id
return m_protocols[i].state; // return its state
}
return PROTOCOL_STATE_TERMINATED;
}
void ProtocolManager::assignProtocolId(ProtocolInfo& protocolInfo)
{
uint32_t newId;
bool exists;
do
// the protocol isn't running right now
for (unsigned int i = 0; i < m_requests.size(); i++)
{
newId = (rand()<<16)+rand();
exists = false;
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].id == newId)
exists = true;
}
} while (exists);
protocolInfo.id = newId;
if (m_requests[i].protocol_info.id == id) // the protocol is going to be started
return PROTOCOL_STATE_RUNNING; // we can say it's running
}
return PROTOCOL_STATE_TERMINATED; // else, it's already finished
}
PROTOCOL_STATE ProtocolManager::getProtocolState(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol) // the protocol is known
return m_protocols[i].state; // return its state
}
for (unsigned int i = 0; i < m_requests.size(); i++)
{
if (m_requests[i].protocol_info.protocol == protocol) // the protocol is going to be started
return PROTOCOL_STATE_RUNNING; // we can say it's running
}
return PROTOCOL_STATE_TERMINATED; // we don't know this protocol at all, it's finished
}
int ProtocolManager::getProtocolID(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol)
return m_protocols[i].id;
}
return 0;
}
void ProtocolManager::assignProtocolId(ProtocolInfo* protocol_info)
{
pthread_mutex_lock(&m_id_mutex);
protocol_info->id = m_next_protocol_id;
m_next_protocol_id++;
pthread_mutex_unlock(&m_id_mutex);
}

View File

@@ -1,3 +1,25 @@
//
// 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.
/*! \file protocol_manager.hpp
* \brief Contains structures and enumerations related to protocol management.
*/
#ifndef PROTOCOL_MANAGER_HPP
#define PROTOCOL_MANAGER_HPP
@@ -9,51 +31,236 @@
class Protocol;
/*!
* \enum PROTOCOL_STATE
* \brief Defines the three states that a protocol can have.
*/
enum PROTOCOL_STATE
{
PROTOCOL_STATE_RUNNING,
PROTOCOL_STATE_PAUSED,
PROTOCOL_STATE_TERMINATED
PROTOCOL_STATE_RUNNING, //!< The protocol is being updated everytime.
PROTOCOL_STATE_PAUSED, //!< The protocol is paused.
PROTOCOL_STATE_TERMINATED //!< The protocol is terminated/does not exist.
};
/*!
* \enum PROTOCOL_REQUEST_TYPE
* \brief Defines actions that can be done about protocols.
* This enum is used essentially to keep the manager thread-safe and
* to avoid protocols modifying directly their state.
*/
enum PROTOCOL_REQUEST_TYPE
{
PROTOCOL_REQUEST_START, //!< Start a protocol
PROTOCOL_REQUEST_STOP, //!< Stop a protocol
PROTOCOL_REQUEST_PAUSE, //!< Pause a protocol
PROTOCOL_REQUEST_UNPAUSE, //!< Unpause a protocol
PROTOCOL_REQUEST_TERMINATE //!< Terminate a protocol
};
/*!
* \struct ProtocolInfo
* \brief Stores the information needed to manage protocols
*/
typedef struct ProtocolInfo
{
PROTOCOL_STATE state; //!< The state of the protocol
Protocol* protocol; //!< A pointer to the protocol
uint32_t id; //!< The unique id of the protocol
} ProtocolInfo;
/*!
* \struct ProtocolRequest
* \brief Represents a request to do an action about a protocol.
*/
typedef struct ProtocolRequest
{
PROTOCOL_REQUEST_TYPE type; //!< The type of request
ProtocolInfo protocol_info; //!< The concerned protocol information
} ProtocolRequest;
/*!
* \class ProtocolManager
* \brief Manages the protocols at runtime.
*
* This class is in charge of storing and managing protocols.
* It is a singleton as there can be only one protocol manager per game
* instance. Any game object that wants to start a protocol must create a
* protocol and give it to this singleton. The protocols are updated in a
* special thread, to ensure that they are processed independently from the
* frames per second. Then, the management of protocols is thread-safe: any
* object can start/pause/stop protocols whithout problems.
*/
class ProtocolManager : public Singleton<ProtocolManager>
{
friend class Singleton<ProtocolManager>;
typedef struct
{
PROTOCOL_STATE state;
Protocol* protocol;
uint32_t id;
} ProtocolInfo;
public:
/*!
* \brief Function that processes incoming events.
* This function is called by the network manager each time there is an
* incoming packet.
*/
virtual void notifyEvent(Event* event);
/*!
* \brief WILL BE COMMENTED LATER
*/
virtual void sendMessage(std::string message);
virtual void notifyEvent(Event* event);
virtual void sendMessage(std::string message);
virtual int startProtocol(Protocol* protocol);
virtual void stopProtocol(Protocol* protocol);
virtual void pauseProtocol(Protocol* protocol);
virtual void unpauseProtocol(Protocol* protocol);
virtual void protocolTerminated(Protocol* protocol);
/*!
* \brief Asks the manager to start a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to start
* \return The unique id of the protocol that is being started.
*/
virtual int requestStart(Protocol* protocol);
/*!
* \brief Asks the manager to stop a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to stop
*/
virtual void requestStop(Protocol* protocol);
/*!
* \brief Asks the manager to pause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to pause
*/
virtual void requestPause(Protocol* protocol);
/*!
* \brief Asks the manager to unpause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to unpause
*/
virtual void requestUnpause(Protocol* protocol);
/*!
* \brief Notifies the manager that a protocol is terminated.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol that is finished
*/
virtual void requestTerminate(Protocol* protocol);
virtual void update();
/*!
* \brief Updates the manager.
*
* This function processes the events queue, notifies the concerned
* protocols that they have events to process. Then ask all protocols
* to update themselves. Finally processes stored requests about
* starting, stoping, pausing etc... protocols.
* This function is called by a thread as often as possible.
* This function is not FPS-dependant.
*/
virtual void update();
virtual int runningProtocolsCount();
virtual PROTOCOL_STATE getProtocolState(uint32_t id);
/*!
* \brief Get the number of protocols running.
* \return The number of protocols that are actually running.
*/
virtual int runningProtocolsCount();
/*!
* \brief Get the state of a protocol using its id.
* \param id : The id of the protocol you seek the state.
* \return The state of the protocol.
*/
virtual PROTOCOL_STATE getProtocolState(uint32_t id);
/*!
* \brief Get the state of a protocol using a pointer on it.
* \param protocol : A pointer to the protocol you seek the state.
* \return The state of the protocol.
*/
virtual PROTOCOL_STATE getProtocolState(Protocol* protocol);
/*!
* \brief Get the id of a protocol.
* \param protocol : A pointer to the protocol you seek the id.
* \return The id of the protocol pointed by the protocol parameter.
*/
virtual int getProtocolID(Protocol* protocol);
protected:
// protected functions
/*!
* \brief Constructor
*/
ProtocolManager();
/*!
* \brief Destructor
*/
virtual ~ProtocolManager();
void assignProtocolId(ProtocolInfo& protocolInfo);
/*!
* \brief Assign an id to a protocol.
* This function will assign m_next_protocol_id as the protocol id.
* This id starts at 0 at the beginning and is increased by 1 each time
* a protocol starts.
* \param protocol_info : The protocol info that needs an id.
*/
void assignProtocolId(ProtocolInfo* protocol_info);
/*!
* \brief Starts a protocol.
* Add the protocol info to the m_protocols vector.
* \param protocol : ProtocolInfo to start.
*/
virtual void startProtocol(ProtocolInfo protocol);
/*!
* \brief Stops a protocol.
* Coes nothing. Noone can stop running protocols for now.
* \param protocol : ProtocolInfo to stop.
*/
virtual void stopProtocol(ProtocolInfo protocol);
/*!
* \brief Pauses a protocol.
* Pauses a protocol and tells it that it's being paused.
* \param protocol : ProtocolInfo to pause.
*/
virtual void pauseProtocol(ProtocolInfo protocol);
/*!
* \brief Unpauses a protocol.
* Unpauses a protocol and notifies it.
* \param protocol : ProtocolInfo to unpause.
*/
virtual void unpauseProtocol(ProtocolInfo protocol);
/*!
* \brief Notes that a protocol is terminated.
* Remove a protocol from the protocols vector.
* \param protocol : ProtocolInfo concerned.
*/
virtual void protocolTerminated(ProtocolInfo protocol);
// protected members
std::vector<ProtocolInfo> m_protocols;
std::vector<Event*> m_eventsToProcess;
/*!
* \brief Contains the running protocols.
* This stores the protocols that are either running or paused, their
* state and their unique id.
*/
std::vector<ProtocolInfo> m_protocols;
/*!
* \brief Contains the network events to pass to protocols.
*/
std::vector<Event*> m_events_to_process;
/*!
* \brief Contains the requests to start/stop etc... protocols.
*/
std::vector<ProtocolRequest> m_requests;
/*! \brief The next id to assign to a protocol.
* This value is incremented by 1 each time a protocol is started.
* If a protocol has an id lower than this value, it means that it have
* been formerly started.
*/
unsigned int m_next_protocol_id;
// mutexes:
pthread_mutex_t m_eventsMutex;
pthread_mutex_t m_protocolsMutex;
/*! Used to ensure that the event queue is used thread-safely. */
pthread_mutex_t m_events_mutex;
/*! Used to ensure that the protocol vector is used thread-safely. */
pthread_mutex_t m_protocols_mutex;
/*! Used to ensure that the request vector is used thread-safely. */
pthread_mutex_t m_requests_mutex;
/*! Used to ensure that the protocol id is used in a thread-safe way.*/
pthread_mutex_t m_id_mutex;
};
#endif // PROTOCOL_MANAGER_HPP

View File

@@ -1,53 +1,89 @@
#include "connect_to_server.hpp"
//
// 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.
#include "../client_network_manager.hpp"
#include "../time.hpp"
#include "protocols/connect_to_server.hpp"
#include "client_network_manager.hpp"
#include "time.hpp"
#include <stdio.h>
#include <stdlib.h>
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_CONNECTION)
// ----------------------------------------------------------------------------
ConnectToServer::ConnectToServer(CallbackObject* callback_object) :
Protocol(callback_object, PROTOCOL_CONNECTION)
{
m_serverIp = 0;
m_serverPort = 0;
m_server_ip = 0;
m_server_port = 0;
m_state = NONE;
}
// ----------------------------------------------------------------------------
ConnectToServer::~ConnectToServer()
{
}
// ----------------------------------------------------------------------------
void ConnectToServer::notifyEvent(Event* event)
{
if (event->type == EVENT_TYPE_CONNECTED && event->peer->getAddress() == m_serverIp && event->peer->getPort() == m_serverPort)
if (event->type == EVENT_TYPE_CONNECTED &&
event->peer->getAddress() == m_server_ip &&
event->peer->getPort() == m_server_port)
{
printf("The Connect To Server protocol has received an event notifying that he's connected to the peer. The peer sent \"%s\"\n", event->data.c_str());
printf("The Connect To Server protocol has received an event notifying \
that he's connected to the peer. The peer sent \"%s\"\n",
event->data.c_str());
m_state = DONE; // we received a message, we are connected
}
}
// ----------------------------------------------------------------------------
void ConnectToServer::setup()
{
m_state = NONE;
if (m_serverIp == 0 || m_serverPort == 0 )
if (m_server_ip == 0 || m_server_port == 0 )
{
printf("You have to set the server's public ip:port of the server.\n");
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}
// ----------------------------------------------------------------------------
void ConnectToServer::update()
{
if (m_state == NONE)
{
static double target = 0;
double currentTime = Time::getSeconds();
while (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
// sometimes the getSeconds method forgets 3600 seconds.
while (currentTime < target-1800)
currentTime += 3600;
if (currentTime > target)
{
NetworkManager::getInstance()->connect(m_serverIp, m_serverPort);
if (NetworkManager::getInstance()->isConnectedTo(m_serverIp, m_serverPort))
NetworkManager::getInstance()->connect(
TransportAddress(m_server_ip, m_server_port));
if (NetworkManager::getInstance()->isConnectedTo(
TransportAddress(m_server_ip, m_server_port)))
{
m_state = DONE;
return;
@@ -58,13 +94,17 @@ void ConnectToServer::update()
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}
// ----------------------------------------------------------------------------
void ConnectToServer::setServerAddress(uint32_t ip, uint16_t port)
{
m_serverIp = ip;
m_serverPort = port;
m_server_ip = ip;
m_server_port = port;
}
// ----------------------------------------------------------------------------

View File

@@ -1,13 +1,31 @@
//
// 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.
#ifndef CONNECT_TO_SERVER_HPP
#define CONNECT_TO_SERVER_HPP
#include "../protocol.hpp"
#include "protocol.hpp"
#include <string>
class ConnectToServer : public Protocol, public CallbackObject
{
public:
ConnectToServer(CallbackObject* callbackObject);
ConnectToServer(CallbackObject* callback_object);
virtual ~ConnectToServer();
virtual void notifyEvent(Event* event);
@@ -17,8 +35,8 @@ class ConnectToServer : public Protocol, public CallbackObject
void setServerAddress(uint32_t ip, uint16_t port);
protected:
uint32_t m_serverIp;
uint16_t m_serverPort;
uint32_t m_server_ip;
uint16_t m_server_port;
enum STATE
{

View File

@@ -1,12 +1,30 @@
#include "get_peer_address.hpp"
//
// 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.
#include "../time.hpp"
#include "../http_functions.hpp"
#include "protocols/get_peer_address.hpp"
#include "time.hpp"
#include "http_functions.hpp"
#include <stdio.h>
#include <stdlib.h>
GetPeerAddress::GetPeerAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
GetPeerAddress::GetPeerAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
}
@@ -28,13 +46,13 @@ void GetPeerAddress::update()
if (m_state == NONE)
{
static double target = 0;
double currentTime = Time::getSeconds();
while (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
currentTime += 3600;
if (currentTime > target)
double current_time = Time::getSeconds();
while (current_time < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
current_time += 3600;
if (current_time > target)
{
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?get&nick=%s", m_peerName.c_str());
sprintf(url, "http://stkconnect.freeserver.me/log.php?get&nick=%s", m_peer_name.c_str());
std::string result = HTTP::getPage(url);
if (result == "")
{
@@ -42,39 +60,39 @@ void GetPeerAddress::update()
pause();
return;
}
std::string ipAddr = result;
ipAddr.erase(ipAddr.find_first_of(':'));
std::string portNb = result;
portNb.erase(0, portNb.find_first_of(':')+1);
uint32_t dstIp = (uint32_t)(atoi(ipAddr.c_str()));
uint16_t dstPort = (uint32_t)(atoi(portNb.c_str()));
if (dstIp == 0 || dstPort == 0)
std::string ip_addr = result;
ip_addr.erase(ip_addr.find_first_of(':'));
std::string port_nb = result;
port_nb.erase(0, port_nb.find_first_of(':')+1);
uint32_t dst_ip = (uint32_t)(atoi(ip_addr.c_str()));
uint16_t dst_port = (uint32_t)(atoi(port_nb.c_str()));
if (dst_ip == 0 || dst_port == 0)
{
printf("__GetPeerAddress> The host you try to reach is not online. There will be a new try in 10 seconds.\n");
target = currentTime+10;
target = current_time+10;
}
else
{
printf("__GetPeerAddress> Public ip of target is %i.%i.%i.%i:%i\n", (dstIp>>24)&0xff, (dstIp>>16)&0xff, (dstIp>>8)&0xff, dstIp&0xff, dstPort);
uint32_t serverIp = ((dstIp&0x000000ff)<<24) // change the server IP to have a network-byte order
+ ((dstIp&0x0000ff00)<<8)
+ ((dstIp&0x00ff0000)>>8)
+ ((dstIp&0xff000000)>>24);
uint16_t serverPort = dstPort;
TransportAddress* addr = static_cast<TransportAddress*>(m_callbackObject);
addr->ip = serverIp;
addr->port = serverPort;
printf("__GetPeerAddress> Public ip of target is %i.%i.%i.%i:%i\n", (dst_ip>>24)&0xff, (dst_ip>>16)&0xff, (dst_ip>>8)&0xff, dst_ip&0xff, dst_port);
uint32_t server_ip = ((dst_ip&0x000000ff)<<24) // change the server IP to have a network-byte order
+ ((dst_ip&0x0000ff00)<<8)
+ ((dst_ip&0x00ff0000)>>8)
+ ((dst_ip&0xff000000)>>24);
uint16_t server_port = dst_port;
TransportAddress* addr = static_cast<TransportAddress*>(m_callback_object);
addr->ip = server_ip;
addr->port = server_port;
m_state = DONE;
}
}
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}
void GetPeerAddress::setPeerName(std::string peerName)
void GetPeerAddress::setPeerName(std::string peer_name)
{
m_peerName = peerName;
m_peer_name = peer_name;
}

View File

@@ -1,21 +1,39 @@
//
// 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.
#ifndef GET_PEER_ADDRESS_HPP
#define GET_PEER_ADDRESS_HPP
#include "../protocol.hpp"
#include "protocol.hpp"
class GetPeerAddress : public Protocol
{
public:
GetPeerAddress(CallbackObject* callbackObject);
GetPeerAddress(CallbackObject* callback_object);
virtual ~GetPeerAddress();
virtual void notifyEvent(Event* event);
virtual void setup();
virtual void update();
void setPeerName(std::string peerName);
void setPeerName(std::string peer_name);
protected:
std::string m_peerName;
std::string m_peer_name;
enum STATE
{

View File

@@ -1,9 +1,27 @@
#include "get_public_address.hpp"
//
// 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.
#include "../network_manager.hpp"
#include "../client_network_manager.hpp"
#include "connect_to_server.hpp"
#include "../network_interface.hpp"
#include "protocols/get_public_address.hpp"
#include "network_manager.hpp"
#include "client_network_manager.hpp"
#include "protocols/connect_to_server.hpp"
#include "network_interface.hpp"
#include <time.h>
#include <stdlib.h>
@@ -21,7 +39,7 @@ int stunRand()
return rand();
}
GetPublicAddress::GetPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
GetPublicAddress::GetPublicAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
}
@@ -45,9 +63,9 @@ void GetPublicAddress::update()
{
// format : 00MMMMMCMMMCMMMM (cf rfc 5389)
uint16_t message_type = 0b0000000000000001; // binding request
m_stunTransactionID[0] = stunRand();
m_stunTransactionID[1] = stunRand();
m_stunTransactionID[2] = stunRand();
m_stun_tansaction_id[0] = stunRand();
m_stun_tansaction_id[1] = stunRand();
m_stun_tansaction_id[2] = stunRand();
uint16_t message_length = 0x0000;
uint8_t bytes[21]; // the message to be sent
@@ -60,72 +78,72 @@ void GetPublicAddress::update()
bytes[3] = (uint8_t)(message_length);
// bytes 4-7 : magic cookie to recognize the stun protocol
bytes[4] = (uint8_t)(m_stunMagicCookie>>24);
bytes[5] = (uint8_t)(m_stunMagicCookie>>16);
bytes[6] = (uint8_t)(m_stunMagicCookie>>8);
bytes[7] = (uint8_t)(m_stunMagicCookie);
bytes[4] = (uint8_t)(m_stun_magic_cookie>>24);
bytes[5] = (uint8_t)(m_stun_magic_cookie>>16);
bytes[6] = (uint8_t)(m_stun_magic_cookie>>8);
bytes[7] = (uint8_t)(m_stun_magic_cookie);
// bytes 8-19 : the transaction id
bytes[8] = (uint8_t)(m_stunTransactionID[0]>>24);
bytes[9] = (uint8_t)(m_stunTransactionID[0]>>16);
bytes[10] = (uint8_t)(m_stunTransactionID[0]>>8);
bytes[11] = (uint8_t)(m_stunTransactionID[0]);
bytes[12] = (uint8_t)(m_stunTransactionID[1]>>24);
bytes[13] = (uint8_t)(m_stunTransactionID[1]>>16);
bytes[14] = (uint8_t)(m_stunTransactionID[1]>>8);
bytes[15] = (uint8_t)(m_stunTransactionID[1]);
bytes[16] = (uint8_t)(m_stunTransactionID[2]>>24);
bytes[17] = (uint8_t)(m_stunTransactionID[2]>>16);
bytes[18] = (uint8_t)(m_stunTransactionID[2]>>8);
bytes[19] = (uint8_t)(m_stunTransactionID[2]);
bytes[8] = (uint8_t)(m_stun_tansaction_id[0]>>24);
bytes[9] = (uint8_t)(m_stun_tansaction_id[0]>>16);
bytes[10] = (uint8_t)(m_stun_tansaction_id[0]>>8);
bytes[11] = (uint8_t)(m_stun_tansaction_id[0]);
bytes[12] = (uint8_t)(m_stun_tansaction_id[1]>>24);
bytes[13] = (uint8_t)(m_stun_tansaction_id[1]>>16);
bytes[14] = (uint8_t)(m_stun_tansaction_id[1]>>8);
bytes[15] = (uint8_t)(m_stun_tansaction_id[1]);
bytes[16] = (uint8_t)(m_stun_tansaction_id[2]>>24);
bytes[17] = (uint8_t)(m_stun_tansaction_id[2]>>16);
bytes[18] = (uint8_t)(m_stun_tansaction_id[2]>>8);
bytes[19] = (uint8_t)(m_stun_tansaction_id[2]);
bytes[20] = '\0';
printf("__GetPublicAddress> Querrying STUN server 132.177.123.6\n");
unsigned int dst = (132<<24)+(177<<16)+(123<<8)+6;
NetworkManager::getInstance()->setManualSocketsMode(true);
NetworkManager::getInstance()->getHost()->sendRawPacket(bytes, 20, dst, 3478);
NetworkManager::getInstance()->getHost()->sendRawPacket(bytes, 20, TransportAddress(dst, 3478));
m_state = TEST_SENT;
}
if (m_state == TEST_SENT)
{
unsigned int dst = (132<<24)+(177<<16)+(123<<8)+6;
uint8_t* data = NetworkManager::getInstance()->getHost()->receiveRawPacket(dst, 3478);
uint8_t* data = NetworkManager::getInstance()->getHost()->receiveRawPacket(TransportAddress(dst, 3478));
assert(data);
// check that the stun response is a response, contains the magic cookie and the transaction ID
if ( data[0] == 0b01 &&
data[1] == 0b01 &&
data[4] == (uint8_t)(m_stunMagicCookie>>24) &&
data[5] == (uint8_t)(m_stunMagicCookie>>16) &&
data[6] == (uint8_t)(m_stunMagicCookie>>8) &&
data[7] == (uint8_t)(m_stunMagicCookie))
data[4] == (uint8_t)(m_stun_magic_cookie>>24) &&
data[5] == (uint8_t)(m_stun_magic_cookie>>16) &&
data[6] == (uint8_t)(m_stun_magic_cookie>>8) &&
data[7] == (uint8_t)(m_stun_magic_cookie))
{
if(
data[8] == (uint8_t)(m_stunTransactionID[0]>>24) &&
data[9] == (uint8_t)(m_stunTransactionID[0]>>16) &&
data[10] == (uint8_t)(m_stunTransactionID[0]>>8 ) &&
data[11] == (uint8_t)(m_stunTransactionID[0] ) &&
data[12] == (uint8_t)(m_stunTransactionID[1]>>24) &&
data[13] == (uint8_t)(m_stunTransactionID[1]>>16) &&
data[14] == (uint8_t)(m_stunTransactionID[1]>>8 ) &&
data[15] == (uint8_t)(m_stunTransactionID[1] ) &&
data[16] == (uint8_t)(m_stunTransactionID[2]>>24) &&
data[17] == (uint8_t)(m_stunTransactionID[2]>>16) &&
data[18] == (uint8_t)(m_stunTransactionID[2]>>8 ) &&
data[19] == (uint8_t)(m_stunTransactionID[2] ))
data[8] == (uint8_t)(m_stun_tansaction_id[0]>>24) &&
data[9] == (uint8_t)(m_stun_tansaction_id[0]>>16) &&
data[10] == (uint8_t)(m_stun_tansaction_id[0]>>8 ) &&
data[11] == (uint8_t)(m_stun_tansaction_id[0] ) &&
data[12] == (uint8_t)(m_stun_tansaction_id[1]>>24) &&
data[13] == (uint8_t)(m_stun_tansaction_id[1]>>16) &&
data[14] == (uint8_t)(m_stun_tansaction_id[1]>>8 ) &&
data[15] == (uint8_t)(m_stun_tansaction_id[1] ) &&
data[16] == (uint8_t)(m_stun_tansaction_id[2]>>24) &&
data[17] == (uint8_t)(m_stun_tansaction_id[2]>>16) &&
data[18] == (uint8_t)(m_stun_tansaction_id[2]>>8 ) &&
data[19] == (uint8_t)(m_stun_tansaction_id[2] ))
{
printf("__GetPublicAddress> The STUN server responded with a valid answer\n");
int messageSize = data[2]*256+data[3];
int message_size = data[2]*256+data[3];
// parse the stun message now:
bool finish = false;
uint8_t* attributes = data+20;
if (messageSize == 0)
if (message_size == 0)
{
printf("__GetPublicAddress> STUN answer does not contain any information.\n");
finish = true;
}
if (messageSize < 4) // cannot even read the size
if (message_size < 4) // cannot even read the size
{
printf("__GetPublicAddress> STUN message is not valid.\n");
finish = true;
@@ -153,10 +171,10 @@ void GetPublicAddress::update()
break;
}
attributes = attributes + 4 + size;
messageSize -= 4 + size;
if (messageSize == 0)
message_size -= 4 + size;
if (message_size == 0)
finish = true;
if (messageSize < 4) // cannot even read the size
if (message_size < 4) // cannot even read the size
{
printf("__GetPublicAddress> STUN message is not valid.\n");
finish = true;
@@ -168,7 +186,7 @@ void GetPublicAddress::update()
printf("__The public address has been found : %i.%i.%i.%i:%i\n", address>>24&0xff, address>>16&0xff, address>>8&0xff, address&0xff, port);
m_state = ADDRESS_KNOWN;
NetworkManager::getInstance()->setManualSocketsMode(false);
TransportAddress* addr = static_cast<TransportAddress*>(m_callbackObject);
TransportAddress* addr = static_cast<TransportAddress*>(m_callback_object);
addr->ip = address;
addr->port = port;
}
@@ -184,6 +202,6 @@ void GetPublicAddress::update()
if (m_state == ADDRESS_KNOWN)
{
// terminate the protocol
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}

View File

@@ -1,12 +1,30 @@
//
// 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.
#ifndef GET_PUBLIC_ADDRESS_HPP
#define GET_PUBLIC_ADDRESS_HPP
#include "../protocol.hpp"
#include "protocol.hpp"
class GetPublicAddress : public Protocol
{
public:
GetPublicAddress(CallbackObject* callbackObject);
GetPublicAddress(CallbackObject* callback_object);
virtual ~GetPublicAddress();
virtual void notifyEvent(Event* event);
@@ -22,8 +40,8 @@ class GetPublicAddress : public Protocol
ADDRESS_KNOWN
};
STATE m_state;
uint32_t m_stunTransactionID[3];
static const uint32_t m_stunMagicCookie = 0x2112A442;
uint32_t m_stun_tansaction_id[3];
static const uint32_t m_stun_magic_cookie = 0x2112A442;
};
#endif // GET_PUBLIC_ADDRESS_HPP

View File

@@ -1,10 +1,28 @@
#include "hide_public_address.hpp"
//
// 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.
#include "../http_functions.hpp"
#include "protocols/hide_public_address.hpp"
#include "http_functions.hpp"
#include <stdio.h>
HidePublicAddress::HidePublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
HidePublicAddress::HidePublicAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
}
@@ -42,7 +60,7 @@ void HidePublicAddress::update()
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}

View File

@@ -1,13 +1,31 @@
//
// 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.
#ifndef HIDE_PUBLIC_ADDRESS_HPP
#define HIDE_PUBLIC_ADDRESS_HPP
#include "../protocol.hpp"
#include "protocol.hpp"
#include <string>
class HidePublicAddress : public Protocol
{
public:
HidePublicAddress(CallbackObject* callbackObject);
HidePublicAddress(CallbackObject* callback_object);
virtual ~HidePublicAddress();
virtual void notifyEvent(Event* event);

View File

@@ -0,0 +1,50 @@
//
// 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.
#include "lobby_room_protocol.hpp"
#include <stdio.h>
LobbyRoomProtocol::LobbyRoomProtocol(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_LOBBY_ROOM)
{
}
LobbyRoomProtocol::~LobbyRoomProtocol()
{
}
void LobbyRoomProtocol::notifyEvent(Event* event)
{
if (event->type == EVENT_TYPE_MESSAGE)
{
printf("Message from %u : \"%s\"\n", event->peer->getAddress(), event->data.c_str());
}
}
void LobbyRoomProtocol::setup()
{
}
void LobbyRoomProtocol::update()
{
}
void LobbyRoomProtocol::sendMessage(std::string message)
{
}

View File

@@ -0,0 +1,47 @@
//
// 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.
#ifndef LOBBY_ROOM_PROTOCOL_HPP
#define LOBBY_ROOM_PROTOCOL_HPP
#include "protocol.hpp"
/*!
* \class LobbyRoomProtocol
* \brief Class used while the game is being prepared.
* This protocol starts when a server opens a game, or when a client joins a game.
* It is used to exchange data about the race settings, like kart selection.
*/
class LobbyRoomProtocol : public Protocol
{
public:
LobbyRoomProtocol(CallbackObject* callback_object);
virtual ~LobbyRoomProtocol();
virtual void notifyEvent(Event* event);
virtual void setup();
virtual void update();
void sendMessage(std::string message);
protected:
};
#endif // LOBBY_ROOM_PROTOCOL_HPP

View File

@@ -1,10 +1,28 @@
//
// 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.
#include "show_public_address.hpp"
#include "../http_functions.hpp"
#include <stdio.h>
ShowPublicAddress::ShowPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
ShowPublicAddress::ShowPublicAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
}
@@ -19,10 +37,10 @@ void ShowPublicAddress::notifyEvent(Event* event)
void ShowPublicAddress::setup()
{
m_state = NONE;
if (m_publicIp == 0 || m_publicPort == 0 || m_username == "" || m_password == "")
if (m_public_ip == 0 || m_public_port == 0 || m_username == "" || m_password == "")
{
printf("__ShowPublicAddress> You have to set the public ip:port, username:password and the host nickname before starting this protocol.\n");
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}
@@ -31,7 +49,7 @@ void ShowPublicAddress::update()
if (m_state == NONE)
{
char url[512];
sprintf(url, "http://stkconnect.freeserver.me/log.php?set&nick=%s&ip=%u&port=%u&pwd=%s", m_username.c_str(), m_publicIp, m_publicPort, m_password.c_str());
sprintf(url, "http://stkconnect.freeserver.me/log.php?set&nick=%s&ip=%u&port=%u&pwd=%s", m_username.c_str(), m_public_ip, m_public_port, m_password.c_str());
std::string result = HTTP::getPage(url);
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c' && result[3] == 'c' && result[4] == 'e' && result[5] == 's' && result[6] == 's')
{
@@ -47,7 +65,7 @@ void ShowPublicAddress::update()
}
else if (m_state == DONE)
{
m_listener->protocolTerminated(this);
m_listener->requestTerminate(this);
}
}
@@ -61,6 +79,6 @@ void ShowPublicAddress::setPassword(std::string password)
}
void ShowPublicAddress::setPublicAddress(uint32_t ip, uint16_t port)
{
m_publicIp = ip;
m_publicPort = port;
m_public_ip = ip;
m_public_port = port;
}

View File

@@ -1,13 +1,31 @@
//
// 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.
#ifndef SHOW_PUBLIC_ADDRESS_HPP
#define SHOW_PUBLIC_ADDRESS_HPP
#include "../protocol.hpp"
#include "protocol.hpp"
#include <string>
class ShowPublicAddress : public Protocol
{
public:
ShowPublicAddress(CallbackObject* callbackObject);
ShowPublicAddress(CallbackObject* callback_object);
virtual ~ShowPublicAddress();
virtual void notifyEvent(Event* event);
@@ -21,8 +39,8 @@ class ShowPublicAddress : public Protocol
protected:
std::string m_username;
std::string m_password;
uint32_t m_publicIp;
uint16_t m_publicPort;
uint32_t m_public_ip;
uint16_t m_public_port;
enum STATE
{

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "server_network_manager.hpp"
#include "protocols/get_public_address.hpp"
@@ -39,34 +57,35 @@ void ServerNetworkManager::start()
printf("_NetworkInterface>Starting the global protocol\n");
// step 1 : retreive public address
int id = ProtocolManager::getInstance()->startProtocol(new GetPublicAddress(&m_publicAddress));
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
Protocol* protocol = new GetPublicAddress(&m_public_address);
ProtocolManager::getInstance()->requestStart(protocol);
while (ProtocolManager::getInstance()->getProtocolState(protocol) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is known.\n");
// step 2 : show the public address for others (here, the server)
ShowPublicAddress* spa = new ShowPublicAddress(NULL);
spa->setPassword(m_playerLogin.password);
spa->setUsername(m_playerLogin.username);
spa->setPublicAddress(m_publicAddress.ip, m_publicAddress.port);
id = ProtocolManager::getInstance()->startProtocol(spa);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
spa->setPassword(m_player_login.password);
spa->setUsername(m_player_login.username);
spa->setPublicAddress(m_public_address.ip, m_public_address.port);
ProtocolManager::getInstance()->requestStart(spa);
while (ProtocolManager::getInstance()->getProtocolState(spa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is being shown online.\n");
}
bool ServerNetworkManager::connectToPeer(std::string peerUsername)
bool ServerNetworkManager::connectToPeer(std::string peer_username)
{
printf("_NetworkInterface>Starting the connection to host protocol\n");
// step 3 : get the peer's addres.
TransportAddress addr;
GetPeerAddress* gpa = new GetPeerAddress(&addr);
gpa->setPeerName(peerUsername);
uint32_t id = ProtocolManager::getInstance()->startProtocol(gpa);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
gpa->setPeerName(peer_username);
ProtocolManager::getInstance()->requestStart(gpa);
while (ProtocolManager::getInstance()->getProtocolState(gpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address of the peer is known.\n");
@@ -74,12 +93,12 @@ bool ServerNetworkManager::connectToPeer(std::string peerUsername)
// step 2 : connect to the peer
ConnectToServer* cts = new ConnectToServer(NULL);
cts->setServerAddress(addr.ip, addr.port);
id = ProtocolManager::getInstance()->startProtocol(cts);
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
ProtocolManager::getInstance()->requestStart(cts);
while (ProtocolManager::getInstance()->getProtocolState(cts) != PROTOCOL_STATE_TERMINATED )
{
}
bool success = false;
if (isConnectedTo(addr.ip, addr.port))
if (isConnectedTo(addr))
{
success = true;
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A PEER.\n");

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef SERVER_NETWORK_MANAGER_HPP
#define SERVER_NETWORK_MANAGER_HPP
@@ -16,7 +34,7 @@ class ServerNetworkManager : public NetworkManager
virtual void run();
void start();
bool connectToPeer(std::string peerUsername);
bool connectToPeer(std::string peer_username);
virtual void packetReceived(char* data);
virtual void sendPacket(char* data);

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef SINGLETON_HPP
#define SINGLETON_HPP

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "stk_host.hpp"
#include "network_manager.hpp"
@@ -7,6 +25,8 @@
#include <pthread.h>
#include <arpa/inet.h>
// ----------------------------------------------------------------------------
void* STKHost::receive_data(void* self)
{
ENetEvent event;
@@ -21,62 +41,87 @@ void* STKHost::receive_data(void* self)
return NULL;
}
// ----------------------------------------------------------------------------
STKHost::STKHost()
{
m_host = NULL;
m_listeningThread = (pthread_t*)(malloc(sizeof(pthread_t)));
m_listening_thread = (pthread_t*)(malloc(sizeof(pthread_t)));
}
// ----------------------------------------------------------------------------
STKHost::~STKHost()
{
}
void STKHost::setupServer(uint32_t address, uint16_t port, int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth)
// ----------------------------------------------------------------------------
void STKHost::setupServer(uint32_t address, uint16_t port, int peer_count,
int channel_limit, uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth)
{
ENetAddress* addr = (ENetAddress*)(malloc(sizeof(ENetAddress)));
addr->host = address;
addr->port = port;
m_host = enet_host_create(addr, peerCount, channelLimit, maxIncomingBandwidth, maxOutgoingBandwidth);
m_host = enet_host_create(addr, peer_count, channel_limit,
max_incoming_bandwidth, max_outgoing_bandwidth);
if (m_host == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet server host.\n");
fprintf (stderr, "An error occurred while trying to create an ENet \
server host.\n");
exit (EXIT_FAILURE);
}
}
void STKHost::setupClient(int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth)
// ----------------------------------------------------------------------------
void STKHost::setupClient(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth)
{
m_host = enet_host_create(NULL, peerCount, channelLimit, maxIncomingBandwidth, maxOutgoingBandwidth);
m_host = enet_host_create(NULL, peer_count, channel_limit,
max_incoming_bandwidth, max_outgoing_bandwidth);
if (m_host == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet client host.\n");
fprintf (stderr, "An error occurred while trying to create an ENet \
client host.\n");
exit (EXIT_FAILURE);
}
}
// ----------------------------------------------------------------------------
void STKHost::startListening()
{
pthread_create(m_listeningThread, NULL, &STKHost::receive_data, this);
pthread_create(m_listening_thread, NULL, &STKHost::receive_data, this);
}
// ----------------------------------------------------------------------------
void STKHost::stopListening()
{
pthread_cancel(*m_listeningThread);
pthread_cancel(*m_listening_thread);
}
void STKHost::sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort)
// ----------------------------------------------------------------------------
void STKHost::sendRawPacket(uint8_t* data, int length, TransportAddress dst)
{
struct sockaddr_in to;
int toLen = sizeof(to);
memset(&to,0,toLen);
int to_len = sizeof(to);
memset(&to,0,to_len);
to.sin_family = AF_INET;
to.sin_port = htons(dstPort);
to.sin_addr.s_addr = htonl(dstIp);
to.sin_port = htons(dst.port);
to.sin_addr.s_addr = htonl(dst.ip);
sendto(m_host->socket, data, length, 0,(sockaddr*)&to, toLen);
sendto(m_host->socket, data, length, 0,(sockaddr*)&to, to_len);
}
// ----------------------------------------------------------------------------
uint8_t* STKHost::receiveRawPacket()
{
uint8_t* buffer; // max size needed normally (only used for stun)
@@ -85,7 +130,8 @@ uint8_t* STKHost::receiveRawPacket()
int len = recv(m_host->socket,buffer,2048, 0);
int i = 0;
while(len < 0) // wait to receive the message because enet sockets are non-blocking
// wait to receive the message because enet sockets are non-blocking
while(len < 0)
{
i++;
len = recv(m_host->socket,buffer,2048, 0);
@@ -94,27 +140,31 @@ uint8_t* STKHost::receiveRawPacket()
printf("Packet received after %i milliseconds\n", i);
return buffer;
}
uint8_t* STKHost::receiveRawPacket(unsigned int dstIp, unsigned short dstPort)
// ----------------------------------------------------------------------------
uint8_t* STKHost::receiveRawPacket(TransportAddress sender)
{
uint8_t* buffer; // max size needed normally (only used for stun)
buffer = (uint8_t*)(malloc(sizeof(uint8_t)*2048));
memset(buffer, 0, 2048);
socklen_t fromlen;
socklen_t from_len;
struct sockaddr addr;
fromlen = sizeof(addr);
int len = recvfrom(m_host->socket, buffer, 2048, 0, &addr, &fromlen);
from_len = sizeof(addr);
int len = recvfrom(m_host->socket, buffer, 2048, 0, &addr, &from_len);
int i = 0;
// wait to receive the message because enet sockets are non-blocking
while(len < 0 || (
(uint8_t)(addr.sa_data[2]) != (dstIp>>24&0xff)
&& (uint8_t)(addr.sa_data[3]) != (dstIp>>16&0xff)
&& (uint8_t)(addr.sa_data[4]) != (dstIp>>8&0xff)
&& (uint8_t)(addr.sa_data[5]) != (dstIp&0xff))) // wait to receive the message because enet sockets are non-blocking
(uint8_t)(addr.sa_data[2]) != (sender.ip>>24&0xff)
&& (uint8_t)(addr.sa_data[3]) != (sender.ip>>16&0xff)
&& (uint8_t)(addr.sa_data[4]) != (sender.ip>>8&0xff)
&& (uint8_t)(addr.sa_data[5]) != (sender.ip&0xff)))
{
i++;
len = recvfrom(m_host->socket, buffer, 2048, 0, &addr, &fromlen);
len = recvfrom(m_host->socket, buffer, 2048, 0, &addr, &from_len);
usleep(1000); // wait 1 millisecond between two checks
}
if (addr.sa_family == AF_INET)
@@ -127,28 +177,39 @@ uint8_t* STKHost::receiveRawPacket(unsigned int dstIp, unsigned short dstPort)
return buffer;
}
// ----------------------------------------------------------------------------
void STKHost::broadcastPacket(char* data)
{
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,ENET_PACKET_FLAG_RELIABLE);
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,
ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(m_host, 0, packet);
}
bool STKHost::peerExists(uint32_t ip, uint16_t port)
// ----------------------------------------------------------------------------
bool STKHost::peerExists(TransportAddress peer)
{
for (unsigned int i = 0; i < m_host->peerCount; i++)
{
if (m_host->peers[i].address.host == ip && m_host->peers[i].address.port == port)
if (m_host->peers[i].address.host == peer.ip &&
m_host->peers[i].address.port == peer.port)
{
return true;
}
}
return false;
}
bool STKHost::isConnectedTo(uint32_t ip, uint16_t port)
// ----------------------------------------------------------------------------
bool STKHost::isConnectedTo(TransportAddress peer)
{
for (unsigned int i = 0; i < m_host->peerCount; i++)
{
if (m_host->peers[i].address.host == ip && m_host->peers[i].address.port == port && m_host->peers[i].state == ENET_PEER_STATE_CONNECTED)
if (m_host->peers[i].address.host == peer.ip &&
m_host->peers[i].address.port == peer.port &&
m_host->peers[i].state == ENET_PEER_STATE_CONNECTED)
{
return true;
}

View File

@@ -1,41 +1,140 @@
//
// 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.
/*! \file stk_host.hpp
* \brief Defines an interface to use network low-level functions easily.
*/
#ifndef STK_HOST_HPP
#define STK_HOST_HPP
#include <enet/enet.h>
#include "types.hpp"
/*! \class STKHost
* \brief Represents the local host.
* This host is either a server host or a client host. A client host is in
* charge of connecting to a server. A server opens a socket for incoming
* connections.
* By default, this host will use ENet to exchange packets. It also defines an
* interface for ENet use. Nevertheless, this class can be used to send and/or
* receive packets whithout ENet adding its headers.
* This class is used by the Network Manager to send packets.
*/
class STKHost
{
friend class STKPeer;
friend class STKPeer; // allow direct enet modifications in implementations
public:
/*! \enum HOST_TYPE
* \brief Defines three host types for the server.
* These values tells the host where he will accept connections from.
*/
enum HOST_TYPE
{
HOST_ANY = 0,
HOST_BROADCAST = 0xFFFFFFFF,
PORT_ANY = 0
HOST_ANY = 0, //!< Any host.
HOST_BROADCAST = 0xFFFFFFFF, //!< Defines the broadcast address.
PORT_ANY = 0 //!< Any port.
};
/*! \brief Constructor */
STKHost();
/*! \brief Destructor */
virtual ~STKHost();
/*! \brief Thread function checking if data is received.
* This function tries to get data from network low-level functions as
* often as possible. When something is received, it generates an
* event and passes it to the Network Manager.
* \param self : used to pass the ENet host to the function.
*/
static void* receive_data(void* self);
void setupServer(uint32_t address, uint16_t port, int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth);
void setupClient(int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth);
/*! \brief Setups the host as a server.
* \param address : The IPv4 address of incoming connections.
* \param port : The port on which the server listens.
* \param peer_count : The maximum number of peers.
* \param channel_limit : The maximum number of channels per peer.
* \param max_incoming_bandwidth : The maximum incoming bandwidth.
* \param max_outgoing_bandwidth : The maximum outgoing bandwidth.
*/
void setupServer(uint32_t address, uint16_t port,
int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth);
/*! \brief Setups the host as a client.
* In fact there is only one peer connected to this host.
* \param peer_count : The maximum number of peers.
* \param channel_limit : The maximum number of channels per peer.
* \param max_incoming_bandwidth : The maximum incoming bandwidth.
* \param max_outgoing_bandwidth : The maximum outgoing bandwidth.
*/
void setupClient(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth);
void startListening();
void stopListening();
/*! \brief Starts the listening of events from ENet.
* Starts a thread that updates it as often as possible.
*/
void startListening();
/*! \brief Stops the listening of events from ENet.
* Stops the thread that was receiving events.
*/
void stopListening();
void sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort);
uint8_t* receiveRawPacket();
uint8_t* receiveRawPacket(unsigned int dstIp, unsigned short dstPort);
void broadcastPacket(char* data);
/*! \brief Sends a packet whithout ENet adding its headers.
* This function is used in particular to achieve the STUN protocol.
* \param data : Data to send.
* \param length : Length of the sent data.
* \param dst : Destination of the packet.
*/
void sendRawPacket(uint8_t* data, int length,
TransportAddress dst);
/*! \brief Receives a packet directly from the network interface.
* Receive a packet whithout ENet processing it.
* \return A string containing the data of the received packet.
*/
uint8_t* receiveRawPacket();
/*! \brief Receives a packet directly from the network interface and
* filter its address.
* Receive a packet whithout ENet processing it. Checks that the
* sender of the packet is the one that corresponds to the sender
* parameter. Does not check the port right now.
* \param sender : Transport address of the original sender of the
* wanted packet.
* \return A string containing the data of the received packet
* matching the sender's ip address.
*/
uint8_t* receiveRawPacket(TransportAddress sender);
/*! \brief Broadcasts a packet to all peers.
* \param data : Data to send.
*/
void broadcastPacket(char* data);
bool peerExists(uint32_t ip, uint16_t port);
bool isConnectedTo(uint32_t ip, uint16_t port);
/*! \brief Tells if a peer is known.
* \return True if the peer is known, false elseway.
*/
bool peerExists(TransportAddress peer_address);
/*! \brief Tells if a peer is known and connected.
* \return True if the peer is known and connected, false elseway.
*/
bool isConnectedTo(TransportAddress peer_address);
protected:
ENetHost* m_host;
pthread_t* m_listeningThread;
ENetHost* m_host; //!< ENet host interfacing sockets.
pthread_t* m_listening_thread; //!< Thread listening network events.
};

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "stk_peer.hpp"
#include <stdio.h>
@@ -14,13 +32,13 @@ STKPeer::~STKPeer()
delete m_peer;
}
bool STKPeer::connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data)
bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data)
{
ENetAddress address;
address.host = ip;
address.port = port;
address.host = host.ip;
address.port = host.port;
ENetPeer* peer = enet_host_connect(host->m_host, &address, 2, 0);
ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0);
if (peer == NULL)
{
printf("Could not try to connect to server.\n");

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef STK_PEER_HPP
#define STK_PEER_HPP
@@ -16,7 +34,7 @@ class STKPeer
virtual void sendPacket(char* data);
static bool connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
static bool connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data);
bool isConnected();

View File

@@ -1,3 +1,21 @@
//
// 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.
#include "time.hpp"
namespace Time

View File

@@ -1,3 +1,21 @@
//
// 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.
#ifndef TIME_HPP
#define TIME_HPP

View File

@@ -1,9 +1,33 @@
//
// 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.
/*! \file types.hpp
* \brief Declares the general types that are used by the network.
*/
#ifndef TYPES_HPP
#define TYPES_HPP
#include <string>
#include <stdint.h>
/*! \class CallbackObject
* \brief Class that must be inherited to pass objects to protocols.
*/
class CallbackObject
{
public:
@@ -11,22 +35,30 @@ class CallbackObject
};
/*! \class TransportAddress
* \brief Describes a transport-layer address.
* For IP networks, a transport address is the couple ip:port.
*/
class TransportAddress : public CallbackObject
{
public:
TransportAddress() {}
TransportAddress(uint32_t p_ip = 0, uint16_t p_port = 0)
{ ip = p_ip; port = p_port; }
uint32_t ip;
uint16_t port;
uint32_t ip; //!< The IPv4 address
uint16_t port; //!< The port number
};
/*! \class PlayerLogin
* \brief Contains the information needed to authenticate a user.
*/
class PlayerLogin : public CallbackObject
{
public:
PlayerLogin() {}
std::string username;
std::string password;
std::string username; //!< Username of the player
std::string password; //!< Password of the player
};