cleaning destructors, using log:: instead of printf or std::cout, cleaning more paths, removed useless fnctions

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13076 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-04 16:59:30 +00:00
parent 7a174fd122
commit 253a3ec1e8
23 changed files with 152 additions and 184 deletions

View File

@ -1190,6 +1190,7 @@ void cleanSuperTuxKart()
if(news_manager) delete news_manager;
if(addons_manager) delete addons_manager;
NetworkManager::kill();
if(grand_prix_manager) delete grand_prix_manager;
if(highscore_manager) delete highscore_manager;
if(attachment_manager) delete attachment_manager;

View File

@ -24,7 +24,7 @@
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/connect_to_server.hpp"
#include <stdio.h>
#include "utils/log.hpp"
ClientNetworkManager::ClientNetworkManager()
{
@ -38,7 +38,7 @@ void ClientNetworkManager::run()
{
if (enet_initialize() != 0)
{
printf("Could not initialize enet.\n");
Log::error("ClientNetworkManager", "Could not initialize enet.\n");
return;
}
m_localhost = new STKHost();
@ -50,14 +50,14 @@ void ClientNetworkManager::run()
bool ClientNetworkManager::connectToHost(std::string serverNickname)
{
printf("_NetworkInterface>Starting the connection to host protocol\n");
Log::info("ClientNetworkManager", "Starting the connection to host protocol\n");
// step 1 : retreive public address
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");
Log::info("ClientNetworkManager", "The public address is known.\n");
// step 2 : show the public address for others (here, the server)
ShowPublicAddress* spa = new ShowPublicAddress(NULL);
@ -68,7 +68,7 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
while (ProtocolManager::getInstance()->getProtocolState(spa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is being shown online.\n");
Log::info("ClientNetworkManager", "The public address is being shown online.\n");
// step 3 : get the server's addres.
TransportAddress addr;
@ -78,7 +78,7 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
while (ProtocolManager::getInstance()->getProtocolState(gpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address of the server is known.\n");
Log::info("ClientNetworkManager", "The public address of the server is known.\n");
// step 4 : connect to the server
ConnectToServer* cts = new ConnectToServer(NULL);
@ -91,11 +91,11 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
if (m_localhost->isConnectedTo(TransportAddress(addr.ip, addr.port)))
{
success = true;
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A SERVER.\n");
Log::info("ClientNetworkManager", "Connection success. You are now connected to a server.\n");
}
else
{
printf("_NetworkInterface> We are NOT connected to the server.\n");
Log::error("ClientNetworkManager", "We are NOT connected to the server.\n");
}
// step 5 : hide our public address
HidePublicAddress* hpa = new HidePublicAddress(NULL);
@ -105,20 +105,15 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
while (ProtocolManager::getInstance()->getProtocolState(hpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is now hidden online.\n");
Log::info("ClientNetworkManager", "The public address is now hidden online.\n");
return success;
}
void ClientNetworkManager::packetReceived(char* data)
{
printf("ClientNetworkManager::packetReceived()\n");
puts(data);
}
void ClientNetworkManager::sendPacket(const char* data)
{
if (m_peers.size() > 1)
printf("Ambiguous send of data\n");
Log::warn("ClientNetworkManager", "Ambiguous send of data.\n");
m_peers[0]->sendPacket(data);
}

View File

@ -34,7 +34,6 @@ class ClientNetworkManager : public NetworkManager
bool connectToHost(std::string serverNickname);
virtual void packetReceived(char* data);
virtual void sendPacket(const char* data);
STKPeer* getPeer();

View File

@ -17,12 +17,8 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/event.hpp"
#include "network/network_manager.hpp"
#include <vector>
#include <stdio.h>
Event::Event(ENetEvent* event)
{
switch (event->type)
@ -57,9 +53,8 @@ Event::Event(ENetEvent* event)
return;
}
}
if (peer == NULL)
if (peer == NULL) // peer does not exist, create him
{
printf("The peer still does not exist in %lu peers\n", peers.size());
STKPeer* new_peer = new STKPeer();
new_peer->m_peer = event->peer;
peer = new_peer;

View File

@ -18,10 +18,10 @@
#include "network/http_functions.hpp"
#include <iostream>
#include "utils/log.hpp"
#include <string>
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
@ -42,7 +42,7 @@ void init()
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(!curl)
printf("Error while loading cURL library.\n");
Log::error("HTTP", "Error while loading cURL library.\n");
}
std::string getPage(std::string url)
@ -53,7 +53,7 @@ std::string getPage(std::string url)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
Log::error("HTTP", "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return readBuffer;
}

View File

@ -26,7 +26,10 @@
#include "network/client_network_manager.hpp"
#include "network/server_network_manager.hpp"
#include <stdio.h>
#include "utils/log.hpp"
#include <pthread.h>
#include <signal.h>
void* protocolManagerUpdate(void* data)
{
@ -44,10 +47,23 @@ NetworkManager::NetworkManager()
m_public_address.ip = 0;
m_public_address.port = 0;
m_protocol_manager_update_thread = NULL;
m_localhost = NULL;
}
NetworkManager::~NetworkManager()
{
if (m_protocol_manager_update_thread)
pthread_cancel(*m_protocol_manager_update_thread);//, SIGKILL);
ProtocolManager::kill();
if (m_localhost)
delete m_localhost;
while(!m_peers.empty())
{
delete m_peers.back();
m_peers.pop_back();
}
}
void NetworkManager::run()
@ -75,15 +91,15 @@ void NetworkManager::setManualSocketsMode(bool manual)
void NetworkManager::notifyEvent(Event* event)
{
printf("EVENT received\n");
Log::info("NetworkManager", "EVENT received\n");
switch (event->type)
{
case EVENT_TYPE_MESSAGE:
printf("Message, Sender : %u, message = \"%s\"\n", event->peer->getAddress(), event->data.c_str());
Log::info("NetworkManager", "Message, Sender : %u, message = \"%s\"\n", event->peer->getAddress(), event->data.c_str());
break;
case EVENT_TYPE_DISCONNECTED:
printf("Somebody is now disconnected. There are now %lu peers.\n", m_peers.size());
printf("Disconnected host: %i.%i.%i.%i:%i\n", event->peer->getAddress()>>24&0xff, event->peer->getAddress()>>16&0xff, event->peer->getAddress()>>8&0xff, event->peer->getAddress()&0xff,event->peer->getPort());
Log::info("NetworkManager", "Somebody is now disconnected. There are now %lu peers.\n", m_peers.size());
Log::info("NetworkManager", "Disconnected host: %i.%i.%i.%i:%i\n", event->peer->getAddress()>>24&0xff, event->peer->getAddress()>>16&0xff, event->peer->getAddress()>>8&0xff, event->peer->getAddress()&0xff,event->peer->getPort());
// remove the peer:
for (unsigned int i = 0; i < m_peers.size(); i++)
{
@ -94,10 +110,10 @@ void NetworkManager::notifyEvent(Event* event)
break;
}
}
printf("ERROR : the peer that has been disconnected was not registered by the Network Manager.\n");
Log::fatal("NetworkManager", "The peer that has been disconnected was not registered by the Network Manager.\n");
break;
case EVENT_TYPE_CONNECTED:
printf("A client has just connected. There are now %lu peers.\n", m_peers.size() + 1);
Log::info("NetworkManager", "A client has just connected. There are now %lu peers.\n", m_peers.size() + 1);
// create the new peer:
m_peers.push_back(event->peer);
break;

View File

@ -39,7 +39,6 @@ class NetworkManager : public Singleton<NetworkManager>
virtual bool connect(TransportAddress peer);
virtual void setManualSocketsMode(bool manual);
virtual void notifyEvent(Event* event);
virtual void packetReceived(char* data) = 0;
virtual void sendPacket(const char* data) = 0;
// raw data management

View File

@ -20,13 +20,11 @@
#include "network/protocol.hpp"
#include "network/network_manager.hpp"
#include "utils/log.hpp"
#include <assert.h>
#include <stdio.h>
#include <cstdlib>
#define RAND_MAX 65536
ProtocolManager::ProtocolManager()
{
pthread_mutex_init(&m_events_mutex, NULL);
@ -38,6 +36,26 @@ ProtocolManager::ProtocolManager()
ProtocolManager::~ProtocolManager()
{
pthread_mutex_lock(&m_events_mutex);
pthread_mutex_lock(&m_protocols_mutex);
pthread_mutex_lock(&m_requests_mutex);
pthread_mutex_lock(&m_id_mutex);
for (unsigned int i = 0; i < m_protocols.size() ; i++)
delete m_protocols[i].protocol;
for (unsigned int i = 0; i < m_events_to_process.size() ; i++)
delete m_events_to_process[i];
m_protocols.clear();
m_requests.clear();
m_events_to_process.clear();
pthread_mutex_unlock(&m_events_mutex);
pthread_mutex_unlock(&m_protocols_mutex);
pthread_mutex_unlock(&m_requests_mutex);
pthread_mutex_unlock(&m_id_mutex);
pthread_mutex_destroy(&m_events_mutex);
pthread_mutex_destroy(&m_protocols_mutex);
pthread_mutex_destroy(&m_requests_mutex);
pthread_mutex_destroy(&m_id_mutex);
}
void ProtocolManager::notifyEvent(Event* event)
@ -122,7 +140,7 @@ void ProtocolManager::requestTerminate(Protocol* protocol)
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);
Log::info("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);
@ -170,7 +188,7 @@ void ProtocolManager::protocolTerminated(ProtocolInfo protocol)
offset++;
}
}
printf("__ProtocolManager> A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
Log::info("ProtocolManager", "A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
pthread_mutex_unlock(&m_protocols_mutex);
}

View File

@ -19,10 +19,8 @@
#include "network/protocols/connect_to_server.hpp"
#include "network/client_network_manager.hpp"
#include "network/time.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "utils/time.hpp"
#include "utils/log.hpp"
// ----------------------------------------------------------------------------
@ -48,9 +46,9 @@ void ConnectToServer::notifyEvent(Event* event)
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());
Log::info("ConnectToServer", "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
}
}
@ -62,7 +60,8 @@ void ConnectToServer::setup()
m_state = NONE;
if (m_server_ip == 0 || m_server_port == 0 )
{
printf("You have to set the server's public ip:port of the server.\n");
Log::error("ConnectToServer", "You have to set the server's public \
ip:port of the server.\n");
m_listener->requestTerminate(this);
}
}
@ -74,10 +73,7 @@ void ConnectToServer::update()
if (m_state == NONE)
{
static double target = 0;
double currentTime = Time::getSeconds();
// sometimes the getSeconds method forgets 3600 seconds.
while (currentTime < target-1800)
currentTime += 3600;
double currentTime = Time::getRealTime();
if (currentTime > target)
{
NetworkManager::getInstance()->connect(
@ -89,7 +85,7 @@ void ConnectToServer::update()
return;
}
target = currentTime+5;
printf("Retrying to connect in 5 seconds.\n");
Log::info("ConnectToServer", "Retrying to connect in 5 seconds.\n");
}
}
else if (m_state == DONE)

View File

@ -18,11 +18,9 @@
#include "network/protocols/get_peer_address.hpp"
#include "network/time.hpp"
#include "network/http_functions.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "utils/time.hpp"
#include "utils/log.hpp"
GetPeerAddress::GetPeerAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
@ -34,6 +32,7 @@ GetPeerAddress::~GetPeerAddress()
void GetPeerAddress::notifyEvent(Event* event)
{
// nothing there. If we receive events, they must be ignored
}
void GetPeerAddress::setup()
@ -46,9 +45,7 @@ void GetPeerAddress::update()
if (m_state == NONE)
{
static double target = 0;
double current_time = Time::getSeconds();
while (current_time < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
current_time += 3600;
double current_time = Time::getRealTime();
if (current_time > target)
{
char url[512];
@ -56,7 +53,7 @@ void GetPeerAddress::update()
std::string result = HTTP::getPage(url);
if (result == "")
{
printf("__GetPeerAddress> The host you try to reach does not exist. Change the host name please.\n");
Log::error("GetPeerAddress", "The host you try to reach does not exist. Change the host name please.\n");
pause();
return;
}
@ -68,12 +65,12 @@ void GetPeerAddress::update()
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");
Log::info("GetPeerAddress", "The host you try to reach is not online. There will be a new try in 10 seconds.\n");
target = current_time+10;
}
else
{
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);
Log::info("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)

View File

@ -23,10 +23,9 @@
#include "network/protocols/connect_to_server.hpp"
#include "network/network_interface.hpp"
#include <time.h>
#include <stdlib.h>
#include "utils/log.hpp"
#include <assert.h>
#include <stdio.h>
int stunRand()
{
@ -98,7 +97,7 @@ void GetPublicAddress::update()
bytes[19] = (uint8_t)(m_stun_tansaction_id[2]);
bytes[20] = '\0';
printf("__GetPublicAddress> Querrying STUN server 132.177.123.6\n");
Log::info("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, TransportAddress(dst, 3478));
@ -132,7 +131,7 @@ void GetPublicAddress::update()
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");
Log::error("GetPublicAddress", "The STUN server responded with a valid answer\n");
int message_size = data[2]*256+data[3];
// parse the stun message now:
@ -140,12 +139,12 @@ void GetPublicAddress::update()
uint8_t* attributes = data+20;
if (message_size == 0)
{
printf("__GetPublicAddress> STUN answer does not contain any information.\n");
Log::error("GetPublicAddress", "STUN answer does not contain any information.\n");
finish = true;
}
if (message_size < 4) // cannot even read the size
{
printf("__GetPublicAddress> STUN message is not valid.\n");
Log::error("GetPublicAddress", "STUN message is not valid.\n");
finish = true;
}
uint16_t port;
@ -176,14 +175,14 @@ void GetPublicAddress::update()
finish = true;
if (message_size < 4) // cannot even read the size
{
printf("__GetPublicAddress> STUN message is not valid.\n");
Log::error("GetPublicAddress", "STUN message is not valid.\n");
finish = true;
}
}
// finished parsing, we know our public transport address
if (valid)
{
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);
Log::info("GetPublicAddress", "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_callback_object);

View File

@ -19,8 +19,7 @@
#include "network/protocols/hide_public_address.hpp"
#include "network/http_functions.hpp"
#include <stdio.h>
#include "utils/log.hpp"
HidePublicAddress::HidePublicAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
@ -48,12 +47,12 @@ void HidePublicAddress::update()
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')
{
printf("__HidePublicAddress> Public address hidden successfully.\n");
Log::info("HidePublicAddress", "Public address hidden successfully.\n");
m_state = DONE;
}
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("__HidePublicAddress> Public address still visible. Re-set nick:password and retry.\n");
Log::warn("HidePublicAddress", "Public address still visible. Re-set nick:password and retry.\n");
m_state = NONE;
pause();
}

View File

@ -18,7 +18,7 @@
#include "network/protocols/lobby_room_protocol.hpp"
#include <stdio.h>
#include "utils/log.hpp"
LobbyRoomProtocol::LobbyRoomProtocol(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_LOBBY_ROOM)
{
@ -32,7 +32,7 @@ void LobbyRoomProtocol::notifyEvent(Event* event)
{
if (event->type == EVENT_TYPE_MESSAGE)
{
printf("Message from %u : \"%s\"\n", event->peer->getAddress(), event->data.c_str());
Log::info("LobbyRoomProtocol", "Message from %u : \"%s\"\n", event->peer->getAddress(), event->data.c_str());
}
}

View File

@ -19,8 +19,7 @@
#include "network/protocols/show_public_address.hpp"
#include "network/http_functions.hpp"
#include <stdio.h>
#include "utils/log.hpp"
ShowPublicAddress::ShowPublicAddress(CallbackObject* callback_object) : Protocol(callback_object, PROTOCOL_SILENT)
{
@ -39,7 +38,7 @@ void ShowPublicAddress::setup()
m_state = NONE;
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");
Log::error("ShowPublicAddress", "You have to set the public ip:port, username:password and the host nickname before starting this protocol.\n");
m_listener->requestTerminate(this);
}
}
@ -53,12 +52,12 @@ void ShowPublicAddress::update()
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')
{
printf("__ShowPublicAddress> Address set.\n");
Log::info("ShowPublicAddress", "Address set.\n");
m_state = DONE;
}
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("__ShowPublicAddress> Login fail. Please re-set username:password and unpause the protocol.\n");
Log::warn("ShowPublicAddress", "Login fail. Please re-set username:password and unpause the protocol.\n");
m_state = NONE;
pause();
}

View File

@ -24,9 +24,9 @@
#include "network/protocols/get_peer_address.hpp"
#include "network/protocols/connect_to_server.hpp"
#include "utils/log.hpp"
#include <enet/enet.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
ServerNetworkManager::ServerNetworkManager()
@ -42,7 +42,7 @@ void ServerNetworkManager::run()
{
if (enet_initialize() != 0)
{
printf("Could not initialize enet.\n");
Log::error("ServerNetworkManager", "Could not initialize enet.\n");
return;
}
NetworkManager::run();
@ -53,16 +53,16 @@ void ServerNetworkManager::start()
m_localhost = new STKHost();
m_localhost->setupServer(STKHost::HOST_ANY, 7321, 32, 2, 0, 0);
m_localhost->startListening();
printf("Server now setup, listening on port 7321.\n");
Log::info("ServerNetworkManager", "Server now setup, listening on port 7321.\n");
printf("_NetworkInterface>Starting the global protocol\n");
Log::info("ServerNetworkManager", "Starting the global protocol\n");
// step 1 : retreive public address
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");
Log::info("ServerNetworkManager", "The public address is known.\n");
// step 2 : show the public address for others (here, the server)
ShowPublicAddress* spa = new ShowPublicAddress(NULL);
@ -73,12 +73,12 @@ void ServerNetworkManager::start()
while (ProtocolManager::getInstance()->getProtocolState(spa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address is being shown online.\n");
Log::info("ServerNetworkManager", "The public address is being shown online.\n");
}
bool ServerNetworkManager::connectToPeer(std::string peer_username)
{
printf("_NetworkInterface>Starting the connection to host protocol\n");
Log::info("ServerNetworkManager", "Starting the connection to host protocol\n");
// step 3 : get the peer's addres.
TransportAddress addr;
@ -88,7 +88,7 @@ bool ServerNetworkManager::connectToPeer(std::string peer_username)
while (ProtocolManager::getInstance()->getProtocolState(gpa) != PROTOCOL_STATE_TERMINATED )
{
}
printf("_NetworkInterface> The public address of the peer is known.\n");
Log::info("ServerNetworkManager", "The public address of the peer is known.\n");
// step 2 : connect to the peer
ConnectToServer* cts = new ConnectToServer(NULL);
@ -101,22 +101,16 @@ bool ServerNetworkManager::connectToPeer(std::string peer_username)
if (isConnectedTo(addr))
{
success = true;
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A PEER.\n");
Log::info("ServerNetworkManager", "Connection success : you are now connected to the peer.\n");
}
else
{
printf("_NetworkInterface> We are NOT connected to the server.\n");
Log::warn("ServerNetworkManager", "We are NOT connected to the peer.\n");
}
return success;
}
void ServerNetworkManager::packetReceived(char* data)
{
printf("ServerNetworkManager::packetReceived()\n");
puts(data);
sendPacket(data);
}
void ServerNetworkManager::sendPacket(const char* data)
{
m_localhost->broadcastPacket(data);

View File

@ -36,7 +36,6 @@ class ServerNetworkManager : public NetworkManager
void start();
bool connectToPeer(std::string peer_username);
virtual void packetReceived(char* data);
virtual void sendPacket(const char* data);
virtual bool isServer() { return false; }

View File

@ -19,14 +19,17 @@
#ifndef SINGLETON_HPP
#define SINGLETON_HPP
#include <iostream>
#include "utils/log.hpp"
template <typename T>
class Singleton
{
protected:
Singleton () { }
virtual ~Singleton () { std::cout << "destroying singleton." << std::endl; delete m_singleton; }
Singleton () { m_singleton = NULL; }
virtual ~Singleton ()
{
Log::info("Singleton", "Destroyed singleton.");
}
public:
template<typename S>
@ -37,7 +40,7 @@ class Singleton
S* result = (dynamic_cast<S*> (m_singleton));
if (result == NULL)
std::cout << "THE SINGLETON HAS NOT BEEN REALOCATED, BUT IS NOT OF THE REQUESTED TYPE." << std::endl;
Log::fatal("Singleton", "THE SINGLETON HAS NOT BEEN REALOCATED, BUT IS NOT OF THE REQUESTED TYPE.");
return result;
}
static T *getInstance()
@ -47,10 +50,9 @@ class Singleton
static void kill ()
{
if (NULL != m_singleton)
if (m_singleton)
{
delete m_singleton;
m_singleton = NULL;
}
}

View File

@ -19,11 +19,12 @@
#include "network/stk_host.hpp"
#include "network/network_manager.hpp"
#include "utils/log.hpp"
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
// ----------------------------------------------------------------------------
@ -46,13 +47,23 @@ void* STKHost::receive_data(void* self)
STKHost::STKHost()
{
m_host = NULL;
m_listening_thread = (pthread_t*)(malloc(sizeof(pthread_t)));
m_listening_thread = NULL;
}
// ----------------------------------------------------------------------------
STKHost::~STKHost()
{
if (m_listening_thread)
{
pthread_cancel(*m_listening_thread);//, SIGKILL);
delete m_listening_thread;
m_listening_thread = NULL;
}
if (m_host)
{
enet_host_destroy(m_host);
}
}
// ----------------------------------------------------------------------------
@ -69,7 +80,7 @@ void STKHost::setupServer(uint32_t address, uint16_t port, int peer_count,
max_incoming_bandwidth, max_outgoing_bandwidth);
if (m_host == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet \
Log::info("STKHost", "An error occurred while trying to create an ENet \
server host.\n");
exit (EXIT_FAILURE);
}
@ -85,7 +96,7 @@ void STKHost::setupClient(int peer_count, int channel_limit,
max_incoming_bandwidth, max_outgoing_bandwidth);
if (m_host == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet \
Log::info("STKHost", "An error occurred while trying to create an ENet \
client host.\n");
exit (EXIT_FAILURE);
}
@ -95,6 +106,7 @@ void STKHost::setupClient(int peer_count, int channel_limit,
void STKHost::startListening()
{
m_listening_thread = (pthread_t*)(malloc(sizeof(pthread_t)));
pthread_create(m_listening_thread, NULL, &STKHost::receive_data, this);
}
@ -102,7 +114,11 @@ void STKHost::startListening()
void STKHost::stopListening()
{
pthread_cancel(*m_listening_thread);
if(m_listening_thread)
{
pthread_cancel(*m_listening_thread);
m_listening_thread = NULL;
}
}
// ----------------------------------------------------------------------------
@ -135,9 +151,8 @@ uint8_t* STKHost::receiveRawPacket()
{
i++;
len = recv(m_host->socket,buffer,2048, 0);
usleep(1000); // wait 1 millisecond between two checks
usleep(1000);
}
printf("Packet received after %i milliseconds\n", i);
return buffer;
}
@ -171,9 +186,8 @@ uint8_t* STKHost::receiveRawPacket(TransportAddress sender)
{
char s[20];
inet_ntop(AF_INET, &(((struct sockaddr_in *)&addr)->sin_addr), s, 20);
printf("IPv4 Address %s\n", s);
Log::info("STKHost", "IPv4 Address of the sender was %s\n", s);
}
printf("Packet received after %i milliseconds\n", i);
return buffer;
}

View File

@ -18,7 +18,8 @@
#include "network/stk_peer.hpp"
#include <stdio.h>
#include "utils/log.hpp"
#include <string.h>
STKPeer::STKPeer()
@ -29,7 +30,10 @@ STKPeer::STKPeer()
STKPeer::~STKPeer()
{
if (m_peer)
delete m_peer;
{
//free(m_peer);
m_peer = NULL;
}
}
bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data)
@ -41,16 +45,16 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, uint32_t
ENetPeer* peer = enet_host_connect(localhost->m_host, &address, 2, 0);
if (peer == NULL)
{
printf("Could not try to connect to server.\n");
Log::error("STKPeer", "Could not try to connect to server.\n");
return false;
}
printf("Connecting to %i.%i.%i.%i:%i.\n", (peer->address.host>>0)&0xff,(peer->address.host>>8)&0xff,(peer->address.host>>16)&0xff,(peer->address.host>>24)&0xff,peer->address.port);
Log::info("STKPeer", "Connecting to %i.%i.%i.%i:%i.\n", (peer->address.host>>0)&0xff,(peer->address.host>>8)&0xff,(peer->address.host>>16)&0xff,(peer->address.host>>24)&0xff,peer->address.port);
return true;
}
void STKPeer::sendPacket(const char* data)
{
//printf("sending packet to %i.%i.%i.%i:%i", (m_peer->address.host>>24)&0xff,(m_peer->address.host>>16)&0xff,(m_peer->address.host>>8)&0xff,(m_peer->address.host>>0)&0xff,m_peer->address.port);
//Log::info("STKPeer", "sending packet to %i.%i.%i.%i:%i", (m_peer->address.host>>24)&0xff,(m_peer->address.host>>16)&0xff,(m_peer->address.host>>8)&0xff,(m_peer->address.host>>0)&0xff,m_peer->address.port);
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(m_peer, 0, packet);
@ -68,7 +72,7 @@ uint16_t STKPeer::getPort()
bool STKPeer::isConnected()
{
printf("PEER STATE %i\n", m_peer->state);
Log::info("STKPeer", "The peer state is %i\n", m_peer->state);
return (m_peer->state == ENET_PEER_STATE_CONNECTED);
}
bool STKPeer::operator==(ENetPeer* peer)

View File

@ -1,32 +0,0 @@
//
// 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/time.hpp"
namespace Time
{
double getSeconds()
{
time_t timer;
time(&timer);
struct tm y2k;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
return difftime(timer,mktime(&y2k)); // get the seconds elapsed since january 2000
}
}

View File

@ -1,29 +0,0 @@
//
// 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
#include <time.h>
namespace Time
{
double getSeconds();
}
#endif // TIME_HPP_INCLUDED

View File

@ -32,6 +32,7 @@ class CallbackObject
{
public:
CallbackObject() {}
~CallbackObject() {}
};
@ -44,6 +45,7 @@ class TransportAddress : public CallbackObject
public:
TransportAddress(uint32_t p_ip = 0, uint16_t p_port = 0)
{ ip = p_ip; port = p_port; }
~TransportAddress() {}
uint32_t ip; //!< The IPv4 address
uint16_t port; //!< The port number
@ -56,6 +58,7 @@ class PlayerLogin : public CallbackObject
{
public:
PlayerLogin() {}
~PlayerLogin() { username.clear(); password.clear(); }
std::string username; //!< Username of the player
std::string password; //!< Password of the player

View File

@ -293,7 +293,7 @@ void RaceManager::startNew(bool from_overworld)
// Create the kart status data structure to keep track of scores, times, ...
// ==========================================================================
m_kart_status.clear();
printf("%d %d %d\n", (unsigned int)m_num_karts, m_ai_kart_list.size(), m_player_karts.size());
printf("%u %lu %lu\n", (unsigned int)m_num_karts, m_ai_kart_list.size(), m_player_karts.size());
assert((unsigned int)m_num_karts == m_ai_kart_list.size()+m_player_karts.size());
// First add the AI karts (randomly chosen)