Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 18, 2008, 10:00:17 PM (17 years ago)
Author:
scheusso
Message:

made clientinformation threadsafe and improved packetbuffer; wrote a static function SpaceShip *SpaceShip::getLocalShip. it returns the pointer to the ship that should be steered

Location:
code/branches/merge/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merge/src/network/ClientInformation.cc

    r1299 r1318  
    4545namespace network
    4646{
     47  boost::recursive_mutex ClientInformation::mutex_;
     48 
    4749  ClientInformation::ClientInformation() {
    4850    gamestateID_=GAMESTATEID_INITIAL;
    4951    preve=0;
    5052    nexte=0;
    51     this->head=false;
     53    this->head_=false;
    5254    synched_=false;
    5355  }
     
    5759    preve=0;
    5860    nexte=0;
    59     this->head=head;
     61    this->head_=head;
    6062    synched_=false;
    6163  }
     
    8082
    8183  ClientInformation::~ClientInformation() {
     84    boost::recursive_mutex::scoped_lock lock(mutex_);
    8285    if(preve!=0)
    8386      preve->setNext(this->nexte);
     
    8790
    8891  ClientInformation *ClientInformation::next() {
     92    boost::recursive_mutex::scoped_lock lock(mutex_);
    8993    if(this!=0)
    9094      return this->nexte;
     
    9397  }
    9498  ClientInformation *ClientInformation::prev() {
     99    boost::recursive_mutex::scoped_lock lock(mutex_);
    95100    if(this!=0)
    96101      return this->preve;
     
    100105
    101106  bool ClientInformation::setPrev(ClientInformation *prev) {
    102     if(!head)
     107    boost::recursive_mutex::scoped_lock lock(mutex_);
     108    if(!head_)
    103109      this->preve = prev;
    104110    else
     
    108114
    109115  bool ClientInformation::setNext(ClientInformation *next) {
     116    boost::recursive_mutex::scoped_lock lock(mutex_);
    110117    this->nexte = next;
    111118    return true;
     
    113120
    114121  ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) {
     122    boost::recursive_mutex::scoped_lock lock(mutex_);
    115123    this->nexte->setPrev(ins);
    116124    ins->setNext(this->nexte);
     
    121129
    122130  ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){
     131    boost::recursive_mutex::scoped_lock lock(mutex_);
    123132    if(!this)
    124133      return NULL;
     
    131140
    132141  void ClientInformation::setID(int clientID){
     142    boost::recursive_mutex::scoped_lock lock(mutex_);
    133143    clientID_ = clientID;
    134144  }
    135145
    136146  bool ClientInformation::setPeer(ENetPeer *peer){
     147    boost::recursive_mutex::scoped_lock lock(mutex_);
    137148    if(!this)
    138149      return false;
     
    142153
    143154  bool ClientInformation::setGamestateID(int id){
     155    boost::recursive_mutex::scoped_lock lock(mutex_);
    144156    if(!this)
    145157      return false;
     
    149161
    150162  int ClientInformation::getID() {
     163    boost::recursive_mutex::scoped_lock lock(mutex_);
    151164    if(!this)
    152165      return CLIENTID_UNKNOWN;
     
    156169
    157170  ENetPeer *ClientInformation::getPeer() {
     171    boost::recursive_mutex::scoped_lock lock(mutex_);
    158172    if(this)
    159173      return peer_;
     
    161175      return NULL;
    162176  }
     177 
     178  bool ClientInformation::getHead(){
     179    boost::recursive_mutex::scoped_lock lock(mutex_);
     180    return head_;
     181  }
     182 
     183  void ClientInformation::setHead(bool h){
     184    boost::recursive_mutex::scoped_lock lock(mutex_);
     185    head_=h;
     186  }
     187 
     188  int ClientInformation::getFailures(){
     189    boost::recursive_mutex::scoped_lock lock(mutex_);
     190    return failures_;
     191  }
     192  void ClientInformation::addFailure(){
     193    boost::recursive_mutex::scoped_lock lock(mutex_);
     194    failures_++;
     195  }
     196  void ClientInformation::resetFailures(){
     197    boost::recursive_mutex::scoped_lock lock(mutex_);
     198    failures_=0;
     199  }
    163200
    164201  int ClientInformation::getGamestateID() {
     202    boost::recursive_mutex::scoped_lock lock(mutex_);
    165203    if(this)
    166204      return gamestateID_;
     
    170208
    171209  ClientInformation *ClientInformation::insertBack(ClientInformation *ins) {
     210    boost::recursive_mutex::scoped_lock lock(mutex_);
    172211    if(!this)
    173212      return NULL;
     
    182221
    183222  bool ClientInformation::removeClient(int clientID) {
     223    boost::recursive_mutex::scoped_lock lock(mutex_);
    184224    if(!this || clientID==CLIENTID_UNKNOWN)
    185225      return false;
     
    194234
    195235  bool ClientInformation::removeClient(ENetPeer *peer) {
     236    boost::recursive_mutex::scoped_lock lock(mutex_);
    196237    if(!this || !peer)
    197238      return false;
    198239    ClientInformation *temp = this;
    199240    while(temp!=0){
    200       if(!temp->head)
     241      if(!temp->head_)
    201242        if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port)
    202243          break;
     
    216257  */
    217258  ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
    218     ClientInformation *temp = this;
    219     if (temp->head)
     259    boost::recursive_mutex::scoped_lock lock(mutex_);
     260    ClientInformation *temp = this;
     261    if (temp->head_)
    220262      temp=temp->next();
    221263    while(temp!=0 && temp->getID()!=clientID){
     
    233275  */
    234276  ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
     277    boost::recursive_mutex::scoped_lock lock(mutex_);
    235278    ClientInformation *temp = this;
    236279    while(temp!=0){
    237       if(temp->head){
     280      if(temp->head_){
    238281        temp = temp->next();
    239282        continue;
     
    248291
    249292  bool ClientInformation::setSynched(bool s) {
     293    boost::recursive_mutex::scoped_lock lock(mutex_);
    250294    if(!this)
    251295      return false;
     
    255299
    256300  bool ClientInformation::getSynched() {
     301    boost::recursive_mutex::scoped_lock lock(mutex_);
    257302    if(this)
    258303      return synched_;
  • code/branches/merge/src/network/ClientInformation.h

    r1299 r1318  
    4444
    4545#include <enet/enet.h>
     46#include <boost/thread/recursive_mutex.hpp>
    4647
    4748#define GAMESTATEID_INITIAL -1
     
    6364    ClientInformation *next();
    6465    ClientInformation *prev();
    65     bool setNext(ClientInformation *next);
    66     bool setPrev(ClientInformation *prev);
    67     ClientInformation *insertAfter(ClientInformation *ins);
    68     ClientInformation *insertBefore(ClientInformation *ins);
    6966    ClientInformation *insertBack(ClientInformation *ins);
    7067   
     
    8077    int getGamestateID();
    8178    ENetPeer *getPeer();
     79    bool getHead();
     80    void setHead(bool h);
    8281   
     82    int getFailures();
     83    void addFailure();
     84    void resetFailures();
    8385   
    8486    bool removeClient(int clientID);
     
    9294    bool getSynched();
    9395
    94     bool head;
    95     unsigned short failures_;
    9696
    97   private:
     97    private:
     98      bool setNext(ClientInformation *next);
     99      bool setPrev(ClientInformation *prev);
     100    ClientInformation *insertAfter(ClientInformation *ins);
     101    ClientInformation *insertBefore(ClientInformation *ins);
     102   
    98103    ClientInformation *preve;
    99104    ClientInformation *nexte;
     
    104109    int ShipID_;   // this is the unique objectID
    105110    bool synched_;
     111    bool head_;
     112    unsigned short failures_;
     113    static boost::recursive_mutex mutex_;
     114   
    106115  };
    107116
  • code/branches/merge/src/network/ConnectionManager.cc

    r1305 r1318  
    274274      return false;
    275275    }
    276     if(temp->prev()->head) { //not good if you use anything else than insertBack
     276    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
    277277      temp->prev()->setID(0); //bugfix: not necessary but usefull
    278278      temp->setID(1);
  • code/branches/merge/src/network/PacketBuffer.cc

    r1299 r1318  
    4141namespace network
    4242{
    43   boost::mutex networkPacketBufferMutex;
     43   boost::recursive_mutex PacketBuffer::mutex_;
    4444
    4545  PacketBuffer::PacketBuffer() {
     
    5252
    5353  bool PacketBuffer::push(ENetEvent *ev) {
    54     boost::mutex::scoped_lock lock(networkPacketBufferMutex);
     54    boost::recursive_mutex::scoped_lock lock(mutex_);
    5555    //std::cout << "event size inside packetbuffer " << ev->packet->dataLength << std::endl;
    5656    //   if(closed)
     
    6161      last=first;
    6262      last->next=NULL;
    63       // change this!!!!!!!
     63      // change this!!!!!!!  ---- we are not doing stl so we won't change this
    6464      last->packet = ev->packet;
    6565      last->address = ev->peer->address;
     
    7676      //last->address = ev->peer->address;
    7777    }
    78     // pseudo bugfix: added a return false statement for error handling
    79     if ( last->packet == ev->packet ) return true;
    80     return false;
     78    lock.unlock();
     79    return true;
    8180  }
    8281
     
    8483  //moving first pointer to next element
    8584  ENetPacket *PacketBuffer::pop() {
    86     boost::mutex::scoped_lock lock(networkPacketBufferMutex);
    87     //std::cout << "packetbuffer pop" << std::endl;
    88     if(first!=NULL /*&& !closed*/){
    89       QueueItem *temp = first;
    90       // get packet
    91       ENetPacket *pck=first->packet;
    92       // remove first element
    93       first = first->next;
    94       delete temp;
    95       //std::cout << "pop size of packet " << pck->dataLength << std::endl;
    96       return pck;
    97     } else{
    98       //std::cout << "nothing to return" << std::endl;
    99       return NULL;
    100     }
     85    ENetAddress address;
     86    return pop(address);
    10187  }
    10288
    10389  ENetPacket *PacketBuffer::pop(ENetAddress &address) {
    104     boost::mutex::scoped_lock lock(networkPacketBufferMutex);
     90    boost::recursive_mutex::scoped_lock lock(mutex_);
    10591    //std::cout << "packetbuffer pop(address)" << std::endl;
    10692    if(first!=NULL /*&& !closed*/){
     
    11298      first = first->next;
    11399      delete temp;
     100      lock.unlock();
    114101      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
    115102      return pck;
    116103    } else{
     104      lock.unlock();
    117105      return NULL;
    118106    }
  • code/branches/merge/src/network/PacketBuffer.h

    r1264 r1318  
    4545
    4646#include <enet/enet.h>
     47#include <boost/thread/recursive_mutex.hpp>
    4748
    4849namespace network
     
    7677    QueueItem *last;
    7778    bool closed;
    78 
     79    static boost::recursive_mutex mutex_;
    7980  };
    8081
  • code/branches/merge/src/network/Server.cc

    r1306 r1318  
    192192    bool added=false;
    193193    while(temp != NULL){
    194       if(temp->head){
     194      if(temp->getHead()){
    195195        temp=temp->next();
    196196        //think this works without continue
     
    214214      //std::cout << "adding gamestate" << std::endl;
    215215      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
    216         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->failures_ << std::endl;
    217         temp->failures_++;
    218         if(temp->failures_ > 20 )
     216        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
     217        temp->addFailure();
     218        if(temp->getFailures() > 20 )
    219219          disconnectClient(temp);
    220220      //std::cout << "added gamestate" << std::endl;
     
    254254    else
    255255      if(clients->findClient(clientID))
    256         clients->findClient(clientID)->failures_=0;
     256        clients->findClient(clientID)->resetFailures();
    257257  }
    258258
  • code/branches/merge/src/orxonox/objects/SpaceShip.cc

    r1306 r1318  
    6464    SpaceShip* SpaceShip::instance_s;
    6565
     66    SpaceShip *SpaceShip::getLocalShip(){
     67      Iterator<SpaceShip> it;
     68      for(it = ObjectList<SpaceShip>::start(); it; ++it){
     69        if((it)->server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==it->objectID ) )
     70          return *it;
     71      }
     72      return NULL;
     73    }
     74   
    6675    SpaceShip::SpaceShip() :
    6776      //testvector_(0,0,0),
  • code/branches/merge/src/orxonox/objects/SpaceShip.h

    r1272 r1318  
    4444    {
    4545        public:
     46         
     47            static SpaceShip *getLocalShip();
     48           
    4649            SpaceShip();
    4750            ~SpaceShip();
Note: See TracChangeset for help on using the changeset viewer.