Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 12, 2008, 7:40:47 PM (16 years ago)
Author:
scheusso
Message:

merged network branch back to trunk

Location:
code/trunk/src/network
Files:
27 edited
2 copied

Legend:

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

    r1740 r1907  
    1717  packet/Gamestate.cc
    1818  packet/Welcome.cc
     19  packet/DeleteObjects.cc
    1920)
    2021
  • code/trunk/src/network/Client.cc

    r1769 r1907  
    4343#include "Synchronisable.h"
    4444#include "core/CoreIncludes.h"
    45 #include "core/ConsoleCommand.h"
    4645#include "packet/Packet.h"
    4746// #include "packet/Acknowledgement.h"
     
    6160    isSynched_=false;
    6261    gameStateFailure_=false;
    63     isServer_ = false;
    6462  }
    6563
     
    7371    isSynched_=false;
    7472    gameStateFailure_=false;
    75     isServer_ = false;
    7673  }
    7774
     
    8582    isSynched_=false;
    8683    gameStateFailure_=false;
    87     isServer_ = false;
    8884  }
    8985
     
    118114  }
    119115
    120   bool Client::processChat(packet::Chat *message, unsigned int clientID){
    121     return message->process();
     116  bool Client::processChat(std::string message, unsigned int playerID){
     117    COUT(1) << "Player " << playerID << ": " << message << std::endl;
     118    return true;
    122119  }
    123 
    124   /*bool Client::sendChat(packet::Chat *chat){
    125     chat->process();
    126     packet::Packet *p = new packet::Packet(chat);
    127     return p->send();
    128   }*/
     120 
     121  /**
     122   * This function implements the method of sending a chat message to the server
     123   * @param message message to be sent
     124   * @return result(true/false)
     125   */
     126  bool Client::chat(std::string message){
     127    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
     128    return m->send();
     129  }
    129130
    130131
    131132  /**
    132   * submits a chat message to the server
    133   * @param message message to send
    134   * @return true/false
    135   */
    136   bool Client::sendChat( std::string message ){
    137     // generate packet and add it to queue
    138     if(!isConnected)
    139       return false;
    140     packet::Chat chat(message, 0);
    141     return chat.send();
    142     // send packets
    143   }
    144 
    145   /**
    146   * Performs a GameState update
    147   */
     133   * Processes incoming packets, sends a gamestate to the server and does the cleanup
     134   * @param time
     135   */
    148136  void Client::tick(float time){
    149137//     COUT(3) << ".";
     
    164152      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
    165153      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
    166       assert(packet->process());
     154      bool b = packet->process();
     155      assert(b);
    167156    }
    168157    if(gamestate.processGamestates())
     
    170159      if(!isSynched_)
    171160        isSynched_=true;
    172     }else
    173       COUT(3) << "gamestate has not been processed sucessfully" << std::endl;
     161    }
    174162    gamestate.cleanup();
    175163    return;
  • code/trunk/src/network/Client.h

    r1735 r1907  
    7373    bool closeConnection();
    7474    bool queuePacket(ENetPacket *packet, int clientID);
    75     bool processChat(packet::Chat *message, unsigned int clientID);
     75    bool processChat(std::string message, unsigned int playerID);
     76    virtual bool chat(std::string message);
    7677    //bool sendChat(packet::Chat *chat);
    7778   
    7879//    static void Chat( std::string message );
    7980   
    80     unsigned int shipID(){return shipID_;}
    81     int playerID(){return clientID_;}
    8281    //static void setShipID( unsigned int shipID){ dynamic_cast<Client *>(instance_)->shipID_=shipID; }
    8382    static void setClientID( unsigned int clientID){ dynamic_cast<Client *>(instance_)->clientID_=clientID; }
     
    8685
    8786  private:
     87    virtual bool isServer_(){return false;}
    8888   
    8989    ClientConnection client_connection;
     
    9191    bool isConnected;
    9292    bool isSynched_;
    93 
    94     bool sendChat( std::string message );
    9593   
    96     // implement data processing functions of PacketDecoder
    97 //     void processChat( chat *data, int clientId );
    98     int clientID_;     // this is the id the server gave to us
    99     int shipID_;
    10094    bool gameStateFailure_;
    10195  };
  • code/trunk/src/network/ClientConnection.cc

    r1755 r1907  
    7777  }
    7878
    79 
    80   /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
    81     if(!buffer.isEmpty()) {
    82       //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
    83       return buffer.pop(address);
    84     }
    85     else{
    86       return NULL;
    87     }
    88   }
    89 
    90   ENetPacket *ClientConnection::getPacket() {
    91     ENetAddress address; //sems that address is not needed
    92     return getPacket(address);
    93   }*/
     79  ClientConnection::~ClientConnection(){
     80    if(established)
     81      closeConnection();
     82  }
    9483
    9584  ENetEvent *ClientConnection::getEvent(){
  • code/trunk/src/network/ClientConnection.h

    r1785 r1907  
    6464    ClientConnection(int port, std::string address);
    6565    ClientConnection(int port, const char* address);
    66     //ENetPacket *getPacket(ENetAddress &address); // thread1
    67     //ENetPacket *getPacket(); // thread1
     66    ~ClientConnection();
    6867    ENetEvent *getEvent();
    6968    // check wheter the packet queue is empty
  • code/trunk/src/network/ConnectionManager.cc

    r1856 r1907  
    8181  boost::recursive_mutex ConnectionManager::enet_mutex;
    8282
    83 //   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
    84 //     assert(instance_==0);
    85 //     instance_=this;
    86 //     quit=false;
    87 //     bindAddress.host = ENET_HOST_ANY;
    88 //     bindAddress.port = NETWORK_PORT;
    89 //   }
    90 
    9183  ConnectionManager::ConnectionManager(int port){
    9284    assert(instance_==0);
     
    114106
    115107  ConnectionManager::~ConnectionManager(){
    116     instance_=0;
    117108    if(!quit)
    118109      quitListener();
    119   }
    120 
    121   /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
    122     if(!buffer.isEmpty())
    123       return buffer.pop(address);
    124     else
    125       return NULL;
    126   }*/
    127 /**
    128 This function only pops the first element in PacketBuffer (first in first out)
    129 used by processQueue in Server.cc
    130 */
    131   /*ENetPacket *ConnectionManager::getPacket(int &clientID) {
    132     ENetAddress address;
    133     ENetPacket *packet=getPacket(address);
    134     ClientInformation *temp =head_->findClient(&address);
    135     if(!temp)
    136       return NULL;
    137     clientID=temp->getID();
    138     return packet;
    139   }*/
     110    instance_=0;
     111  }
     112
    140113
    141114  ENetEvent *ConnectionManager::getEvent(){
     
    164137  }
    165138
    166 //   bool ConnectionManager::addPacket(Packet::Packet *packet){
    167 //     ClientInformation *temp = instance_->head_->findClient(packet->getClientID());
    168 //     if(!temp){
    169 //       COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
    170 //       return false;
    171 //     }
    172 //     ENetPacket *packet = new ENetPacket;
    173 //     //  TODO: finish implementation
    174 //   }
    175 //
    176139
    177140  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
     
    373336    orxonox::ObjectList<orxonox::SpaceShip>::iterator it;
    374337    for(it = orxonox::ObjectList<orxonox::SpaceShip>::begin(); it; ++it){
    375       if(it->objectID!=id)
     338      if(it->getObjectID()!=id)
    376339        continue;
    377340      delete *it;
  • code/trunk/src/network/GamestateClient.cc

    r1769 r1907  
    156156    if(myShip_){
    157157      //      unsigned char *data = new unsigned char[myShip_->getSize()];
    158       int size=myShip_->getSize2(0, 0x1);
     158      int size=myShip_->getSize(0, 0x1);
    159159      if(size==0)
    160160        return false;
     
    182182  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
    183183    if(gs->isCompressed())
    184       assert(gs->decompressData());
     184    {
     185      bool b = gs->decompressData();
     186      assert(b);
     187    }
    185188    if(gs->isDiffed()){
    186189      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
     
    189192        return 0;
    190193      }
    191 //      assert(base); //TODO: fix this
     194//       assert(base); //TODO: fix this
    192195      packet::Gamestate *undiffed = gs->undiff(base);
    193196      delete gs;
  • code/trunk/src/network/GamestateManager.cc

    r1769 r1907  
    6161
    6262  bool GamestateManager::update(){
    63     cleanup();
     63//     cleanup();
    6464    return getSnapshot();
    6565  }
     
    9292    reference = new packet::Gamestate();
    9393    reference->collectData(++id_);
    94     COUT(4) << "inserting gamestate: " << reference << std::endl;
    95     gamestateMap.insert(std::pair<int, packet::Gamestate*>(id_, reference));
    96     gamestateUsed[id_]=0;
     94    //COUT(4) << "inserting gamestate: " << reference << std::endl;
     95    //gamestateMap_.insert(std::pair<int, packet::Gamestate*>(id_, reference));
     96//     gamestateUsed[id_]=0;
    9797    return true;
    9898  }
     
    104104   *
    105105   */
    106   void GamestateManager::cleanup(){
     106/*  void GamestateManager::cleanup(){
    107107    std::map<int,int>::iterator it = gamestateUsed.begin();
    108108    while(it!=gamestateUsed.end()){
     
    124124      it++;
    125125    }
    126   }
     126  }*/
    127127
    128128  packet::Gamestate *GamestateManager::popGameState(int clientID) {
     
    131131    packet::Gamestate *gs;
    132132    int gID = ClientInformation::findClient(clientID)->getGamestateID();
    133     //COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
     133    gs = reference->doSelection(clientID);
     134//     gs = new packet::Gamestate(*reference);
     135//     gs = new packet::Gamestate(*reference);
     136    // save the (undiffed) gamestate in the clients gamestate map
     137    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
    134138    //chose wheather the next gamestate is the first or not
     139    packet::Gamestate *client=NULL;
    135140    if(gID != GAMESTATEID_INITIAL){
    136       packet::Gamestate *client=NULL;
    137       std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID);
    138       if(it!=gamestateMap.end())
    139         client = it->second;
    140       if(client)
    141         gs = reference->diff(client);
    142       else
    143         gs = new packet::Gamestate(*reference);
    144     } else {
    145       COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
    146       gs = new packet::Gamestate(*reference);
    147     }
    148     assert(gs->compressData());
     141      std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
     142      if(clientMap!=gamestateMap_.end()){
     143        std::map<int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
     144        if(it!=clientMap->second.end())
     145          client = it->second;
     146      }
     147    }
     148    if(client){
     149//       COUT(3) << "diffing" << std::endl;
     150      gs = gs->diff(client);
     151    }
     152    else{
     153//       COUT(3) << "not diffing" << std::endl;
     154      gs = new packet::Gamestate(*gs);
     155    }
     156    bool b = gs->compressData();
     157    assert(b);
    149158    return gs;
    150159  }
     
    153162  bool GamestateManager::ack(int gamestateID, int clientID) {
    154163    ClientInformation *temp = ClientInformation::findClient(clientID);
    155     if(temp==0)
    156       return false;
     164    assert(temp);
    157165    int curid = temp->getGamestateID();
    158166   
    159167    if(gamestateID == 0){
    160168      temp->setGamestateID(GAMESTATEID_INITIAL);
    161       if(curid!=GAMESTATEID_INITIAL){
    162         assert(gamestateUsed.find(curid)!=gamestateUsed.end());
    163         --(gamestateUsed.find(curid)->second);
    164       }
    165169      return true;
    166170    }
    167     //if(curid > gamestateID)
     171   
    168172    assert(curid<gamestateID);
    169       // the network packets got messed up
    170       //return true;
    171173    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
    172     // decrease usage of gamestate and save it
    173 //     deleteUnusedGameState(curid);
    174     //increase gamestateused
    175     std::map<int, int>::iterator it = gamestateUsed.find(curid);
    176     if(curid!=GAMESTATEID_INITIAL){
    177       if(it!=gamestateUsed.end())
    178         --(it->second);
    179     }
    180     it = gamestateUsed.find(gamestateID);
    181     if(it!=gamestateUsed.end()){
    182       ++(it->second);
    183       temp->setGamestateID(gamestateID);
    184     }
     174    std::map<int, packet::Gamestate*>::iterator it, tempit;
     175    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
     176      delete it->second;
     177      tempit=it++;
     178      gamestateMap_[clientID].erase(tempit);
     179    }
     180    temp->setGamestateID(gamestateID);
    185181    return true;
    186182  }
    187183
    188184  void GamestateManager::removeClient(ClientInformation* client){
    189     if(!client)
    190       return;
    191     if(client->getGamestateID()>=0)
    192       gamestateUsed[client->getGamestateID()]--;
     185    assert(client);
     186    std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
     187    // first delete all remained gamestates
     188    std::map<int, packet::Gamestate*>::iterator it;
     189    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
     190      delete it->second;
     191    // now delete the clients gamestatemap
     192    gamestateMap_.erase(clientMap);
    193193  }
    194194 
    195195  bool GamestateManager::processGamestate(packet::Gamestate *gs){
    196196    if(gs->isCompressed())
    197        assert(gs->decompressData());
     197    {
     198       bool b = gs->decompressData();
     199       assert(b);
     200    }
    198201    assert(!gs->isDiffed());
    199202    return gs->spreadData();
  • code/trunk/src/network/GamestateManager.h

    r1785 r1907  
    8181    void removeClient(ClientInformation *client);
    8282    private:
    83     void cleanup(); // "garbage handler"
     83//     void cleanup(); // "garbage handler"
    8484    bool processGamestate(packet::Gamestate *gs);
    8585
    86     std::map<int, packet::Gamestate*> gamestateMap; //map gsID to gamestate*
    87     std::map<int, int> gamestateUsed; // save the number of clients, that use the specific gamestate
     86    std::map<unsigned int, std::map<int, packet::Gamestate*> > gamestateMap_;
     87    //std::map<int, packet::Gamestate*> gamestateMap; //map gsID to gamestate*
     88    //std::map<int, int> gamestateUsed; // save the number of clients, that use the specific gamestate
    8889    std::map<int, packet::Gamestate*> gamestateQueue;
    8990    packet::Gamestate *reference;
  • code/trunk/src/network/Host.cc

    r1751 r1907  
    3030
    3131#include "Host.h"
     32#include "core/ConsoleCommand.h"
    3233#include "packet/Packet.h"
    3334
    3435namespace network {
    3536
     37SetConsoleCommandShortcut(Host, Chat);
     38
    3639Host *Host::instance_=0;
    3740 
     41/**
     42 * @brief Constructor: assures that only one reference will be created and sets the pointer
     43 */
    3844Host::Host()
    3945{
     46  clientID_=0;
    4047  assert(instance_==0);
    4148  instance_=this;
     
    4350
    4451
     52/**
     53 * @brief Destructor: resets the instance pointer to 0
     54 */
    4555Host::~Host()
    4656{
     
    4858}
    4959
     60/**
     61 * This function is used to add an enetpacket to be sent to another peer
     62 * @param packet Packet to be added
     63 * @param clientID ID of the client the packet should be sent to
     64 * @return success?
     65 */
    5066bool Host::addPacket(ENetPacket *packet, int clientID){
    5167  if(instance_)
     
    7086// }
    7187
    72 int Host::getPlayerID(){
     88/**
     89 * This function returns the ID of the player
     90 * @return playerID
     91 */
     92unsigned int Host::getPlayerID(){
    7393  if(!instance_)
    7494    return 0;
    75   return instance_->playerID();
     95  return instance_->clientID_;
    7696}
    7797
    78 // unsigned int Host::getShipID(){
    79 //   if(!instance_)
    80 //     assert(0);
    81 //   return instance_->shipID();
    82 // }
     98bool Host::Chat(std::string message){
     99  if(!instance_)
     100    return false;
     101  return instance_->chat(message);
     102}
     103
     104bool Host::incomingChat(std::string message, unsigned int playerID){
     105  return instance_->processChat(message, playerID);
     106}
    83107
    84108}//namespace network
  • code/trunk/src/network/Host.h

    r1751 r1907  
    3737
    3838/**
    39         @author Oliver Scheuss
     39*       @brief Base class of Server and Client
     40*       This is the Base class of the Server and Client classes
     41*       - Makes server and client a singleton
     42*       - defines static functions available on both server and client
     43*       - is the interface to be used when communicating with the network
     44*       @author Oliver Scheuss
    4045*/
    4146class Host{
     
    4550    //virtual bool sendChat(packet::Chat *chat)=0;
    4651    virtual bool queuePacket(ENetPacket *packet, int clientID)=0;
    47     virtual unsigned int shipID()=0;
    48     virtual int playerID()=0;
     52    virtual bool chat(std::string message)=0;
     53    virtual bool processChat(std::string message, unsigned int playerID)=0;
     54    virtual bool isServer_()=0;
    4955
    5056
     
    5460    virtual ~Host();
    5561    static Host *instance_;
    56     bool isServer_;     
     62    unsigned int clientID_;
     63    unsigned int shipID_;
    5764
    5865  public:
     
    6168    //static bool chat(std::string& message);
    6269//     static bool receiveChat(packet::Chat *message, unsigned int clientID);
    63     static int getPlayerID();
     70    static unsigned int getPlayerID();
    6471    static unsigned int getShipID(){return instance_->shipID_;}
    6572    static void setClientID(unsigned int id){ instance_->clientID_ = id; }
    6673    static void setShipID(unsigned int id){ instance_->shipID_ = id; }
    67     static bool isServer(){ return instance_->isServer_; }             
     74    static bool isServer(){ return instance_->isServer_(); }           
     75    static bool Chat(std::string message);
     76    static bool incomingChat(std::string message, unsigned int playerID);
    6877  private:
    69     unsigned int clientID_;
    70     unsigned int shipID_;
    7178};
    7279
  • code/trunk/src/network/Server.cc

    r1856 r1907  
    5656#include "packet/Packet.h"
    5757#include "packet/Welcome.h"
     58#include "packet/DeleteObjects.h"
    5859#include <util/Convert.h>
    5960
     
    7172    connection = new ConnectionManager();
    7273    gamestates_ = new GamestateManager();
    73     isServer_ = true;
    7474  }
    7575
     
    7878    connection = new ConnectionManager(port);
    7979    gamestates_ = new GamestateManager();
    80     isServer_ = true;
    8180  }
    8281
     
    9089    connection = new ConnectionManager(port, bindAddress);
    9190    gamestates_ = new GamestateManager();
    92     isServer_ = true;
    9391  }
    9492
     
    102100    connection = new ConnectionManager(port, bindAddress);
    103101    gamestates_ = new GamestateManager();
    104     isServer_ = true;
     102  }
     103 
     104  /**
     105  * @brief Destructor
     106  */
     107  Server::~Server(){
     108    if(connection)
     109      delete connection;
     110    if(gamestates_)
     111      delete gamestates_;
    105112  }
    106113
     
    121128  }
    122129
    123   bool Server::processChat(packet::Chat *message, unsigned int clientID){
     130  bool Server::processChat(std::string message, unsigned int playerID){
    124131    ClientInformation *temp = ClientInformation::getBegin();
     132    packet::Chat *chat;
    125133    while(temp){
    126       message->setClientID(temp->getID());
    127       if(!message->send())
    128         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    129       temp = temp->next();
    130     }
    131     return message->process();
    132   }
    133 
    134   /**
    135   * This function sends out a message to all clients
    136   * @param msg message
    137   * @return true/false
    138   */
    139   bool Server::sendChat(packet::Chat *chat) {
    140     //TODO: change this (no informations about who wrote a message)
    141     assert(0);
    142     ClientInformation *temp = ClientInformation::getBegin();
    143     while(temp){
     134      chat = new packet::Chat(message, playerID);
    144135      chat->setClientID(temp->getID());
    145136      if(!chat->send())
    146137        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    147     }
    148     return chat->process();;
    149   }
    150 
    151   /**
    152   * This function sends out a message to all clients
    153   * @param msg message
    154   * @return true/false
    155   */
    156 //   bool Server::sendChat(const char *msg) {
    157 //     char *message = new char [strlen(msg)+10+1];
    158 //     sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
    159 //     COUT(1) << message << std::endl;
    160 //     ENetPacket *packet = packet_gen.chatMessage(message);
    161 //     COUT(5) <<"Server: adding Packets" << std::endl;
    162 //     return connection->addPacketAll(packet);
    163 //   }
     138      temp = temp->next();
     139    }
     140    COUT(1) << "Player " << playerID << ": " << message << std::endl;
     141    return true;
     142  }
     143
    164144
    165145  /**
     
    174154    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
    175155      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
    176 //      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;
    177156      gamestates_->processGamestates();
    178157      updateGamestate();
    179158    }
    180     /*while(timeSinceLastUpdate_>1./NETWORK_FREQUENCY)
    181       timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;*/
    182 //     usleep(5000); // TODO remove
    183     return;
    184159  }
    185160
     
    230205    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    231206    sendGameState();
     207    sendObjectDeletes();
    232208    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
    233209    //std::cout << "sent gamestate" << std::endl;
     
    276252      // gs gets automatically deleted by enet callback
    277253    }
    278     /*if(added) {
    279       //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
    280       return connection->sendPackets();
    281     }*/
    282     //COUT(5) << "Server: had no gamestates to send" << std::endl;
    283     return true;
    284   }
    285 
    286 //   void Server::processChat( chat *data, int clientId){
    287 //     char *message = new char [strlen(data->message)+10+1];
    288 //     sprintf(message, "Player %d: %s", clientId, data->message);
    289 //     COUT(1) << message << std::endl;
    290 //     ENetPacket *pck = packet_gen.chatMessage(message);
    291 //     connection->addPacketAll(pck);
    292 //     delete[] data->message;
    293 //     delete data;
    294 //   }
     254    return true;
     255  }
     256
     257  bool Server::sendObjectDeletes(){
     258    ClientInformation *temp = ClientInformation::getBegin();
     259    packet::DeleteObjects *del = new packet::DeleteObjects();
     260    if(!del->fetchIDs())
     261      return true;  //everything ok (no deletes this tick)
     262//     COUT(3) << "sending DeleteObjects" << std::endl;
     263    while(temp != NULL){
     264      if( !(temp->getSynched()) ){
     265        COUT(5) << "Server: not sending gamestate" << std::endl;
     266        temp=temp->next();
     267        continue;
     268      }
     269      int cid = temp->getID(); //get client id
     270      packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
     271      assert(cd);
     272      cd->setClientID(cid);
     273      if ( !cd->send() )
     274        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
     275      temp=temp->next();
     276      // gs gets automatically deleted by enet callback
     277    }
     278    return true;
     279  }
     280
    295281
    296282  bool Server::addClient(ENetEvent *event){
     
    328314    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
    329315    w->setClientID(temp->getID());
    330     assert(w->send());
     316    bool b = w->send();
     317    assert(b);
    331318    packet::Gamestate *g = new packet::Gamestate();
    332319    g->setClientID(temp->getID());
    333     assert(g->collectData(0));
    334     assert(g->compressData());
    335     assert(g->send());
     320    b = g->collectData(0);
     321    assert(b);
     322    b = g->compressData();
     323    assert(b);
     324    b = g->send();
     325    assert(b);
    336326    return true;
    337327  }
     
    347337    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
    348338    no->classID = id->getNetworkID();
    349     client->setShipID(no->objectID);
     339    client->setShipID(no->getObjectID());
    350340    no->setPosition(orxonox::Vector3(0,0,80));
    351341    no->setScale(10);
     
    376366    gamestates_->removeClient(client);
    377367    while(it){
    378       if(it->objectID!=client->getShipID()){
     368      if(it->getObjectID()!=client->getShipID()){
    379369        ++it;
    380370        continue;
     
    397387    gamestates_->removeClient(client);
    398388  }
     389 
     390  bool Server::chat(std::string message){
     391    ClientInformation *temp = ClientInformation::getBegin();
     392    packet::Chat *chat;
     393    while(temp){
     394      chat = new packet::Chat(message, Host::getPlayerID());
     395      chat->setClientID(temp->getID());
     396      if(!chat->send())
     397        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     398      temp = temp->next();
     399    }
     400    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
     401    return true;
     402  }
    399403
    400404}
  • code/trunk/src/network/Server.h

    r1785 r1907  
    6363    Server(int port, std::string bindAddress);
    6464    Server(int port, const char *bindAddress);
     65    ~Server();
    6566   
    6667    void open();
    6768    void close();
    68     bool processChat(packet::Chat *message, unsigned int clientID);
    69     bool sendChat(packet::Chat *chat);
     69    bool processChat(std::string message, unsigned int playerID);
    7070    bool queuePacket(ENetPacket *packet, int clientID);
    7171    void tick(float time);
     
    7474    void updateGamestate();
    7575  private:
     76    virtual bool isServer_(){return true;}
    7677    unsigned int shipID(){return 0;}
    77     int playerID(){return 0;}
     78    unsigned int playerID(){return 0;}
    7879   
    7980    bool addClient(ENetEvent *event);
     
    8586    bool processPacket( ENetPacket *packet, ENetPeer *peer );
    8687    bool sendGameState();
    87    
     88    bool sendObjectDeletes();
     89    virtual bool chat(std::string message);
    8890   
    8991    //void processChat( chat *data, int clientId);
  • code/trunk/src/network/Synchronisable.cc

    r1856 r1907  
    5151namespace network
    5252{
    53 
     53 
     54
     55  std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_;
     56  std::queue<unsigned int> Synchronisable::deletedObjects_;
    5457
    5558  int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
     
    5760  /**
    5861  * Constructor:
    59   * calls registarAllVariables, that has to be implemented by the inheriting classID
     62  * Initializes all Variables and sets the right objectID
    6063  */
    6164  Synchronisable::Synchronisable(){
    6265    RegisterRootObject(Synchronisable);
    63     static int idCounter=0;
    64     datasize=0;
     66    static uint32_t idCounter=0;
    6567    objectFrequency_=1;
    66     objectMode_=0x1; // by default do not send data to servere
     68    objectMode_=0x1; // by default do not send data to server
    6769    objectID=idCounter++;
    6870    syncList = new std::list<synchronisableVariable *>;
    69     //registerAllVariables();
    70   }
    71 
     71  }
     72
     73  /**
     74   * Destructor:
     75   * Delete all callback objects and remove objectID from the objectMap_
     76   */
    7277  Synchronisable::~Synchronisable(){
    7378    // delete callback function objects
    74     if(!orxonox::Identifier::isCreatingHierarchy())
     79    if(!orxonox::Identifier::isCreatingHierarchy()){
    7580      for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++)
    7681        delete (*it)->callback;
    77   }
    78 
     82      deletedObjects_.push(objectID);
     83//       COUT(3) << "destruct synchronisable +++" << objectID << " | " << classID << std::endl;
     84//       COUT(3) << " bump ---" << objectID << " | " << &objectMap_ << std::endl;
     85//       assert(objectMap_[objectID]->objectID==objectID);
     86//       objectMap_.erase(objectID);
     87    }
     88  }
     89
     90  /**
     91   * This function gets called after all neccessary data has been passed to the object
     92   * Overload this function and recall the create function of the parent class
     93   * @return true/false
     94   */
    7995  bool Synchronisable::create(){
    8096    this->classID = this->getIdentifier()->getNetworkID();
    81     COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
     97//     COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
     98   
     99//     COUT(3) << "construct synchronisable +++" << objectID << " | " << classID << std::endl;
     100//     objectMap_[objectID]=this;
     101//     assert(objectMap_[objectID]==this);
     102//     assert(objectMap_[objectID]->objectID==objectID);
    82103    return true;
    83104  }
    84105
    85106
     107  /**
     108   * This function sets the internal mode for synchronisation
     109   * @param b true if this object is located on a client or on a server
     110   */
    86111  void Synchronisable::setClient(bool b){
    87112    if(b) // client
     
    91116  }
    92117
    93   bool Synchronisable::fabricate(unsigned char*& mem, int mode)
     118  /**
     119   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
     120   * After calling this function the mem pointer will be increased by the size of the needed data
     121   * @param mem pointer to where the appropriate data is located
     122   * @param mode defines the mode, how the data should be loaded
     123   * @return pointer to the newly created synchronisable
     124   */
     125  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, int mode)
    94126  {
    95     unsigned int size, objectID, classID;
    96     size = *(unsigned int *)mem;
    97     objectID = *(unsigned int*)(mem+sizeof(unsigned int));
    98     classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
    99 
    100     if(size==3*sizeof(unsigned int)){ //not our turn, dont do anything
    101       mem+=3*sizeof(unsigned int);
    102       return true;
    103     }
    104 
    105     orxonox::Identifier* id = ClassByID(classID);
    106     if(!id){
    107       COUT(3) << "We could not identify a new object; classid: " << classID << " uint: " << (unsigned int)classID << " objectID: " << objectID << " size: " << size << std::endl;
    108       assert(0);
    109       return false; // most probably the gamestate is corrupted
    110     }
     127    synchronisableHeader *header = (synchronisableHeader *)mem;
     128
     129    COUT(3) << "fabricating object with id: " << header->objectID << std::endl;
     130
     131    orxonox::Identifier* id = ClassByID(header->classID);
     132    assert(id);
    111133    orxonox::BaseObject *bo = id->fabricate();
    112134    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
    113135    assert(no);
    114     no->objectID=objectID;
    115     no->classID=classID;
     136    no->objectID=header->objectID;
     137    no->classID=header->classID;
    116138    COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
    117139          // update data and create object/entity...
    118     if( !no->updateData(mem, mode) ){
    119       COUT(1) << "We couldn't update the object: " << objectID << std::endl;
     140    bool b = no->updateData(mem, mode);
     141    assert(b);
     142    b = no->create();
     143    assert(b);
     144    return no;
     145  }
     146
     147 
     148  /**
     149   * Finds and deletes the Synchronisable with the appropriate objectID
     150   * @param objectID objectID of the Synchronisable
     151   * @return true/false
     152   */
     153  bool Synchronisable::deleteObject(unsigned int objectID){
     154//     assert(getSynchronisable(objectID));
     155    if(!getSynchronisable(objectID))
    120156      return false;
    121     }
    122     if( !no->create() )
    123     {
    124       COUT(1) << "We couldn't manifest (create() ) the object: " << objectID << std::endl;
     157    assert(getSynchronisable(objectID)->objectID==objectID);
     158//     delete objectMap_[objectID];
     159    Synchronisable *s = getSynchronisable(objectID);
     160    if(s)
     161      delete s;
     162    else
    125163      return false;
    126     }
    127164    return true;
    128165  }
    129 
     166 
     167  /**
     168   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
     169   * @param objectID objectID of the Synchronisable
     170   * @return pointer to the Synchronisable with the objectID
     171   */
     172  Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){
     173    orxonox::ObjectList<Synchronisable>::iterator it;
     174    for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
     175      if( it->getObjectID()==objectID )
     176           return *it;
     177    }
     178    return NULL;
     179
     180//     std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID);
     181//     if(i==objectMap_.end())
     182//       return NULL;
     183//     assert(i->second->objectID==objectID);
     184//     return (*i).second;
     185  }
     186
     187 
    130188  /**
    131189  * This function is used to register a variable to be synchronized
     
    133191  * @param var pointer to the variable
    134192  * @param size size of the datatype the variable consists of
     193  * @param t the type of the variable (network::DATA or network::STRING
     194  * @param mode same as in getData
     195  * @param cb callback object that should get called, if the value of the variable changes
    135196  */
    136197  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
     
    143204    temp->callback = cb;
    144205    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
    145     // increase datasize
    146     datasize+=sizeof(int)+size;
    147206    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
    148207    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
    149208    syncList->push_back(temp);
    150   }
    151 
    152   /**
    153    * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
    154    * Difference to the above function:
     209#ifndef NDEBUG
     210    std::list<synchronisableVariable *>::iterator it = syncList->begin();
     211    while(it!=syncList->end()){
     212      assert(*it!=var);
     213      it++;
     214    }
     215#endif
     216  }
     217
     218  /**
     219   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
    155220   * takes a pointer to already allocated memory (must have at least getSize bytes length)
    156221   * structure of the bitstream:
    157    * (var1_size,var1,var2_size,var2,...)
    158    * varx_size: size = sizeof(int)
    159    * varx: size = varx_size
    160    * @return data containing all variables and their sizes
    161    */
    162   bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){
     222   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
     223   * length of varx: size saved int syncvarlist
     224   * @param mem pointer to allocated memory with enough size
     225   * @param id gamestateid of the gamestate to be saved (important for priorities)
     226   * @param mode defines the direction in which the data will be send/received
     227   *             0x1: server->client
     228   *             0x2: client->server (not recommended)
     229   *             0x3: bidirectional
     230   * @return true: if !isMyTick or if everything was successfully saved
     231   */
     232  bool Synchronisable::getData(uint8_t*& mem, unsigned int id, int mode){
     233    //if this tick is we dont synchronise, then abort now
     234    if(!isMyTick(id))
     235      return true;
    163236    //std::cout << "inside getData" << std::endl;
    164237    unsigned int tempsize = 0;
     
    167240    if(classID==0)
    168241      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
    169     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
     242    assert(this->classID==this->getIdentifier()->getNetworkID());
     243//     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
    170244    std::list<synchronisableVariable *>::iterator i;
    171245    unsigned int size;
    172     size=getSize2(id, mode);
     246    size=getSize(id, mode);
    173247
    174248    // start copy header
    175     memcpy(mem, &size, sizeof(unsigned int));
    176     mem+=sizeof(unsigned int);
    177     memcpy(mem, &(this->objectID), sizeof(unsigned int));
    178     mem+=sizeof(unsigned int);
    179     memcpy(mem, &(this->classID), sizeof(unsigned int));
    180     mem+=sizeof(unsigned int);
    181     tempsize+=12;
     249    synchronisableHeader *header = (synchronisableHeader *)mem;
     250    header->size = size;
     251    header->objectID = this->objectID;
     252    header->classID = this->classID;
     253    header->dataAvailable = true;
     254    tempsize+=sizeof(synchronisableHeader);
     255    mem+=sizeof(synchronisableHeader);
    182256    // end copy header
    183257
    184     //if this tick is we dont synchronise, then abort now
    185     if(!isMyTick(id))
    186       return true;
    187258
    188259    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
    189260    // copy to location
    190261    for(i=syncList->begin(); i!=syncList->end(); ++i){
    191       //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
    192262      if( ((*i)->mode & mode) == 0 ){
    193263        COUT(5) << "not getting data: " << std::endl;
     
    217287
    218288  /**
    219    * This function takes a syncData struct and takes it to update the variables
    220    * @param vars data of the variables
     289   * This function takes a bytestream and loads the data into the registered variables
     290   * @param mem pointer to the bytestream
     291   * @param mode same as in getData
    221292   * @return true/false
    222293   */
    223   bool Synchronisable::updateData(unsigned char*& mem, int mode){
    224     unsigned char *data = mem;
     294  bool Synchronisable::updateData(uint8_t*& mem, int mode){
    225295    if(mode==0x0)
    226296      mode=state_;
     
    231301    }
    232302
     303    uint8_t *data=mem;
    233304    // start extract header
    234     unsigned int objectID, classID, size;
    235     size = *(int *)mem;
    236     mem+=sizeof(size);
    237     objectID = *(int *)mem;
    238     mem+=sizeof(objectID);
    239     classID = *(int *)mem;
    240     mem+=sizeof(classID);
     305    synchronisableHeader *syncHeader = (synchronisableHeader *)mem;
     306    assert(syncHeader->objectID==this->objectID);
     307    if(syncHeader->dataAvailable==false){
     308      mem+=syncHeader->size;
     309      return true;
     310    }
     311
     312    mem+=sizeof(synchronisableHeader);
    241313    // stop extract header
    242     assert(this->objectID==objectID);
    243     assert(this->classID==classID);
    244     if(size==3*sizeof(unsigned int)) //if true, only the header is available
    245       return true;
    246       //assert(0);
    247 
    248     COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl;
    249     for(i=syncList->begin(); i!=syncList->end() && mem <= data+size; i++){
     314    assert(this->objectID==syncHeader->objectID);
     315//    assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ?
     316   
     317    COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl;
     318    for(i=syncList->begin(); i!=syncList->end() && mem <= data+syncHeader->size; i++){
    250319      if( ((*i)->mode ^ mode) == 0 ){
    251320        COUT(5) << "synchronisable: not updating variable " << std::endl;
     
    263332          break;
    264333        case STRING:
    265           (*i)->size = *(int *)mem;
     334          (*i)->size = *(uint32_t *)mem;
    266335          COUT(5) << "string size: " << (*i)->size << std::endl;
    267336          mem+=sizeof(int);
     
    283352  /**
    284353  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
     354  * @param id id of the gamestate
     355  * @param mode same as getData
    285356  * @return amount of bytes
    286357  */
    287   int Synchronisable::getSize(unsigned int id, int mode){
     358  uint32_t Synchronisable::getSize(unsigned int id, int mode){
    288359    if(!isMyTick(id))
    289360      return 0;
    290     int tsize=0;
     361    int tsize=sizeof(synchronisableHeader);
    291362    if(mode==0x0)
    292363      mode=state_;
     
    311382
    312383  /**
    313    * This function returns the total amount of bytes needed by getData to save the whole content of the variables
    314    * @return amount of bytes
    315    */
    316   int Synchronisable::getSize2(unsigned int id, int mode){
    317     return sizeof(synchronisableHeader) + getSize( id, mode );
    318   }
    319 
    320   /**
    321    *
    322    * @param id
    323    * @return
     384   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
     385   * @param id gamestate id
     386   * @return true/false
    324387   */
    325388  bool Synchronisable::isMyTick(unsigned int id){
    326 //     return true;
     389    return ( (objectMode_&state_)!=0 );
     390  }
     391
     392  bool Synchronisable::doSelection(unsigned int id){
    327393    return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
    328394  }
    329395
    330   bool Synchronisable::isMyData(unsigned char* mem)
     396  /**
     397   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
     398   * @param mem pointer to the bytestream
     399   */
     400  bool Synchronisable::isMyData(uint8_t* mem)
    331401  {
    332     unsigned int objectID, classID, size;
    333     size = *(int *)mem;
    334     mem+=sizeof(size);
    335     objectID = *(int *)mem;
    336     mem+=sizeof(objectID);
    337     classID = *(int *)mem;
    338     mem+=sizeof(classID);
    339 
    340     assert(classID == this->classID);
    341     return (objectID == this->objectID);
    342   }
    343 
     402    synchronisableHeader *header = (synchronisableHeader *)mem;
     403    assert(header->objectID==this->objectID);
     404    return header->dataAvailable;
     405  }
     406
     407  /**
     408   * This function sets the synchronisation mode of the object
     409   * If set to 0x1 variables will only be synchronised to the client
     410   * If set to 0x2 variables will only be synchronised to the server
     411   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
     412   * @param mode same as in registerVar
     413   */
    344414  void Synchronisable::setObjectMode(int mode){
    345415    assert(mode==0x1 || mode==0x2 || mode==0x3);
  • code/trunk/src/network/Synchronisable.h

    r1841 r1907  
    3333
    3434#include <list>
     35#include <map>
     36#include <queue>
    3537#include "core/OrxonoxClass.h"
    3638#include "core/XMLIncludes.h"
    3739#include "NetworkCallback.h"
    3840
     41#define REGISTERDATA(varname) registerVar(&varname, sizeof(varname), network::DATA)
     42#define REGISTERDATA_WITHDIR(varname, mode) registerVar(&varname, sizeof(varname), network::DATA, mode)
     43#define REGISTERSTRING(stringname) registerVar(&stringname, stringname.length()+1, network::STRING)
     44#define REGISTERSTRING_WITHDIR(stringname, mode) registerVar(&stringname, stringname.length()+1, network::STRING, mode)
     45
     46//TODO: this is only a very ugly hack ...
     47namespace orxonox{
     48class SpaceShip;
     49}
     50
    3951namespace network
    4052{
     53  namespace direction{
     54    enum syncdirection{
     55      toclient=0x1,
     56      toserver=0x2,
     57      bidirectional=0x3
     58    };
     59  }
     60 
     61  namespace syncmode{
     62    enum mode{
     63      one=0,
     64      always=1
     65    };
     66  }
     67 
    4168  enum variableType{
    4269    DATA,
     
    4572
    4673  struct synchronisableHeader{
    47     unsigned int size;
    48     unsigned int objectID;
    49     unsigned int classID;
     74    uint32_t size:31;
     75    bool dataAvailable:1;
     76    uint32_t objectID;
     77    uint32_t classID;
    5078  };
    5179
     
    5886  }SYNCVAR;
    5987
    60 
    6188  /**
    6289  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
    63    * Every class, t
    64   int mode;hat inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. Additionally it also has to provide a Constructor, that takes exactly the variables in this linked list.
     90   * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
    6591  * @author Oliver Scheuss
    6692  */
    6793  class _NetworkExport Synchronisable : virtual public orxonox::OrxonoxClass{
    6894  public:
     95    friend class packet::Gamestate;
     96    friend class GamestateClient;
     97    friend class Server;
     98    friend class orxonox::SpaceShip;
     99    virtual ~Synchronisable();
     100
    69101   
    70     virtual ~Synchronisable();
    71     unsigned int objectID;
    72     unsigned int classID;
    73 
    74     void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    75     bool getData(unsigned char*& men, unsigned int id, int mode=0x0);
    76     int getSize2(unsigned int id, int mode=0x0);
    77     bool updateData(unsigned char*& mem, int mode=0x0);
    78     bool isMyData(unsigned char* mem);
    79     void setObjectMode(int mode);
    80     void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; }
    81    
    82     virtual void registerAllVariables()=0;
    83102    virtual bool create();
    84103    static void setClient(bool b);
    85104   
    86     static bool fabricate(unsigned char*& mem, int mode=0x0);
     105    static Synchronisable *fabricate(uint8_t*& mem, int mode=0x0);
     106    static bool deleteObject(unsigned int objectID);
     107    static Synchronisable *getSynchronisable(unsigned int objectID);
     108    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
     109    static unsigned int popDeletedObject(){ unsigned int i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
     110   
     111    inline unsigned int getObjectID(){return objectID;}
     112    inline unsigned int getClassID(){return classID;}
    87113  protected:
    88114    Synchronisable();
     115    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
     116    void setObjectMode(int mode);
     117    void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; }
     118    virtual void registerAllVariables()=0;
     119   
     120   
    89121  private:
    90     int getSize(unsigned int id, int mode=0x0);
     122    bool getData(uint8_t*& men, unsigned int id, int mode=0x0);
     123    uint32_t getSize(unsigned int id, int mode=0x0);
     124    bool updateData(uint8_t*& mem, int mode=0x0);
     125    bool isMyData(uint8_t* mem);
     126    bool doSelection(unsigned int id);
    91127    bool isMyTick(unsigned int id);
     128   
     129    unsigned int objectID;
     130    unsigned int classID;
     131   
    92132    std::list<synchronisableVariable *> *syncList;
    93     int datasize;
    94133    static int state_; // detemines wheter we are server (default) or client
    95134    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
    96135    unsigned int objectFrequency_;
    97136    int objectMode_;
     137    static std::map<unsigned int, Synchronisable *> objectMap_;
     138    static std::queue<unsigned int> deletedObjects_;
    98139  };
    99140}
  • code/trunk/src/network/packet/Acknowledgement.cc

    r1769 r1907  
    4040#define _ACKID              _PACKETID + sizeof(network::packet::ENUM::Type)
    4141 
    42 Acknowledgement::Acknowledgement( unsigned int id, int clientID )
     42Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID )
    4343 : Packet()
    4444{
    4545  flags_ = flags_ | PACKET_FLAGS_ACK;
    46   data_=new unsigned char[ getSize() ];
     46  data_=new uint8_t[ getSize() ];
    4747  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement;
    48   *(unsigned int *)(data_ + _ACKID ) = id;
     48  *(uint32_t *)(data_ + _ACKID ) = id;
    4949  clientID_=clientID;
    5050}
    5151
    52 Acknowledgement::Acknowledgement( unsigned char *data, int clientID )
     52Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID )
    5353  : Packet(data, clientID)
    5454{
     
    6060
    6161unsigned int Acknowledgement::getSize() const{
    62   return _ACKID + sizeof(unsigned int);
     62  return _ACKID + sizeof(uint32_t);
    6363}
    6464
     
    7070
    7171unsigned int Acknowledgement::getAckID(){
    72   return *(unsigned int *)(data_ + _ACKID);
     72  return *(uint32_t *)(data_ + _ACKID);
    7373}
    7474
  • code/trunk/src/network/packet/Acknowledgement.h

    r1763 r1907  
    4040{
    4141public:
    42   Acknowledgement( unsigned int id, int clientID );
    43   Acknowledgement( unsigned char* data, int clientID );
     42  Acknowledgement( unsigned int id, unsigned int clientID );
     43  Acknowledgement( uint8_t* data, unsigned int clientID );
    4444  ~Acknowledgement();
    4545 
  • code/trunk/src/network/packet/Chat.cc

    r1763 r1907  
    2929#include "Chat.h"
    3030#include <assert.h>
     31#include "network/Host.h"
    3132
    3233namespace network {
    3334namespace packet {
    3435 
    35 #define PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
    36 #define _PACKETID         0
    37 #define _MESSAGELENGTH    _PACKETID + sizeof(ENUM::Type)
    38 #define _MESSAGE          _MESSAGELENGTH + sizeof(unsigned int)
     36#define   PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
     37#define   _PACKETID         0
     38const int _PLAYERID     =   _PACKETID + sizeof(ENUM::Type);
     39#define   _MESSAGELENGTH    _PLAYERID + sizeof(uint32_t)
     40#define   _MESSAGE          _MESSAGELENGTH + sizeof(uint32_t)
    3941
    40 Chat::Chat( std::string& message, int clientID )
     42Chat::Chat( std::string message, unsigned int playerID )
    4143 : Packet()
    4244{
     
    4446  messageLength_ = message.length()+1;
    4547  data_=new unsigned char[ getSize() ];
    46   *(ENUM::Type *)&data_[ _PACKETID ] = ENUM::Chat;
    47   *(unsigned int *)&data_[ _MESSAGELENGTH ] = messageLength_;
    48   memcpy( &data_[ _MESSAGE ], (void *)message.c_str(), messageLength_ );
    49   clientID_=clientID;
     48  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Chat;
     49  *(unsigned int *)(data_ + _PLAYERID ) = playerID;
     50  *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
     51  memcpy( data_+_MESSAGE, (void *)message.c_str(), messageLength_ );
    5052}
    5153
    52 Chat::Chat( unsigned char *data, int clientID )
     54Chat::Chat( uint8_t* data, unsigned int clientID )
    5355  : Packet(data, clientID)
    5456{
    55   messageLength_ = *(unsigned int *)&data[ _MESSAGELENGTH ];
     57  messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
    5658}
    5759
     
    6567
    6668bool Chat::process(){
    67   //TODO: change this !!!
    68   assert(0);
     69  bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));
    6970  delete this;
    70   return true;
     71  return b;
    7172}
    7273
    7374unsigned char *Chat::getMessage(){
    74   return &data_[ _MESSAGE ];
     75  return data_ + _MESSAGE;
    7576}
    7677
  • code/trunk/src/network/packet/Chat.h

    r1837 r1907  
    1616{
    1717public:
    18   Chat( std::string& message, int clientID );
    19   Chat( unsigned char* data, int clientID );
     18  Chat( std::string message, unsigned int playerID );
     19  Chat( uint8_t* data, unsigned int clientID );
    2020  ~Chat();
    2121 
     
    2626  unsigned char *getMessage();
    2727private:
    28   unsigned int messageLength_;
    29   int clientID_;
     28  uint32_t messageLength_;
     29  unsigned int clientID_;
    3030};
    3131
  • code/trunk/src/network/packet/ClassID.cc

    r1856 r1907  
    4040#define _PACKETID             0
    4141#define _CLASSID              _PACKETID + sizeof(ENUM::Type)
    42 #define _CLASSNAMELENGTH      _CLASSID + sizeof(unsigned int)
     42#define _CLASSNAMELENGTH      _CLASSID + sizeof(uint32_t)
    4343#define _CLASSNAME            _CLASSNAMELENGTH + sizeof(classNameLength_)
    4444
     
    5757}
    5858
    59 ClassID::ClassID( unsigned char *data, int clientID )
     59ClassID::ClassID( uint8_t* data, unsigned int clientID )
    6060  : Packet(data, clientID)
    6161{
     
    6868
    6969unsigned int ClassID::getSize() const{
    70   return sizeof(network::packet::ENUM::Type) + 2*sizeof(unsigned int) + classNameLength_;
     70  return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t) + classNameLength_;
    7171}
    7272
     
    8282
    8383unsigned int ClassID::getClassID(){
    84   return *(unsigned int *)&data_[ _CLASSID ];
     84  return *(uint32_t *)(data_ + _CLASSID);
    8585}
    8686
  • code/trunk/src/network/packet/ClassID.h

    r1763 r1907  
    4343public:
    4444  ClassID( unsigned int classID, std::string className );
    45   ClassID( unsigned char* data, int clientID );
     45  ClassID( uint8_t* data, unsigned int clientID );
    4646  ~ClassID();
    4747 
     
    5353  unsigned char *getClassName();
    5454private:
    55   unsigned int classNameLength_;
     55  uint32_t classNameLength_;
    5656};
    5757
  • code/trunk/src/network/packet/Gamestate.cc

    r1767 r1907  
    4646#define HEADER GAMESTATE_HEADER(data_)
    4747
     48 
     49#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
     50 
    4851Gamestate::Gamestate()
    4952{
    50 }
    51 
    52 Gamestate::Gamestate(unsigned char *data, int clientID):
     53  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     54}
     55
     56Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
    5357    Packet(data, clientID)
    5458{
     59  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     60}
     61
     62Gamestate::Gamestate(uint8_t *data)
     63{
     64  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     65  data_=data;
    5566}
    5667
     
    7485    return false;
    7586  }
    76 
     87 
     88#ifndef NDEBUG
     89  std::list<Synchronisable*> slist;
     90  std::list<Synchronisable*>::iterator iit;
     91#endif
    7792  //start collect data synchronisable by synchronisable
    78   unsigned char *mem=data_;
     93  uint8_t *mem=data_;
    7994  mem+=sizeof(GamestateHeader);
    8095  orxonox::ObjectList<Synchronisable>::iterator it;
    8196  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
    82     tempsize=it->getSize2(id, mode);
     97    tempsize=it->getSize(id, mode);
    8398
    8499    if(currentsize+tempsize > size){
     
    88103      int addsize=tempsize;
    89104      while(++temp)
    90         addsize+=temp->getSize2(id, mode);
    91       data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
     105        addsize+=temp->getSize(id, mode);
     106      data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
    92107      if(!data_)
    93108        return false;
     
    95110    }// stop allocate additional memory
    96111
     112#ifndef NDEBUG
     113    for(iit=slist.begin(); iit!=slist.end(); iit++)
     114      assert((*iit)!=*it);
     115    slist.push_back(*it);
     116#endif
     117   
     118    //if(it->doSelection(id))
     119    dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
    97120    if(!it->getData(mem, id, mode))
    98121      return false; // mem pointer gets automatically increased because of call by reference
     
    105128  HEADER->packetType = ENUM::Gamestate;
    106129  assert( *(ENUM::Type *)(data_) == ENUM::Gamestate);
    107   HEADER->normsize = currentsize;
     130  HEADER->datasize = currentsize;
    108131  HEADER->id = id;
    109132  HEADER->diffed = false;
     
    122145  assert(!HEADER->compressed);
    123146  assert(!HEADER->diffed);
    124   unsigned int size, objectID, classID;
    125   unsigned char *mem=data_+sizeof(GamestateHeader);
     147  uint8_t *mem=data_+sizeof(GamestateHeader);
    126148    // get the start of the Synchronisable list
    127   orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
    128 
    129   while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){
    130       // extract synchronisable header
    131     size = *(unsigned int *)mem;
    132     objectID = *(unsigned int*)(mem+sizeof(unsigned int));
    133     classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
    134 
    135     if(!it || it->objectID!=objectID || it->classID!=classID){
    136         // bad luck ;)
    137         // delete the synchronisable (obviously seems to be deleted on the server)
    138       while(it && it->objectID!=objectID)
    139         removeObject(it);
    140 
    141       if(!it){
    142         //fabricate the new synchronisable
    143         if(!Synchronisable::fabricate(mem, mode))
    144           return false;
    145         it=orxonox::ObjectList<Synchronisable>::end();
    146       }else{
    147         if(! it->updateData(mem, mode))
    148         {
    149           COUT(1) << "We couldn't update objectID: " \
    150             << objectID << "; classID: " << classID << std::endl;
    151         }
    152       }
    153     } else
     149  //orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
     150  Synchronisable *s;
     151
     152  // update the data of the objects we received
     153  while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
     154    synchronisableHeader *objectheader = (synchronisableHeader*)mem;
     155
     156    s = Synchronisable::getSynchronisable( objectheader->objectID );
     157    if(!s)
    154158    {
    155         // we have our object
    156       if(! it->updateData(mem, mode))
    157       {
    158         COUT(1) << "We couldn't update objectID: " \
    159             << objectID << "; classID: " << classID << std::endl;
    160       }
    161     }
    162     ++it;
     159      s = Synchronisable::fabricate(mem, mode);
     160      assert(s);
     161//       if(!s)
     162//         return false;
     163    }
     164    else
     165    {
     166      bool b = s->updateData(mem, mode);
     167      assert(b);
     168      //if(!s->updateData(mem, mode))
     169        //return false;
     170    }
    163171  }
    164172
    165173  return true;
    166174}
     175
     176
    167177
    168178int Gamestate::getID(){
     
    177187  else
    178188  {
    179     return HEADER->normsize+sizeof(GamestateHeader);
     189    return HEADER->datasize+sizeof(GamestateHeader);
    180190  }
    181191}
    182192
    183193bool Gamestate::operator==(packet::Gamestate gs){
    184   unsigned char *d1 = data_+sizeof(GamestateHeader);
    185   unsigned char *d2 = gs.data_+sizeof(GamestateHeader);
     194  uint8_t *d1 = data_+sizeof(GamestateHeader);
     195  uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
    186196  assert(!isCompressed());
    187197  assert(!gs.isCompressed());
    188   while(d1<data_+HEADER->normsize)
     198  while(d1<data_+HEADER->datasize)
    189199  {
    190200    if(*d1!=*d2)
     
    201211}
    202212
     213
     214
    203215bool Gamestate::compressData()
    204216{
    205217  assert(HEADER);
    206218  assert(!HEADER->compressed);
    207   uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
     219  uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
    208220  if(buffer==0)
    209221    return false;
    210222
    211   unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
    212   unsigned char *dest = GAMESTATE_START(ndata);
     223  uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
     224  uint8_t *dest = GAMESTATE_START(ndata);
    213225  //unsigned char *dest = new unsigned char[buffer];
    214   unsigned char *source = GAMESTATE_START(data_);
     226  uint8_t *source = GAMESTATE_START(data_);
    215227  int retval;
    216   retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
     228  retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
    217229  switch ( retval ) {
    218230    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
     
    223235#ifndef NDEBUG
    224236  //decompress and compare the start and the decompressed data
    225   unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];
    226   unsigned char *d2 = GAMESTATE_START(rdata);
    227   uLongf length2 = HEADER->normsize;
     237  uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     238  uint8_t *d2 = GAMESTATE_START(rdata);
     239  uLongf length2 = HEADER->datasize;
    228240  uncompress(d2, &length2, dest, buffer);
    229   for(unsigned int i=0; i<HEADER->normsize; i++){
     241  for(unsigned int i=0; i<HEADER->datasize; i++){
    230242    assert(*(source+i)==*(d2+i));
    231243  }
     
    235247  //copy and modify header
    236248#ifndef NDEBUG
    237   HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize);
     249  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
    238250#endif
    239251  *GAMESTATE_HEADER(ndata) = *HEADER;
     
    245257  HEADER->compressed = true;
    246258  assert(HEADER->compressed);
    247   COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
     259  COUT(3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
    248260  return true;
    249261}
     
    252264  assert(HEADER);
    253265  assert(HEADER->compressed);
    254   COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;
    255   unsigned int normsize = HEADER->normsize;
     266  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
     267  unsigned int datasize = HEADER->datasize;
    256268  unsigned int compsize = HEADER->compsize;
    257269  unsigned int bufsize;
    258   assert(compsize<=normsize);
    259   bufsize = normsize;
     270  assert(compsize<=datasize);
     271  bufsize = datasize;
    260272  assert(bufsize!=0);
    261   unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
    262   unsigned char *dest = ndata + sizeof(GamestateHeader);
    263   unsigned char *source = data_ + sizeof(GamestateHeader);
     273  uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
     274  uint8_t *dest = ndata + sizeof(GamestateHeader);
     275  uint8_t *source = data_ + sizeof(GamestateHeader);
    264276  int retval;
    265277  uLongf length=bufsize;
     
    272284  }
    273285#ifndef NDEBUG
    274   assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
     286  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
    275287#endif
    276288
     
    282294  data_ = ndata;
    283295  HEADER->compressed = false;
    284   assert(HEADER->normsize==normsize);
     296  assert(HEADER->datasize==datasize);
    285297  assert(HEADER->compsize==compsize);
    286298  return true;
     
    293305  assert(!HEADER->diffed);
    294306  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    295   unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
     307  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
    296308  unsigned int of=0; // pointers offset
    297309  unsigned int dest_length=0;
    298   dest_length=HEADER->normsize;
     310  dest_length=HEADER->datasize;
    299311  if(dest_length==0)
    300312    return NULL;
    301   unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
    302   unsigned char *dest = ndata + sizeof(GamestateHeader);
    303   while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
     313  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
     314  uint8_t *dest = ndata + sizeof(GamestateHeader);
     315  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
    304316    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    305317    ++of;
    306318  }
    307   if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
    308     unsigned char n=0;
    309     if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
     319  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     320    uint8_t n=0;
     321    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
    310322      while(of<dest_length){
    311323        *(dest+of)=n^*(gs+of);
     
    324336}
    325337
     338Gamestate* Gamestate::doSelection(unsigned int clientID){
     339  assert(data_);
     340  std::map<unsigned int, Synchronisable *>::iterator it;
     341 
     342  // allocate memory for new data
     343  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     344  // create a gamestate out of it
     345  Gamestate *gs = new Gamestate(gdata);
     346  uint8_t *newdata = gdata + sizeof(GamestateHeader);
     347  uint8_t *origdata = GAMESTATE_START(data_);
     348 
     349  //copy the GamestateHeader
     350  *(GamestateHeader*)gdata = *HEADER;
     351 
     352  synchronisableHeader *oldobjectheader, *newobjectheader;
     353  unsigned int objectOffset;
     354 
     355  //copy in the zeros
     356  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     357    oldobjectheader = (synchronisableHeader*)origdata;
     358    newobjectheader = (synchronisableHeader*)newdata;
     359    unsigned int objectsize = oldobjectheader->size;
     360    assert(it->second->objectID==oldobjectheader->objectID);
     361    *newobjectheader = *oldobjectheader;
     362    objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDate variables in the objectheader
     363    if(it->second->doSelection(HEADER->id)){
     364      newobjectheader->dataAvailable=true; //TODO: probably not neccessary
     365      while(objectOffset<objectsize){
     366        *(newdata + objectOffset)=*(origdata + objectOffset);    // copy the data
     367        objectOffset++;
     368      }
     369    }else{
     370      newobjectheader->dataAvailable=false;
     371      while(objectOffset<objectsize){
     372        *(newdata+objectOffset)=0;    // set to 0
     373        objectOffset++;
     374      }
     375      assert(objectOffset==objectsize);
     376    }
     377    newdata += objectsize;
     378    origdata += objectsize;
     379  }
     380  return gs;
     381}
     382
     383
     384Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
     385  // asserts
     386  assert(data_);
     387  assert(base->data_);
     388  assert(!GAMESTATE_HEADER(base->data_)->diffed);
     389  assert(!GAMESTATE_HEADER(base->data_)->compressed);
     390  assert(!HEADER->compressed);
     391  assert(!HEADER->diffed);
     392 
     393  //preparations
     394  std::map<unsigned int, Synchronisable *>::iterator it;
     395  uint8_t *origdata, *basedata, *destdata, *ndata;
     396  unsigned int objectOffset, streamOffset=0;    //data offset
     397  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
     398  synchronisableHeader *origheader;
     399  synchronisableHeader *destheader;
     400 
     401  origdata = GAMESTATE_START(this->data_);
     402  basedata = GAMESTATE_START(base->data_);
     403  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
     404  destdata = ndata + sizeof(GamestateHeader);
     405 
     406  // do the diff
     407  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     408    assert(streamOffset<HEADER->datasize);
     409    bool sendData = it->second->doSelection(HEADER->id);
     410    origheader = (synchronisableHeader *)(origdata+streamOffset);
     411    destheader = (synchronisableHeader *)(destdata+streamOffset);
     412   
     413    //copy and partially diff the object header
     414    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     415    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
     416    *(bool*)(destdata+sizeof(uint32_t)) = sendData;
     417    if(sendData){
     418      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
     419      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
     420    }else{
     421      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
     422      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
     423    }
     424    objectOffset=sizeof(synchronisableHeader);
     425    streamOffset+=sizeof(synchronisableHeader);
     426   
     427    //now handle the object data or fill with zeros
     428    while(objectOffset<origheader->size ){
     429     
     430      if(sendData && streamOffset<minsize)
     431        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     432      else if(sendData)
     433        *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
     434      else
     435        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
     436     
     437      objectOffset++;
     438      streamOffset++;
     439    }
     440    destdata+=objectOffset;
     441    origdata+=objectOffset;
     442    basedata+=objectOffset;
     443  }
     444 
     445  //copy over the gamestate header and set the diffed flag
     446  *(GamestateHeader *)ndata = *HEADER; //copy over the header
     447  Gamestate *gs = new Gamestate(ndata);
     448  GAMESTATE_HEADER(ndata)->diffed=true;
     449  return gs;
     450}
     451
     452Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
     453  // asserts
     454  assert(data_);
     455  assert(base->data_);
     456  assert(!GAMESTATE_HEADER(base->data_)->diffed);
     457  assert(!GAMESTATE_HEADER(base->data_)->compressed);
     458  assert(!HEADER->compressed);
     459  assert(HEADER->diffed);
     460 
     461  //preparations
     462  std::map<unsigned int, Synchronisable *>::iterator it;
     463  uint8_t *origdata, *basedata, *destdata, *ndata;
     464  unsigned int objectOffset, streamOffset=0;    //data offset
     465  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
     466  synchronisableHeader *origheader;
     467  synchronisableHeader *destheader;
     468 
     469  origdata = GAMESTATE_START(this->data_);
     470  basedata = GAMESTATE_START(base->data_);
     471  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
     472  destdata = ndata + sizeof(GamestateHeader);
     473 
     474  // do the undiff
     475  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     476    assert(streamOffset<HEADER->datasize);
     477    origheader = (synchronisableHeader *)(origdata+streamOffset);
     478    destheader = (synchronisableHeader *)(destdata+streamOffset);
     479    bool sendData;
     480   
     481    //copy and partially diff the object header
     482    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     483    *(unsigned int*)destdata = *(unsigned int*)origdata; //size (do not diff)
     484    *(bool*)(destdata+sizeof(unsigned int)) = *(bool*)(origdata+sizeof(unsigned int));
     485    sendData = *(bool*)(origdata+sizeof(unsigned int));
     486    if(sendData){
     487      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+sizeof(unsigned int)+sizeof(bool)); //objectid (diff it)
     488      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+2*sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+2*sizeof(unsigned int)+sizeof(bool)); //classid (diff it)
     489    }else{
     490      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0;
     491      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0;
     492    }
     493    objectOffset=sizeof(synchronisableHeader);
     494    streamOffset+=sizeof(synchronisableHeader);
     495   
     496    //now handle the object data or fill with zeros
     497    while(objectOffset<origheader->size ){
     498     
     499      if(sendData && streamOffset<minsize)
     500        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     501      else if(sendData)
     502        *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
     503      else
     504        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
     505     
     506      objectOffset++;
     507      streamOffset++;
     508    }
     509    destdata+=objectOffset;
     510    origdata+=objectOffset;
     511    basedata+=objectOffset;
     512  }
     513 
     514  //copy over the gamestate header and set the diffed flag
     515  *(GamestateHeader *)ndata = *HEADER; //copy over the header
     516  Gamestate *gs = new Gamestate(ndata);
     517  GAMESTATE_HEADER(ndata)->diffed=false;
     518  return gs;
     519}
     520
    326521Gamestate *Gamestate::undiff(Gamestate *base)
    327522{
     
    330525  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
    331526  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    332   unsigned char *basep = GAMESTATE_START(base->data_);
    333   unsigned char *gs = GAMESTATE_START(this->data_);
     527  uint8_t *basep = GAMESTATE_START(base->data_);
     528  uint8_t *gs = GAMESTATE_START(this->data_);
    334529  unsigned int of=0; // pointers offset
    335530  unsigned int dest_length=0;
    336   dest_length=HEADER->normsize;
     531  dest_length=HEADER->datasize;
    337532  if(dest_length==0)
    338533    return NULL;
    339   unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
    340   unsigned char *dest = ndata + sizeof(GamestateHeader);
    341   while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
     534  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
     535  uint8_t *dest = ndata + sizeof(GamestateHeader);
     536  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
    342537    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    343538    ++of;
    344539  }
    345   if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
    346     unsigned char n=0;
    347     if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
     540  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     541    uint8_t n=0;
     542    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
    348543      while(of < dest_length){
    349544        *(dest+of)=n^*(gs+of);
     
    370565    // get total size of gamestate
    371566  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
    372     size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
     567    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
    373568//  size+=sizeof(GamestateHeader);
    374569  return size;
  • code/trunk/src/network/packet/Gamestate.h

    r1763 r1907  
    2929#include "Packet.h"
    3030#include "network/Synchronisable.h"
     31#include <map>
    3132#ifndef NDEBUG
    3233#include "util/CRC32.h"
    3334#endif
     35
    3436
    3537#ifndef NETWORK_PACKETGAMESTATE_H
     
    4244struct GamestateHeader{
    4345  ENUM::Type packetType;
    44   int id; // id of the gamestate
    45   unsigned int compsize;
    46   unsigned int normsize;
    47   int base_id; // id of the base-gamestate diffed from
    48   bool diffed; // wheter diffed or not
    49   bool complete; // wheter it is a complete gamestate or only partial
    50   bool compressed;
     46  int32_t id; // id of the gamestate
     47  uint32_t compsize;
     48  uint32_t datasize;
     49  int32_t base_id; // id of the base-gamestate diffed from
     50  bool diffed:1; // wheter diffed or not
     51  bool complete:1; // wheter it is a complete gamestate or only partial
     52  bool compressed:1;
    5153#ifndef NDEBUG
    5254  uint32_t crc32;
     
    5557
    5658/**
    57         @author
     59        @author Oliver Scheuss
    5860*/
    5961class Gamestate: public Packet{
    6062  public:
    6163    Gamestate();
    62     Gamestate(unsigned char *data, int clientID);
     64    Gamestate(uint8_t *data, unsigned int clientID);
     65    Gamestate(uint8_t *data);
    6366
    6467    ~Gamestate();
     
    7174    int getBaseID();
    7275    Gamestate *diff(Gamestate *base);
     76    Gamestate* intelligentDiff(Gamestate *base, unsigned int clientID);
    7377    Gamestate *undiff(Gamestate *base);
     78    Gamestate* intelligentUnDiff(Gamestate *base);
     79    Gamestate* doSelection(unsigned int clientID);
    7480    bool compressData();
    7581    bool decompressData();
    7682
    7783    // Packet functions
     84  private:
    7885    virtual unsigned int getSize() const;
    7986    virtual bool process();
     
    8390    unsigned int calcGamestateSize(unsigned int id, int mode=0x0);
    8491    void removeObject(orxonox::ObjectListIterator<Synchronisable> &it);
    85 
    86 
    87     //Bytestream *bs_;
    88     //GamestateHeader *header_;
     92    std::map<unsigned int, Synchronisable*> dataMap_;
    8993};
    9094
  • code/trunk/src/network/packet/Packet.cc

    r1763 r1907  
    4242#include "Gamestate.h"
    4343#include "Welcome.h"
     44#include "DeleteObjects.h"
    4445#include "network/Host.h"
    4546#include "core/CoreIncludes.h"
     
    6667}
    6768
    68 Packet::Packet(unsigned char *data, int clientID){
     69Packet::Packet(uint8_t *data, unsigned int clientID){
    6970  flags_ = PACKET_FLAG_DEFAULT;
    7071  packetDirection_ = ENUM::Incoming;
     
    7475}
    7576
    76 /*Packet::Packet(ENetPacket *packet, ENetPeer *peer){
    77   packetDirection_ = ENUM::Incoming;
    78   enetPacket_ = packet;
    79   clientID_ = ClientInformation::findClient(&peer->address)->getID();
    80   data_ = packet->data;
    81 }*/
    8277
    8378Packet::Packet(const Packet &p){
     
    8782  clientID_ = p.clientID_;
    8883  if(p.data_){
    89     data_ = new unsigned char[p.getSize()];
     84    data_ = new uint8_t[p.getSize()];
    9085    memcpy(data_, p.data_, p.getSize());
    9186  }else
     
    125120    case ENUM::Gamestate:
    126121    case ENUM::Welcome:
     122    case ENUM::DeleteObjects:
    127123      break;
    128124    default:
     
    138134
    139135Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){
    140   unsigned char *data = packet->data;
     136  uint8_t *data = packet->data;
    141137  unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
    142138  Packet *p;
    143   COUT(3) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl;
     139  COUT(5) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl;
    144140  switch( *(ENUM::Type *)(data + _PACKETID) )
    145141  {
    146142    case ENUM::Acknowledgement:
    147       COUT(3) << "ack" << std::endl;
     143      COUT(4) << "ack" << std::endl;
    148144      p = new Acknowledgement( data, clientID );
    149145      break;
    150146    case ENUM::Chat:
    151       COUT(3) << "chat" << std::endl;
     147      COUT(4) << "chat" << std::endl;
    152148      p = new Chat( data, clientID );
    153149      break;
    154150    case ENUM::ClassID:
    155       COUT(3) << "classid" << std::endl;
     151      COUT(4) << "classid" << std::endl;
    156152      p = new ClassID( data, clientID );
    157153      break;
    158154    case ENUM::Gamestate:
    159       COUT(3) << "gamestate" << std::endl;
     155      COUT(4) << "gamestate" << std::endl;
    160156      // TODO: remove brackets
    161157      p = new Gamestate( data, clientID );
    162158      break;
    163159    case ENUM::Welcome:
    164       COUT(3) << "welcome" << std::endl;
     160      COUT(4) << "welcome" << std::endl;
    165161      p = new Welcome( data, clientID );
     162      break;
     163    case ENUM::DeleteObjects:
     164      COUT(4) << "deleteobjects" << std::endl;
     165      p = new DeleteObjects( data, clientID );
    166166      break;
    167167    default:
  • code/trunk/src/network/packet/Packet.h

    r1763 r1907  
    3232#include <enet/enet.h>
    3333
     34#include "util/Integers.h"
     35
    3436namespace network {
    3537
     
    4749    ClassID,
    4850    Chat,
    49     Welcome
     51    Welcome,
     52    DeleteObjects
    5053  };
    5154}
     
    7477  protected:
    7578    Packet();
    76     Packet(unsigned char *data, int clientID);
     79    Packet(uint8_t *data, unsigned int clientID);
    7780//    Packet(ENetPacket *packet, ENetPeer *peer);
    7881    enet_uint32 flags_;
    79     int clientID_;
     82    unsigned int clientID_;
    8083    ENUM::Direction packetDirection_;
    81     unsigned char *data_;
     84    uint8_t *data_;
    8285  private:
    8386    static std::map<ENetPacket *, Packet *> packetMap_;
  • code/trunk/src/network/packet/Welcome.cc

    r1763 r1907  
    4242#define _PACKETID             0
    4343#define _CLIENTID             _PACKETID + sizeof(ENUM::Type)
    44 #define _SHIPID               _CLIENTID + sizeof(unsigned int)
     44#define _SHIPID               _CLIENTID + sizeof(uint32_t)
    4545 
    4646  Welcome::Welcome( unsigned int clientID, unsigned int shipID )
     
    4949  flags_ = flags_ | PACKET_FLAGS_CLASSID;
    5050  assert(getSize());
    51   data_=new unsigned char[ getSize() ];
     51  data_=new uint8_t[ getSize() ];
    5252  assert(data_);
    5353  *(packet::ENUM::Type *)(data_ + _PACKETID ) = packet::ENUM::Welcome;
    54   *(unsigned int *)&data_[ _CLIENTID ] = clientID;
    55   *(unsigned int *)&data_[ _SHIPID ] = shipID;
     54  *(uint32_t *)&data_[ _CLIENTID ] = clientID;
     55  *(uint32_t *)&data_[ _SHIPID ] = shipID;
    5656}
    5757
    58 Welcome::Welcome( unsigned char *data, int clientID )
     58Welcome::Welcome( uint8_t* data, unsigned int clientID )
    5959  : Packet(data, clientID)
    6060{
     
    6565}
    6666
    67 unsigned char *Welcome::getData(){
     67uint8_t *Welcome::getData(){
    6868  return data_;
    6969}
    7070
    7171unsigned int Welcome::getSize() const{
    72   return sizeof(network::packet::ENUM::Type) + 2*sizeof(unsigned int);
     72  return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t);
    7373}
    7474
    7575bool Welcome::process(){
    7676  unsigned int shipID, clientID;
    77   clientID = *(unsigned int *)&data_[ _CLIENTID ];
    78   shipID = *(unsigned int *)&data_[ _SHIPID ];
     77  clientID = *(uint32_t *)&data_[ _CLIENTID ];
     78  shipID = *(uint32_t *)&data_[ _SHIPID ];
    7979  Host::setClientID(clientID);
    8080  Host::setShipID(shipID);
  • code/trunk/src/network/packet/Welcome.h

    r1763 r1907  
    4141public:
    4242  Welcome( unsigned int clientID, unsigned int shipID );
    43   Welcome( unsigned char* data, int clientID );
     43  Welcome( uint8_t* data, unsigned int clientID );
    4444  virtual ~Welcome();
    4545 
    46   unsigned char *getData();
     46  uint8_t *getData();
    4747  inline unsigned int getSize() const;
    4848  bool process();
Note: See TracChangeset for help on using the changeset viewer.