Several compile time bug fixes for linux, removed warnings,

fixed = vs == bug in network_manager.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2237 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2008-09-07 14:12:14 +00:00
parent 9191f89333
commit bb894b414d
9 changed files with 15 additions and 282 deletions

View File

@@ -65,7 +65,7 @@ bool GrandPrixData::checkConsistency()
{
try
{
Track *track=track_manager->getTrack(m_tracks[i]);
track_manager->getTrack(m_tracks[i]);
}
catch(std::exception& e)
{

View File

@@ -478,4 +478,4 @@ void CharSel::handle(GameAction action, int value)
return;
} // if cursor down
BaseGUI::handle(action, value);
} // handle
} // handle

View File

@@ -83,7 +83,7 @@ namespace lisp
Lisp*
Parser::read()
{
Lisp* result=0;
Lisp* result=NULL;
switch(m_token)
{
case Lexer::TOKEN_EOF:

View File

@@ -1,168 +0,0 @@
// $Id: network_manager.hpp 2128 2008-06-13 00:53:52Z cosmosninja $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2008 Joerg Henrichs, Stephen Leak
//
// 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 "kart_packet.hpp"
#include <string>
#include <math.h>
#include <stdexcept>
// need a more elegant way of setting the data_size, esp when strings are being used
// also, looking at how the packets are actually used, we can probably serialise as
// part of the constructor, it seems packets to be sent are always created in a
// single line
KartPacket::KartPacket(int data_size)
{
m_data_size = data_size;
m_pkt = enet_packet_create (NULL, data_size*sizeof(int), ENET_PACKET_FLAG_RELIABLE);
m_pos = 0;
m_data = (int*) m_pkt->data;
}
KartPacket::KartPacket(ENetPacket* pkt)
{
m_pkt = pkt;
m_data = (int*) pkt->data;
m_data_size = pkt->dataLength/sizeof(int);
m_pos = 0;
}
KartPacket::~KartPacket()
{
enet_packet_destroy(m_pkt);
}
bool KartPacket::pushInt(int &data)
{
if (m_pos > m_data_size)
return false;
m_data[m_pos] = htonl(data);
++m_pos;
return true;
}
int KartPacket::pullInt()
{
m_pos++;
return ntohl(m_data[m_pos-1]);
}
bool KartPacket::pushFloat(float &data)
{
int *dcast = (int*) &data;
return pushInt(*dcast);
}
float KartPacket::pullFloat()
{ // ugly...
int i = pullInt();
float *f = (float*) &i;
return *f;
}
bool KartPacket::pushString(std::string &data)
{ // urk, this is a little ugly. want to put the string on a 4-byte boundary
int len = (int)ceil((float)data.length() / 4.0f) * 4; // round length up to next 4-char boundary
if (m_pos+len > m_data_size)
return false; // FIXME: resize the packet so it fits
memcpy (&(m_data[m_pos]), data.c_str(), data.length()+1);
m_pos += len;
return true;
}
std::string KartPacket::pullString()
{
char *str = (char*) &(m_data[m_pos]);
int len = strlen(str)+1;
int len4 = len/sizeof(int) + 1;
m_pos += len4; // I think this is correct ..
return std::string(str);
}
bool KartPacket::send(ENetPeer& peer)
{
this->serialise();
enet_peer_send(&peer, 0, m_pkt);
return true;
}
// ---------------------------------------------------------------------
ClientHostIdPacket::ClientHostIdPacket(int id)
: KartPacket(2)
{ // create a ClientHostIdPacket with id as data
m_type = CLIENT_HOST_ID_PACKET;
m_id = id;
}
ClientHostIdPacket::ClientHostIdPacket(ENetPacket* pkt)
: KartPacket(pkt)
{ // create a ClientHostIdPacket based on a received packet
m_type = static_cast<PacketType> (pullInt()); // (ntohl(m_data[0]));
if (m_type != CLIENT_HOST_ID_PACKET)
// FIXME: do something more elegant here
throw std::runtime_error("Received packet mismatch!");
m_id = pullInt(); // ntohl(m_data[1]);
}
void ClientHostIdPacket::serialise()
{
int type = m_type;
pushInt(type);
pushInt(m_id);
}
// --------------------------------------------------------------------
LocalKartInfoPacket::LocalKartInfoPacket(const std::string& player_name,
const std::string& kart_name)
: KartPacket(128) // a bit arbitrary, just to check it works
{
m_type = LOCAL_KART_INFO_PACKET;
m_player_name = std::string(player_name);
m_kart_name = std::string(kart_name);
}
LocalKartInfoPacket::LocalKartInfoPacket(ENetPacket* pkt)
: KartPacket(pkt)
{ // create a LocalKartInfoPacket based on a received packet
m_type = static_cast<PacketType> (pullInt()); // (ntohl(m_data[0]));
if (m_type != LOCAL_KART_INFO_PACKET)
// FIXME: do something more elegant here
throw std::runtime_error("Received packet mismatch!");
m_player_name = pullString(); // new string((char*) &(m_data[1]));
m_kart_name = pullString(); // new string(&(m_data[1]));
}
//LocalKartInfoPacket::~LocalKartInfoPacket()
//{
// delete m_player_name;
// delete m_kart_name;
// // FIXME: poss mem leak here, need to call ~KartPacket
//}
void LocalKartInfoPacket::serialise()
{
int type = m_type;
pushInt(type);
pushString(m_player_name);
pushString(m_kart_name);
}

View File

@@ -1,102 +0,0 @@
// $Id: network_manager.hpp 2128 2008-06-13 00:53:52Z cosmosninja $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2008 Joerg Henrichs, Stephen Leak
//
// 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 HEADER_KART_PACKET_H
#define HEADER_KART_PACKET_H
#include <string>
#ifdef HAVE_ENET
# include "enet/enet.h"
#else
# error "need enet for this implementation!"
#endif
// sjl: when a message is received, need to work out what kind of message it
// is and therefore what to do with it
class KartPacket
{ // collects and serialises/deserialises kart info to send
public:
KartPacket(int data_size); // create from scratch (to send)
KartPacket(ENetPacket* pkt); // create from (received) ENetacket
~KartPacket();
enum PacketType { LOCAL_KART_INFO_PACKET, CLIENT_HOST_ID_PACKET };
PacketType getType();
bool send(ENetPeer& peer);
protected:
bool pushInt(int &data);
bool pushFloat(float &data);
bool pushString(std::string &data);
int pullInt();
float pullFloat();
std::string pullString();
PacketType m_type;
int *m_data;
//virtual ENetPacket* serialise(); // convert KartPacket into ENetPacket
virtual void serialise() =0; // convert KartPacket into ENetPacket
private:
ENetPacket *m_pkt;
int m_data_size;
int m_pos; // simple stack counter for constructing packet data
};
class ClientHostIdPacket : public KartPacket
{ // during init phase, server sends this to client
public:
ClientHostIdPacket(int id); // create one to send
ClientHostIdPacket(ENetPacket* pkt); // wait for one from server
int getId() { return m_id; };
protected:
void serialise(); // convert KartPacket into ENetPacket
private:
int m_id;
};
class LocalKartInfoPacket : public KartPacket
{ // before a race, each client sends server one of these for each player on that client
public:
LocalKartInfoPacket(const std::string& player_name,
const std::string& kart_name);
LocalKartInfoPacket(ENetPacket* pkt);
// ~LocalKartInfoPacket();
std::string getPlayerName();
std::string getKartName();
protected:
void serialise(); // convert KartPacket into ENetPacket
private:
std::string m_player_name;
std::string m_kart_name;
};
#endif

View File

@@ -186,7 +186,7 @@ void NetworkManager::handleDisconnection(ENetEvent *event)
return;
}
fprintf(stderr, "%x:%d disconnected (host id %d).\n", event->peer->address.host,
event->peer->address.port, (int)event->peer->data );
event->peer->address.port, (int)(long)event->peer->data );
m_num_clients--;
} // handleDisconnection
@@ -199,14 +199,14 @@ void NetworkManager::handleMessageAtServer(ENetEvent *event)
case NS_ACCEPT_CONNECTIONS:
{
ConnectMessage m(event->packet);
m_client_names[(int)event->peer->data] = m.getId();
m_client_names[(int)(long)event->peer->data] = m.getId();
m_num_clients++;
return;
}
case NS_CHARACTER_SELECT:
{
CharacterSelectedMessage m(event->packet);
int hostid=(int)event->peer->data;
int hostid=(int)(long)event->peer->data;
assert(hostid>=1 && hostid<=m_num_clients);
if(m_num_local_players[hostid]==-1) // first package from that host
{
@@ -222,8 +222,8 @@ void NetworkManager::handleMessageAtServer(ENetEvent *event)
// one message from each client, and the size of the kart_info
// array is the same as the number of all players (which does not
// yet include the number of players on the host).
if(m_barrier_count = m_num_clients &&
m_num_all_players==m_kart_info.size())
if(m_barrier_count == m_num_clients &&
m_num_all_players==(int)m_kart_info.size())
{
// we can't send the race info yet, since the server might
// not yet have selected all characters!
@@ -241,6 +241,7 @@ void NetworkManager::handleMessageAtServer(ENetEvent *event)
broadcastToClients(m);
}
}
default: assert(0); // should not happen
} // switch m_state
} // handleMessageAtServer
@@ -268,6 +269,7 @@ void NetworkManager::handleMessageAtClient(ENetEvent *event)
m_state = NS_RACING;
break;
}
default: assert(0); // should not happen
} // switch m_state
} // handleMessageAtClient

View File

@@ -24,7 +24,6 @@
#include <vector>
#include "network/remote_kart_info.hpp"
#include "network/kart_packet.hpp"
#ifdef HAVE_ENET
# include "enet/enet.h"
@@ -72,7 +71,9 @@ private:
void handleMessageAtServer(ENetEvent *event);
void handleMessageAtClient(ENetEvent *event);
void handleDisconnection(ENetEvent *event);
unsigned int getHostId(ENetPeer *p) const {return (int)p->data; }
// the first cast to long avoid compiler errors on 64 bit systems
// about lost precision, then cast long to int to get the right type
unsigned int getHostId(ENetPeer *p) const {return (int)(long)p->data; }
void sendToServer(Message *m);
void broadcastToClients(Message &m);

View File

@@ -29,4 +29,4 @@ public:
WorldLoadedMessage() :Message(MT_WORLD_LOADED) {allocate(0);}
WorldLoadedMessage(ENetPacket* pkt):Message(pkt, MT_WORLD_LOADED) {}
}; // WorldLoadedMessage
#endif
#endif

View File

@@ -97,4 +97,4 @@ void TrackManager::updateGroups(const Track* track)
}
} // updateGroups
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------