Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 1, 2008, 3:54:20 PM (17 years ago)
Author:
rgrieder
Message:
  • @everyone: Do not create a branch until I've added the svn:eol-style property correctly. Otherwise this would cost me another 4 hours or so when we want to merge back.
  • merged network branch back to trunk
  • I had to omit the changes from last evening concerning the line endings
  • might not work yet because of the line endings
  • @beni: script branch is the only branch still open. you probably will have to apply a patch because of inconsistent new lines
Location:
code/trunk/src/network
Files:
25 edited

Legend:

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

    r1360 r1502  
    8585    isConnected=false;
    8686    isSynched_=false;
     87    gameStateFailure_=false;
    8788  }
    8889
     
    9596    isConnected=false;
    9697    isSynched_=false;
     98    gameStateFailure_=false;
    9799  }
    98100
     
    105107    isConnected=false;
    106108    isSynched_=false;
     109    gameStateFailure_=false;
    107110  }
    108111
     
    120123    isConnected=client_connection.createConnection();
    121124    if(isConnected){
    122       COUT(3) << "sending connectrequest" << std::endl;
    123       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
    124         COUT(1) << "could not create connection" << std::endl;
     125//       COUT(3) << "sending connectrequest" << std::endl;
     126//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
     127//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
    125128    }else
    126       COUT(1) << "could not create connection" << std::endl;
     129      COUT(1) << "could not create connection laber" << std::endl;
    127130    return isConnected;
    128131  }
     
    137140  }
    138141
    139   /**
    140   * submits a MouseAction to the server
    141   * @param x x Coordinate
    142   * @param y y Coordinate
    143   * @return true/false
    144   */
    145   bool Client::sendMouse(double x, double y){
    146     // generate packet and add it to the queue
    147     if(!isConnected)
    148       return false;
    149     if(!client_connection.addPacket(pck_gen.mousem(x, y)))
    150       return false;
    151     // send packets
    152     return client_connection.sendPackets();
    153   }
    154 
    155   /**
    156   * submits a Keystrike to the server
    157   * @param key_code code to submit
    158   * @return true/false
    159   */
    160   bool Client::sendKeyboard(char key_code){
    161     // generate packet and add it to queue
    162     if(!isConnected)
    163       return false;
    164     if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
    165       return false;
    166     // send packets
    167     return client_connection.sendPackets();
    168   }
     142 
     143
     144 
    169145
    170146  /**
     
    177153    if(!isConnected)
    178154      return false;
    179     if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))
    180       return client_connection.sendPackets();
     155    return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
     156      //return client_connection.sendPackets();
    181157    // send packets
    182158    return false;
    183   }
    184 
    185   /**
    186   * Adds a MouseAction to the PacketQueue
    187   * @param x x Coordinate
    188   * @param y y Coordinate
    189   * @return true/false
    190   */
    191   bool Client::addMouse(double x, double y){
    192     // generate packet and add it to the queue
    193     if(client_connection.addPacket(pck_gen.mousem(x, y)))
    194       return true;
    195     else
    196       return false;
    197   }
    198 
    199   /**
    200   * Adds a Keystrike to the PacketQueue
    201   * @param key_code
    202   * @return true/false
    203   */
    204   bool Client::addKeyboard(char key_code){
    205     // generate packet and add it to queue
    206     if(client_connection.addPacket(pck_gen.keystrike(key_code)))
    207       return true;
    208     else
    209       return false;
    210   }
    211 
    212   /**
    213   * Sends out all the packets queued by addXXX
    214   */
    215   bool Client::sendPackets(){
    216     if(!isConnected)
    217       return false;
    218     ENetEvent event;
    219     // send packets
    220     client_connection.sendPackets(&event);
    221     if(event.type==ENET_EVENT_TYPE_NONE)
    222       return true;
    223     else
    224       return false;
    225159  }
    226160
     
    243177      }
    244178    }
    245     ENetPacket *packet;
     179    ENetEvent *event;
    246180    // stop if the packet queue is empty
    247181    while(!(client_connection.queueEmpty())){
    248       packet = client_connection.getPacket();
    249       COUT(5) << "tick packet size " << packet->dataLength << std::endl;
    250       elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
    251     }
    252     if(!client_connection.sendPackets())
    253       COUT(3) << "Problem sending packets to server" << std::endl;
    254     return;
    255   }
    256 
    257   void Client::processGamestate( GameStateCompressed *data, int clientID){
    258     int id = data->id;
    259     COUT(5) << "received gamestate id: " << data->id << std::endl;
    260     if(gamestate.pushGameState(data)){
     182      event = client_connection.getEvent();
     183      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
     184      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     185    }
     186    int gameStateID = gamestate.processGameState();
     187    if(gameStateID==GAMESTATEID_INITIAL)
     188      if(gameStateFailure_){
     189        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
     190          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
     191        else
     192          COUT(4) << "negatively acked a gamestate" << std::endl;
     193        }
     194      else
     195        gameStateFailure_=true;
     196    else if(gameStateID!=0){
     197      // ack gamestate and set synched
    261198      if(!isSynched_)
    262199        isSynched_=true;
    263       if(!client_connection.addPacket(pck_gen.acknowledgement(id)))
    264         return;
    265         // we do this at the end of a tick
    266       if(!client_connection.sendPackets())
    267         COUT(2) << "Could not send acknowledgment" << std::endl;
    268     }
     200      gameStateFailure_=false;
     201      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
     202        COUT(3) << "could not ack gamestate" << std::endl;
     203    }// otherwise we had no gamestate to load
     204    gamestate.cleanup();
     205    /*if(!client_connection.sendPackets())
     206      COUT(3) << "Problem sending packets to server" << std::endl;*/
     207    return;
     208  }
     209
     210  void Client::processGamestate( GameStateCompressed *data, int clientID){
     211    COUT(5) << "received gamestate id: " << data->id << std::endl;
     212    gamestate.addGameState(data);
    269213  }
    270214
     
    275219      id->setNetworkID(clid->clid);
    276220    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
     221    COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
    277222    delete clid;
    278223    return;
  • code/trunk/src/network/Client.h

    r1293 r1502  
    7575    bool closeConnection();
    7676
    77     bool sendMouse(double x, double y);
    78     bool sendKeyboard(char key_code);
    7977    bool sendChat( std::string message );
    80 
    81     bool addMouse(double x, double y);
    82     bool addKeyboard(char key_code);
    83 
    84     bool sendPackets();
    8578   
    8679    int getShipID(){return shipID_;}
     
    110103    int clientID_;     // this is the id the server gave to us
    111104    int shipID_;
     105    bool gameStateFailure_;
    112106  };
    113107
  • code/trunk/src/network/ClientConnection.cc

    r1293 r1502  
    5050{
    5151  //static boost::thread_group network_threads;
     52
     53  boost::recursive_mutex ClientConnection::enet_mutex_;
    5254
    5355  ClientConnection::ClientConnection(int port, std::string address) {
     
    7577
    7678
    77   ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
     79  /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
    7880    if(!buffer.isEmpty()) {
    7981      //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
     
    8890    ENetAddress address; //sems that address is not needed
    8991    return getPacket(address);
     92  }*/
     93 
     94  ENetEvent *ClientConnection::getEvent(){
     95    if(!buffer.isEmpty())
     96      return buffer.pop();
     97    else
     98      return NULL;
    9099  }
    91100
     
    98107    //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
    99108    // wait 10 seconds for the connection to be established
    100     return waitEstablished(10000);
     109    return waitEstablished(3000);
    101110  }
    102111
     
    117126      return false;
    118127    }
     128    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    119129    if(enet_peer_send(server, 0, packet)<0)
    120130      return false;
     
    123133  }
    124134
    125   bool ClientConnection::sendPackets(ENetEvent *event) {
     135  bool ClientConnection::sendPackets() {
    126136    if(server==NULL)
    127137      return false;
    128     if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
    129       return true;}
    130     else
    131       return false;
    132   }
    133 
    134   bool ClientConnection::sendPackets() {
    135     ENetEvent event;
    136     if(server==NULL)
    137       return false;
    138     if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
    139       return true;}
    140     else
    141       return false;
     138    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     139    enet_host_flush(client);
     140    lock.unlock();
     141    return true;
    142142  }
    143143
    144144  void ClientConnection::receiverThread() {
    145145    // what about some error-handling here ?
    146     enet_initialize();
    147146    atexit(enet_deinitialize);
    148     ENetEvent event;
    149     client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
    150     if(client==NULL)
     147    ENetEvent *event;
     148    {
     149      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     150      enet_initialize();
     151      client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
     152      lock.unlock();
     153    }
     154    if(client==NULL) {
     155      COUT(2) << "ClientConnection: could not create client host" << std::endl;
    151156      // add some error handling here ==========================
    152157      quit=true;
     158    }
    153159    //connect to the server
    154160    if(!establishConnection()){
     161      COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl;
    155162      quit=true;
    156163      return;
    157164    }
     165    event = new ENetEvent;
    158166    //main loop
    159167    while(!quit){
    160168      //std::cout << "connection loop" << std::endl;
    161       if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
    162         // we should never reach this point
    163         quit=true;
    164         // add some error handling here ========================
     169      {
     170        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     171        if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
     172          // we should never reach this point
     173          quit=true;
     174          continue;
     175          // add some error handling here ========================
     176        }
     177        lock.unlock();
    165178      }
    166       switch(event.type){
     179      switch(event->type){
    167180        // log handling ================
    168181      case ENET_EVENT_TYPE_CONNECT:
     182        break;
    169183      case ENET_EVENT_TYPE_RECEIVE:
    170184        COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
    171         if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
     185        if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
    172186        COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
     187        event = new ENetEvent;
    173188        break;
    174189      case ENET_EVENT_TYPE_DISCONNECT:
     
    178193        break;
    179194      case ENET_EVENT_TYPE_NONE:
    180         continue;
     195        //receiverThread_->yield();
     196        usleep(1000);
     197        break;
    181198      }
    182       //receiverThread_->yield();
    183199    }
    184200    // now disconnect
     
    186202    if(!disconnectConnection())
    187203      // if disconnecting failed destroy conn.
     204      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    188205      enet_peer_reset(server);
    189206    return;
     
    192209  bool ClientConnection::disconnectConnection() {
    193210    ENetEvent event;
     211    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    194212    enet_peer_disconnect(server, 0);
    195213    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
     
    212230    ENetEvent event;
    213231    // connect to peer (server is type ENetPeer*)
     232    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    214233    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
    215     if(server==NULL)
     234    if(server==NULL) {
     235      COUT(2) << "ClientConnection: server == NULL" << std::endl;
    216236      // error handling
    217237      return false;
     238    }
    218239    // handshake
    219     if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
     240    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){
    220241      established=true;
    221242      return true;
    222243    }
    223     else
    224       return false;
     244    else {
     245      COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
     246      return false;
     247    }
    225248  }
    226249
  • code/trunk/src/network/ClientConnection.h

    r1293 r1502  
    5454#define NETWORK_PORT 55556
    5555#define NETWORK_CLIENT_MAX_CONNECTIONS 5
    56 #define NETWORK_CLIENT_TIMEOUT 100
    57 #define NETWORK_SEND_WAIT 5
     56#define NETWORK_CLIENT_TIMEOUT 1
    5857#define NETWORK_CLIENT_CHANNELS 2
    5958
     
    6362    ClientConnection(int port, std::string address);
    6463    ClientConnection(int port, const char* address);
    65     ENetPacket *getPacket(ENetAddress &address); // thread1
    66     ENetPacket *getPacket(); // thread1
     64    //ENetPacket *getPacket(ENetAddress &address); // thread1
     65    //ENetPacket *getPacket(); // thread1
     66    ENetEvent *getEvent();
    6767    // check wheter the packet queue is empty
    6868    bool queueEmpty();
     
    7575    bool sendPackets();
    7676    // send out all queued packets and save result in event
    77     bool sendPackets(ENetEvent *event);
     77    //bool sendPackets(ENetEvent *event);
    7878    bool waitEstablished(int milisec);
    7979    bool isConnected(){return established;}
     
    9595    ENetPeer *server;
    9696    boost::thread *receiverThread_;
     97   
     98    static boost::recursive_mutex enet_mutex_;
    9799  };
    98100
  • code/trunk/src/network/ClientInformation.cc

    r1360 r1502  
    4545namespace network
    4646{
    47   boost::recursive_mutex ClientInformation::mutex_;
    4847 
    4948  ClientInformation::ClientInformation() {
     
    8483
    8584  ClientInformation::~ClientInformation() {
    86     boost::recursive_mutex::scoped_lock lock(mutex_);
    87     if(preve!=0)
    88       preve->setNext(this->nexte);
    89     if(nexte!=0)
    90       nexte->setPrev(this->preve);
     85    if(prev()!=0)
     86      prev()->setNext(this->next());
     87    if(next()!=0)
     88      next()->setPrev(this->prev());
    9189  }
    9290
    9391  ClientInformation *ClientInformation::next() {
    94     boost::recursive_mutex::scoped_lock lock(mutex_);
    9592    if(this!=0)
    9693      return this->nexte;
     
    9996  }
    10097  ClientInformation *ClientInformation::prev() {
    101     boost::recursive_mutex::scoped_lock lock(mutex_);
    10298    if(this!=0)
    10399      return this->preve;
     
    107103
    108104  bool ClientInformation::setPrev(ClientInformation *prev) {
    109     boost::recursive_mutex::scoped_lock lock(mutex_);
    110105    if(!head_)
    111106      this->preve = prev;
     
    116111
    117112  bool ClientInformation::setNext(ClientInformation *next) {
    118     boost::recursive_mutex::scoped_lock lock(mutex_);
    119113    this->nexte = next;
    120114    return true;
     
    122116
    123117  ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) {
    124     boost::recursive_mutex::scoped_lock lock(mutex_);
    125     this->nexte->setPrev(ins);
    126     ins->setNext(this->nexte);
     118    this->next()->setPrev(ins);
     119    ins->setNext(this->next());
    127120    ins->setPrev(this);
    128121    this->nexte = ins;
     
    131124
    132125  ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){
    133     boost::recursive_mutex::scoped_lock lock(mutex_);
    134126    if(!this)
    135127      return NULL;
    136128    this->prev()->setNext(ins);
    137     ins->setPrev(this->preve);
     129    ins->setPrev(this->prev());
    138130    this->preve=ins;
    139131    ins->setNext(this);
     
    142134
    143135  void ClientInformation::setID(int clientID){
    144     boost::recursive_mutex::scoped_lock lock(mutex_);
     136    if(!this)
     137      return;
    145138    clientID_ = clientID;
    146139  }
    147140
    148141  bool ClientInformation::setPeer(ENetPeer *peer){
    149     boost::recursive_mutex::scoped_lock lock(mutex_);
    150142    if(!this)
    151143      return false;
     
    154146  }
    155147
    156   bool ClientInformation::setGamestateID(int id){
    157     boost::recursive_mutex::scoped_lock lock(mutex_);
     148  bool ClientInformation::setGameStateID(int id){
    158149    if(!this)
    159150      return false;
     
    163154 
    164155  bool ClientInformation::setPartialGamestateID(int id){
    165     boost::recursive_mutex::scoped_lock lock(mutex_);
    166156    if(!this)
    167157      return false;
     
    171161
    172162  int ClientInformation::getID() {
    173     boost::recursive_mutex::scoped_lock lock(mutex_);
    174163    if(!this)
    175164      return CLIENTID_UNKNOWN;
     
    179168
    180169  ENetPeer *ClientInformation::getPeer() {
    181     boost::recursive_mutex::scoped_lock lock(mutex_);
    182170    if(this)
    183171      return peer_;
     
    187175 
    188176  bool ClientInformation::getHead(){
    189     boost::recursive_mutex::scoped_lock lock(mutex_);
    190177    return head_;
    191178  }
    192179 
    193180  void ClientInformation::setHead(bool h){
    194     boost::recursive_mutex::scoped_lock lock(mutex_);
    195181    head_=h;
    196182  }
    197183 
    198184  int ClientInformation::getFailures(){
    199     boost::recursive_mutex::scoped_lock lock(mutex_);
    200185    return failures_;
    201186  }
    202187  void ClientInformation::addFailure(){
    203     boost::recursive_mutex::scoped_lock lock(mutex_);
    204188    failures_++;
    205189  }
    206190  void ClientInformation::resetFailures(){
    207     boost::recursive_mutex::scoped_lock lock(mutex_);
    208191    failures_=0;
    209192  }
    210193
    211194  int ClientInformation::getGamestateID() {
    212     boost::recursive_mutex::scoped_lock lock(mutex_);
    213195    if(this)
    214196      return gamestateID_;
     
    218200 
    219201  int ClientInformation::getPartialGamestateID() {
    220     boost::recursive_mutex::scoped_lock lock(mutex_);
    221202    if(this)
    222203      return partialGamestateID_;
     
    226207
    227208  ClientInformation *ClientInformation::insertBack(ClientInformation *ins) {
    228     boost::recursive_mutex::scoped_lock lock(mutex_);
    229209    if(!this)
    230210      return NULL;
     
    239219
    240220  bool ClientInformation::removeClient(int clientID) {
    241     boost::recursive_mutex::scoped_lock lock(mutex_);
    242221    if(!this || clientID==CLIENTID_UNKNOWN)
    243222      return false;
     
    252231
    253232  bool ClientInformation::removeClient(ENetPeer *peer) {
    254     boost::recursive_mutex::scoped_lock lock(mutex_);
    255233    if(!this || !peer)
    256234      return false;
     
    275253  */
    276254  ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
    277     boost::recursive_mutex::scoped_lock lock(mutex_);
    278255    ClientInformation *temp = this;
    279256    if (temp->head_)
     
    293270  */
    294271  ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
    295     boost::recursive_mutex::scoped_lock lock(mutex_);
    296272    ClientInformation *temp = this;
    297273    while(temp!=0){
     
    309285
    310286  bool ClientInformation::setSynched(bool s) {
    311     boost::recursive_mutex::scoped_lock lock(mutex_);
    312287    if(!this)
    313288      return false;
     
    317292
    318293  bool ClientInformation::getSynched() {
    319     boost::recursive_mutex::scoped_lock lock(mutex_);
    320294    if(this)
    321295      return synched_;
  • code/trunk/src/network/ClientInformation.h

    r1360 r1502  
    4949#define CLIENTID_UNKNOWN -2
    5050
     51// WATCH OUT: THE CLIENTINFORMATION LIST IS NOT THREADSAFE ANYMORE
     52
    5153namespace network
    5254{
     
    6971    void setID(int clientID);
    7072    bool setPeer(ENetPeer *peer);
    71     bool setGamestateID(int id);
     73    bool setGameStateID(int id);
    7274    bool setPartialGamestateID(int id);
    7375    inline void setShipID(int id){ShipID_=id;}
     
    114116    bool head_;
    115117    unsigned short failures_;
    116     static boost::recursive_mutex mutex_;
    117118   
    118119  };
  • code/trunk/src/network/ConnectionManager.cc

    r1360 r1502  
    4444#include "core/CoreIncludes.h"
    4545#include "core/BaseObject.h"
     46#include "objects/SpaceShip.h"
    4647#include "util/Math.h"
    47 #include "objects/SpaceShip.h"
     48#include "util/Sleep.h"
    4849#include "ClientInformation.h"
    4950#include "ConnectionManager.h"
     
    6566 
    6667  ConnectionManager::ConnectionManager():receiverThread_(0){}
     68  boost::recursive_mutex ConnectionManager::enet_mutex_;
    6769 
    6870  ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     
    7274    head_ = head;
    7375  }
     76 
     77  ConnectionManager::ConnectionManager(ClientInformation *head, int port){
     78    quit=false;
     79    bindAddress.host = ENET_HOST_ANY;
     80    bindAddress.port = port;
     81    head_ = head;
     82  }
    7483
    7584  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) {
     
    8796  }
    8897
    89   ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
     98  /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
    9099    if(!buffer.isEmpty())
    91100      return buffer.pop(address);
    92101    else
    93102      return NULL;
    94   }
     103  }*/
    95104/**
    96105This function only pops the first element in PacketBuffer (first in first out)
    97106used by processQueue in Server.cc
    98107*/
    99   ENetPacket *ConnectionManager::getPacket(int &clientID) {
     108  /*ENetPacket *ConnectionManager::getPacket(int &clientID) {
    100109    ENetAddress address;
    101110    ENetPacket *packet=getPacket(address);
     
    105114    clientID=temp->getID();
    106115    return packet;
     116  }*/
     117 
     118  ENetEvent *ConnectionManager::getEvent(){
     119    if(!buffer.isEmpty())
     120      return buffer.pop();
     121    else
     122      return NULL;
    107123  }
    108124
     
    129145    if(!temp)
    130146      return false;
     147    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    131148    if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0)
    132149      return false;
     
    136153  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
    137154    ClientInformation *temp = head_->findClient(clientID);
    138     if(!temp)
    139       return false;
    140     if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0)
    141       return false;
     155    if(!temp){
     156      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
     157      return false;
     158    }
     159    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     160    if(enet_peer_send(temp->getPeer(), 0, packet)!=0){
     161      COUT(3) << "C.Man: addPacket enet_peer_send failed" << std::endl;
     162      return false;
     163    }
    142164    return true;
    143165  }
    144166
    145167  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
     168    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    146169    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
    147170      if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
     
    151174  }
    152175
    153   bool ConnectionManager::sendPackets(ENetEvent *event) {
     176  // we actually dont need that function, because host_service does that for us
     177  bool ConnectionManager::sendPackets() {
    154178    if(server==NULL)
    155179      return false;
    156     if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
    157       return true;
    158     else
    159       return false;
    160   }
    161 
    162   bool ConnectionManager::sendPackets() {
    163     ENetEvent event;
    164     if(server==NULL)
    165       return false;
    166     if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
    167       return true;
    168     else
    169       return false;
     180    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     181    enet_host_flush(server);
     182    lock.unlock();
     183    return true;
    170184  }
    171185
    172186  void ConnectionManager::receiverThread() {
    173187    // what about some error-handling here ?
    174     enet_initialize();
     188    ENetEvent *event;
    175189    atexit(enet_deinitialize);
    176     ENetEvent *event = new ENetEvent;
    177     server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     190    { //scope of the mutex
     191      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     192      enet_initialize();
     193      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     194      lock.unlock();
     195    }
    178196    if(server==NULL){
    179197      // add some error handling here ==========================
     
    182200    }
    183201
     202    event = new ENetEvent;
    184203    while(!quit){
    185       if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    186         // we should never reach this point
    187         quit=true;
    188         // add some error handling here ========================
     204      { //mutex scope
     205        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     206        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
     207          // we should never reach this point
     208          quit=true;
     209          continue;
     210          // add some error handling here ========================
     211        }
     212        lock.unlock();
    189213      }
    190214      switch(event->type){
    191215        // log handling ================
    192216        case ENET_EVENT_TYPE_CONNECT:
    193           addClient(event);
     217          COUT(3) << "adding event_type_connect to queue" << std::endl;
     218        case ENET_EVENT_TYPE_DISCONNECT:
     219          //addClient(event);
    194220          //this is a workaround to ensure thread safety
    195           COUT(5) << "Con.Man: connection event has occured" << std::endl;
    196           break;
     221          //COUT(5) << "Con.Man: connection event has occured" << std::endl;
     222          //break;
    197223        case ENET_EVENT_TYPE_RECEIVE:
    198224          //std::cout << "received data" << std::endl;
    199225          COUT(5) << "Con.Man: receive event has occured" << std::endl;
    200226          // only add, if client has connected yet and not been disconnected
    201           if(head_->findClient(&event->peer->address))
     227          //if(head_->findClient(&event->peer->address))
    202228            processData(event);
    203           else
    204             COUT(3) << "received a packet from a client we don't know" << std::endl;
     229            event = new ENetEvent;
     230//           else
     231//             COUT(3) << "received a packet from a client we don't know" << std::endl;
    205232          break;
    206         case ENET_EVENT_TYPE_DISCONNECT:
    207           clientDisconnect(event->peer);
     233        //case ENET_EVENT_TYPE_DISCONNECT:
     234          //clientDisconnect(event->peer);
     235          //break;
     236        case ENET_EVENT_TYPE_NONE:
     237          //receiverThread_->yield();
     238          usleep(1000);
    208239          break;
    209         case ENET_EVENT_TYPE_NONE:
    210           receiverThread_->yield();
    211           break;
    212240      }
    213241//       usleep(100);
    214       receiverThread_->yield(); //TODO: find apropriate
     242      //receiverThread_->yield(); //TODO: find apropriate
    215243    }
    216244    disconnectClients();
    217245    // if we're finishied, destroy server
    218     enet_host_destroy(server);
     246    {
     247      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     248      enet_host_destroy(server);
     249      lock.unlock();
     250    }
    219251  }
    220252 
     
    226258    ClientInformation *temp = head_->next();
    227259    while(temp!=0){
    228       enet_peer_disconnect(temp->getPeer(), 0);
     260      {
     261        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     262        enet_peer_disconnect(temp->getPeer(), 0);
     263        lock.unlock();
     264      }
    229265      temp = temp->next();
    230266    }
    231267    //bugfix: might be the reason why server crashes when clients disconnects
    232     //temp = temp->next();
    233268    temp = head_->next();
    234     while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
     269    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     270    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
    235271      switch (event.type)
    236272      {
     
    258294  }
    259295
    260   bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
    261     COUT(4) << "removing client from list" << std::endl;
    262     return removeClient(head_->findClient(&(peer->address))->getID());
    263   }
    264 /**
    265 This function adds a client that connects to the clientlist of the server
    266 NOTE: if you change this, don't forget to change the test function
    267 addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid
    268 */
    269   bool ConnectionManager::addClient(ENetEvent *event) {
    270     ClientInformation *temp = head_->insertBack(new ClientInformation);
    271     if(!temp){
    272       COUT(2) << "Conn.Man. could not add client" << std::endl;
    273       return false;
    274     }
    275     if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
    276       temp->prev()->setID(0); //bugfix: not necessary but usefull
    277       temp->setID(1);
    278     }
    279     else
    280       temp->setID(temp->prev()->getID()+1);
    281     temp->setPeer(event->peer);
    282     COUT(3) << "Con.Man: added client id: " << temp->getID() << std::endl;
    283     return true;
    284   }
     296
    285297
    286298  int ConnectionManager::getClientID(ENetPeer peer) {
     
    316328      ++it;
    317329    }
    318     sendPackets();
     330    //sendPackets();
    319331    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
    320332  }
    321333
    322   bool ConnectionManager::createClient(int clientID){
    323     ClientInformation *temp = head_->findClient(clientID);
    324     if(!temp){
    325       COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
    326       return false;
    327     }
    328     COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
    329     syncClassid(temp->getID());
    330     COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
    331     // TODO: this is only a hack, untill we have a possibility to define default player-join actions
    332     if(!createShip(temp))
    333       COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
    334     else
    335       COUT(3) << "created spaceship" << std::endl;
    336     temp->setSynched(true);
    337     COUT(3) << "sending welcome" << std::endl;
    338     sendWelcome(temp->getID(), temp->getShipID(), true);
    339     return true;
    340   }
    341  
    342   bool ConnectionManager::removeClient(int clientID){
    343     orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
    344     ClientInformation *client = head_->findClient(clientID);
    345     if(!client)
    346       return false;
    347     while(it){
    348       if(it->objectID!=client->getShipID()){
    349         ++it;
    350         continue;
    351       }
    352       orxonox::Iterator<orxonox::SpaceShip> temp=it;
    353       ++it;
    354       delete  *temp;
    355       return head_->removeClient(clientID);
    356     }
    357     return false;
    358   }
    359  
    360   bool ConnectionManager::createShip(ClientInformation *client){
    361     if(!client)
    362       return false;
    363     orxonox::Identifier* id = ID("SpaceShip");
    364     if(!id){
    365       COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
    366       return false;
    367     }
    368     orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
    369     no->setPosition(orxonox::Vector3(0,80,0));
    370     no->setScale(10);
    371     no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
    372     no->setMesh("assff.mesh");
    373     no->setMaxSpeed(500);
    374     no->setMaxSideAndBackSpeed(50);
    375     no->setMaxRotation(1.0);
    376     no->setTransAcc(200);
    377     no->setRotAcc(3.0);
    378     no->setTransDamp(75);
    379     no->setRotDamp(1.0);
    380     no->setCamera("cam_"+client->getID());
    381     no->classID = id->getNetworkID();
    382     no->create();
    383    
    384     client->setShipID(no->objectID);
    385     return true;
    386   }
     334 
    387335 
    388336  bool ConnectionManager::removeShip(ClientInformation *client){
     
    399347  bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){
    400348    if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){
    401       sendPackets();
     349      //sendPackets();
    402350      return true;
    403351    }else
     
    406354 
    407355  void ConnectionManager::disconnectClient(ClientInformation *client){
    408     enet_peer_disconnect(client->getPeer(), 0);
     356    {
     357      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     358      enet_peer_disconnect(client->getPeer(), 0);
     359      lock.unlock();
     360    }
    409361    removeShip(client);
    410362  }
     
    418370 
    419371 
    420 //   int ConnectionManager::getNumberOfClients() {
    421 //     
    422 //     return clientsShip.size();
    423 //   }
    424  
    425   /*void ConnectionManager::addClientsObjectID( int clientID, int objectID ) {
    426   COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl;
    427   clientsShip.insert( std::make_pair( clientID, objectID ) );
     372
    428373}
    429 
    430   int ConnectionManager::getClientsShipID( int clientID ) {
    431   return clientsShip[clientID];
    432 }
    433 
    434   int ConnectionManager::getObjectsClientID( int objectID ) {
    435   std::map<int, int>::iterator iter;
    436   for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
    437   if( iter->second == objectID ) return iter->first;
    438 }
    439   return -99;
    440 }
    441 
    442   void ConnectionManager::deleteClientIDReg( int clientID ) {
    443   clientsShip.erase( clientID );
    444 }
    445 
    446   void ConnectionManager::deleteObjectIDReg( int objectID ) {
    447   std::map<int, int>::iterator iter = clientsShip.begin();
    448   for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
    449   if( iter->second == objectID ) break;
    450 }
    451   clientsShip.erase( iter->first );
    452 }*/
    453 }
  • code/trunk/src/network/ConnectionManager.h

    r1293 r1502  
    4848#include <enet/enet.h>
    4949#include <boost/thread/thread.hpp>
     50#include <boost/thread/recursive_mutex.hpp>
    5051
    5152#include "PacketBuffer.h"
     
    6162#define NETWORK_PORT 55556
    6263#define NETWORK_MAX_CONNECTIONS 50
    63 #define NETWORK_WAIT_TIMEOUT 5000
    64 #define NETWORK_SEND_WAIT 5
     64#define NETWORK_WAIT_TIMEOUT 1
    6565
    6666  struct ClientList{
     
    7474    ConnectionManager();
    7575    ConnectionManager(ClientInformation *head);
     76    ConnectionManager(ClientInformation *head, int port);
    7677    ConnectionManager(int port, const char *address, ClientInformation *head);
    7778    ConnectionManager(int port, std::string address, ClientInformation *head);
    78     ENetPacket *getPacket(ENetAddress &address); // thread1
    79     ENetPacket *getPacket(int &clientID);
     79    //ENetPacket *getPacket(ENetAddress &address); // thread1
     80    //ENetPacket *getPacket(int &clientID);
     81    ENetEvent *getEvent();
    8082    bool queueEmpty();
    8183    void createListener();
     
    8486    bool addPacket(ENetPacket *packet, int ID);
    8587    bool addPacketAll(ENetPacket *packet);
    86     bool sendPackets(ENetEvent *event);
     88  //  bool sendPackets(ENetEvent *event);
    8789    bool sendPackets();
    88     bool createClient(int clientID);
     90    //bool createClient(int clientID);
    8991    void disconnectClient(ClientInformation *client);
     92    void syncClassid(int clientID);
     93    bool sendWelcome(int clientID, int shipID, bool allowed);
    9094
    9195  private:
    92     bool clientDisconnect(ENetPeer *peer);
    93     bool removeClient(int clientID);
     96//     bool clientDisconnect(ENetPeer *peer);
     97//     bool removeClient(int clientID);
    9498    bool processData(ENetEvent *event);
    95     bool addClient(ENetEvent *event);
     99    //bool addClient(ENetEvent *event);
    96100    void receiverThread();
    97101    void disconnectClients();
    98102    int getClientID(ENetPeer peer);
    99103    int getClientID(ENetAddress address);
    100     void syncClassid(int clientID);
    101104    ENetPeer *getClientPeer(int clientID);
    102     bool createShip(ClientInformation *client);
     105    //bool createShip(ClientInformation *client);
    103106    bool removeShip(ClientInformation *client);
    104     bool sendWelcome(int clientID, int shipID, bool allowed);
    105107    bool addFakeConnectRequest(ENetEvent *ev);
    106108    PacketBuffer buffer;
     
    114116
    115117    boost::thread *receiverThread_;
     118    static boost::recursive_mutex enet_mutex_;
    116119//     int getNumberOfClients();
    117120    //functions to map what object every clients uses
  • code/trunk/src/network/GameStateClient.cc

    r1360 r1502  
    2121 *
    2222 *   Author:
    23  *      ...
     23 *      Oliver Scheuss
    2424 *   Co-authors:
    25  *      ...
     25 *      Dumeni Manatschal
    2626 *
    2727 */
     
    3535#include "Synchronisable.h"
    3636
    37 #define GAMESTATEID_INITIAL -1
    3837
    3938namespace network
     
    4342    int id;
    4443  };
    45  
     44
    4645  GameStateClient::GameStateClient() {
    4746    COUT(5) << "this: " << this << std::endl;
    4847    last_diff_=0;
    4948    last_gamestate_=GAMESTATEID_INITIAL-1;
     49    tempGameState_=NULL;
     50    myShip_=NULL;
    5051  }
    5152
     
    9596    return false;
    9697  }
    97  
     98
    9899  GameStateCompressed *GameStateClient::popPartialGameState(){
    99100    GameState *gs = getPartialSnapshot();
     101    if(!gs)
     102      return NULL;
    100103    GameStateCompressed *cgs = compress_(gs);
    101104    delete[] gs->data;
     
    103106    return cgs;
    104107  }
    105  
     108
     109  void GameStateClient::addGameState(GameStateCompressed *gs){
     110    if(tempGameState_!=NULL){
     111      //delete the obsolete gamestate
     112      if(tempGameState_->id>gs->id)
     113        return;
     114      delete[] tempGameState_->data;
     115      delete tempGameState_;
     116    }
     117    tempGameState_=gs;
     118  }
     119  int GameStateClient::processGameState(){
     120    if(tempGameState_==NULL)
     121      return 0;
     122    int id=tempGameState_->id;
     123    bool b = saveShipCache();
     124    if(pushGameState(tempGameState_)){
     125      if(b)
     126        loadShipCache();
     127      return id;
     128    }
     129    else
     130      return GAMESTATEID_INITIAL;
     131  }
     132
    106133
    107134  /**
     
    151178          orxonox::Identifier* id = ID((unsigned int)sync.classID);
    152179          if(!id){
    153             COUT(4) << "We could not identify a new object; classid: " << sync.classID << std::endl;
    154             return false;
     180            COUT(3) << "We could not identify a new object; classid: " << sync.classID << " uint: " << (unsigned int)sync.classID << " objectID: " << sync.objectID << " size: " << sync.length << std::endl;
     181            return false; // most probably the gamestate is corrupted
    155182          }
    156183          Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate());
     
    168195          }
    169196          if( !no->create() )
     197          {
    170198            COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
     199          }
    171200          it=orxonox::ObjectList<Synchronisable>::end();
    172201        }
     
    174203        // we have our object
    175204        if(! it->updateData(sync))
     205        {
    176206          COUT(1) << "We couldn't update objectID: " \
    177207          << sync.objectID << "; classID: " << sync.classID << std::endl;
     208        }
    178209      }
    179210      ++it;
     
    213244    }
    214245    //retval->data = (unsigned char*)malloc(size);
     246    if(size==0)
     247      return NULL;
    215248    retval->data = new unsigned char[size];
    216249    if(!retval->data){
     
    245278    return retval;
    246279  }
    247  
    248  
     280
     281
    249282  GameState *GameStateClient::undiff(GameState *old, GameState *diff) {
    250283    if(!old || !diff)
     
    253286    int of=0; // pointers offset
    254287    int dest_length=0;
    255     if(old->size>=diff->size)
     288    /*if(old->size>=diff->size)
    256289      dest_length=old->size;
    257     else
     290    else*/
    258291      dest_length=diff->size;
    259292//     unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
     293    if(dest_length==0)
     294      return NULL;
    260295    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
    261296    while(of<old->size && of<diff->size){
     
    270305          of++;
    271306        }
    272       } else{
     307      } /*else{
    273308        while(of<dest_length){
    274309          *(dp+of)=*(ap+of)^n;
    275310          of++;
    276311        }
    277       }
     312      }*/
    278313    }
    279314    // should be finished now
     
    297332
    298333    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
     334    if(buffer==0)
     335      return NULL;
    299336    unsigned char *dest = new unsigned char[buffer];
    300337    int retval;
     
    303340    switch ( retval ) {
    304341      case Z_OK: COUT(5) << "G.St.Cl: compress: successfully compressed" << std::endl; break;
    305       case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 
     342      case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl;
    306343      return NULL;
    307344      case Z_BUF_ERROR: COUT(2) << "G.St.Cl: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
     
    321358    return compressedGamestate;
    322359  }
    323  
    324  
     360
     361
    325362  GameState *GameStateClient::decompress(GameStateCompressed *a) {
    326363    //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
     
    333370      bufsize = normsize;
    334371//     unsigned char* dest = (unsigned char*)malloc( bufsize );
     372    if(bufsize==0)
     373      return NULL;
    335374    unsigned char *dest = new unsigned char[bufsize];
    336375    int retval;
     
    376415    return t;
    377416  }
    378  
     417
    379418  void GameStateClient::cleanup(){
    380419    std::map<int, GameState*>::iterator temp, it = gameStateMap.begin();
     
    388427      gameStateMap.erase(temp);
    389428    }
     429    tempGameState_=NULL;
    390430  }
    391431
     
    397437    }
    398438    COUT(4) << std::endl;
    399    
    400   }
    401  
    402  
     439
     440  }
     441
     442  bool GameStateClient::saveShipCache(){
     443    if(myShip_==NULL)
     444      myShip_ = orxonox::SpaceShip::getLocalShip();
     445    if(myShip_){
     446      //      unsigned char *data = new unsigned char[myShip_->getSize()];
     447      int size=myShip_->getSize(0x3);
     448      if(size==0)
     449        return false;
     450      unsigned char *data = new unsigned char [size];
     451      shipCache_ = myShip_->getData(data, 0x1);
     452      return true;
     453    }else
     454      return false;
     455  }
     456
     457  bool GameStateClient::loadShipCache(){
     458    if(myShip_){
     459      myShip_->updateData(shipCache_, 0x2);
     460      if(shipCache_.data){
     461        delete[] shipCache_.data;
     462      }
     463      return true;
     464    }else
     465      return false;
     466  }
     467
    403468    //##### ADDED FOR TESTING PURPOSE #####
    404469  GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
    405470    return decompress( gc );
    406471  }
    407  
     472
    408473  GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
    409474    return undiff( g_old, g_diffed );
    410475  }
    411476  //##### ADDED FOR TESTING PURPOSE #####
    412  
    413  
     477
     478
    414479}
    415480
  • code/trunk/src/network/GameStateClient.h

    r1360 r1502  
    2121 *
    2222 *   Author:
    23  *      ...
     23 *      Oliver Scheuss
    2424 *   Co-authors:
    25  *      ...
     25 *      Dumeni Manatschal
    2626 *
    2727 */
     
    3333//
    3434//
    35 // Author:  <>, (C) 2007
     35// Author:  Oliver Scheuss, (C) 2007
    3636//
    3737// Copyright: See COPYING file that comes with this distribution
     
    4646#include "core/CorePrereqs.h"
    4747#include "PacketTypes.h"
     48#include "objects/SpaceShip.h"
    4849
     50
     51#define GAMESTATEID_INITIAL -1
    4952
    5053namespace network
     
    5659    ~GameStateClient();
    5760   
     61    void addGameState(GameStateCompressed *gs);
     62    int processGameState();
     63    GameStateCompressed *popPartialGameState();
     64    void cleanup();
     65  private:
    5866    bool pushGameState(GameStateCompressed *compstate);
    59     GameStateCompressed *popPartialGameState();
    60   private:
    6167    bool loadSnapshot(GameState *state);
    6268    GameState *getPartialSnapshot();
    63     void cleanup();
    6469    GameState *undiff(GameState *old, GameState *diff);
    6570    GameStateCompressed *compress_(GameState *a);
     
    6974    void removeObject(orxonox::Iterator<Synchronisable> &it);
    7075    void printGameStateMap();
     76    bool saveShipCache();
     77    bool loadShipCache();
    7178
    7279    int           last_diff_;
    7380    int           last_gamestate_;
    7481    std::map<int, GameState *> gameStateMap;
     82    GameStateCompressed *tempGameState_; // we save the received gamestates here during processQueue
     83    orxonox::SpaceShip *myShip_;
     84    syncData shipCache_;
    7585   
    7686   
  • code/trunk/src/network/GameStateManager.cc

    r1360 r1502  
    6868    printGameStates();
    6969    return;
     70  }
     71 
     72  void GameStateManager::addGameState(GameStateCompressed *gs, int clientID){
     73    if(!gs)
     74      return;
     75    std::map<int, GameStateCompressed*>::iterator it = gameStateQueue.find(clientID);
     76    if(it!=gameStateQueue.end()){
     77      // delete obsolete gamestate
     78      delete[] it->second->data;
     79      delete it->second;
     80    }
     81    gameStateQueue[clientID] = gs;
     82    return;
     83  }
     84 
     85  void GameStateManager::processGameStates(){
     86    std::map<int, GameStateCompressed*>::iterator it;
     87    // now push only the most recent gamestates we received (ignore obsolete ones)
     88    for(it = gameStateQueue.begin(); it!=gameStateQueue.end(); it++){
     89      pushGameState(it->second, it->first);
     90    }
     91    // now clear the queue
     92    gameStateQueue.clear();
    7093  }
    7194 
     
    117140        client = it->second;
    118141      GameState *server = reference;
    119       COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl;
     142      COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl;
    120143      if(client)
    121144        return encode(client, server);
     
    170193    }
    171194    //retval->data = (unsigned char*)malloc(size);
     195    if(size==0)
     196      return NULL;
    172197    retval->data = new unsigned char[size];
    173198    if(!retval->data){
     
    237262      data+=sizeof(int);
    238263      sync.classID = *(int*)data;
     264      if(sync.classID == 0) // TODO: remove this
     265        COUT(3) << "received a classid 0" << std::endl;
    239266      data+=sizeof(int);
    240267      sync.data = data;
     
    246273
    247274      if(!it){
    248         // the object does not exist yet
     275        // the objectaber ich glaub die does not exist yet
    249276        COUT(4) << "loadsnapshot: creating new object " << std::endl;
    250277        //COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
     
    314341  }
    315342
    316   GameState *GameStateManager::diff(GameState *a, GameState *b) {
    317     unsigned char *ap = a->data, *bp = b->data;
     343  GameState *GameStateManager::diff(GameState *alt, GameState *neu) {
     344    unsigned char *ap = alt->data, *bp = neu->data;
    318345    int of=0; // pointers offset
    319346    int dest_length=0;
    320     if(a->size>=b->size)
    321       dest_length=a->size;
    322     else
    323       dest_length=b->size;
     347    /*if(alt->size>neu->size)
     348      dest_length=alt->size;
     349    else*/
     350      dest_length=neu->size;
     351    if(dest_length==0)
     352      return NULL;
    324353    //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
    325354    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
    326     while(of<a->size && of<b->size){
     355    while(of<alt->size && of<neu->size){
    327356      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
    328357      ++of;
    329358    }
    330     if(a->size!=b->size){ // do we have to fill up ?
     359    if(alt->size!=neu->size){ // do we have to fill up ?
    331360      unsigned char n=0;
    332       if(a->size<b->size){
     361      if(alt->size<neu->size){
    333362        while(of<dest_length){
    334363          *(dp+of)=n^*(bp+of);
    335364          of++;
    336365        }
    337       } else{
     366      } /*else{
    338367        while(of<dest_length){
    339368          *(dp+of)=*(ap+of)^n;
    340369          of++;
    341370        }
    342       }
     371      }*/
    343372    }
    344373
    345374    GameState *r = new GameState;
    346     r->id = b->id;
     375    r->id = neu->id;
    347376    r->size = dest_length;
    348377    r->diffed = true;
    349     r->base_id = a->id;
     378    r->base_id = alt->id;
    350379    r->data = dp;
    351380    r->complete = true;
     
    362391    //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
    363392    //unsigned char* dest = (unsigned char*)malloc( buffer );
     393    if(buffer==0)
     394      return NULL;
    364395    unsigned char *dest = new unsigned char[buffer];
    365396    //COUT(4) << "dest: " << dest << std::endl;
     
    404435      bufsize = normsize;
    405436//     unsigned char* dest = (unsigned char*)malloc( bufsize );
     437    if(bufsize==0)
     438      return NULL;
    406439    unsigned char *dest = new unsigned char[bufsize];
    407440    int retval;
     
    436469      return;
    437470    int curid = temp->getGamestateID();
     471   
     472    if(gamestateID == GAMESTATEID_INITIAL){
     473      temp->setGameStateID(GAMESTATEID_INITIAL);
     474      if(curid!=GAMESTATEID_INITIAL)
     475        --(gameStateUsed.find(curid)->second);
     476      return;
     477    }
    438478    if(curid > gamestateID)
    439479      // the network packets got messed up
     
    446486      --(gameStateUsed.find(curid)->second);
    447487    ++(gameStateUsed.find(gamestateID)->second);
    448     temp->setGamestateID(gamestateID);
     488    temp->setGameStateID(gamestateID);
    449489    /*
    450490    GameState *old = clientGameState[clientID];
  • code/trunk/src/network/GameStateManager.h

    r1293 r1502  
    7272    ~GameStateManager();
    7373   
     74    void addGameState(GameStateCompressed *gs, int clientID);
     75    void processGameStates();
     76   
    7477    void update();
    7578    GameStateCompressed *popGameState(int clientID);
    76     bool pushGameState(GameStateCompressed *gs, int clientID);
    7779    void ackGameState(int clientID, int gamestateID);
    7880    void removeClient(ClientInformation *client);
    79   private:
     81    private:
     82    bool pushGameState(GameStateCompressed *gs, int clientID);
    8083    void cleanup(); // "garbage handler"
    8184    GameState *getSnapshot();
     
    8386    GameStateCompressed *encode(GameState *a, GameState *b);
    8487    GameStateCompressed *encode(GameState *a);
    85     GameState *diff(GameState *a, GameState *b);
     88    GameState *diff(GameState *alt, GameState *neu);
    8689    GameStateCompressed *compress_(GameState *a);
    8790    GameState *decompress(GameStateCompressed *a);
     
    9194    std::map<int, GameState*> gameStateMap; //map gsID to gamestate*
    9295    std::map<int, int> gameStateUsed; // save the number of clients, that use the specific gamestate
     96    std::map<int, GameStateCompressed*> gameStateQueue;
    9397    GameState *reference;
    9498    ClientInformation *head_;
  • code/trunk/src/network/NetworkPrereqs.h

    r1360 r1502  
    3535#define _NetworkPrereqs_H__
    3636
    37 #include "OrxonoxPlatform.h"
     37#include "util/OrxonoxPlatform.h"
    3838
    3939//-----------------------------------------------------------------------
     
    5454#else
    5555#  define _NetworkExport
    56 #endif
    57 
    58 //-----------------------------------------------------------------------
    59 // fixed width integers
    60 //-----------------------------------------------------------------------
    61 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC
    62 typedef __int8            int8_t;
    63 typedef __int16           int16_t;
    64 typedef __int32           int32_t;
    65 typedef __int64           int64_t;
    66 typedef unsigned __int8   uint8_t;
    67 typedef unsigned __int16  uint16_t;
    68 typedef unsigned __int32  uint32_t;
    69 typedef unsigned __int64  uint64_t;
    70 #else
    71 # include "inttypes.h"
    7256#endif
    7357
  • code/trunk/src/network/PacketBuffer.cc

    r1360 r1502  
    6262      last->next=NULL;
    6363      // change this!!!!!!!  ---- we are not doing stl so we won't change this
    64       last->packet = ev->packet;
    65       last->address = ev->peer->address;
     64      last->event = ev;
    6665      //last->address = ev->peer->address;
    6766    } else {
     
    7271      last->next=NULL;
    7372      // save the packet to the new element
    74       last->packet = ev->packet;
    75       last->address = ev->peer->address;
     73      last->event = ev;
    7674      //last->address = ev->peer->address;
    7775    }
     
    8280  //returns the first element in the list without deleting it but
    8381  //moving first pointer to next element
    84   ENetPacket *PacketBuffer::pop() {
     82  /*ENetPacket *PacketBuffer::pop() {
    8583    ENetAddress address;
    8684    return pop(address);
    87   }
    88 
    89   ENetPacket *PacketBuffer::pop(ENetAddress &address) {
     85  }*/
     86 
     87  ENetEvent *PacketBuffer::pop(){
    9088    boost::recursive_mutex::scoped_lock lock(mutex_);
    9189    //std::cout << "packetbuffer pop(address)" << std::endl;
     
    9391      QueueItem *temp = first;
    9492      // get packet
    95       ENetPacket *pck=first->packet;
    96       address = first->address;
     93      ENetEvent *ev=first->event;
     94      //address = first->address;
     95      // remove first element
     96      first = first->next;
     97      delete temp;
     98      lock.unlock();
     99      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
     100      return ev;
     101    } else{
     102      lock.unlock();
     103      return NULL;
     104    }
     105  }
     106
     107  /*ENetPacket *PacketBuffer::pop(ENetAddress &address) {
     108    boost::recursive_mutex::scoped_lock lock(mutex_);
     109    //std::cout << "packetbuffer pop(address)" << std::endl;
     110    if(first!=NULL ){
     111      QueueItem *temp = first;
     112      // get packet
     113      ENetPacket *pck=first->event->packet;
     114      address = first->event->peer->address;
    97115      // remove first element
    98116      first = first->next;
     
    105123      return NULL;
    106124    }
    107   }
     125  }*/
    108126
    109127  bool PacketBuffer::isEmpty() {
  • code/trunk/src/network/PacketBuffer.h

    r1360 r1502  
    5555
    5656  struct QueueItem{
    57     ENetPacket *packet;
    58     ENetAddress address;
     57    ENetEvent *event;
     58    //ENetAddress address;
    5959    QueueItem *next;
    6060  };
     
    6868    void print();
    6969    // pops a packet from the queue
    70     ENetPacket *pop();
    71     ENetPacket *pop(ENetAddress &address);
     70    //ENetPacket *pop();
     71    //ENetPacket *pop(ENetAddress &address);
     72    ENetEvent *pop();
    7273    // pushs a packet to the queue
    7374    bool push(ENetEvent *ev);
  • code/trunk/src/network/PacketDecoder.cc

    r1360 r1502  
    6868    case COMMAND:
    6969      return command( packet, clientId );
    70     case MOUSE:
    71       mousem( packet, clientId );
    72       return true;
    73     case KEYBOARD:
    74       keystrike( packet, clientId );
    75       return true;
    7670    case CHAT:
    7771      chatMessage( packet, clientId );
     
    9084    return false;
    9185  }
    92  
     86
    9387  bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){
    9488    uint32_t submittetcrc;
     
    108102    return false;
    109103  }
    110  
     104
    111105  // ATTENTION: TODO watch, that arguments we pass over to the processFunction gets deleted in THE PROCESSXXX function
    112106
     
    124118    enet_packet_destroy( packet );
    125119  }
    126  
     120
    127121  bool PacketDecoder::command( ENetPacket* packet, int clientId ){
    128122    int length = *(int*)((unsigned char *)packet->data+sizeof(int));
     123    if(length<=0)
     124      return false;
    129125    void *data = (void *)new unsigned char[length];
    130126    memcpy(data, (void *)(packet->data+2*sizeof(int)), length);
     
    133129  }
    134130
    135   void PacketDecoder::mousem( ENetPacket* packet, int clientId )
    136   {
    137     mouse* mouseMove = new mouse;
    138     //copy data of packet->data to new struct
    139     *mouseMove = *(mouse*)packet->data;
    140 
    141     //clean memory
    142     enet_packet_destroy( packet );
    143     printMouse( mouseMove ); //debug info
    144   }
    145 
    146   void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
    147   {
    148     keyboard* key = new keyboard;
    149     *key = *(keyboard*)packet->data; //see above
    150 
    151     //clean memory
    152     enet_packet_destroy( packet );
    153     printKey( key ); //debug info
    154 
    155   }
    156 
    157131  void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
    158132  {
    159133    chat* chatting = new chat;
     134    if(packet->dataLength==4)
     135      return;
    160136    chatting->id = (int)*packet->data; //first copy id into new struct
    161137    //since the chat message is a char*, allocate the memory needed
     
    185161      return;
    186162    }
    187     //since it's not alowed to use void* for pointer arithmetic
    188     //FIXME: variable never used
    189     unsigned char* data = (unsigned char *)(packet->data);
    190163    //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
    191164    memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
     
    202175    //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
    203176    if(currentState->compsize==0)
     177    {
    204178      COUT(2) << "PacketDecoder: compsize is 0" << std::endl;
     179    }
    205180//     currentState->data = (unsigned char*)(malloc( currentState->compsize ));
     181    if(currentState->compsize==0)
     182      return;
    206183    currentState->data = new unsigned char[currentState->compsize];
    207184    if(currentState->data==NULL)
     185    {
    208186      COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl;
     187    }
    209188    //copy the GameStateCompressed data
    210189    memcpy( (void*)(currentState->data), (const void*)(packet->data+5*sizeof( int ) + 2*sizeof(bool)), currentState->compsize );
     
    221200    cid->id = ((classid *)(packet->data))->id;
    222201    cid->clid = ((classid *)(packet->data))->clid;
     202    if(cid->length==0)
     203      return;
    223204//     cid->message = (const char *)malloc(cid->length);
    224205    cid->message = new char[cid->length];
     
    229210    processClassid(cid);
    230211  }
    231  
    232  
     212
     213
    233214  bool PacketDecoder::decodeWelcome( ENetPacket* packet, int clientID ){
    234215    welcome *w = new welcome;
     
    240221    return processWelcome(w);
    241222  }
    242  
     223
    243224  bool PacketDecoder::decodeConnectRequest( ENetPacket *packet, int clientID ){
    244225    connectRequest *con = new connectRequest;
     
    279260    return;
    280261  }
    281  
     262
    282263  bool PacketDecoder::processWelcome( welcome *w ){
    283264    delete w;
    284265    return true;
    285266  }
    286  
     267
    287268  bool PacketDecoder::processConnectRequest( connectRequest *con, int clientID ){
    288269    COUT(3) << "packetdecoder: processing connectRequest" << std::endl;
     
    299280  }
    300281
    301   void PacketDecoder::printMouse( mouse* data )
    302   {
    303     COUT(5) << "data id: " << data->id << std::endl;
    304     COUT(5) << "data:    " << data->x << " " << data->y << std::endl;
    305   }
    306 
    307   void PacketDecoder::printKey( keyboard* data )
    308   {
    309     COUT(5) << "data id: " << data->id << std::endl;
    310     COUT(5) << "data:    " << (char)data->press << std::endl;
    311   }
    312282
    313283  void PacketDecoder::printChat( chat* data, int clientId )
    314284  {
    315285    if(clientId!=CLIENTID_CLIENT)
     286    {
    316287      COUT(5) << "client: " << clientId << std::endl;
     288    }
    317289    COUT(5) << "data id: " << data->id << std::endl;
    318290    COUT(5) << "data:    " << data->message << std::endl;
  • code/trunk/src/network/PacketGenerator.cc

    r1360 r1502  
    7878  {
    7979    COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
    80     ack* ackreq = new ack;
     80   
     81    ENetPacket *packet = enet_packet_create( NULL , sizeof( ack ), reliable );
     82    ack* ackreq = (ack *)packet->data;
    8183    ackreq->id = ACK;
    8284    ackreq->a = state;
    83 
    84     ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
    85     delete ackreq;
     85    //delete ackreq;
    8686    return packet;
    8787  }
     
    8989  ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE )
    9090  {
     91    if(dataLength==0)
     92      return NULL;
    9193    unsigned char *stream = new unsigned char[dataLength + 2*sizeof(int)];
    9294    if(!stream)
     
    101103  }
    102104
    103   /*### mouseupdates */
    104   ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
    105   {
    106     COUT(4) << "PacketGenerator: generating new mouse" << std::endl;
    107     mouse* mousemove = new mouse;
    108     mousemove->id = MOUSE;
    109     mousemove->x = x;
    110     mousemove->y = y;
    111 
    112     ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
    113     delete mousemove;
    114     return packet;
    115   }
    116 
    117   /*### keystrikes updates */
    118   ENetPacket* PacketGenerator::keystrike( char press, int reliable )
    119   {
    120     COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;
    121     keyboard* key = new keyboard;
    122     key->id = KEYBOARD;
    123     key->press = press;
    124 
    125     ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
    126     delete key;
    127     return packet;
    128   }
    129105
    130106  /*### chat messages packet */
     
    150126    //std::cout << "totalLen " << totalLen << std::endl;
    151127    //unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
    152     unsigned char *data = new unsigned char[totalLen];
     128    if(totalLen==0)
     129      return NULL;
     130    ENetPacket *packet = enet_packet_create( NULL , totalLen+sizeof(uint32_t), reliable );
     131    //unsigned char *data = new unsigned char[totalLen];
     132    unsigned char *data = packet->data;
    153133    memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id
    154134    memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
     
    163143    //create an enet packet with the generated bytestream
    164144    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
    165     ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
    166     delete[] data;
     145    //delete[] data;
    167146    if(!addCRC(packet))
    168147      COUT(3) << "could not add crc to gamestate packet" << std::endl;
     
    173152  {
    174153    //unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
     154    if(classname.length()==0)
     155      return NULL;
    175156    unsigned char *data = new unsigned char[3*sizeof(int)+classname.length()+1];
    176157    std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl;
     
    206187  bool PacketGenerator::addCRC( ENetPacket *packet){
    207188    unsigned char *data = packet->data;
    208     uint32_t crc32=calcCRC(data, packet->dataLength);
     189    uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t));
    209190    // now append the crc to the packet data
    210     int oldlength = packet->dataLength;
    211     if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
     191    int oldlength = packet->dataLength-sizeof(uint32_t);
     192    //if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
    212193      memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
    213194      return true;
    214     }else{
    215       COUT(3) << "could not add crc to gamestate" << std::endl;
    216       return false;
    217     }
     195    //}else{
     196     // COUT(3) << "could not add crc to gamestate" << std::endl;
     197     // return false;
     198    //}
    218199  }
    219200
  • code/trunk/src/network/PacketManager.h

    r1360 r1502  
    5959    ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability
    6060    ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE );
    61     ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE );
    62     ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );
    6361    ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE );
    6462    ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates
     
    9492    void acknowledgement( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9593    bool command( ENetPacket* packet, int clientId );
    96     void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    97     void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9894    void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9995    void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT );
     
    113109    //print functions
    114110    void printAck( ack* data );
    115     void printMouse( mouse* data );
    116     void printKey( keyboard* data );
    117111    void printChat( chat* data, int clientId );
    118112    void printGamestate( GameStateCompressed *data );
  • code/trunk/src/network/PacketTypes.h

    r1293 r1502  
    4848  enum packet_id {
    4949    ACK,
    50     MOUSE,
    5150    COMMAND,
    52     KEYBOARD,
    5351    CHAT,
    5452    GAMESTATE ,
     
    104102  };
    105103
    106   struct mouse {
    107     int id;
    108     double x;
    109     double y;
    110   };
    111 
    112   struct keyboard {
    113     int id;
    114     char press;
    115   };
    116104  //only in this class, not PacketGenerator, used as pattern to put incoming
    117105  //bytes inside
  • code/trunk/src/network/Server.cc

    r1360 r1502  
    4242
    4343#include <iostream>
     44
    4445
    4546#include "ConnectionManager.h"
     
    4950//#include "NetworkFrameListener.h"
    5051#include "util/Sleep.h"
     52#include "objects/SpaceShip.h"
    5153
    5254
     
    5658 
    5759#define MAX_FAILURES 20;
    58  
     60#define NETWORK_FREQUENCY 30
    5961 
    6062  /**
     
    6365  */
    6466  Server::Server() {
     67    timeSinceLastUpdate_=0;
    6568    packet_gen = PacketGenerator();
    6669    clients = new ClientInformation(true);
     
    6871    gamestates = new GameStateManager(clients);
    6972  }
     73 
     74  Server::Server(int port){
     75    timeSinceLastUpdate_=0;
     76    packet_gen = PacketGenerator();
     77    clients = new ClientInformation(true);
     78    connection = new ConnectionManager(clients, port);
     79    gamestates = new GameStateManager(clients);
     80  }
    7081
    7182  /**
     
    7586  */
    7687  Server::Server(int port, std::string bindAddress) {
     88    timeSinceLastUpdate_=0;
    7789    packet_gen = PacketGenerator();
    7890    clients = new ClientInformation();
     
    8799  */
    88100  Server::Server(int port, const char *bindAddress) {
     101    timeSinceLastUpdate_=0;
    89102    packet_gen = PacketGenerator();
    90103    clients = new ClientInformation();
     
    117130    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
    118131    //std::cout <<"adding packets" << std::endl;
    119     if(connection->addPacketAll(packet))
    120     //std::cout <<"added packets" << std::endl;
    121       return connection->sendPackets();
    122     else
    123       return false;
     132    return connection->addPacketAll(packet);
    124133  }
    125134
     
    132141    ENetPacket *packet = packet_gen.chatMessage(msg);
    133142    COUT(4) <<"Server: adding Packets" << std::endl;
    134     connection->addPacketAll(packet);
    135     //std::cout <<"added packets" << std::endl;
    136     if (connection->sendPackets()){
    137       COUT(4) << "Server: Sucessfully" << std::endl;
    138       return true;
    139     }
    140     return false;
     143    return connection->addPacketAll(packet);
    141144  }
    142145
     
    148151  void Server::tick(float time) {
    149152    processQueue();
    150     updateGamestate();
    151 //     usleep(500000); // TODO remove
     153    //this steers our network frequency
     154    timeSinceLastUpdate_+=time;
     155    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
     156      timeSinceLastUpdate_-=(1./NETWORK_FREQUENCY);
     157      gamestates->processGameStates();
     158      updateGamestate();
     159    }
     160//     usleep(5000); // TODO remove
    152161    return;
    153162  }
     
    157166  */
    158167  void Server::processQueue() {
    159     ENetPacket *packet;
     168    ENetEvent *event;
    160169    int clientID=-1;
    161170    while(!connection->queueEmpty()){
    162171      //std::cout << "Client " << clientID << " sent: " << std::endl;
    163172      //clientID here is a reference to grab clientID from ClientInformation
    164       packet = connection->getPacket(clientID);
    165       if(!packet)
    166         continue;
     173      event = connection->getEvent();
     174      if(!event)
     175        continue;
     176      assert(event->type != ENET_EVENT_TYPE_NONE);
     177      switch( event->type ) {
     178      case ENET_EVENT_TYPE_CONNECT:
     179        COUT(3) << "processing event_Type_connect" << std::endl;
     180        addClient(event);
     181        break;
     182      case ENET_EVENT_TYPE_DISCONNECT:
     183        if(clients->findClient(&event->peer->address))
     184          disconnectClient(event);
     185        break;
     186      case ENET_EVENT_TYPE_RECEIVE:
     187        if(clients->findClient(&event->peer->address)){
     188          clientID = clients->findClient(&event->peer->address)->getID();
     189          if( !elaborate(event->packet, clientID) )
     190            COUT(3) << "Server: could not elaborate" << std::endl;
     191        }
     192        break;
     193      }
     194      delete event;
    167195      //if statement to catch case that packetbuffer is empty
    168       if( !elaborate(packet, clientID) )
    169         COUT(3) << "Server: could not elaborate" << std::endl;
    170196    }
    171197  }
     
    210236      if(gs==NULL){
    211237        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
    212         return false;
     238        continue;
    213239      }
    214240      //std::cout << "adding gamestate" << std::endl;
    215       if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
     241      ENetPacket *packet = packet_gen.gstate(gs);
     242      if(!packet)
     243        continue;
     244      if ( !(connection->addPacket(packet, cid)) ){
    216245        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
    217246        temp->addFailure();
    218         if(temp->getFailures() > 20 )
    219           disconnectClient(temp);
     247        /*if(temp->getFailures() > 0 )
     248          disconnectClient(temp);*/
    220249      //std::cout << "added gamestate" << std::endl;
    221       }
     250      }else
     251        temp->resetFailures();
    222252      added=true;
    223253      temp=temp->next();
     
    226256      delete gs;
    227257    }
    228     if(added) {
     258    /*if(added) {
    229259      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
    230260      return connection->sendPackets();
    231     }
    232     COUT(5) << "Server: had no gamestates to send" << std::endl;
    233     return false;
     261    }*/
     262    //COUT(5) << "Server: had no gamestates to send" << std::endl;
     263    return true;
    234264  }
    235265
    236266  void Server::processAck( ack *data, int clientID) {
    237     COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
     267    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
    238268    gamestates->ackGameState(clientID, data->a);
    239269    delete data;
     
    241271 
    242272  bool Server::processConnectRequest( connectRequest *con, int clientID ){
    243     COUT(3) << "processing connectRequest " << std::endl;
     273    //(COUT(3) << "processing connectRequest " << std::endl;
    244274    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    245     connection->createClient(clientID);
     275    //createClient(clientID);
    246276    delete con;
    247277    return true;
     
    250280  void Server::processGamestate( GameStateCompressed *data, int clientID){
    251281    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
    252     if(!gamestates->pushGameState(data, clientID))
    253         COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
     282    gamestates->addGameState(data, clientID);
     283        /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
    254284    else
    255285      if(clients->findClient(clientID))
    256         clients->findClient(clientID)->resetFailures();
     286        clients->findClient(clientID)->resetFailures();*/
     287  }
     288 
     289  bool Server::addClient(ENetEvent *event){
     290    ClientInformation *temp = clients->insertBack(new ClientInformation);
     291    if(!temp){
     292      COUT(2) << "Server: could not add client" << std::endl;
     293      return false;
     294    }
     295    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
     296      temp->prev()->setID(0); //bugfix: not necessary but usefull
     297      temp->setID(1);
     298    }
     299    else
     300      temp->setID(temp->prev()->getID()+1);
     301    temp->setPeer(event->peer);
     302    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
     303    return createClient(temp->getID());
     304  }
     305 
     306  bool Server::createClient(int clientID){
     307    ClientInformation *temp = clients->findClient(clientID);
     308    if(!temp){
     309      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
     310      return false;
     311    }
     312    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     313    connection->syncClassid(temp->getID());
     314    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
     315    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
     316    if(!createShip(temp))
     317      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
     318    else
     319      COUT(3) << "created spaceship" << std::endl;
     320    temp->setSynched(true);
     321    COUT(3) << "sending welcome" << std::endl;
     322    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
     323    return true;
     324  }
     325 
     326  bool Server::createShip(ClientInformation *client){
     327    if(!client)
     328      return false;
     329    orxonox::Identifier* id = ID("SpaceShip");
     330    if(!id){
     331      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
     332      return false;
     333    }
     334    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
     335    no->setPosition(orxonox::Vector3(0,0,80));
     336    no->setScale(10);
     337    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
     338    no->setMesh("assff.mesh");
     339    no->setMaxSpeed(500);
     340    no->setMaxSideAndBackSpeed(50);
     341    no->setMaxRotation(1.0);
     342    no->setTransAcc(200);
     343    no->setRotAcc(3.0);
     344    no->setTransDamp(75);
     345    no->setRotDamp(1.0);
     346    no->setCamera("cam_"+client->getID());
     347    no->classID = id->getNetworkID();
     348    no->create();
     349   
     350    client->setShipID(no->objectID);
     351    return true;
     352  }
     353 
     354  bool Server::disconnectClient(ENetEvent *event){
     355    COUT(4) << "removing client from list" << std::endl;
     356    //return removeClient(head_->findClient(&(peer->address))->getID());
     357   
     358    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
     359    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
     360    ClientInformation *client = clients->findClient(&event->peer->address);
     361    if(!client)
     362      return false;
     363    while(it){
     364      if(it->objectID!=client->getShipID()){
     365        ++it;
     366        continue;
     367      }
     368      orxonox::Iterator<orxonox::SpaceShip> temp=it;
     369      ++it;
     370      delete  *temp;
     371      return clients->removeClient(event->peer);
     372    }
     373    return false;
    257374  }
    258375
  • code/trunk/src/network/Server.h

    r1293 r1502  
    6060  public:
    6161    Server();
     62    Server(int port);
    6263    Server(int port, std::string bindAddress);
    6364    Server(int port, const char *bindAddress);
     
    7172    void updateGamestate();
    7273  private:
     74    bool addClient(ENetEvent *event);
     75    bool createClient(int clientID);
     76    bool createShip(ClientInformation *client);
     77    bool disconnectClient(ENetEvent *event);
    7378    void disconnectClient(int clientID);
    7479    void disconnectClient( ClientInformation *client);
     
    8287
    8388    ClientInformation *clients;
     89    float timeSinceLastUpdate_;
    8490  };
    8591
  • code/trunk/src/network/Synchronisable.cc

    r1360 r1502  
    6666
    6767  Synchronisable::~Synchronisable(){
     68  }
     69 
     70  bool Synchronisable::create(){
     71    this->classID = this->getIdentifier()->getNetworkID();
     72    COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
     73    return true;
    6874  }
    6975 
     
    151157  * @return data containing all variables and their sizes
    152158  */
    153   syncData Synchronisable::getData(unsigned char *mem){
     159  syncData Synchronisable::getData(unsigned char *mem, int mode){
    154160    //std::cout << "inside getData" << std::endl;
     161    if(mode==0x0)
     162      mode=state_;
    155163    if(classID==0)
    156164      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
     
    167175    for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
    168176      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
    169       if( ((*i)->mode & state_) == 0 ){
     177      if( ((*i)->mode & mode) == 0 ){
    170178        COUT(5) << "not getting data: " << std::endl;
    171179        continue;  // this variable should only be received
     
    194202  * @return true/false
    195203  */
    196   bool Synchronisable::updateData(syncData vars){
     204  bool Synchronisable::updateData(syncData vars, int mode){
     205    if(mode==0x0)
     206      mode=state_;
    197207    unsigned char *data=vars.data;
    198208    std::list<synchronisableVariable *>::iterator i;
     
    203213    COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
    204214    for(i=syncList->begin(); i!=syncList->end(); i++){
    205       if( ((*i)->mode ^ state_) == 0 ){
     215      if( ((*i)->mode ^ mode) == 0 ){
    206216        COUT(5) << "synchronisable: not updating variable " << std::endl;
    207         continue;  // this variable should only be updated
     217        continue;  // this variable should only be set
    208218      }
    209219      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
     
    230240  * @return amount of bytes
    231241  */
    232   int Synchronisable::getSize(){
     242  int Synchronisable::getSize(int mode){
    233243    int tsize=0;
     244    if(mode==0x0)
     245      mode=state_;
    234246    std::list<synchronisableVariable *>::iterator i;
    235247    for(i=syncList->begin(); i!=syncList->end(); i++){
    236       if( ((*i)->mode & state_) == 0 )
     248      if( ((*i)->mode & mode) == 0 )
    237249        continue;  // this variable should only be received, so dont add its size to the send-size
    238250      switch((*i)->type){
  • code/trunk/src/network/Synchronisable.h

    r1293 r1502  
    8383    void registerVar(void *var, int size, variableType t, int mode=1);
    8484    //  syncData getData();
    85     syncData getData(unsigned char *mem);
    86     int getSize();
    87     bool updateData(syncData vars);
     85    syncData getData(unsigned char *mem, int mode=0x0);
     86    int getSize(int mode=0x0);
     87    bool updateData(syncData vars, int mode=0x0);
    8888    void setBacksync(bool sync);
    8989    bool getBacksync();
    9090    virtual void registerAllVariables()=0;
    91     virtual bool create()=0;
     91    virtual bool create();
    9292    static void setClient(bool b);
    9393  protected:
  • code/trunk/src/network/diffTest.cc

    r1360 r1502  
    549549
    550550  std::cout << "test setGamestateID" << std::endl;
    551   temp->setGamestateID( 8 );
     551  temp->setGameStateID( 8 );
    552552  printClientInformationBox( temp );
    553553
     
    559559  newInfo->setSynched( true );
    560560  newInfo->setPeer( NULL );
    561   toool->setGamestateID( 199 );
     561  toool->setGameStateID( 199 );
    562562  toool->setID( numberOfClients+1);
    563563  toool->setSynched( true );
  • code/trunk/src/network/dummyclient3.cc

    r1293 r1502  
    5151  Client client( str, PORT );
    5252  if ( client.establishConnection() )
     53  {
    5354    COUT(3) << "connection established" << std::endl;
    54   else COUT(0) << "problems establishing connection" << std::endl;
     55  }
     56  else
     57  {
     58    COUT(0) << "problems establishing connection" << std::endl;
     59  }
    5560  char message[10000];
    5661  char signs[] = "abcdefghijklmnopqrstuvwxy";
Note: See TracChangeset for help on using the changeset viewer.