Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

merged network branch back into trunk

Location:
code/trunk/src/network
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/network/Client.cc

    r1735 r1751  
    6161    isSynched_=false;
    6262    gameStateFailure_=false;
     63    isServer_ = false;
    6364  }
    6465
     
    7273    isSynched_=false;
    7374    gameStateFailure_=false;
     75    isServer_ = false;
    7476  }
    7577
     
    8385    isSynched_=false;
    8486    gameStateFailure_=false;
     87    isServer_ = false;
    8588  }
    8689
     
    151154    if(client_connection.isConnected() && isSynched_){
    152155      COUT(4) << "popping partial gamestate: " << std::endl;
    153       /*packet::Gamestate *gs = gamestate.getGamestate();
     156      packet::Gamestate *gs = gamestate.getGamestate();
    154157      if(gs){
    155158        COUT(4) << "client tick: sending gs " << gs << std::endl;
     
    157160          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
    158161        // gs gets automatically deleted by enet callback
    159       }*/
     162      }
    160163    }
    161164    ENetEvent *event;
     
    168171    }
    169172    int gameStateID = gamestate.processGamestates();
    170     /*if(gameStateID==GAMESTATEID_INITIAL)
     173    if(gameStateID==GAMESTATEID_INITIAL)
    171174      if(gameStateFailure_){
    172         packet::Acknowledgement ack(GAMESTATEID_INITIAL, 0);
    173         if(!ack.send())
     175        packet::Acknowledgement *ack = new packet::Acknowledgement(GAMESTATEID_INITIAL, 0);
     176        if(!ack->send())
    174177          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
    175178        else
     
    183186        isSynched_=true;
    184187      gameStateFailure_=false;
    185       packet::Acknowledgement ack(gameStateID, 0);
    186       if(!ack.send())
     188      packet::Acknowledgement *ack = new packet::Acknowledgement(gameStateID, 0);
     189      if(!ack->send())
    187190        COUT(3) << "could not ack gamestate" << std::endl;
    188     }*/// otherwise we had no gamestate to load
     191    }// otherwise we had no gamestate to load
    189192    gamestate.cleanup();
    190193    return;
  • code/trunk/src/network/GamestateClient.cc

    r1747 r1751  
    7575    int id = GAMESTATEID_INITIAL;
    7676    bool b = saveShipCache();
    77     if(processGamestate(tempGamestate_)){
    78       if(b)
    79         loadShipCache();
    80       id = tempGamestate_->getID();
    81     }
     77    packet::Gamestate *processed = processGamestate(tempGamestate_);
     78    assert(processed);
     79    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
     80    tempGamestate_=0;
     81    gamestateMap_[processed->getID()]=processed;
     82    last_diff_ = processed->getBaseID();
     83    last_gamestate_ = processed->getID();
     84    if(b)
     85      loadShipCache();
     86    id = processed->getID();
    8287    cleanup();
    8388    return id;
     
    130135    if(myShip_){
    131136      //      unsigned char *data = new unsigned char[myShip_->getSize()];
    132       int size=myShip_->getSize(0x1);
     137      int size=myShip_->getSize2(0, 0x1);
    133138      if(size==0)
    134139        return false;
    135140      shipCache_ = new unsigned char [size];
    136141      unsigned char *temp = shipCache_;
    137       if(!myShip_->getData2(temp, 0x1))
     142      if(!myShip_->getData(temp, 0, 0x1))
    138143        COUT(3) << "could not save shipCache" << std::endl;
    139144      return true;
     
    152157  }
    153158
    154   bool GamestateClient::processGamestate(packet::Gamestate *gs){
    155     assert(gs->decompressData());
    156     if(gs->isDiffed())
    157       assert(gs->undiff(gamestateMap_[gs->getBaseID()]));
    158     return gs->spreadData();
     159  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
     160    if(gs->isCompressed())
     161      assert(gs->decompressData());
     162    if(gs->isDiffed()){
     163      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
     164      assert(base);
     165      packet::Gamestate *undiffed = gs->undiff(base);
     166      delete gs;
     167      gs=undiffed;
     168      COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
     169    }
     170    if(gs->spreadData())
     171      return gs;
     172    else
     173      return NULL;
    159174  }
    160175
  • code/trunk/src/network/GamestateClient.h

    r1747 r1751  
    6666    void cleanup();
    6767  private:
    68     bool processGamestate(packet::Gamestate *gs);
    69     void removeObject(orxonox::ObjectListIterator<Synchronisable> &it);
     68    packet::Gamestate *processGamestate(packet::Gamestate *gs);
     69    void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it);
    7070    void printGamestateMap();
    7171    bool saveShipCache();
  • code/trunk/src/network/GamestateManager.cc

    r1735 r1751  
    129129    //why are we searching the same client's gamestate id as we searched in
    130130    //Server::sendGameState?
     131    packet::Gamestate *gs;
    131132    int gID = ClientInformation::findClient(clientID)->getGamestateID();
    132     COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
    133 //     COUT(3) << "gamestatemap: " << &gameStateMap << std::endl;
     133    //COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
    134134    //chose wheather the next gamestate is the first or not
    135135    if(gID != GAMESTATEID_INITIAL){
    136       // TODO something with the gamestatemap is wrong
    137136      packet::Gamestate *client=NULL;
    138137      std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID);
    139138      if(it!=gamestateMap.end())
    140139        client = it->second;
    141       packet::Gamestate *gs;
    142140      if(client)
    143141        gs = reference->diff(client);
    144142      else
    145143        gs = new packet::Gamestate(*reference);
    146       gs->compressData();
    147       return gs;
    148144    } else {
    149145      COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
    150 //       ackGameState(clientID, reference->id);
    151       return new packet::Gamestate(*reference);
    152       // return an undiffed gamestate and set appropriate flags
    153     }
     146      gs = new packet::Gamestate(*reference);
     147    }
     148    assert(gs->compressData());
     149    return gs;
    154150  }
    155151 
     
    194190    if(client->getGamestateID()>=0)
    195191      gamestateUsed[client->getGamestateID()]--;
    196     ClientInformation::removeClient(client->getID());
    197192  }
    198193 
    199194  bool GamestateManager::processGamestate(packet::Gamestate *gs){
    200     assert(gs->decompressData());
     195    if(gs->isCompressed())
     196       assert(gs->decompressData());
    201197    assert(!gs->isDiffed());
    202198    return gs->spreadData();
  • code/trunk/src/network/Host.cc

    r1735 r1751  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
    129#include <assert.h>
    230
  • code/trunk/src/network/Host.h

    r1740 r1751  
    5454    virtual ~Host();
    5555    static Host *instance_;
     56    bool isServer_;     
    5657
    5758  public:
     
    6465    static void setClientID(unsigned int id){ instance_->clientID_ = id; }
    6566    static void setShipID(unsigned int id){ instance_->shipID_ = id; }
     67    static bool isServer(){ return instance_->isServer_; }             
    6668  private:
    6769    unsigned int clientID_;
  • code/trunk/src/network/Server.cc

    r1747 r1751  
    5454#include "packet/Packet.h"
    5555#include "packet/Welcome.h"
     56#include <util/Convert.h>
    5657
    5758namespace network
     
    6869    connection = new ConnectionManager();
    6970    gamestates_ = new GamestateManager();
     71    isServer_ = true;
    7072  }
    7173
     
    7476    connection = new ConnectionManager(port);
    7577    gamestates_ = new GamestateManager();
     78    isServer_ = true;
    7679  }
    7780
     
    8588    connection = new ConnectionManager(port, bindAddress);
    8689    gamestates_ = new GamestateManager();
     90    isServer_ = true;
    8791  }
    8892
     
    96100    connection = new ConnectionManager(port, bindAddress);
    97101    gamestates_ = new GamestateManager();
     102    isServer_ = true;
    98103  }
    99104
     
    260265      //std::cout << "adding gamestate" << std::endl;
    261266      gs->setClientID(cid);
    262       assert(gs->compressData());
    263267      if ( !gs->send() ){
    264268        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
     
    294298      return false;
    295299    }
    296     if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack
    297       temp->prev()->setID(0); //bugfix: not necessary but usefull
     300    if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
    298301      temp->setID(1);
    299302    }
     
    324327    w->setClientID(temp->getID());
    325328    assert(w->send());
     329    packet::Gamestate *g = new packet::Gamestate();
     330    g->setClientID(temp->getID());
     331    assert(g->collectData(0));
     332    assert(g->compressData());
     333    assert(g->send());
    326334    return true;
    327335  }
     
    349357    no->setTransDamp(75);
    350358    no->setRotDamp(1.0);
    351     no->setCamera("cam_"+client->getID());
     359    no->setCamera(std::string("cam_") + convertToString(client->getID()));
    352360    no->create();
    353     no->setBacksync(true);
    354361
    355362    return true;
     
    365372    if(!client)
    366373      return false;
     374    gamestates_->removeClient(client);
    367375    while(it){
    368376      if(it->objectID!=client->getShipID()){
  • code/trunk/src/network/Synchronisable.cc

    r1735 r1751  
    6363    static int idCounter=0;
    6464    datasize=0;
     65    objectFrequency_=1;
     66    objectMode_=0x1; // by default do not send data to servere
    6567    objectID=idCounter++;
    6668    syncList = new std::list<synchronisableVariable *>;
     
    8183  }
    8284
     85 
    8386  void Synchronisable::setClient(bool b){
    8487    if(b) // client
     
    9598    classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
    9699   
     100    if(size==3*sizeof(unsigned int)) //not our turn, dont do anything
     101      return true;
     102   
    97103    orxonox::Identifier* id = ID(classID);
    98104    if(!id){
    99105      COUT(3) << "We could not identify a new object; classid: " << classID << " uint: " << (unsigned int)classID << " objectID: " << objectID << " size: " << size << std::endl;
     106      assert(0);
    100107      return false; // most probably the gamestate is corrupted
    101108    }
     
    140147    syncList->push_back(temp);
    141148  }
    142 
    143 
    144   /**
    145   * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
    146   * Difference to the above function:
    147   * takes a pointer to already allocated memory (must have at least getSize bytes length)
    148   * structure of the bitstream:
    149   * (var1_size,var1,var2_size,var2,...)
    150   * varx_size: size = sizeof(int)
    151   * varx: size = varx_size
    152   * @return data containing all variables and their sizes
    153   */
    154   syncData Synchronisable::getData(unsigned char *mem, int mode){
    155     //std::cout << "inside getData" << std::endl;
    156     if(mode==0x0)
    157       mode=state_;
    158     if(classID==0)
    159       COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
    160     this->classID=this->getIdentifier()->getNetworkID();
    161     std::list<synchronisableVariable *>::iterator i;
    162     syncData retVal;
    163     retVal.objectID=this->objectID;
    164     retVal.classID=this->classID;
    165     retVal.length=getSize();
    166     COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;
    167     retVal.data=mem;
    168     // copy to location
    169     int n=0; //offset
    170     for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
    171       //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
    172       if( ((*i)->mode & mode) == 0 ){
    173         COUT(5) << "not getting data: " << std::endl;
    174         continue;  // this variable should only be received
    175       }
    176       switch((*i)->type){
    177       case DATA:
    178         memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);
    179         n+=(*i)->size;
    180         break;
    181       case STRING:
    182         memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );
    183         n+=sizeof(int);
    184         const char *data = ( ( *(std::string *) (*i)->var).c_str());
    185         memcpy( retVal.data+n, (void*)data, (*i)->size);
    186         COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
    187         n+=(*i)->size;
    188         break;
    189       }
    190     }
    191     return retVal;
    192   }
    193149 
    194150  /**
    195151   * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
    196   * Difference to the above function:
     152   * Difference to the above function:
    197153   * takes a pointer to already allocated memory (must have at least getSize bytes length)
    198   * structure of the bitstream:
     154   * structure of the bitstream:
    199155   * (var1_size,var1,var2_size,var2,...)
    200156   * varx_size: size = sizeof(int)
     
    202158   * @return data containing all variables and their sizes
    203159   */
    204   bool Synchronisable::getData2(unsigned char*& mem, int mode){
     160  bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){
    205161    //std::cout << "inside getData" << std::endl;
    206162    unsigned int tempsize = 0;
     
    209165    if(classID==0)
    210166      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
    211     this->classID=this->getIdentifier()->getNetworkID();
     167    this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
    212168    std::list<synchronisableVariable *>::iterator i;
    213169    unsigned int size;
    214     size=getSize2(mode);
     170    size=getSize2(id, mode);
    215171   
    216172    // start copy header
     
    224180    // end copy header
    225181   
     182    //if this tick is we dont synchronise, then abort now
     183    if(!isMyTick(id))
     184      return true;
    226185   
    227186    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
     
    254213  }
    255214
    256   /*bool Synchronisable::getData(Bytestream& bs, int mode)
    257   {
    258     //std::cout << "inside getData" << std::endl;
    259     if(mode==0x0)
    260       mode=state_;
    261     if(classID==0)
    262       COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
    263     this->classID=this->getIdentifier()->getNetworkID();
    264     std::list<synchronisableVariable *>::iterator i;
    265     bs << this->getSize();
    266     bs << this->objectID;
    267     bs << this->classID;
    268     // copy to location
    269     for(i=syncList->begin(); i!=syncList->end(); ++i){
    270       if( ((*i)->mode & mode) == 0 ){
    271         COUT(5) << "not getting data: " << std::endl;
    272         continue;  // this variable should only be received
    273       }
    274       switch((*i)->type){
    275         case DATA:
    276           bs << *(*i)->var;
    277           //std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);
    278           //n+=(*i)->size;
    279           break;
    280         case STRING:
    281           bs << *(String *)((*i)->var);
    282           //memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );
    283           //n+=sizeof(int);
    284           //const char *data = ( ( *(std::string *) (*i)->var).c_str());
    285           //std::memcpy( retVal.data+n, (void*)data, (*i)->size);
    286           //COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
    287           //n+=(*i)->size;
    288           break;
    289       }
    290     }
    291     return true;
    292   }*/
    293 
    294  
    295   /**
    296   * This function takes a syncData struct and takes it to update the variables
    297   * @param vars data of the variables
    298   * @return true/false
    299   */
    300   bool Synchronisable::updateData(syncData vars, int mode){
    301     if(mode==0x0)
    302       mode=state_;
    303     unsigned char *data=vars.data;
    304     std::list<synchronisableVariable *>::iterator i;
    305     if(syncList->empty()){
    306       COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
    307       return false;
    308     }
    309     COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
    310     for(i=syncList->begin(); i!=syncList->end(); i++){
    311       if( ((*i)->mode ^ mode) == 0 ){
    312         COUT(5) << "synchronisable: not updating variable " << std::endl;
    313         continue;  // this variable should only be set
    314       }
    315       COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
    316       bool callback=false;
    317       switch((*i)->type){
    318       case DATA:
    319         if((*i)->callback) // check whether this variable changed (but only if callback was set)
    320           if(strncmp((char *)(*i)->var, (char *)data, (*i)->size)!=0)
    321             callback=true;
    322         memcpy((void*)(*i)->var, data, (*i)->size);
    323         data+=(*i)->size;
    324         break;
    325       case STRING:
    326         (*i)->size = *(int *)data;
    327         COUT(5) << "string size: " << (*i)->size << std::endl;
    328         data+=sizeof(int);
    329         if((*i)->callback) // check whether this string changed
    330           if( *(std::string *)((*i)->var) != std::string((char *)data) )
    331             callback=true;
    332         *((std::string *)((*i)->var)) = std::string((const char*)data);
    333         COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
    334         data += (*i)->size;
    335         break;
    336       }
    337       // call the callback function, if defined
    338       if(callback && (*i)->callback)
    339         (*i)->callback->call();
    340     }
    341     return true;
    342   }
    343215 
    344216  /**
     
    368240    assert(this->objectID==objectID);
    369241    assert(this->classID==classID);
     242    if(size==3*sizeof(unsigned int)) //if true, only the header is available
     243      return true;
     244      //assert(0);
    370245   
    371246    COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl;
     
    408283  * @return amount of bytes
    409284  */
    410   int Synchronisable::getSize(int mode){
     285  int Synchronisable::getSize(unsigned int id, int mode){
     286    if(!isMyTick(id))
     287      return 0;
    411288    int tsize=0;
    412289    if(mode==0x0)
     
    435312   * @return amount of bytes
    436313   */
    437   int Synchronisable::getSize2(int mode){
    438     return 3*sizeof(unsigned int) + getSize( mode );
     314  int Synchronisable::getSize2(unsigned int id, int mode){
     315    return sizeof(synchronisableHeader) + getSize( id, mode );
     316  }
     317 
     318  /**
     319   *
     320   * @param id
     321   * @return
     322   */
     323  bool Synchronisable::isMyTick(unsigned int id){
     324//     return true;
     325    return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
    439326  }
    440327 
     
    453340  }
    454341 
    455   void Synchronisable::setBacksync(bool sync){
    456     backsync_=sync;
    457   }
    458 
    459   bool Synchronisable::getBacksync(){
    460     return backsync_;
    461   }
     342  void Synchronisable::setObjectMode(int mode){
     343    assert(mode==0x1 || mode==0x2 || mode==0x3);
     344    objectMode_=mode;
     345  }
     346
    462347
    463348}
  • code/trunk/src/network/Synchronisable.h

    r1747 r1751  
    3434#include <list>
    3535#include "core/OrxonoxClass.h"
     36#include "util/XMLIncludes.h"
    3637#include "NetworkCallback.h"
    3738
     
    4344  };
    4445
    45   struct syncData{
    46     unsigned int length;
     46  struct synchronisableHeader{
     47    unsigned int size;
    4748    unsigned int objectID;
    4849    unsigned int classID;
    49     unsigned char *data;
    5050  };
    5151
     
    6767  class _NetworkExport Synchronisable : virtual public orxonox::OrxonoxClass{
    6868  public:
    69 
     69   
    7070    virtual ~Synchronisable();
    7171    unsigned int objectID;
     
    7373
    7474    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    75     //  syncData getData();
    76     syncData getData(unsigned char *mem, int mode=0x0);
    77     bool getData2(unsigned char*& men, int mode=0x0);
    78     //bool getData(Bytestream& bs, int mode=0x0);
    79     int getSize(int mode=0x0);
    80     int getSize2(int mode=0x0);
    81     bool updateData(syncData vars, int mode=0x0);
     75    bool getData(unsigned char*& men, unsigned int id, int mode=0x0);
     76    int getSize2(unsigned int id, int mode=0x0);
    8277    bool updateData(unsigned char*& mem, int mode=0x0);
    8378    bool isMyData(unsigned char* mem);
    84     void setBacksync(bool sync);
    85     bool getBacksync();
     79    void setObjectMode(int mode);
     80    void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; }
     81   
    8682    virtual void registerAllVariables()=0;
    8783    virtual bool create();
     
    9288    Synchronisable();
    9389  private:
    94     /*  bool removeObject(ObjectList<Synchronisable>::iterator it);*/
    95 
     90    int getSize(unsigned int id, int mode=0x0);
     91    bool isMyTick(unsigned int id);
    9692    std::list<synchronisableVariable *> *syncList;
    9793    int datasize;
    9894    static int state_; // detemines wheter we are server (default) or client
    9995    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
     96    unsigned int objectFrequency_;
     97    int objectMode_;
    10098  };
    10199}
  • code/trunk/src/network/packet/Acknowledgement.cc

    r1735 r1751  
    4545  data_=new unsigned char[ getSize() ];
    4646  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement;
    47   *(unsigned int *)&data_[ _ACKID ] = id;
     47  *(unsigned int *)(data_ + _ACKID ) = id;
    4848  clientID_=clientID;
    4949}
  • code/trunk/src/network/packet/Gamestate.cc

    r1747 r1751  
    4141namespace packet {
    4242
    43 
    44 #define GAMESTATE_START(data) data + sizeof(GamestateHeader)
     43#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
    4544#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
    4645#define HEADER GAMESTATE_HEADER(data_)
     
    6362{
    6463  int tempsize=0, currentsize=0;
    65   assert(data_==0 /*&& bs_==0*/);
    66   int size = calcGamestateSize(mode);
     64  assert(data_==0);
     65  int size = calcGamestateSize(id, mode);
    6766
    6867  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
    69     //retval->data = (unsigned char*)malloc(size);
    7068  if(size==0)
    7169    return false;
    7270  data_ = new unsigned char[size + sizeof(GamestateHeader)];
    73   //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size);
    7471  if(!data_){
    7572    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
     
    8279  orxonox::ObjectList<Synchronisable>::iterator it;
    8380  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
    84     tempsize=it->getSize2(mode);
     81    tempsize=it->getSize2(id, mode);
    8582
    8683    if(currentsize+tempsize > size){
     
    9087      int addsize=tempsize;
    9188      while(++temp)
    92         addsize+=temp->getSize2(mode);
     89        addsize+=temp->getSize2(id, mode);
    9390      data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
    9491      if(!data_)
     
    9794    }// stop allocate additional memory
    9895
    99     if(!it->getData2(mem, mode))
     96    if(!it->getData(mem, id, mode))
    10097      return false; // mem pointer gets automatically increased because of call by reference
    10198    // increase size counter by size of current synchronisable
     
    121118bool Gamestate::spreadData(int mode)
    122119{
    123   assert(data_ && !HEADER->compressed && !HEADER->diffed);
     120  assert(data_);
     121  assert(!HEADER->compressed);
     122  assert(!HEADER->diffed);
    124123  unsigned int size, objectID, classID;
    125124  unsigned char *mem=data_+sizeof(GamestateHeader);
     
    142141        //fabricate the new synchronisable
    143142        if(!Synchronisable::fabricate(mem, mode))
    144           /*return false*/;
     143          return false;
    145144        it=orxonox::ObjectList<Synchronisable>::end();
    146145      }
     
    175174}
    176175
     176bool Gamestate::operator==(packet::Gamestate gs){
     177  unsigned char *d1 = data_+sizeof(GamestateHeader);
     178  unsigned char *d2 = gs.data_+sizeof(GamestateHeader);
     179  assert(!isCompressed());
     180  assert(!gs.isCompressed());
     181  while(d1<data_+HEADER->normsize)
     182  {
     183    if(*d1!=*d2)
     184      return false;
     185    d1++;
     186    d2++;
     187  }
     188  return true;
     189}
     190
    177191bool Gamestate::process()
    178192{
     
    183197{
    184198  assert(HEADER);
    185   uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1;
     199  assert(!HEADER->compressed);
     200  uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
    186201  if(buffer==0)
    187202    return false;
     
    189204  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
    190205  unsigned char *dest = GAMESTATE_START(ndata);
     206  //unsigned char *dest = new unsigned char[buffer];
     207  unsigned char *source = GAMESTATE_START(data_);
    191208  int retval;
    192   retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );
     209  retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
    193210  switch ( retval ) {
    194211    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
    195     case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl;
    196     return false;
    197     case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
    198     return false;
    199     case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
    200     return false;
    201   }
     212    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
     213    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
     214    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
     215  }
     216#ifndef NDEBUG
     217  //decompress and compare the start and the decompressed data
     218  unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];
     219  unsigned char *d2 = GAMESTATE_START(rdata);
     220  uLongf length2 = HEADER->normsize;
     221  uncompress(d2, &length2, dest, buffer);
     222  for(unsigned int i=0; i<HEADER->normsize; i++){
     223    assert(*(source+i)==*(d2+i));
     224  }
     225  delete[] rdata;
     226#endif
    202227
    203228  //copy and modify header
    204   HEADER->compsize = buffer;
    205   HEADER->compressed = true;
     229#ifndef NDEBUG
     230  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize);
     231#endif
    206232  *GAMESTATE_HEADER(ndata) = *HEADER;
    207233  //delete old data
     
    209235  //save new data
    210236  data_ = ndata;
     237  HEADER->compsize = buffer;
     238  HEADER->compressed = true;
    211239  assert(HEADER->compressed);
    212240  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
     
    215243bool Gamestate::decompressData()
    216244{
     245  assert(HEADER);
    217246  assert(HEADER->compressed);
    218   //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
    219   int normsize = HEADER->normsize;
    220   int compsize = HEADER->compsize;
    221   int bufsize;
    222   if(normsize < compsize)
    223     bufsize = compsize;
    224   else
    225     bufsize = normsize;
    226   if(bufsize==0)
    227     return false;
     247  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;
     248  unsigned int normsize = HEADER->normsize;
     249  unsigned int compsize = HEADER->compsize;
     250  unsigned int bufsize;
     251  assert(compsize<=normsize);
     252  bufsize = normsize;
     253  assert(bufsize!=0);
    228254  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
    229255  unsigned char *dest = ndata + sizeof(GamestateHeader);
     256  unsigned char *source = data_ + sizeof(GamestateHeader);
    230257  int retval;
    231   uLongf length=normsize;
    232   retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );
     258  uLongf length=bufsize;
     259  retval = uncompress( dest, &length, source, (uLong)compsize );
    233260  switch ( retval ) {
    234261    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
     
    237264    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
    238265  }
    239 
    240   HEADER->compressed = false;
     266#ifndef NDEBUG
     267  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
     268#endif
     269 
    241270  //copy over the header
    242271  *GAMESTATE_HEADER(ndata) = *HEADER;
    243272  //delete old (compressed data)
    244273  delete[] data_;
    245   //set new pointers and create bytestream
     274  //set new pointers
    246275  data_ = ndata;
    247   //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize);
    248 
     276  HEADER->compressed = false;
     277  assert(HEADER->normsize==normsize);
     278  assert(HEADER->compsize==compsize);
    249279  return true;
    250280}
     
    252282Gamestate *Gamestate::diff(Gamestate *base)
    253283{
     284  assert(HEADER);
     285  assert(!HEADER->compressed);
     286  assert(!HEADER->diffed);
    254287  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    255288  unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
     
    277310  *GAMESTATE_HEADER(ndata) = *HEADER;
    278311  GAMESTATE_HEADER(ndata)->diffed = true;
    279   Gamestate *g = new Gamestate(ndata, 0);
     312  GAMESTATE_HEADER(ndata)->base_id = base->getID();
     313  Gamestate *g = new Gamestate(ndata, getClientID());
     314  g->flags_=flags_;
     315  g->packetDirection_ = packetDirection_;
    280316  return g;
    281317}
     
    283319Gamestate *Gamestate::undiff(Gamestate *base)
    284320{
    285   assert(this && base);
     321  assert(this && base);assert(HEADER);
     322  assert(HEADER->diffed);
    286323  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
    287324  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
     
    310347  *GAMESTATE_HEADER(ndata) = *HEADER;
    311348  GAMESTATE_HEADER(ndata)->diffed = false;
    312   Gamestate *g = new Gamestate(ndata, 0);
     349  Gamestate *g = new Gamestate(ndata, getClientID());
     350  g->flags_=flags_;
     351  g->packetDirection_ = packetDirection_;
     352  assert(!g->isDiffed());
     353  assert(!g->isCompressed());
    313354  return g;
    314355}
    315356
    316357
    317 unsigned int Gamestate::calcGamestateSize(int mode)
     358unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode)
    318359{
    319360  int size=0;
     
    322363    // get total size of gamestate
    323364  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
    324     size+=it->getSize2(mode); // size of the actual data of the synchronisable
     365    size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
    325366//  size+=sizeof(GamestateHeader);
    326367  return size;
     
    342383  }
    343384
     385  bool Gamestate::isCompressed(){
     386    return HEADER->compressed;
     387  }
     388 
    344389  int Gamestate::getBaseID(){
    345390    return HEADER->base_id;
  • code/trunk/src/network/packet/Gamestate.h

    r1747 r1751  
    2929#include "Packet.h"
    3030#include "network/Synchronisable.h"
    31 //#include "util/Bytestream.h"
     31#ifndef NDEBUG
     32#include "util/CRC32.h"
     33#endif
    3234#include "core/CoreIncludes.h"
    3335
     
    4850  bool complete; // wheter it is a complete gamestate or only partial
    4951  bool compressed;
     52#ifndef NDEBUG
     53  uint32_t crc32;
     54#endif
    5055};
    5156
     
    6469    int getID();
    6570    bool isDiffed();
     71    bool isCompressed();
    6672    int getBaseID();
    6773    Gamestate *diff(Gamestate *base);
     
    7480    virtual bool process();
    7581
    76 
     82    bool operator ==(packet::Gamestate gs);
    7783  private:
    78     unsigned int calcGamestateSize(int mode=0x0);
    79     void removeObject(orxonox::ObjectListIterator<Synchronisable> &it);
     84    unsigned int calcGamestateSize(unsigned int id, int mode=0x0);
     85    void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it);
    8086
    8187
  • code/trunk/src/network/packet/Packet.cc

    r1735 r1751  
    9292
    9393Packet::~Packet(){
    94   if(enetPacket_)
     94  if(enetPacket_){
     95    assert(enetPacket_->freeCallback==0);
    9596    enet_packet_destroy(enetPacket_);
     97  }
    9698  if(data_)
    9799    delete[] data_;
     
    109111    }
    110112    enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
    111     //enetPacket_->freeCallback = &Packet::deletePacket;
    112     enetPacket_->freeCallback = &blub;
     113    enetPacket_->freeCallback = &Packet::deletePacket;
     114//     enetPacket_->freeCallback = &blub;
    113115    packetMap_[enetPacket_] = this;
    114116  }
    115   /*switch( *(ENUM::Type *)(data_ + _PACKETID) )
     117#ifndef NDEBUG
     118  switch( *(ENUM::Type *)(data_ + _PACKETID) )
    116119  {
    117120    case ENUM::Acknowledgement:
     
    120123    case ENUM::Gamestate:
    121124    case ENUM::Welcome:
    122       COUT(3) << "welcome" << std::endl;
    123       p = new Welcome( data, clientID );
     125      break;
    124126    default:
    125       assert(0); //TODO: repair this
     127      assert(0); //there was some error, if this is the case
    126128      break;
    127   }*/
    128   network::Host::addPacket( enetPacket_, clientID_);
     129  }
     130#endif
     131  ENetPacket *temp = enetPacket_;
    129132  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
     133  network::Host::addPacket( temp, clientID_);
    130134  return true;
    131135}
     
    168172void Packet::deletePacket(ENetPacket *packet){
    169173  assert(packetMap_[packet]);
     174  assert(packetMap_[packet]->enetPacket_==0);
    170175  delete packetMap_[packet];
    171176}
  • code/trunk/src/network/packet/Packet.h

    r1735 r1751  
    7878    enet_uint32 flags_;
    7979    int clientID_;
     80    ENUM::Direction packetDirection_;
    8081    unsigned char *data_;
    8182  private:
    8283    static std::map<ENetPacket *, Packet *> packetMap_;
    8384    ENetPacket *enetPacket_;
    84     ENUM::Direction packetDirection_;
    8585};
    8686
  • code/trunk/src/network/packet/Welcome.cc

    r1735 r1751  
    3232#include "Welcome.h"
    3333#include "network/Host.h"
     34#include "network/Synchronisable.h"
    3435#include "core/CoreIncludes.h"
    3536#include <assert.h>
     
    7980  Host::setShipID(shipID);
    8081  COUT(3) << "Welcome set clientId: " << clientID << " shipID: " << shipID << std::endl;
     82  Synchronisable::setClient(true);
    8183  delete this;
    8284  return true;
Note: See TracChangeset for help on using the changeset viewer.