Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1705


Ignore:
Timestamp:
Sep 4, 2008, 9:45:05 PM (16 years ago)
Author:
scheusso
Message:

further changes

Location:
code/branches/network/src/network
Files:
4 added
6 deleted
22 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/CMakeLists.txt

    r1699 r1705  
    44  ClientInformation.cc
    55  ConnectionManager.cc
    6   GameStateManager.cc
    7   GameStateClient.cc
     6  GamestateManager.cc
     7  GamestateClient.cc
     8  GamestateHandler.cc
    89  PacketBuffer.cc
    9   PacketDecoder.cc
    10   PacketGenerator.cc
    1110  Server.cc
    1211  Synchronisable.cc
     
    1716  packet/Acknowledgement.cc
    1817  packet/Gamestate.cc
     18  packet/Welcome.cc
    1919  Host.cc
    2020)
  • code/branches/network/src/network/Client.cc

    r1699 r1705  
    4545#include "core/ConsoleCommand.h"
    4646#include "packet/Packet.h"
     47#include "packet/Acknowledgement.h"
    4748
    4849namespace network
     
    127128    return p->send();
    128129  }
     130 
    129131
    130132  /**
     
    137139    if(!isConnected)
    138140      return false;
    139     return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
     141    packet::Packet p(new packet::Chat(message, clientID_));
     142    return p.send();
    140143      //return client_connection.sendPackets();
    141144    // send packets
    142     return false;
    143145  }
    144146
     
    150152    if(client_connection.isConnected() && isSynched_){
    151153      COUT(4) << "popping partial gamestate: " << std::endl;
    152       GameStateCompressed *gs = gamestate.popPartialGameState();
     154      packet::Gamestate *gs = gamestate.getGamestate();
    153155      if(gs){
    154156        COUT(4) << "client tick: sending gs " << gs << std::endl;
    155         ENetPacket *packet = pck_gen.gstate(gs);
    156         if( packet == NULL || !client_connection.addPacket(packet))
     157        packet::Packet p(gs);
     158        if( !p.send() )
    157159          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
    158160        // now delete it to save memory
    159         delete[] gs->data;
    160161        delete gs;
    161162      }
     
    166167      event = client_connection.getEvent();
    167168      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
    168       elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     169      packet::Packet packet(event->packet, event->peer);
     170      assert(packet.getPacketContent()->process());
    169171    }
    170     int gameStateID = gamestate.processGameState();
     172    int gameStateID = gamestate.processGamestates();
    171173    if(gameStateID==GAMESTATEID_INITIAL)
    172174      if(gameStateFailure_){
    173         if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
     175        packet::Packet packet(new packet::Acknowledgement(GAMESTATEID_INITIAL, 0));
     176        if(!packet.send())
    174177          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
    175178        else
     
    183186        isSynched_=true;
    184187      gameStateFailure_=false;
    185       if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
     188      packet::Packet packet(new packet::Acknowledgement(gameStateID, 0));
     189      if(!packet.send())
    186190        COUT(3) << "could not ack gamestate" << std::endl;
    187191    }// otherwise we had no gamestate to load
    188192    gamestate.cleanup();
    189     /*if(!client_connection.sendPackets())
    190       COUT(3) << "Problem sending packets to server" << std::endl;*/
    191193    return;
    192194  }
    193195
    194   void Client::processGamestate( GameStateCompressed *data, int clientID){
    195     COUT(5) << "received gamestate id: " << data->id << std::endl;
    196     gamestate.addGameState(data);
    197   }
    198 
    199   void Client::processClassid(classid *clid){
    200     orxonox::Identifier *id;
    201     id=ID(std::string(clid->message));
    202     if(id!=NULL)
    203       id->setNetworkID(clid->clid);
    204     COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
    205     COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
    206     delete clid;
    207     return;
    208   }
    209  
    210   bool Client::ackGamestateID(int gamestateID, int clientID){
    211     return true;
    212     //TODO: maybe change this (not needed at the moment)
    213   }
    214 
    215 //   void Client::processChat( chat *data, int clientId){
    216 //     COUT(1) << data->message << std::endl;
    217 //     delete[] data->message;
    218 //     delete data;
    219 //   }
    220  
    221   bool Client::processWelcome( welcome *w ){
    222     COUT(4) << "processing welcome message" << std::endl;
    223     clientID_ = w->clientID;
    224     shipID_ = w->shipID;
    225     delete w;
    226     return true;
    227   }
    228 
    229196}
  • code/branches/network/src/network/Client.h

    r1699 r1705  
    4949#include "packet/Chat.h"
    5050#include "ClientConnection.h"
    51 #include "PacketManager.h"
    52 #include "GameStateClient.h"
     51#include "GamestateClient.h"
    5352//#include "NetworkFrameListener.h"
    5453
     
    6463  *
    6564  */
    66   class _NetworkExport Client : PacketDecoder, public Host{
     65  class _NetworkExport Client : public Host{
    6766  public:
    6867    Client();
     
    8180    unsigned int shipID(){return shipID_;}
    8281    int playerID(){return clientID_;}
    83 
     82    static void setShipID( unsigned int shipID){ dynamic_cast<Client *>(instance_)->shipID_=shipID; }
     83    static void setClientID( unsigned int clientID){ dynamic_cast<Client *>(instance_)->clientID_=clientID; }
     84   
    8485    void tick(float time);
    8586
     
    8788   
    8889    ClientConnection client_connection;
    89     PacketGenerator pck_gen;
    90     GameStateClient gamestate;
     90    GamestateClient gamestate;
    9191    bool isConnected;
    9292    bool isSynched_;
     
    9595   
    9696    // implement data processing functions of PacketDecoder
    97     void processGamestate( GameStateCompressed *data, int clientID);
    98     void processClassid(classid *clid);
    99     bool ackGamestateID(int gamestateID, int clientID);
    10097//     void processChat( chat *data, int clientId );
    101     bool processWelcome( welcome *w );
    10298    int clientID_;     // this is the id the server gave to us
    10399    int shipID_;
  • code/branches/network/src/network/ClientInformation.cc

    r1666 r1705  
    123123  }
    124124
    125   bool ClientInformation::setGameStateID(int id){
     125  bool ClientInformation::setGamestateID(int id){
    126126    if(!this)
    127127      return false;
  • code/branches/network/src/network/ClientInformation.h

    r1699 r1705  
    7070    void setID(int clientID);
    7171    bool setPeer(ENetPeer *peer);
    72     bool setGameStateID(int id);
     72    bool setGamestateID(int id);
    7373    bool setPartialGamestateID(int id);
    7474    inline void setShipID(unsigned int id){ShipID_=id;}
  • code/branches/network/src/network/ConnectionManager.cc

    r1699 r1705  
    5151#include "ConnectionManager.h"
    5252#include "Synchronisable.h"
     53#include "packet/ClassID.h"
    5354
    5455namespace std
     
    7576    bindAddress.port = NETWORK_PORT;
    7677  }
    77   boost::recursive_mutex ConnectionManager::enet_mutex_;
     78  boost::recursive_mutex ConnectionManager::enet_mutex;
    7879 
    7980//   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     
    172173 
    173174  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
    174     boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_);
     175    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
    175176    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
    176177      return false;
     
    190191    if(!instance_)
    191192      return false;
    192     boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_);
     193    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
    193194    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
    194195      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
     
    203204    if(server==NULL || !instance_)
    204205      return false;
    205     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     206    boost::recursive_mutex::scoped_lock lock(enet_mutex);
    206207    enet_host_flush(server);
    207208    lock.unlock();
     
    214215    atexit(enet_deinitialize);
    215216    { //scope of the mutex
    216       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     217      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    217218      enet_initialize();
    218219      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     
    228229    while(!quit){
    229230      { //mutex scope
    230         boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     231        boost::recursive_mutex::scoped_lock lock(enet_mutex);
    231232        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    232233          // we should never reach this point
     
    270271    // if we're finishied, destroy server
    271272    {
    272       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     273      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    273274      enet_host_destroy(server);
    274275      lock.unlock();
     
    284285    while(temp!=0){
    285286      {
    286         boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     287        boost::recursive_mutex::scoped_lock lock(enet_mutex);
    287288        enet_peer_disconnect(temp->getPeer(), 0);
    288289        lock.unlock();
     
    292293    //bugfix: might be the reason why server crashes when clients disconnects
    293294    temp = ClientInformation::getBegin()->next();
    294     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     295    boost::recursive_mutex::scoped_lock lock(enet_mutex);
    295296    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
    296297      switch (event.type)
     
    337338    std::string classname;
    338339    orxonox::Identifier *id;
     340    packet::Packet packet;
     341    packet.setClientID(clientID);
     342    packet::ClassID *classid;
    339343    std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin();
    340344    while(it != orxonox::Factory::getFactoryEnd()){
     
    348352      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
    349353
    350       while(!addPacket(packet_gen.clid( (int)network_id, classname ), clientID) && failures < 10){
     354      classid = new packet::ClassID(network_id, classname);
     355      packet.setPacketContent(classid);
     356      while(!packet.send() && failures < 10){
    351357        failures++;
    352358      }
     359      delete classid;
    353360      ++it;
    354361    }
     362    packet.setPacketContent(0);
    355363    //sendPackets();
    356364    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
     
    370378  }
    371379 
    372   bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){
    373     if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){
    374       //sendPackets();
    375       return true;
    376     }else
    377       return false;
    378   }
    379380 
    380381  void ConnectionManager::disconnectClient(ClientInformation *client){
    381382    {
    382       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     383      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    383384      enet_peer_disconnect(client->getPeer(), 0);
    384385      lock.unlock();
     
    387388  }
    388389 
    389   bool ConnectionManager::addFakeConnectRequest(ENetEvent *ev){
    390     ENetEvent event;
    391     event.peer=ev->peer;
    392     event.packet = packet_gen.generateConnectRequest();
    393     return buffer.push(&event);
    394   }
    395  
    396  
    397390
    398391}
  • code/branches/network/src/network/ConnectionManager.h

    r1666 r1705  
    5151
    5252#include "PacketBuffer.h"
    53 #include "PacketManager.h"
    5453#include "packet/Packet.h"
    5554
     
    7372
    7473  class ConnectionManager{
    75   public:
     74    public:
     75    static boost::recursive_mutex enet_mutex;
    7676    ConnectionManager();
    7777    //ConnectionManager(ClientInformation *head);
     
    9595    void disconnectClient(ClientInformation *client);
    9696    void syncClassid(int clientID);
    97     bool sendWelcome(int clientID, int shipID, bool allowed);
    9897
    9998  private:
     
    109108    //bool createShip(ClientInformation *client);
    110109    bool removeShip(ClientInformation *client);
    111     bool addFakeConnectRequest(ENetEvent *ev);
    112110    PacketBuffer buffer;
    113     PacketGenerator packet_gen;
    114111
    115112    ENetHost *server;
     
    119116
    120117    boost::thread *receiverThread_;
    121     static boost::recursive_mutex enet_mutex_;
    122118    static ConnectionManager *instance_;
    123119
  • code/branches/network/src/network/Host.cc

    r1699 r1705  
    5454}
    5555
    56 bool Host::ackGamestate(int gamestateID, int clientID){
    57   return instance_->ackGamestateID(gamestateID, clientID);
    58 }
    59 
    6056}//namespace network
  • code/branches/network/src/network/Host.h

    r1699 r1705  
    3131#include <string>
    3232
     33#include "NetworkPrereqs.h"
    3334#include "packet/Chat.h"
    3435
     
    4445    virtual bool sendChat(packet::Chat *chat)=0;
    4546    virtual bool queuePacket(ENetPacket *packet, int clientID)=0;
    46     virtual bool ackGamestateID(int gamestateID, int clientID)=0;
    4747    virtual unsigned int shipID()=0;
    4848    virtual int playerID()=0;
    4949   
    50     static Host *instance_;
    5150   
    5251   
     
    5453    Host();
    5554    ~Host();
     55    static Host *instance_;
    5656   
    5757  public:
     
    6262    static int getPlayerID();
    6363    static unsigned int getShipID();
    64     static bool ackGamestate(int gamestateID, int clientID);
     64   
     65    // packet process functions
    6566};
    6667
  • code/branches/network/src/network/NetworkPrereqs.h

    r1505 r1705  
    6666  class ClientInformation;
    6767  class ConnectionManager;
    68   class GameStateClient;
    69   class GameStateManager;
     68  class GamestateClient;
     69  class GamestateManager;
     70  class GamestateHandler;
    7071  class PacketBuffer;
    71   class PacketDecoder;
    72   class PacketGenerator;
    7372  class Server;
    7473  class ServerFrameListener;
    7574  class Synchronisable;
    76   struct ack;
    77   struct chat;
    78   struct classid;
    79   struct welcome;
    80   struct connectRequest;
    8175  struct ClientList;
    82   struct GameState;
    83   struct GameStateCompressed;
    84   struct keyboard;
    85   struct mouse;
    8676  struct PacketEnvelope;
    8777  struct QueueItem;
    8878  struct syncData;
    8979  struct synchronisableVariable;
     80  namespace packet{
     81    class Gamestate;
     82    class Packet;
     83    class PacketContent;
     84    class Acknowledgement;
     85    class ClassID;
     86    class Welcome;
     87  }
    9088}
    9189
  • code/branches/network/src/network/Server.cc

    r1699 r1705  
    4545
    4646#include "ConnectionManager.h"
    47 #include "PacketTypes.h"
    48 #include "GameStateManager.h"
     47#include "GamestateManager.h"
    4948#include "ClientInformation.h"
    5049#include "util/Sleep.h"
     
    5352#include "packet/Chat.h"
    5453#include "packet/Packet.h"
     54#include "packet/Welcome.h"
    5555
    5656namespace network
     
    6565  Server::Server() {
    6666    timeSinceLastUpdate_=0;
    67     packet_gen = PacketGenerator();
    6867    connection = new ConnectionManager();
    69     gamestates = new GameStateManager();
     68    gamestates_ = new GamestateManager();
    7069  }
    7170 
    7271  Server::Server(int port){
    7372    timeSinceLastUpdate_=0;
    74     packet_gen = PacketGenerator();
    7573    connection = new ConnectionManager(port);
    76     gamestates = new GameStateManager();
     74    gamestates_ = new GamestateManager();
    7775  }
    7876
     
    8482  Server::Server(int port, std::string bindAddress) {
    8583    timeSinceLastUpdate_=0;
    86     packet_gen = PacketGenerator();
    8784    connection = new ConnectionManager(port, bindAddress);
    88     gamestates = new GameStateManager();
     85    gamestates_ = new GamestateManager();
    8986  }
    9087
     
    9693  Server::Server(int port, const char *bindAddress) {
    9794    timeSinceLastUpdate_=0;
    98     packet_gen = PacketGenerator();
    9995    connection = new ConnectionManager(port, bindAddress);
    100     gamestates = new GameStateManager();
     96    gamestates_ = new GamestateManager();
    10197  }
    10298
     
    173169      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
    174170//      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;
    175       gamestates->processGameStates();
     171      gamestates_->processGamestates();
    176172      updateGamestate();
    177173    }
     
    191187  void Server::processQueue() {
    192188    ENetEvent *event;
    193     int clientID=-1;
    194189    while(!connection->queueEmpty()){
    195190      //std::cout << "Client " << clientID << " sent: " << std::endl;
     
    209204        break;
    210205      case ENET_EVENT_TYPE_RECEIVE:
    211         if(ClientInformation::findClient(&event->peer->address)){
    212           clientID = ClientInformation::findClient(&event->peer->address)->getID();
    213           if( !elaborate(event->packet, clientID) )
    214             COUT(3) << "Server: could not elaborate" << std::endl;
    215         }
     206        if(!processPacket(event->packet, event->peer))
     207          COUT(3) << "processing incoming packet failed" << std::endl;
    216208        break;
    217209      default:
     
    227219  */
    228220  void Server::updateGamestate() {
    229     gamestates->update();
     221    gamestates_->update();
    230222    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
    231223    //std::cout << "updated gamestate, sending it" << std::endl;
     
    236228  }
    237229
     230  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
     231    packet::Packet p = packet::Packet(packet, peer);
     232    return p.getPacketContent()->process();
     233  }
     234 
    238235  /**
    239236  * sends the gamestate
     
    257254      int cid = temp->getID(); //get client id
    258255      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
    259       GameStateCompressed *gs = gamestates->popGameState(cid);
     256      packet::Gamestate *gs = gamestates_->popGameState(cid);
    260257      if(gs==NULL){
    261258        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
     
    263260      }
    264261      //std::cout << "adding gamestate" << std::endl;
    265       ENetPacket *packet = packet_gen.gstate(gs);
    266       if(!packet)
    267         continue;
    268       if ( !(connection->addPacket(packet, cid)) ){
     262      packet::Packet packet(gs);
     263      packet.setClientID(cid);
     264      if ( !packet.send() ){
    269265        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
    270266        temp->addFailure();
     
    277273      temp=temp->next();
    278274      // now delete gamestate
    279       delete[] gs->data;
    280275      delete gs;
    281276    }
     
    286281    //COUT(5) << "Server: had no gamestates to send" << std::endl;
    287282    return true;
    288   }
    289 
    290   void Server::processAck( ack *data, int clientID) {
    291     COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
    292     gamestates->ackGameState(clientID, data->a);
    293     delete data;
    294   }
    295  
    296   bool Server::processConnectRequest( connectRequest *con, int clientID ){
    297     //(COUT(3) << "processing connectRequest " << std::endl;
    298     //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    299     //createClient(clientID);
    300     delete con;
    301     return true;
    302   }
    303  
    304   bool Server::ackGamestateID(int gamestateID, int clientID){
    305     gamestates->ackGameState(clientID, gamestateID);
    306     return true;
    307   }
    308  
    309   void Server::processGamestate( GameStateCompressed *data, int clientID){
    310     COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
    311     gamestates->addGameState(data, clientID);
    312         /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
    313     else
    314       if(clients->findClient(clientID))
    315         clients->findClient(clientID)->resetFailures();*/
    316283  }
    317284 
     
    359326    temp->setSynched(true);
    360327    COUT(3) << "sending welcome" << std::endl;
    361     connection->sendWelcome(temp->getID(), temp->getShipID(), true);
     328    packet::Packet packet(new packet::Welcome(temp->getID(), temp->getShipID()));
     329    assert(packet.send());
    362330    return true;
    363331  }
     
    421389  void Server::disconnectClient( ClientInformation *client){
    422390    connection->disconnectClient(client);
    423     gamestates->removeClient(client);
     391    gamestates_->removeClient(client);
    424392  }
    425393 
  • code/branches/network/src/network/Server.h

    r1699 r1705  
    4747
    4848#include "Host.h"
    49 #include "PacketManager.h"
    50 
    51 
     49#include "GamestateManager.h"
    5250
    5351namespace network
     
    5957  * It implements all functions necessary for a Server
    6058  */
    61   class _NetworkExport Server : public PacketDecoder, public Host{
     59  class _NetworkExport Server : public Host{
    6260  public:
    6361    Server();
     
    8583    void disconnectClient(int clientID);
    8684    void disconnectClient( ClientInformation *client);
     85    bool processPacket( ENetPacket *packet, ENetPeer *peer );
    8786    bool sendGameState();
    88     void processAck( ack *data, int clientID);
    89     bool processConnectRequest( connectRequest *con, int clientID );
    90     void processGamestate( GameStateCompressed *data, int clientID);
    9187   
    92     bool ackGamestateID(int gamestateID, int clientID);
    9388   
    9489    //void processChat( chat *data, int clientId);
    9590    ConnectionManager *connection;
    96     GameStateManager *gamestates;
    97     PacketGenerator packet_gen;
     91    GamestateManager *gamestates_;
    9892
    9993   
  • code/branches/network/src/network/packet/Acknowledgement.cc

    r1701 r1705  
    22#include "Packet.h"
    33#include "network/Host.h"
     4#include "network/GamestateHandler.h"
    45
    56namespace network {
     
    2122
    2223Acknowledgement::Acknowledgement( unsigned char *data, int clientID )
    23   : PacketContent(data)
     24  : PacketContent(data, clientID)
    2425{
    2526  clientID_=clientID;
     
    3940
    4041bool Acknowledgement::process(){
    41   return Host::ackGamestate(data_[_ACKID], clientID_);
     42  return GamestateHandler::ackGamestate(data_[_ACKID], clientID_);
    4243}
    4344
  • code/branches/network/src/network/packet/Acknowledgement.h

    r1701 r1705  
    5050  unsigned int getAckID();
    5151private:
    52   int clientID_;
    5352};
    5453
  • code/branches/network/src/network/packet/Chat.cc

    r1701 r1705  
    2323
    2424Chat::Chat( unsigned char *data, int clientID )
    25   : PacketContent(data)
     25  : PacketContent(data, clientID)
    2626{
    2727  messageLength_ = *(unsigned int *)&data[ _MESSAGELENGTH ];
    28   clientID_ = clientID;
    2928}
    3029
  • code/branches/network/src/network/packet/ClassID.cc

    r1701 r1705  
    33#include "core/CoreIncludes.h"
    44#include <string>
    5 
     5#include <assert.h>
    66
    77namespace network {
     
    1919  flags_ = flags_ | PACKET_FLAGS_CLASSID;
    2020  classNameLength_=className.length()+1;
     21  assert(getSize());
    2122  data_=new unsigned char[ getSize() ];
    2223  if(!data_)
     
    2829}
    2930
    30 ClassID::ClassID( unsigned char *data )
    31   : PacketContent(data)
     31ClassID::ClassID( unsigned char *data, int clientID )
     32  : PacketContent(data, clientID)
    3233{
    3334  memcpy( (void *)&classNameLength_, &data[ sizeof(ENUM::Type) + sizeof(int) ], sizeof(classNameLength_) );
  • code/branches/network/src/network/packet/ClassID.h

    r1701 r1705  
    4343public:
    4444  ClassID( unsigned int classID, std::string className );
    45   ClassID( unsigned char* data );
     45  ClassID( unsigned char* data, int clientID );
    4646  ~ClassID();
    4747 
  • code/branches/network/src/network/packet/Gamestate.cc

    r1701 r1705  
    11#include "Gamestate.h"
    22#include "network/ClientInformation.h"
    3 
     3#include "network/GamestateHandler.h"
    44
    55#include <zlib.h>
     
    2121}
    2222
    23 Gamestate::Gamestate(unsigned char *data, bool compressed):
    24     PacketContent(data)
     23Gamestate::Gamestate(unsigned char *data, bool compressed, int clientID):
     24    PacketContent(data, clientID)
    2525{
    2626  compressed_ = compressed;
     
    133133}
    134134
     135int Gamestate::getID(){
     136  return HEADER->id;
     137}
     138
    135139unsigned char *Gamestate::getData()
    136140{
     
    146150  {
    147151    return HEADER->normsize+sizeof(GamestateHeader);
    148     //assert(bs_);
    149     //return bs_->getSize();
    150152  }
    151153}
     
    153155bool Gamestate::process()
    154156{
    155   //FIXME: TODO: implement this function
    156   return true;
     157  return GamestateHandler::addGamestate(this, getClientID());
    157158}
    158159
     
    182183  *GAMESTATE_HEADER(ndata) = *HEADER;
    183184  //delete old data
    184 //  delete bs_;
    185185  delete[] data_;
    186186  //save new data
     
    250250  }
    251251
    252   Gamestate *g = new Gamestate(ndata, false);
     252  Gamestate *g = new Gamestate(ndata, false, 0);
    253253  return g;
    254254}
     
    282282  }
    283283 
    284   Gamestate *g = new Gamestate(ndata, false);
     284  Gamestate *g = new Gamestate(ndata, false, 0);
    285285  return g;
    286286}
  • code/branches/network/src/network/packet/Gamestate.h

    r1701 r1705  
    5454  public:
    5555    Gamestate();
    56     Gamestate(unsigned char *data, bool compressed);
     56    Gamestate(unsigned char *data, bool compressed, int clientID);
    5757   
    5858    ~Gamestate();
     
    6060    bool collectData(int id, int mode=0x0);
    6161    bool spreadData(int mode=0x0);
     62    int getID();
     63    Gamestate *diff(Gamestate *base);
     64    Gamestate *undiff(Gamestate *base);
     65    bool compressData();
     66    bool decompressData();
    6267   
    6368    // PacketContent functions
     
    6873   
    6974  private:
    70     bool compressData();
    71     bool decompressData();
    72     Gamestate *diff(Gamestate *base);
    73     Gamestate *undiff(Gamestate *base);
    7475    unsigned int calcGamestateSize(int mode=0x0);
    75     //unsigned char *getGs(){ return bs_->getData(); }
    7676    void removeObject(orxonox::Iterator<Synchronisable> &it);
    7777
  • code/branches/network/src/network/packet/Packet.cc

    r1701 r1705  
    1010#include "ClassID.h"
    1111#include "network/Host.h"
    12 //#include "Gamestate.h"
     12#include "core/CoreIncludes.h"
    1313
    1414namespace network{
     
    3939  clientID_ = ClientInformation::findClient(&peer->address)->getID();
    4040  packetContent_ = createPacketContent(packet);
     41  delete packet;
     42  delete peer;
    4143}
    4244
     
    4951}
    5052
     53void Packet::setPacketContent(PacketContent *content){
     54  packetContent_ = content;
     55  if(enetPacket_){
     56    enet_packet_destroy(enetPacket_);
     57    enetPacket_=0;
     58  }
     59}
    5160
    5261bool Packet::send(){
    53   if(!packetDirection_ == ENUM::Outgoing || !packetDirection_ == ENUM::Bidirectional )
     62  if(packetDirection_ != ENUM::Outgoing && packetDirection_ != ENUM::Bidirectional )
    5463    return false;
    5564  if(!enetPacket_){
    5665    if(!packetContent_)
    5766      return false;
     67    COUT(3) << "creating packet with data: " << packetContent_->getData() << " size: " << packetContent_->getSize() << " and flags: " << packetContent_->getFlags() << std::endl;
    5868    enetPacket_ = enet_packet_create(packetContent_->getData(), packetContent_->getSize(), packetContent_->getFlags());
    5969  }
    6070  network::Host::addPacket( enetPacket_, clientID_);
     71  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
    6172  return true;
    6273}
     
    7384      break;
    7485    case ENUM::ClassID:
    75       packetContent_ = new ClassID( data );
     86      packetContent_ = new ClassID( data, clientID_ );
    7687      break;
    7788    case ENUM::Gamestate:
  • code/branches/network/src/network/packet/Packet.h

    r1701 r1705  
    4747    Gamestate,
    4848    ClassID,
    49     Chat
     49    Chat,
     50    Welcome
    5051  };
    5152}
     
    6566   
    6667    void setClientID( int id ){ clientID_ = id; }
    67     void setPacketContent(PacketContent *content){ packetContent_=content; }
     68    void setPacketContent(PacketContent *content);
    6869   
    6970    bool send();
  • code/branches/network/src/network/packet/PacketContent.h

    r1701 r1705  
    4747    virtual unsigned char *getData()=0;
    4848    virtual unsigned int getSize() const =0;
     49    int getClientID(){return clientID_;}
    4950    virtual bool process()=0;
    5051    enet_uint32 getFlags(){ return flags_; }
     
    5354        { flags_ = PACKET_FLAG_DEFAULT;
    5455        data_=0; }
    55     PacketContent( unsigned char *data )
    56         { flags_ = PACKET_FLAG_DEFAULT; data_=data; }
     56    PacketContent( unsigned char *data, int clientID )
     57        { flags_ = PACKET_FLAG_DEFAULT; data_=data; clientID_=clientID; }
    5758    PacketContent(const PacketContent& p){
    5859      flags_=p.flags_;
     
    6667    enet_uint32 flags_;
    6768    unsigned char *data_;
     69    int clientID_;
    6870  private:
    6971};
Note: See TracChangeset for help on using the changeset viewer.