Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 10, 2018, 3:06:55 PM (6 years ago)
Author:
merholzl
Message:

Merged Masterserver, refresh button had to be removed

Location:
code/branches/mergeFS18
Files:
43 edited
2 copied

Legend:

Unmodified
Added
Removed
  • code/branches/mergeFS18

  • code/branches/mergeFS18/data/overlays/lastTeamStandingHUD.oxo

    r9348 r12027  
    22  <OverlayGroup name="lastTeamStandingHUD" scale = "1, 1">
    33    <LastTeamStandingInfos
    4      position  = "0.14, 0.02"
     4     position  = "0.5, 0.02"
    55     pickpoint = "0.0, 0.0"
    66     font      = "ShareTechMono"
     
    1414
    1515    <OverlayText
    16      position  = "0.02, 0.02"
     16     position  = "0.38, 0.02"
    1717     pickpoint = "0.0, 0.0"
    1818     font      = "ShareTechMono"
     
    2424
    2525    <OverlayText
    26      position  = "0.02, 0.055"
     26     position  = "0.38, 0.055"
    2727     pickpoint = "0.0, 0.0"
    2828     font      = "ShareTechMono"
     
    3434
    3535    <LastTeamStandingInfos
    36      position  = "0.14, 0.055"
     36     position  = "0.5, 0.055"
    3737     pickpoint = "0.0, 0.0"
    3838     font      = "ShareTechMono"
  • code/branches/mergeFS18/src/libraries/network/Client.cc

    r11071 r12027  
    166166          }
    167167        }
    168         //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now
    169 //         if(gs){
    170 //           orxout(verbose, context::network) << "client tick: sending gs " << gs << endl;
    171 //           if( !gs->send() )
    172 //             orxout(internal_warning, context::network) << "Problem adding partial gamestate to queue" << endl;
    173 //         // gs gets automatically deleted by enet callback
    174 //         }
     168
    175169        FunctionCallManager::sendCalls(static_cast<Host*>(this));
    176170      }
    177171    }
    178 //     sendPackets(); // flush the enet queue
    179172
    180173    Connection::processQueue();
     
    185178        isSynched_=true;
    186179    }
    187 //     GamestateManager::cleanup();;
    188 //     Connection::sendPackets();
     180
    189181
    190182    return;
  • code/branches/mergeFS18/src/libraries/network/Client.h

    r11071 r12027  
    9090  private:
    9191    Client(const Client& copy); // not used
    92     virtual bool isServer_() override{return false;}
     92    virtual bool isServer_() override { return false; }
    9393    virtual void processPacket(packet::Packet* packet) override;
    9494
  • code/branches/mergeFS18/src/libraries/network/ClientConnection.cc

    r11071 r12027  
    105105      {
    106106        // manually add server to list of peers
    107         /*incomingEvent inEvent = */Connection::preprocessConnectEvent(event);
    108 //         addPeer(inEvent.peerID);
     107        Connection::preprocessConnectEvent(event);
     108
    109109        // start communication thread
    110110        this->established_=true;
     
    155155    assert( this->server_ );
    156156    assert( packet );
    157 //     return Connection::addPacket( packet, NETWORK_PEER_ID_SERVER, channelID );
    158157    // HACK: actually there should be a way to do this using addPacket and the correct peerID
    159158    return Connection::broadcastPacket(packet, channelID);
     
    168167    this->established_=false;
    169168    orxout(internal_error, context::network) << "Received disconnect Packet from Server!" << endl;
    170         // server closed the connection
     169    // server closed the connection
    171170    this->stopCommunicationThread();
    172171    this->connectionClosed();
  • code/branches/mergeFS18/src/libraries/network/ClientConnection.h

    r11071 r12027  
    3636{
    3737
    38   class _NetworkExport ClientConnection: public Connection{
     38  class _NetworkExport ClientConnection: public Connection {
    3939  public:
    4040    ClientConnection();
     
    4444    void setPort( unsigned int port );
    4545
    46 //     ENetEvent *getEvent();
    47     // check wheter the packet queue is empty
    48 //     bool queueEmpty();
    4946    // create a new listener thread
    5047    virtual bool establishConnection();
  • code/branches/mergeFS18/src/libraries/network/ClientConnectionListener.cc

    r11071 r12027  
    3131#include "core/CoreIncludes.h"
    3232#include "core/GameMode.h"
    33 // #include "ClientInformation.h"
    3433
    3534namespace orxonox
     
    3736    RegisterAbstractClass(ClientConnectionListener).inheritsFrom<Listable>();
    3837
     38    /**
     39     * Constructor
     40     * Register the object
     41     */
    3942    ClientConnectionListener::ClientConnectionListener()
    4043    {
     
    4245    }
    4346
     47    /**
     48     * Call clientConnected() on all ClientConnectionListeners.
     49     * @param clientID The ID of the newly connected client
     50     */
    4451    void ClientConnectionListener::broadcastClientConnected(unsigned int clientID)
    4552    {
     
    4855    }
    4956
     57    /**
     58     * Call clientDisconnected() on all ClientConnectionListeners.
     59     * @param clientID The ID of the newly disconnected client
     60     */
    5061    void ClientConnectionListener::broadcastClientDisconnected(unsigned int clientID)
    5162    {
     
    5364            listener->clientDisconnected(clientID);
    5465    }
    55 
    56 //     void ClientConnectionListener::getConnectedClients()
    57 //     {
    58 //         ClientInformation* client = ClientInformation::getBegin();
    59 //         while (client)
    60 //         {
    61 //             this->clientConnected(client->getID());
    62 //             client = client->next();
    63 //         }
    64 //     }
    6566}
    6667
  • code/branches/mergeFS18/src/libraries/network/ClientConnectionListener.h

    r9667 r12027  
    3535namespace orxonox
    3636{
     37    /**
     38     * An abstract base class. Derived classes must implement clientConnected() and clientDisconnected().
     39     */
    3740    class _NetworkExport ClientConnectionListener : virtual public Listable
    3841    {
     
    4649            virtual void clientConnected(unsigned int clientID) = 0;
    4750            virtual void clientDisconnected(unsigned int clientID) = 0;
    48 
    49         protected:
    50 //             void getConnectedClients();
    5151    };
    5252}
  • code/branches/mergeFS18/src/libraries/network/Connection.cc

    r11071 r12027  
    3838
    3939#include "packet/Packet.h"
     40#include "util/Output.h"
    4041#include <util/Sleep.h>
    4142
     
    4546  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
    4647
     48  /**
     49   * Constructor
     50   * @param firstPeerId The initial value of nextPeerID_
     51   */
    4752  Connection::Connection(uint32_t firstPeerID):
    4853    host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
    4954  {
     55    // Global initialization of ENet
    5056    enet_initialize();
     57
     58    // Register enet_deinitialize to be executed when the program exits normally
    5159    atexit(enet_deinitialize);
     60
     61    // Create mutexes for incoming and outgoing events
    5262    this->incomingEventsMutex_ = new boost::mutex;
    5363    this->outgoingEventsMutex_ = new boost::mutex;
    54 //     this->overallMutex_ = new boost::mutex;
    55   }
    56 
     64  }
     65
     66  /**
     67   * Destructor
     68   */
    5769  Connection::~Connection()
    5870  {
     71    // Delete the mutexes
    5972    delete this->incomingEventsMutex_;
    6073    delete this->outgoingEventsMutex_;
    6174  }
    6275
     76  /**
     77   * Start the main communication thread.
     78   */
    6379  void Connection::startCommunicationThread()
    6480  {
     
    6783  }
    6884 
     85  /**
     86   * Stop the main communication thread.
     87   */
    6988  void Connection::stopCommunicationThread()
    7089  {
    7190    this->bCommunicationThreadRunning_ = false;
    72     if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
    73     {
    74       // force thread to stop
     91    // Wait for peaceful termination
     92    if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME))
     93    {
     94      // Force thread to stop if the waiting time runs out.
    7595      this->communicationThread_->interrupt();
    7696    }
     
    7898  }
    7999
     100  /**
     101   * Send an outgoing event of type 'disconnectPeer'.
     102   * @param peerID The peer to which the event is sent
     103   */
    80104  void Connection::disconnectPeer(uint32_t peerID)
    81105  {
    82 //     this->overallMutex_->lock();
    83106    outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 };
    84107   
     
    86109    this->outgoingEvents_.push_back(outEvent);
    87110    this->outgoingEventsMutex_->unlock();
    88 //     this->overallMutex_->unlock();
    89   }
    90  
     111  }
     112
     113  /**
     114   * Send an outgoing event of type 'disconnectPeers'.
     115   */
    91116  void Connection::disconnectPeers()
    92117  {
     
    98123  }
    99124
     125  /**
     126   * Send a packet.
     127   * @param packet Pointer to the packet to send
     128   * @param peerID The peer to which the event is sent
     129   * @param channelId The channel ID
     130   */
    100131  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
    101132  {
    102 //     this->overallMutex_->lock();
    103133    outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID };
    104134   
     
    106136    this->outgoingEvents_.push_back(outEvent);
    107137    this->outgoingEventsMutex_->unlock();
    108 //     this->overallMutex_->unlock();
    109   }
    110  
     138  }
     139 
     140  /**
     141   * Send a broadcast packet.
     142   * @param packet Pointer to the packet to send
     143   * @param channelId The channel ID
     144   */
    111145  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
    112146  {
    113 //     this->overallMutex_->lock();
    114147    outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID };
    115148   
     
    117150    this->outgoingEvents_.push_back(outEvent);
    118151    this->outgoingEventsMutex_->unlock();
    119 //     this->overallMutex_->unlock();
    120   }
    121 
    122  
     152  }
     153
     154 
     155  /**
     156   * Main communication thread
     157   */
    123158  void Connection::communicationThread()
    124159  {
    125160    ENetEvent event;
    126161   
    127 //     this->overallMutex_->lock();
    128     while( bCommunicationThreadRunning_ )
     162    while(this->bCommunicationThreadRunning_)
    129163    {
    130164      // Receive all pending incoming Events (such as packets, connects and disconnects)
    131       while( enet_host_check_events( this->host_, &event ) > 0 )
     165      while(enet_host_check_events(this->host_, &event ) > 0)
    132166      {
    133         processIncomingEvent(event);
     167        this->processIncomingEvent(event);
    134168      }
    135169     
    136 //       this->overallMutex_->unlock();
     170      // Sleep for 1ms
    137171      msleep(1);
    138 //       this->overallMutex_->lock();
    139172     
    140173      // Send all waiting outgoing packets
     
    142175      uint32_t outgoingEventsCount = this->outgoingEvents_.size();
    143176      this->outgoingEventsMutex_->unlock();
    144       while( outgoingEventsCount > 0 )
     177
     178      while(outgoingEventsCount > 0)
    145179      {
    146 //         orxout(verbose, context::network) << "outgoing event" << endl;
    147180        this->outgoingEventsMutex_->lock();
    148181        outgoingEvent outEvent = this->outgoingEvents_.front();
     
    150183        this->outgoingEventsMutex_->unlock();
    151184       
    152         processOutgoingEvent(outEvent);
     185        this->processOutgoingEvent(outEvent);
    153186       
    154187        this->outgoingEventsMutex_->lock();
     
    158191     
    159192      // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
    160       if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
     193      if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0)
    161194      {
    162         processIncomingEvent(event);
     195        this->processIncomingEvent(event);
    163196      }
    164197    }
    165 //     this->overallMutex_->unlock();
    166   }
    167  
     198  }
     199 
     200  /**
     201   * Handle an incoming event.
     202   * @param event The incoming event
     203   */
    168204  void Connection::processIncomingEvent(ENetEvent& event)
    169205  {
    170206    incomingEvent inEvent;
    171207    // preprocess event
    172     switch( event.type )
     208    switch(event.type)
    173209    {
    174210      case ENET_EVENT_TYPE_CONNECT:
     
    192228  }
    193229 
     230  /**
     231   * Send an event.
     232   * @param event The event to send
     233   */
    194234  void Connection::processOutgoingEvent(outgoingEvent& event)
    195235  {
    196236    ENetPeer* peer;
    197     switch( event.type )
     237    switch(event.type)
    198238    {
    199239      case OutgoingEventType::sendPacket:
    200240        // check whether the peer is still/already in the peer list
    201         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     241        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    202242        {
    203243          peer = this->peerMap_[event.peerID];
    204           enet_peer_send( peer, event.channelID, event.packet );
     244          enet_peer_send(peer, event.channelID, event.packet);
    205245        }
    206246        else
    207247        {
    208248          // peer probably already disconnected so just discard packet
    209           assert(event.peerID<this->nextPeerID_);
     249          orxout(message) << "Trying to send packet to peer that is not in peer list. Ignoring packet." << endl;
    210250          enet_packet_destroy(event.packet);
    211251        }
    212252        break;
    213253      case OutgoingEventType::disconnectPeer:
    214         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     254        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    215255        {
    216256          peer = this->peerMap_[event.peerID];
     
    220260        {
    221261          // peer probably already disconnected so just discard disconnect event
    222           assert(event.peerID<this->nextPeerID_);
     262          assert(event.peerID < this->nextPeerID_);
    223263        }
    224264        break;
    225265      case OutgoingEventType::disconnectPeers:
    226         disconnectPeersInternal();
     266        this->disconnectPeersInternal();
    227267        break;
    228268      case OutgoingEventType::broadcastPacket:
     
    234274  }
    235275
    236 
    237276  void Connection::disconnectPeersInternal()
    238277  {
     
    241280      enet_peer_disconnect(mapEntry.second, 0);
    242281    }
    243     uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
     282    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT;
    244283    uint32_t i = 0;
    245284    while( this->peerMap_.size() && i++ < iterations )
     
    361400  }
    362401
    363 
    364402}
  • code/branches/mergeFS18/src/libraries/network/Connection.h

    r11071 r12027  
    9696
    9797  protected:
    98     Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER+1);
     98    Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER + 1);
    9999   
    100100    void startCommunicationThread();
     
    110110    void processQueue();
    111111    void waitOutgoingQueue();     // wait for the outgoing queue to become empty (everything processed by communication thread)
    112     virtual void addPeer(uint32_t peerID)=0;
    113     virtual void removePeer(uint32_t peerID)=0;
    114     virtual void processPacket( packet::Packet* packet)=0;
     112    virtual void addPeer(uint32_t peerID) = 0;
     113    virtual void removePeer(uint32_t peerID) = 0;
     114    virtual void processPacket( packet::Packet* packet) = 0;
    115115   
    116116    incomingEvent preprocessConnectEvent(ENetEvent& event);
     
    124124
    125125    ENetHost*                     host_;
     126
    126127  private:
    127128    void communicationThread();
     
    130131    bool                          bCommunicationThreadRunning_;
    131132    ENetAddress*                  bindAddress_;
     133
     134    // Queue for incoming events
    132135    std::deque<incomingEvent>     incomingEvents_;
     136
     137    // Queue for outgoing events
    133138    std::deque<outgoingEvent>     outgoingEvents_;
     139
    134140    boost::mutex*                 incomingEventsMutex_;
    135141    boost::mutex*                 outgoingEventsMutex_;
  • code/branches/mergeFS18/src/libraries/network/FunctionCallManager.cc

    r11784 r12027  
    4242void FunctionCallManager::addCall(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
    4343{
    44   if(sPeerMap_.find(peerID)==sPeerMap_.end())
     44  // If the peerID doesn't exist yet in the map...
     45  if(sPeerMap_.find(peerID) == sPeerMap_.end())
    4546  {
     47    // ... add a new FunctionCalls packet for the peer
    4648    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
    4749    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
    4850  }
     51
     52  // Add a new function call to the peer
    4953  FunctionCallManager::sPeerMap_[peerID]->addCall(functionID, objectID, mt1, mt2, mt3, mt4, mt5);
    5054}
    5155
    52 // Send calls
    53 
     56/**
     57 * Send all function calls in sPeerMap_ to a given host, then clear sPeerMap_
     58 * @param host The host to send the function calls to
     59 */
    5460void FunctionCallManager::sendCalls(orxonox::Host* host)
    5561{
     
    6268}
    6369
     70/**
     71 * Place an incoming function call in the queue for processing.
     72 */
    6473void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID)
    6574{
     
    6776}
    6877
     78/**
     79 * Process queue of incoming function calls.
     80 */
    6981void FunctionCallManager::processBufferedFunctionCalls()
    7082{
    7183  std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t>>>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
    72   while( it!=FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
     84  while( it != FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
    7385  {
    7486    OrxAssert( Host::getActiveInstance(), "No Host class existing" );
  • code/branches/mergeFS18/src/libraries/network/FunctionCallManager.h

    r11071 r12027  
    5353  static void processBufferedFunctionCalls();
    5454
     55  // Maps peer IDs to function calls
    5556  static std::map<uint32_t, packet::FunctionCalls*>                           sPeerMap_;
     57
    5658  static std::vector<std::pair<FunctionCall,std::pair<uint32_t, uint32_t>>> sIncomingFunctionCallBuffer_;
     59
    5760protected:
    5861  FunctionCallManager();
  • code/branches/mergeFS18/src/libraries/network/GamestateHandler.cc

    r11071 r12027  
    3232namespace orxonox {
    3333
    34 // GamestateHandler *GamestateHandler::instance_=nullptr;
    35 
    3634GamestateHandler::GamestateHandler()
    3735{
  • code/branches/mergeFS18/src/libraries/network/GamestateHandler.h

    r8327 r12027  
    3737
    3838/**
    39     @author Oliver Scheuss
     39 * An interface for any class that wants to handle gamestates.
     40 * @author Oliver Scheuss
    4041*/
    4142class _NetworkExport GamestateHandler
     
    5152    virtual bool      addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0;
    5253    virtual bool      ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0;
    53     virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID )=0;
    54     virtual uint32_t  getCurrentGamestateID()=0;
     54    virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID ) = 0;
     55    virtual uint32_t  getCurrentGamestateID() = 0;
    5556};
    5657
  • code/branches/mergeFS18/src/libraries/network/GamestateManager.cc

    r11071 r12027  
    4343#include <cassert>
    4444#include <queue>
    45 // #include <boost/thread/mutex.hpp>
    4645
    4746#include "packet/Acknowledgement.h"
     
    5554#include "util/Clock.h"
    5655#include "util/OrxAssert.h"
    57 // #include "TrafficControl.h"
    5856
    5957namespace orxonox
     
    6260  currentGamestate_(nullptr), id_(0)
    6361  {
    64 //     trafficControl_ = new TrafficControl();
    65 //     threadMutex_ = new boost::mutex();
    66 //     threadPool_ = new ThreadPool();
    6762  }
    6863
    6964  GamestateManager::~GamestateManager()
    7065  {
    71     if( this->currentGamestate_ )
    72         delete this->currentGamestate_;
    73     for( const auto& mapEntry : gamestateQueue )
    74       delete mapEntry.second;
    75     for( const auto& mapEntryPeer : peerMap_ )
    76     {
    77       for( const auto& mapEntryGamestate : mapEntryPeer.second.gamestates )
    78         delete mapEntryGamestate.second;
    79     }
    80 //     this->trafficControl_->destroy();
    81 //     delete this->threadMutex_;
    82 //     delete this->threadPool_;
    83   }
    84 
    85   bool GamestateManager::update(){
    86 //     cleanup();
    87     return getSnapshot();
     66    if(this->currentGamestate_)
     67    {
     68      delete this->currentGamestate_;
     69      this->currentGamestate_ = nullptr;
     70    }
     71
     72    for(const auto& gsPair : this->gamestateQueue)
     73    {
     74      delete gsPair.second;
     75    }
     76
     77    for(const auto& peerPair : this->peerMap_)
     78    {
     79      for(const auto& gsPair : peerPair.second.gamestates)
     80      {
     81        delete gsPair.second;
     82      }
     83    }
     84  }
     85
     86  bool GamestateManager::update()
     87  {
     88    return this->getSnapshot();
    8889  }
    8990
     
    9192  {
    9293    assert(gs);
    93     std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
    94     if(it!=gamestateQueue.end())
     94    // Search the queue for a gamestate for the client
     95    std::map<unsigned int, packet::Gamestate*>::iterator it = this->gamestateQueue.find(clientID);
     96    if(it != this->gamestateQueue.end())
    9597    {
    9698      // delete obsolete gamestate
    9799      delete it->second;
    98100    }
    99     gamestateQueue[clientID] = gs;
     101    // update the client's gamestate
     102    this->gamestateQueue[clientID] = gs;
     103
    100104    return true;
    101105  }
    102106
     107  /**
     108   * Process the queued gamestates.
     109   */
    103110  bool GamestateManager::processGamestates()
    104111  {
    105     if( this->gamestateQueue.empty() )
     112    // Queue is empty, nothing to do
     113    if(this->gamestateQueue.empty())
     114    {
    106115        return true;
     116    }
     117
    107118    // now push only the most recent gamestates we received (ignore obsolete ones)
    108     for(const auto& mapEntry : gamestateQueue)
    109     {
    110       OrxVerify(processGamestate(mapEntry.second), "ERROR: could not process Gamestate");
    111       sendAck( mapEntry.second->getID(), mapEntry.second->getPeerID() );
    112       delete mapEntry.second;
     119    for(const auto& gsPair : this->gamestateQueue)
     120    {
     121      OrxVerify(this->processGamestate(gsPair.second), "ERROR: could not process Gamestate");
     122      this->sendAck(gsPair.second->getID(), gsPair.second->getPeerID());
     123      delete gsPair.second;
    113124    }
    114125    // now clear the queue
    115     gamestateQueue.clear();
     126    this->gamestateQueue.clear();
     127
    116128    //and call all queued callbacks
    117129    NetworkCallbackManager::callCallbacks();
     130
    118131    return true;
    119132  }
    120133 
     134  /**
     135   * Send Acknowledgement packet.
     136   * @param gamestateId The gamestate ID we want to acknowledge
     137   * @param peerID The ID of the peer we want to send the Acknowledgement to
     138   */
    121139  bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
    122140  {
    123     assert( gamestateID != ACKID_NACK );
     141    assert(gamestateID != ACKID_NACK);
    124142    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
    125     if( !this->sendPacket(ack))
     143    if(!this->sendPacket(ack))
    126144    {
    127145      orxout(internal_warning, context::network) << "could not ack gamestate: " << gamestateID << endl;
     
    135153  }
    136154
    137 
    138   bool GamestateManager::getSnapshot(){
    139     if ( currentGamestate_ != nullptr )
    140       delete currentGamestate_;
     155  /**
     156   * Update the current gamestate.
     157   */
     158  bool GamestateManager::getSnapshot()
     159  {
     160    // Delete current gamestate
     161    if (this->currentGamestate_)
     162    {
     163      delete this->currentGamestate_;
     164      this->currentGamestate_ = nullptr;
     165    }
     166
    141167    uint8_t gsMode;
    142     if( GameMode::isMaster() )
     168    if(GameMode::isMaster())
     169    {
    143170      gsMode = packet::GAMESTATE_MODE_SERVER;
    144     else
     171    }
     172    else
     173    {
    145174      gsMode = packet::GAMESTATE_MODE_CLIENT;
     175    }
     176
    146177    uint32_t newID;
    147     if( GameMode::isMaster() )
    148       newID = ++id_;
    149     else
    150     {
    151       assert(peerMap_.size()!=0);
    152       newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID;
    153       if( newID == GAMESTATEID_INITIAL )
     178    if(GameMode::isMaster())
     179    {
     180      newID = ++this->id_;
     181    }
     182    else
     183    {
     184      assert(this->peerMap_.size() != 0);
     185      newID = this->peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID;
     186      if(newID == GAMESTATEID_INITIAL)
    154187      {
    155188        return false;
     
    157190    }
    158191   
    159     currentGamestate_ = new packet::Gamestate();
    160    
    161     if(!currentGamestate_->collectData(newID, gsMode))
    162     { //we have no data to send
    163       delete currentGamestate_;
    164       currentGamestate_=nullptr;
     192    // Create a new gamestate
     193    this->currentGamestate_ = new packet::Gamestate();
     194    if(!this->currentGamestate_->collectData(newID, gsMode))
     195    {
     196      // we have no data to send
     197      delete this->currentGamestate_;
     198      this->currentGamestate_ = nullptr;
    165199      return false;
    166200    }
     201
    167202    return true;
    168203  }
    169204
     205  /**
     206   * Return a vector with the gamestates of all peers.
     207   */
    170208  std::vector<packet::Gamestate*> GamestateManager::getGamestates()
    171209  {
    172     if(!currentGamestate_)
     210    // Current gamestate is empty
     211    if(!this->currentGamestate_){
    173212      return std::vector<packet::Gamestate*>();
     213    }
     214
    174215    std::vector<packet::Gamestate*> peerGamestates;
    175    
    176     for( const auto& mapEntry : peerMap_ )
    177     {
    178       if( !mapEntry.second.isSynched )
     216    for(const auto& mapEntry : this->peerMap_)
     217    {
     218      if(!mapEntry.second.isSynched)
    179219      {
    180220        orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl;
     
    198238      peerGamestates.push_back(nullptr);  // insert an empty gamestate* to be changed
    199239      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
    200       if( peerGamestates.back()==nullptr )
     240      if(peerGamestates.back() == nullptr)
     241      {
    201242        // nothing to send to remove pointer from vector
    202243        peerGamestates.pop_back();
    203       //FunctorMember<GamestateManager>* functor =
    204 //       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
    205 //       executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ );
    206 //       (*static_cast<Executor*>(executor))();
    207 //       this->threadPool_->passFunction( executor, true );
    208 //       (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ );
    209     }
    210 
    211 //     threadPool_->synchronise();
     244      }
     245    }
    212246
    213247    return peerGamestates;
     
    215249
    216250
    217   void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
    218     //why are we searching the same client's gamestate id as we searched in
    219     //Server::sendGameState?
     251  void GamestateManager::finishGamestate(unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate)
     252  {
    220253    // save the (undiffed) gamestate in the clients gamestate map
    221     //chose wheather the next gamestate is the first or not
    222 
    223 //     packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
    224 //       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
    225 //     packet::Gamestate* gs = gamestate;
     254    // choose whether the next gamestate is the first or not
     255
     256    // Create a copy of the gamestate
    226257    packet::Gamestate *gs = new packet::Gamestate(*gamestate); //this is neccessary because the gamestate are being kept (to diff them later on) for each client seperately
    227 //     packet::Gamestate *gs = new packet::Gamestate();
    228 //     gs->collectData( id_, 0x1 );
    229 //     this->threadMutex_->lock();
    230     peerMap_[peerID].gamestates[gamestate->getID()]=gs;
    231 //     this->threadMutex_->unlock();
     258    this->peerMap_[peerID].gamestates[gamestate->getID()] = gs;
     259
     260    // Start the clock
    232261    Clock clock;
    233262    clock.capture();
     
    236265    {
    237266      packet::Gamestate *diffed1 = gs->diffVariables(base);
    238       if( diffed1->getDataSize() == 0 )
     267      if(diffed1->getDataSize() == 0)
    239268      {
    240269        delete diffed1;
     
    249278    }
    250279
    251 
    252 //     OrxVerify(gs->compressData(), "");
     280    // Stop the clock
    253281    clock.capture();
    254282    orxout(verbose_more, context::network) << "diff and compress time: " << clock.getDeltaTime() << endl;
    255 //     orxout(verbose_more, context::network) << "sending gamestate with id " << gs->getID();
    256 //     if(gamestate->isDiffed())
    257 //       orxout(verbose_more, context::network) << " and baseid " << gs->getBaseID() << endl;
    258 //     else
    259 //       orxout(verbose_more, context::network) << endl;
     283
     284
    260285    gs->setPeerID(peerID);
    261286    destgamestate = gs;
     
    263288
    264289
     290  /**
     291   * Acknowledge a received gamestate.
     292   * @param gamestateID The ID of the gamestate to be acknowledged
     293   * @param peerID The ID of the peer to send the Acknowledgement to
     294   */
    265295  bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID)
    266296  {
    267 //     ClientInformation *temp = ClientInformation::findClient(peerID);
    268 //     assert(temp);
     297    // Search for the peer in the peer map
    269298    std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID);
    270     assert(it!=this->peerMap_.end());
     299    assert(it != this->peerMap_.end());
     300
    271301    unsigned int curid = it->second.lastAckedGamestateID;
    272302
    273303    assert(gamestateID != ACKID_NACK);
    274 //     if(gamestateID == ACKID_NACK){
    275 //       it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
    276 // //       temp->setGamestateID(GAMESTATEID_INITIAL);
    277 //       // now delete all saved gamestates for this client
    278 //       std::map<uint32_t, packet::Gamestate*>::iterator it2;
    279 //       for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
    280 //         delete it2->second;
    281 //       }
    282 //       it->second.gamestates.clear();
    283 //       return true;
    284 //     }
    285 
    286 //    assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); // this line is commented out because acknowledgements are unreliable and may arrive in distorted order
    287     if( gamestateID <= curid && curid != GAMESTATEID_INITIAL )
     304
     305    // The gamestate has already been acknowledged, nothing to do
     306    if(gamestateID <= curid && curid != GAMESTATEID_INITIAL)
     307    {
    288308        return true;
    289 orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl;
     309    }
     310
     311    orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl;
    290312    std::map<uint32_t, packet::Gamestate*>::iterator it2;
    291     for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); )
    292     {
    293       if( it2->second->getID() < gamestateID )
     313    for (it2 = it->second.gamestates.begin(); it2 != it->second.gamestates.end();)
     314    {
     315      if(it2->second->getID() < gamestateID)
    294316      {
    295317        delete it2->second;
     
    297319      }
    298320      else
     321      {
    299322        ++it2;
     323      }
    300324    }
    301325   
    302 //     std::map<unsigned int, packet::Gamestate*>::iterator it;
    303 //     for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){
    304 //       delete it->second;
    305 //       gamestateMap_[peerID].erase(it++);
    306 //     }
     326    // update the last acked gamestate
    307327    it->second.lastAckedGamestateID = gamestateID;
    308 //     temp->setGamestateID(gamestateID);
    309 //     TrafficControl::processAck(peerID, gamestateID);
     328
    310329    return true;
    311330  }
    312331 
     332  /**
     333   * Return the ID of the last received gamestate for a certain peer
     334   * @param peerID The ID of the peer\
     335   */
    313336  uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID)
    314337  {
    315     assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
    316     if( this->peerMap_.find(peerID) != this->peerMap_.end() )
     338    if(this->peerMap_.find(peerID) != this->peerMap_.end())
     339    {
    317340      return this->peerMap_[peerID].lastReceivedGamestateID;
    318     else
     341    }
     342    else
     343    {
    319344      return GAMESTATEID_INITIAL;
     345    }
    320346  }
    321347 
    322  
     348  /**
     349   * Add a peer to the game.
     350   * @param peerID The ID of the peer to add.
     351   */
    323352  void GamestateManager::addPeer(uint32_t peerID)
    324353  {
    325     assert(peerMap_.find(peerID)==peerMap_.end());
    326     peerMap_[peerID].peerID = peerID;
    327     peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
    328     peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
    329     if( GameMode::isMaster() )
    330       peerMap_[peerID].isSynched = false;
    331     else
    332       peerMap_[peerID].isSynched = true;
    333   }
    334 
     354    // Ensure that the peer doesn't already exist.
     355    assert(this->peerMap_.find(peerID) == this->peerMap_.end());
     356
     357    // Create the peerInfo struct for the peer
     358    this->peerMap_[peerID].peerID = peerID;
     359    this->peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
     360    this->peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
     361    if(GameMode::isMaster())
     362    {
     363      this->peerMap_[peerID].isSynched = false;
     364    }
     365    else
     366    {
     367      this->peerMap_[peerID].isSynched = true;
     368    }
     369  }
     370
     371  /**
     372   * Remove a peer from the game.
     373   * @param peerID The ID of the peer to be removed
     374   */
    335375  void GamestateManager::removePeer(uint32_t peerID)
    336376  {
    337     assert(peerMap_.find(peerID)!=peerMap_.end());
    338     for( const auto& mapEntry : peerMap_[peerID].gamestates )
     377    // Make sure that the peer exists
     378    assert(this->peerMap_.find(peerID) != this->peerMap_.end());
     379
     380    for (const auto& mapEntry : this->peerMap_[peerID].gamestates)
    339381    {
    340382      delete mapEntry.second;
    341383    }
    342     peerMap_.erase(peerMap_.find(peerID));
    343   }
    344 
    345 
    346 //   void GamestateManager::removeClient(ClientInformation* client){
    347 //     assert(client);
    348 //     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>>::iterator clientMap = gamestateMap_.find(client->getID());
    349 //     // first delete all remained gamestates
    350 //     std::map<unsigned int, packet::Gamestate*>::iterator it;
    351 //     for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
    352 //       delete it->second;
    353 //     // now delete the clients gamestatemap
    354 //     gamestateMap_.erase(clientMap);
    355 //   }
    356 
     384    this->peerMap_.erase(this->peerMap_.find(peerID));
     385  }
     386
     387  /**
     388   * Process an incoming Gamestate packet.
     389   * @param gs Pointer to the incoming Gamestate packet
     390   */
    357391  bool GamestateManager::processGamestate(packet::Gamestate *gs)
    358392  {
     393    // Decompress if necessary
    359394    if(gs->isCompressed())
    360395    {
     
    362397    }
    363398    assert(!gs->isDiffed());
     399
    364400    uint8_t gsMode;
    365     if( GameMode::isMaster() )
     401    if(GameMode::isMaster())
     402    {
    366403      gsMode = packet::GAMESTATE_MODE_SERVER;
    367     else
     404    }
     405    else
     406    {
    368407      gsMode = packet::GAMESTATE_MODE_CLIENT;
    369     if( gs->spreadData(gsMode) )
     408    }
     409
     410    if(gs->spreadData(gsMode))
    370411    {
    371412      this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID();
     
    373414    }
    374415    else
     416    {
    375417      return false;
     418    }
    376419  }
    377420
  • code/branches/mergeFS18/src/libraries/network/GamestateManager.h

    r11071 r12027  
    5858  * - writing gamestates to universe
    5959  * - diffing gamestates
     60  *
     61  * Inherited by Host
     62  *
    6063  * EN/DECODATION:
    6164  * a: last Gamestate a client has received
    6265  * b: new Gamestate
    6366  * x: diffed and compressed gamestate
    64   * x=(a^b)
    65   * b=(a^x)
    66   * diff(a,diff(a,x))=x (hope this is correct)
     67  * x = (a ^ b)
     68  * b = (a ^ x)
     69  * diff(a, diff(a, x)) = x (hope this is correct)
    6770  * @author Oliver Scheuss
    6871  */
     
    7578      uint32_t  lastAckedGamestateID;     //!< id of the last gamestate on which we received an ack from the peer
    7679      bool      isSynched;
    77       std::map< uint32_t, packet::Gamestate* > gamestates;
     80      std::map<uint32_t, packet::Gamestate*> gamestates;
    7881    };
    7982
     
    8689    virtual bool      ackGamestate(unsigned int gamestateID, unsigned int peerID) override;
    8790    virtual uint32_t  getLastReceivedGamestateID( unsigned int peerID ) override;
    88     virtual uint32_t  getCurrentGamestateID() override{ if( currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }
     91    virtual uint32_t  getCurrentGamestateID() override { if(currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }
    8992
    9093    bool processGamestates();
     
    98101    void addPeer( uint32_t peerID );
    99102    void setSynched( uint32_t peerID )
    100       { assert(peerMap_.find(peerID)!=peerMap_.end()); peerMap_[peerID].isSynched = true; }
     103      { assert(peerMap_.find(peerID) != peerMap_.end()); peerMap_[peerID].isSynched = true; }
    101104    void removePeer( uint32_t peerID );
    102     bool hasPeers(){ return this->peerMap_.size()!=0; }
    103 //     void removeClient(ClientInformation *client);
     105    bool hasPeers(){ return this->peerMap_.size() != 0; }
     106
    104107  protected:
    105108    virtual bool sendPacket( packet::Packet* packet ) = 0;
     109
    106110  private:
    107111    bool processGamestate(packet::Gamestate *gs);
    108112
    109 //     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>> gamestateMap_;
    110113    std::map<unsigned int, packet::Gamestate*> gamestateQueue;
    111 //     std::map<unsigned int, uint32_t> lastProcessedGamestateID_;
     114
     115    // Each peerID maps to a peerInfo struct
    112116    std::map<uint32_t, peerInfo> peerMap_;
     117
     118    // always contains the latest gamestate
    113119    packet::Gamestate* currentGamestate_;
    114 //     TrafficControl *trafficControl_;
    115120    unsigned int id_;
    116 //     boost::mutex* threadMutex_;
    117     ThreadPool*   /*thread*/Pool_;
     121    ThreadPool* Pool_;
    118122  };
    119123
  • code/branches/mergeFS18/src/libraries/network/Host.cc

    r11071 r12027  
    4444  SetConsoleCommand(__CC_printRTT_group, __CC_printRTT_name, &Host::printRTT);
    4545
    46   // Host*               Host::instance_=nullptr;
    47   uint32_t            Host::clientID_s=0;
    48 //   uint32_t            Host::shipID_s=-1;
     46  uint32_t            Host::clientID_s = 0;
    4947  std::vector<Host*>  Host::instances_s;
    5048
     
    5452  Host::Host()
    5553  {
    56   //   assert(instance_==nullptr);
    57     instances_s.push_back(this);
     54    Host::instances_s.push_back(this);
     55
    5856    ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(this);
     57
    5958    this->bIsActive_ = false;
    6059  }
     
    6665  Host::~Host()
    6766  {
    68     assert( std::find( instances_s.begin(), instances_s.end(), this )!=instances_s.end() );
    69     instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this ));
     67    assert(std::find( instances_s.begin(), instances_s.end(), this ) != instances_s.end());
     68    Host::instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this ));
    7069    ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(nullptr);
    7170  }
     
    8079  void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID)
    8180  {
    82     for(Host* host : instances_s)
     81    for (Host* host : instances_s)
    8382    {
    84       if( host->isActive() )
     83      if (host->isActive())
    8584      {
    8685        host->queuePacket(packet, clientID, channelID);
     
    9796  void Host::sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID)
    9897  {
    99     for(Host* host : instances_s)
    100       if( host->isActive() )
     98    for (Host* host : instances_s)
     99    {
     100      if (host->isActive())
     101      {
    101102        host->doSendChat(message, sourceID, targetID);
     103      }
     104    }
    102105  }
    103106
     
    108111  {
    109112    for (NetworkChatListener* listener : ObjectList<NetworkChatListener>())
     113    {
    110114      listener->incomingChat(message, sourceID);
     115    }
    111116  }
    112117
     
    114119  bool Host::isServer()
    115120  {
    116     for (Host* host : instances_s)
     121    for (Host* host : Host::instances_s)
    117122    {
    118       if( host->isActive() )
     123      if (host->isActive())
    119124      {
    120         if( host->isServer_() )
     125        if (host->isServer_())
     126        {
    121127          return true;
     128        }
    122129      }
    123130    }
     
    125132  }
    126133
     134  /**
     135   * Singleton implementation. Return the first active instance.
     136   */
    127137  Host* Host::getActiveInstance()
    128138  {
    129139    std::vector<Host*>::iterator it = Host::instances_s.begin();
    130     while( it != Host::instances_s.end() )
     140    while (it != Host::instances_s.end())
    131141    {
    132       if( (*it)->isActive() )
     142      if ((*it)->isActive())
     143      {
    133144        return *it;
     145      }
    134146      else
     147      {
    135148        ++it;
     149      }
    136150    }
    137151    return nullptr;
  • code/branches/mergeFS18/src/libraries/network/Host.h

    r8858 r12027  
    4040  const unsigned int CLIENTID_SERVER = 0;
    4141  const unsigned int NETWORK_FREQUENCY = 25;
    42   const float NETWORK_PERIOD = 1.0f/NETWORK_FREQUENCY;
     42  const float NETWORK_PERIOD = 1.0f / NETWORK_FREQUENCY;
    4343
    4444/**
     
    5555
    5656  private:
    57     virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)=0;
     57    virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) = 0;
    5858    virtual bool isServer_()=0;
    5959
     
    6363    void setActive( bool bActive ){ bIsActive_ = bActive; }
    6464
    65     virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID)=0;
    66     virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID)=0;
     65    virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0;
     66    virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0;
    6767
    6868  public:
     
    7474    static bool isServer();
    7575    static void sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID);
    76     virtual void printRTT()=0;
     76    virtual void printRTT() = 0;
    7777    bool isActive(){ return bIsActive_; }
     78   
    7879  private:
    7980    static uint32_t clientID_s;
  • code/branches/mergeFS18/src/libraries/network/LANDiscoverable.cc

    r11071 r12027  
    121121            info.setClientNumber(this->clientNumber);
    122122            info.send(event.peer);
    123 //             ENetPacket* packet = enet_packet_create( LAN_DISCOVERY_ACK, strlen(LAN_DISCOVERY_ACK)+1, ENET_PACKET_FLAG_RELIABLE );
    124 //             enet_peer_send(event.peer, 0, packet );
    125123            enet_host_flush(this->host_);
    126124          }
  • code/branches/mergeFS18/src/libraries/network/LANDiscoverable.h

    r10622 r12027  
    4343      void setActivity( bool bActive );
    4444      void update();
    45       void updateClientNumber(int clientNumber) {this->clientNumber = clientNumber;}
    46 ;
     45      void updateClientNumber(int clientNumber) { this->clientNumber = clientNumber; }
    4746      /** Function used for the configuration file parameter update */
    4847      void setConfigValues();
  • code/branches/mergeFS18/src/libraries/network/LANDiscovery.cc

    r11071 r12027  
    6161    address.port = LAN_DISCOVERY_PORT;
    6262
    63     /* TODO: check for availability of each protocol */
    6463    /* IPv4 */
    6564    address.host = ENET_HOST_BROADCAST;
     
    102101              this->servers_.push_back(info);
    103102          }
    104 //           enet_address_get_host_ip(&event.peer->address, buffer, buflen );
    105 //           serverIPs.push_back(std::string(buffer));
    106103          break;
    107104        default:
  • code/branches/mergeFS18/src/libraries/network/MasterServer.cc

    r11083 r12027  
    4040  SetConsoleCommand( "ms-listservers", &MasterServer::listServers );
    4141  SetConsoleCommand( "ms-delserver", &MasterServer::delServer );
    42   //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );
    4342
    4443  /* forward declaration so the linker doesn't complain */
     
    357356
    358357        /* incoming data */
    359       case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
     358      case ENET_EVENT_TYPE_RECEIVE:
     359        eventData( event ); break;
     360       
    360361      default: break;
    361362    }
  • code/branches/mergeFS18/src/libraries/network/MasterServerComm.cc

    r11071 r12027  
    3838     * the initialize method to facilitate debugging
    3939     */
    40     /* register object in orxonox */
    4140  }
    4241
     
    4847      return 1;
    4948    }
    50 
    51     /* initialize the event holder */
    52 //     this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
    53    
    5449
    5550    /* initiate the client */
     
    226221    /* One could just use enet_host_service() instead. */
    227222    enet_host_flush( this->client );
    228    
    229     /* free the packet */
    230     // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
    231 //     enet_packet_destroy( packet );
    232223
    233224    /* all done. */
     
    248239    /* One could just use enet_host_service() instead. */
    249240    enet_host_flush( this->client );
    250     // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
    251 //     enet_packet_destroy( packet );
    252241
    253242    /* all done. */
  • code/branches/mergeFS18/src/libraries/network/Server.cc

    r11103 r12027  
    5555#include "packet/Gamestate.h"
    5656#include "packet/Welcome.h"
    57 // #include "ClientInformation.h"
    5857#include "FunctionCallManager.h"
    5958#include "GamestateManager.h"
     
    6968  Server::Server()
    7069  {
    71     this->timeSinceLastUpdate_=0;
    72   }
    73 
     70    this->timeSinceLastUpdate_ = 0;
     71  }
     72
     73  /**
     74  * Constructor
     75  * @param port Port to listen on
     76  */
    7477  Server::Server(int port)
    7578  {
    76     this->setPort( port );
    77     this->timeSinceLastUpdate_=0;
    78   }
    79 /*
    80   Server::Server(int port, const std::string name)
    81   {
    82     this->setPort( port );
    83     this->timeSinceLastUpdate_=0;
    84     this->serverName_=name;
    85   }*/
     79    this->setPort(port);
     80    this->timeSinceLastUpdate_ = 0;
     81  }
     82
    8683  /**
    8784  * Constructor
     
    9188  Server::Server(int port, const std::string& bindAddress)
    9289  {
    93     this->setPort( port );
    94     this->setBindAddress( bindAddress );
    95     this->timeSinceLastUpdate_=0;
     90    this->setPort(port);
     91    this->setBindAddress(bindAddress);
     92    this->timeSinceLastUpdate_ = 0;
    9693  }
    9794
     
    156153    LANDiscoverable::update();
    157154
    158     if ( GamestateManager::hasPeers() )
     155    if (GamestateManager::hasPeers())
    159156    {
    160157      // process incoming gamestates
     
    163160
    164161      // send function calls to clients
    165       FunctionCallManager::sendCalls( static_cast<Host*>(this) );
    166 
    167       //this steers our network frequency
    168       timeSinceLastUpdate_+=time.getDeltaTime();
    169       if(timeSinceLastUpdate_>=NETWORK_PERIOD)
     162      FunctionCallManager::sendCalls(static_cast<Host*>(this));
     163
     164      // this steers our network frequency
     165      timeSinceLastUpdate_ += time.getDeltaTime();
     166      if(timeSinceLastUpdate_ >= NETWORK_PERIOD)
    170167      {
    171         timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
    172         updateGamestate();
     168        timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
     169        this->updateGamestate();
    173170      }
    174 //       sendPackets(); // flush the enet queue
    175171    }
    176172  }
     
    182178
    183179  /**
    184    * @brief: returns ping time to client in milliseconds
    185    */
    186   unsigned int Server::getRTT(unsigned int clientID)
    187   {
    188 //     assert(ClientInformation::findClient(clientID));
    189 //     return ClientInformation::findClient(clientID)->getRTT();
    190     // TODO: reimplement
    191     return 0;
    192   }
    193 
     180   * Print ping time to client in milliseconds.
     181   */
    194182  void Server::printRTT()
    195183  {
    196 //     for( ClientInformation* temp=ClientInformation::getBegin(); temp!=nullptr; temp=temp->next() )
    197 //       orxout(message) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl;
    198   }
    199 
    200   /**
    201    * @brief: return packet loss ratio to client (scales from 0 to 1)
    202    */
    203   float Server::getPacketLoss(unsigned int clientID)
    204   {
    205 //     assert(ClientInformation::findClient(clientID));
    206 //     return ClientInformation::findClient(clientID)->getPacketLoss();
    207     return 0.;
    208   }
    209 
    210   /**
    211   * takes a new snapshot of the gamestate and sends it to the clients
    212   */
     184    // TODO: Implement
     185  }
     186
     187  /**
     188   * Take a new snapshot of the gamestate and send it to the clients.
     189   */
    213190  void Server::updateGamestate()
    214191  {
    215     if( this->clientIDs_.size()==0 )
    216       //no client connected
     192    if(this->clientIDs_.size() == 0)
     193    {
     194      // no client connected
    217195      return;
     196    }
    218197    GamestateManager::update();
    219 //     orxout(verbose_more, context::network) << "Server: one gamestate update complete, goig to sendGameState" << endl;
    220     //orxout(verbose_more, context::network) << "updated gamestate, sending it" << endl;
    221     //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    222     sendGameStates();
    223     sendObjectDeletes();
    224 //     orxout(verbose_more, context::network) << "Server: one sendGameState turn complete, repeat in next tick" << endl;
    225     //orxout(verbose_more, context::network) << "sent gamestate" << endl;
    226   }
    227 
    228   /**
    229   * sends the current gamestate to all peers
    230   */
     198    this->sendGameStates();
     199    this->sendObjectDeletes();
     200  }
     201
     202  /**
     203   * Send the current gamestate to all peers.
     204   */
    231205  bool Server::sendGameStates()
    232206  {
    233207    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
    234     for( packet::Gamestate* gamestate : gamestates )
     208    for(packet::Gamestate* gamestate : gamestates)
    235209    {
    236210      gamestate->send(static_cast<Host*>(this));
     
    240214
    241215
     216  /**
     217   * Send 'DeleteObjects' packet
     218   */
    242219  bool Server::sendObjectDeletes()
    243220  {
    244 //     ClientInformation *temp = ClientInformation::getBegin();
    245 //     if( temp == nullptr )
    246       //no client connected
    247     if( this->clientIDs_.size()==0 )
     221    // no client connected
     222    if(this->clientIDs_.size() == 0)
     223    {
    248224      return true;
     225    }
     226
    249227    packet::DeleteObjects *del = new packet::DeleteObjects();
    250228    if(!del->fetchIDs())
     
    253231      return true;  //everything ok (no deletes this tick)
    254232    }
    255 //     orxout(verbose, context::network) << "sending DeleteObjects" << endl;
    256 //     while(temp != nullptr){
    257 //       if( !(temp->getSynched()) )
    258 //       {
    259 //         orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl;
    260 //         temp=temp->next();
    261 //         continue;
    262 //       }
    263 //       int cid = temp->getID(); //get client id
    264 //       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
    265 //       assert(cd);
     233
    266234    del->setPeerID(NETWORK_PEER_ID_BROADCAST);
    267     if ( !del->send( static_cast<Host*>(this) ) )
     235    if (!del->send( static_cast<Host*>(this)))
     236    {
    268237      orxout(internal_warning, context::network) << "Server: could not broadcast deleteObjects packet" << endl;
    269 //       temp=temp->next();
    270       // gs gets automatically deleted by enet callback
    271 //     }
    272 //     delete del;
     238    }
     239
    273240    return true;
    274241  }
    275242
    276 
     243  /**
     244   * Add a new peer to the server.
     245   */
    277246  void Server::addPeer(uint32_t peerID)
    278247  {
    279 //     static unsigned int newid=1;
    280 //
    281 //     orxout(internal_info, context::network) << "Server: adding client" << endl;
    282 //     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    283 //     if(!temp)
    284 //     {
    285 //       orxout(internal_warning, context::network) << "Server: could not add client" << endl;
    286 //     }
    287 //     temp->setID(newid);
    288 //     temp->setPeer(event->peer);
    289 
    290248    // inform all the listeners
    291249    this->clientIDs_.push_back(peerID);
     
    296254    GamestateManager::addPeer(peerID);
    297255
    298 //     ++newid;
    299 
    300256    orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl;
    301257
    302     createClient(peerID);
    303 }
    304 
     258    this->createClient(peerID);
     259  }
     260
     261  /**
     262   * Remove a peer from the server.
     263   */
    305264  void Server::removePeer(uint32_t peerID)
    306265  {
    307266    orxout(verbose, context::network) << "removing client from list" << endl;
    308 //     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    309 //     if(!client)
    310 //       return;
    311 //     else
    312 //     {
    313   std::vector<uint32_t>::iterator it;
    314   for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
    315   {
    316     if( *it == peerID )
    317     {
    318       this->clientIDs_.erase(it);
    319       break;
    320     }
    321   }
    322   WANDiscoverable::updateClientNumber(this->clientIDs_.size());
    323   LANDiscoverable::updateClientNumber(this->clientIDs_.size());
    324 
    325   ClientConnectionListener::broadcastClientDisconnected(peerID);
    326   GamestateManager::removePeer(peerID);
    327       //ServerConnection::disconnectClient( client );
    328       //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
    329 //       delete client;
    330 //     }
    331   }
    332 
     267
     268    // erase the peer from the list
     269    std::vector<uint32_t>::iterator it;
     270    for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it)
     271    {
     272      if(*it == peerID)
     273      {
     274        this->clientIDs_.erase(it);
     275        break;
     276      }
     277    }
     278
     279    WANDiscoverable::updateClientNumber(this->clientIDs_.size());
     280    LANDiscoverable::updateClientNumber(this->clientIDs_.size());
     281
     282    // Send 'clientDisconnected' message
     283    ClientConnectionListener::broadcastClientDisconnected(peerID);
     284
     285    GamestateManager::removePeer(peerID);
     286  }
     287
     288  /**
     289   * Process an incoming packet.
     290   */
    333291  void Server::processPacket(packet::Packet* packet)
    334292  {
    335     if( packet->isReliable() )
    336     {
    337       if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     293    if(packet->isReliable())
     294    {
     295      if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID())
     296      {
    338297        packet->process(static_cast<Host*>(this));
     298      }
    339299      else
     300      {
    340301        this->packetQueue_.push_back(packet);
     302      }
    341303    }
    342304    else
     305    {
    343306      packet->process(static_cast<Host*>(this));
    344   }
    345 
    346 
     307    }
     308  }
     309
     310  /**
     311   * Create a client.
     312   */
    347313  bool Server::createClient(int clientID)
    348314  {
    349 //     ClientInformation *temp = ClientInformation::findClient(clientID);
    350 //     if(!temp)
    351 //     {
    352 //       orxout(internal_error, context::network) << "Server. could not create client with id: " << clientID << endl;
    353 //       return false;
    354 //     }
    355 //     orxout(verbose, context::network) << "Con.Man: creating client id: " << temp->getID() << endl;
    356315
    357316    // synchronise class ids
    358     syncClassid(clientID);
     317    this->syncClassid(clientID);
    359318
    360319    // now synchronise functionIDs
    361320    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
    362321    fIDs->setPeerID(clientID);
    363     bool b = fIDs->send( static_cast<Host*>(this) );
     322    bool b = fIDs->send(static_cast<Host*>(this));
    364323    assert(b);
    365324
    366 //     temp->setSynched(true);
    367325    GamestateManager::setSynched(clientID);
    368326
     327    // Send 'Welcome' packet to client
    369328    orxout(verbose, context::network) << "sending welcome" << endl;
    370329    packet::Welcome *w = new packet::Welcome(clientID);
    371330    w->setPeerID(clientID);
    372     b = w->send( static_cast<Host*>(this) );
     331    b = w->send(static_cast<Host*>(this));
    373332    assert(b);
     333
    374334    (void)b; // avoid compiler warning
    375 //     packet::Gamestate *g = new packet::Gamestate();
    376 //     g->setPeerID(clientID);
    377 //     b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
    378 //     assert(b);
    379 //     if(!b)
    380 //       return false; //no data for the client
    381 // //     b = g->compressData();
    382 // //     assert(b);
    383 //     b = g->send( static_cast<Host*>(this) );
    384 //     assert(b);
    385335    return true;
    386336  }
    387337
    388   void Server::disconnectClient( uint32_t clientID )
    389   {
    390     ServerConnection::disconnectClient( clientID );
    391     GamestateManager::removePeer( clientID );
    392     // inform all the listeners
    393     // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
     338  /**
     339   * Disconnect a client.
     340   */
     341  void Server::disconnectClient(uint32_t clientID)
     342  {
     343    ServerConnection::disconnectClient(clientID);
     344    GamestateManager::removePeer(clientID);
    394345  }
    395346
     
    403354  {
    404355    // check if the target exists. just ignore the message otherwise
    405     if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore
     356    if (!this->isValidTarget(targetID))
     357    {
    406358      return;
     359    }
    407360
    408361    // send the message to the target
    409362    packet::Chat* packet = new packet::Chat(message, sourceID, targetID);
    410363    packet->setPeerID(targetID);
    411     packet->send( static_cast<Host*>(this) );
     364    packet->send(static_cast<Host*>(this));
    412365
    413366    // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners
    414367    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID())
     368    {
    415369      Host::doReceiveChat(message, sourceID, targetID);
     370    }
    416371  }
    417372
     
    431386  bool Server::isValidTarget(unsigned int targetID)
    432387  {
     388    // Broadcast or server ID are okay
    433389    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER)
     390    {
    434391      return true;
    435 
    436     for( uint32_t id : this->clientIDs_ )
    437       if( id == targetID )
     392    }
     393
     394    // IDs in the client list are okay also
     395    for(uint32_t id : this->clientIDs_)
     396    {
     397      if(id == targetID)
     398      {
    438399        return true;
     400      }
     401    }
    439402
    440403    return false;
     
    443406  void Server::syncClassid(unsigned int clientID)
    444407  {
    445     int failures=0;
    446408    packet::ClassID *classid = new packet::ClassID();
    447409    classid->setPeerID(clientID);
    448     while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
     410
     411    int failures = 0;
     412    while(!classid->send(static_cast<Host*>(this)) && failures < 10)\
     413    {
    449414      failures++;
    450415    }
  • code/branches/mergeFS18/src/libraries/network/Server.h

    r11071 r12027  
    3838#include "core/CorePrereqs.h"
    3939#include "Host.h"
    40 // #include "GamestateManager.h"
    4140#include "ServerConnection.h"
    4241#include "LANDiscoverable.h"
     
    6261    void close();
    6362    virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) override;
    64     virtual bool sendPacket( packet::Packet* packet ) override{ return packet->send( static_cast<Host*>(this) ); }
     63    virtual bool sendPacket( packet::Packet* packet ) override { return packet->send( static_cast<Host*>(this) ); }
    6564    void update(const Clock& time);
    66     unsigned int getRTT(unsigned int clientID);
    6765    virtual void printRTT() override;
    68     float getPacketLoss(unsigned int clientID);
    69     int getClientCount() { return this->clientIDs_.size();}
    70     std::string getServerName() { return this->serverName_;}
     66    int getClientCount() { return this->clientIDs_.size(); }
     67    std::string getServerName() { return this->serverName_; }
    7168
    7269  protected:
    7370    void updateGamestate();
    7471  private:
    75     virtual bool isServer_() override{return true;}
    76     unsigned int playerID(){return 0;}
     72    virtual bool isServer_() override { return true; }
     73    unsigned int playerID() { return 0; }
    7774
    7875    virtual void addPeer(uint32_t peerID) override;
  • code/branches/mergeFS18/src/libraries/network/ServerConnection.cc

    r11071 r12027  
    3636#include "util/Output.h"
    3737#include <util/Sleep.h>
    38 // #include "ClientInformation.h"
    3938
    4039namespace orxonox
    4140{
    42 
     41  /**
     42   * Constructor
     43   */
    4344  ServerConnection::ServerConnection():
    4445    bListening_(false)
    4546  {
    4647    this->bindAddress_ = new ENetAddress();
    47 //     memset(this->bindAddress_, 0, sizeof(ENetAddress));
    4848    this->bindAddress_->host = ENET_HOST_ANY;
    4949    this->bindAddress_->port = NETWORK_PORT;
     
    5151  }
    5252
     53  /**
     54   * Destructor
     55   */
    5356  ServerConnection::~ServerConnection()
    5457  {
    55     if ( this->bListening_ )
    56       closeListener();
     58    if (this->bListening_)
     59    {
     60      this->closeListener();
     61    }
    5762    delete this->bindAddress_;
    5863  }
    5964
    60   void ServerConnection::setBindAddress( const std::string& bindAddress )
     65  /**
     66   * Set address on which to listen.
     67   * @param bindAddress The address on which to listen
     68   */
     69  void ServerConnection::setBindAddress(const std::string& bindAddress)
    6170  {
    62     if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0)
    63         orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl;
     71    if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0)
     72    {
     73      orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl;
     74    }
    6475  }
    6576
    66   void ServerConnection::setPort( unsigned int port ) {
     77  /**
     78   * Set port on which to listen on.
     79   * @param port The port on which to listen on.
     80   */
     81  void ServerConnection::setPort(unsigned int port) {
    6782      this->bindAddress_->port = port;
    6883  }
     
    7388    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0);
    7489   
    75     if ( this->host_ == nullptr )
     90    if (this->host_ == nullptr)
    7691    {
    7792        orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl;
     
    8196    // enable compression
    8297    this->enableCompression();
     98
     99    // ensure that either IPv4 or IPv6 succeeded
    83100    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
     101
    84102    if (this->host_->socket4 == ENET_SOCKET_NULL)
     103    {
    85104        orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl;
     105    }
    86106    else if (this->host_->socket6 == ENET_SOCKET_NULL)
     107    {
    87108        orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl;
     109    }
    88110    else
     111    {
    89112        orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl;
     113    }
    90114   
    91115    // start communication thread
     
    95119  }
    96120
     121  /**
     122   * Stop listening.
     123   */
    97124  bool ServerConnection::closeListener()
    98125  {
    99     this->bListening_=false;
    100     disconnectClients();
     126    this->bListening_ = false;
     127    this->disconnectClients();
    101128    Connection::stopCommunicationThread();
    102129    enet_host_destroy(this->host_);
     
    104131  }
    105132
     133  /**
     134   * Add outgoing packet to queue.
     135   * @param packet The packet to send
     136   * @param clientID The ID of the recipient
     137   * @param channelID The channel ID
     138   */
    106139  void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID)
    107140  {
    108     if ( clientID == NETWORK_PEER_ID_BROADCAST )
     141    if (clientID == NETWORK_PEER_ID_BROADCAST)
    109142    {
    110       broadcastPacket(packet, channelID);
     143      this->broadcastPacket(packet, channelID);
    111144    }
    112145    else
    113146    {
    114 //       ClientInformation *temp = ClientInformation::findClient(clientID);
    115 //       if(!temp){
    116 //         orxout(internal_warning, context::network) << "C.Man: addPacket findClient failed" << endl;
    117 //       }
    118147      Connection::addPacket(packet, clientID, channelID);
    119148    }
    120149  }
    121150
    122 //   void ServerConnection::disconnectClient(ClientInformation *client)
    123 //   {
    124 //     Connection::disconnectPeer( client->getPeer() );
    125 //   }
    126 
     151  /**
     152   * Terminate connection with a peer.
     153   * @param clientID The peer with which to terminate the connection.
     154   */
    127155  void ServerConnection::disconnectClient(int clientID)
    128156  {
    129 //     ClientInformation *client = ClientInformation::findClient(clientID);
    130 //     if(client)
    131157    Connection::disconnectPeer(clientID);
    132158  }
    133159
     160  /**
     161   * Disconnect all peers.
     162   */
    134163  void ServerConnection::disconnectClients()
    135164  {
     
    138167    return;
    139168  }
    140 
    141 
    142 //   int ServerConnection::getClientID(ENetPeer* peer)
    143 //   {
    144 //     return getClientID(&(peer->address));
    145 //   }
    146 
    147 //   int ServerConnection::getClientID(ENetAddress* address)
    148 //   {
    149 //     return ClientInformation::findClient(address)->getID();
    150 //   }
    151 //
    152 //   ENetPeer *ServerConnection::getClientPeer(int clientID)
    153 //   {
    154 //     return ClientInformation::findClient(clientID)->getPeer();
    155 //   }
    156 
    157 
    158169}
  • code/branches/mergeFS18/src/libraries/network/ServerConnection.h

    r8327 r12027  
    4747{
    4848
    49   class _NetworkExport ServerConnection : public Connection{
     49  class _NetworkExport ServerConnection : public Connection
     50  {
    5051  public:
    5152    ~ServerConnection();
     
    5758    bool closeListener();
    5859    void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID);
    59 //     virtual void disconnectClient(ClientInformation *client);
    6060    void disconnectClient(int clientID);
     61
    6162  protected:
    6263    ServerConnection();
     
    6465
    6566  private:
    66 //     int getClientID(ENetPeer* peer);
    67 //     int getClientID(ENetAddress* address);
    68 //     ENetPeer* getClientPeer(int clientID);
    69 
    7067    ENetAddress* bindAddress_;
    71 
    7268    bool bListening_;
    7369
  • code/branches/mergeFS18/src/libraries/network/packet/Acknowledgement.cc

    r11071 r12027  
    3737
    3838#define PACKET_FLAGS_ACK    0
     39// Offset to start of packet ID
    3940#define _PACKETID           0
     41// Offset to start of ACK ID
    4042#define _ACKID              _PACKETID + sizeof(packet::Type)
    4143
     44/**
     45 * Constructor
     46 * Acknowledgement.data_ is 40 bits in size:
     47 * [0, 7]:   Packet Type
     48 * [8, 39]:  Acknowledgement ID
     49 */
    4250Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID )
    4351 : Packet()
    4452{
    45   flags_ = flags_ | PACKET_FLAGS_ACK;
    46   data_=new uint8_t[ getSize() ];
    47   *(Type *)(data_ + _PACKETID ) = Type::Acknowledgement;
    48   *(uint32_t *)(data_ + _ACKID ) = id;
    49   peerID_=peerID;
     53  this->flags_ |= PACKET_FLAGS_ACK;
     54  this->data_ = new uint8_t[ this->getSize() ];
     55  *(Type *)(this->data_ + _PACKETID) = Type::Acknowledgement;
     56  *(uint32_t *)(this->data_ + _ACKID) = id;
     57  this->peerID_ = peerID;
    5058}
    5159
     
    5967}
    6068
    61 unsigned int Acknowledgement::getSize() const{
     69unsigned int Acknowledgement::getSize() const {
    6270  return _ACKID + sizeof(uint32_t);
    6371}
    6472
    65 bool Acknowledgement::process(orxonox::Host* host){
     73bool Acknowledgement::process(orxonox::Host* host) {
    6674  orxout(verbose_more, context::packets) << "processing ACK with ID: " << getAckID() << endl;
    6775  bool b = host->ackGamestate(getAckID(), peerID_);
     
    7078}
    7179
    72 unsigned int Acknowledgement::getAckID(){
     80unsigned int Acknowledgement::getAckID() {
    7381  return *(uint32_t *)(data_ + _ACKID);
    7482}
  • code/branches/mergeFS18/src/libraries/network/packet/Acknowledgement.h

    r11071 r12027  
    3434
    3535namespace orxonox {
     36       
    3637const unsigned int ACKID_NACK = 0;
     38
    3739namespace packet {
    38 /**
    39     @author
    40 */
     40
    4141class _NetworkExport Acknowledgement : public Packet
    4242{
  • code/branches/mergeFS18/src/libraries/network/packet/Chat.cc

    r11071 r12027  
    3838#define   PACKET_FLAGS_CHAT PacketFlag::Reliable
    3939
    40 /* Some lengths */
     40/* Some lengths / offsets */
    4141#define _PACKETID         0
    4242#define _SOURCEID         _PACKETID + sizeof(Type)
     
    4949{
    5050  /* Add chat flag to packet flags */
    51   flags_ = flags_ | PACKET_FLAGS_CHAT;
     51  this->flags_ |= PACKET_FLAGS_CHAT;
    5252
    5353  /* set message length to length of input string + 1 */
    54   messageLength_ = message.length()+1;
     54  this->messageLength_ = message.length() + 1;
    5555
    5656  /* allocate memory for the data */
    57   data_=new unsigned char[ getSize() ];
     57  this->data_ = new unsigned char[ getSize() ];
    5858
    5959  *(Type *)(data_ + _PACKETID ) = Type::Chat;
    6060  *(unsigned int *)(data_ + _SOURCEID ) = sourceID;
    6161  *(unsigned int *)(data_ + _TARGETID ) = targetID;
    62   *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
     62  *(unsigned int *)(data_ + _MESSAGELENGTH ) = this->messageLength_;
    6363
    6464  /* cast the hell out of the message string, and copy it into the
    6565   * data buffer.
    6666   */
    67   memcpy( data_+_MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), messageLength_ );
     67  memcpy( this->data_ + _MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), this->messageLength_ );
    6868}
    6969
     
    7171  : Packet(data, clientID)
    7272{
    73   messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
     73  this->messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
    7474}
    7575
     
    7979
    8080unsigned int Chat::getSize() const{
    81   return _MESSAGE + messageLength_;
     81  return _MESSAGE + this->messageLength_;
    8282}
    8383
    8484bool Chat::process(orxonox::Host* host){
    85   host->doReceiveChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_SOURCEID), *(uint32_t *)(data_+_TARGETID));
     85  host->doReceiveChat(std::string((const char*)this->data_ + _MESSAGE),
     86                                  *(uint32_t *)(this->data_+_SOURCEID),
     87                                  *(uint32_t *)(this->data_+_TARGETID));
    8688  delete this;
    8789  return true;
    8890}
    8991
    90 unsigned char *Chat::getMessage(){
    91   return data_ + _MESSAGE;
     92unsigned char *Chat::getMessage() {
     93  return this->data_ + _MESSAGE;
    9294}
    9395
  • code/branches/mergeFS18/src/libraries/network/packet/Chat.h

    r11071 r12027  
    5555
    5656  /* Get the length of the message (not the full size of the packet) */
    57   unsigned int getMessageLength(){ return messageLength_; };
     57  unsigned int getMessageLength() { return this->messageLength_; };
    5858
    5959  /* return message content */
  • code/branches/mergeFS18/src/libraries/network/packet/ClassID.cc

    r11071 r12027  
    4646
    4747
    48 ClassID::ClassID( ) : Packet(){
     48ClassID::ClassID() : Packet() {
    4949  Identifier *id;
    50   unsigned int nrOfClasses=0;
    51   unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses
     50  unsigned int nrOfClasses = 0;
     51  unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses
    5252  uint32_t network_id;
    53   flags_ = flags_ | PACKET_FLAGS_CLASSID;
     53  this->flags_ |= PACKET_FLAGS_CLASSID;
    5454  std::queue<std::pair<uint32_t, std::string>> tempQueue;
    5555
    56   //calculate total needed size (for all strings and integers)
    57   for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()){
     56  // calculate total needed size (for all strings and integers)
     57  for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {
    5858    id = mapEntry.second;
    5959    if(id == nullptr || !id->hasFactory())
     
    6464    tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) );
    6565    ++nrOfClasses;
    66     packetSize += (classname.size()+1)+sizeof(network_id)+sizeof(uint32_t);
     66    packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t);
    6767  }
    6868
    69   this->data_=new uint8_t[ packetSize ];
     69  this->data_ = new uint8_t[ packetSize ];
    7070  //set the appropriate packet id
    7171  assert(this->data_);
    7272  *(Type *)(this->data_ + _PACKETID ) = Type::ClassID;
    7373
    74   uint8_t *temp=data_+sizeof(uint32_t);
     74  uint8_t *temp = this->data_ + sizeof(uint32_t);
    7575  // save the number of all classes
    76   *(uint32_t*)temp = nrOfClasses;
     76  *(uint32_t*) temp = nrOfClasses;
    7777  temp += sizeof(uint32_t);
    7878
    7979  // now save all classids and classnames
    8080  std::pair<uint32_t, std::string> tempPair;
    81   uint32_t tempsize = 2*sizeof(uint32_t); // packetid and nrOfClasses
     81  uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses
    8282  while( !tempQueue.empty() ){
    8383    tempPair = tempQueue.front();
    8484    tempQueue.pop();
    85     *(uint32_t*)temp = tempPair.first;
    86     *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1;
    87     memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1);
    88     temp+=2*sizeof(uint32_t)+tempPair.second.size()+1;
    89     tempsize+=2*sizeof(uint32_t)+tempPair.second.size()+1;
     85    *(uint32_t*) temp = tempPair.first;
     86    *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size() + 1;
     87    memcpy(temp + 2 * sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size() + 1);
     88    temp += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
     89    tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
    9090  }
    91   assert(tempsize==packetSize);
     91  assert(tempsize == packetSize);
    9292
    9393  orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl;
     
    104104}
    105105
    106 uint32_t ClassID::getSize() const{
    107   uint8_t *temp = data_+sizeof(uint32_t); // packet identification
     106uint32_t ClassID::getSize() const {
     107  uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification
    108108  uint32_t totalsize = sizeof(uint32_t); // packet identification
    109   uint32_t nrOfClasses = *(uint32_t*)temp;
     109  uint32_t nrOfClasses = *(uint32_t*) temp;
    110110  temp += sizeof(uint32_t);
    111111  totalsize += sizeof(uint32_t); // storage size for nr of all classes
    112112
    113   for(unsigned int i=0; i<nrOfClasses; i++){
    114     totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
    115     temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
     113  for(unsigned int i=0; i < nrOfClasses; i++) {
     114    totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t));
     115    temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
    116116  }
    117117  return totalsize;
    118118}
    119119
    120 
    121 bool ClassID::process(orxonox::Host* host){
     120bool ClassID::process(orxonox::Host* host) {
    122121  int nrOfClasses;
    123   uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
     122  uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid
    124123  uint32_t networkID;
    125124  uint32_t stringsize;
    126125  unsigned char *classname;
    127 
    128126
    129127  //clear the map of network ids
     
    134132  Identifier *id;
    135133  // read the total number of classes
    136   nrOfClasses = *(uint32_t*)temp;
     134  nrOfClasses = *(uint32_t*) temp;
    137135  temp += sizeof(uint32_t);
    138136
    139   for( int i=0; i<nrOfClasses; i++){
    140     networkID = *(uint32_t*)temp;
    141     stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
    142     classname = temp+2*sizeof(uint32_t);
    143     id=ClassByString( std::string((const char*)classname) );
     137  for( int i = 0; i < nrOfClasses; i++) {
     138    networkID = *(uint32_t*) temp;
     139    stringsize = *(uint32_t*) (temp + sizeof(uint32_t));
     140    classname = temp + 2 * sizeof(uint32_t);
     141    id = ClassByString( std::string((const char*) classname) );
    144142    orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl;
    145     if(id==nullptr){
     143    if(id == nullptr) {
    146144      orxout(user_error, context::packets) << "Received a bad classname" << endl;
    147145      abort();
    148146    }
    149147    id->setNetworkID( networkID );
    150     temp += 2*sizeof(uint32_t) + stringsize;
     148    temp += 2 * sizeof(uint32_t) + stringsize;
    151149  }
    152150  delete this;
  • code/branches/mergeFS18/src/libraries/network/packet/DeleteObjects.cc

    r11071 r12027  
    4545 : Packet()
    4646{
    47   flags_ = flags_ | PACKET_FLAG_DELETE;
     47  this->flags_ |= PACKET_FLAG_DELETE;
    4848}
    4949
     
    6060{
    6161  unsigned int number = Synchronisable::getNumberOfDeletedObject();
    62   if(number==0)
     62  if(number == 0)
    6363    return false;
    6464  orxout(verbose, context::packets) << "sending DeleteObjects: ";
     
    8383{
    8484  assert(data_);
    85   return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t);
     85  return _OBJECTIDS + *(uint32_t*) (this->data_ + _QUANTITY) * sizeof(uint32_t);
    8686}
    8787
    8888bool DeleteObjects::process(orxonox::Host* host)
    8989{
    90   for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++)
     90  for(unsigned int i = 0; i < *(unsigned int *) (this->data_+_QUANTITY); i++)
    9191  {
    9292    orxout(verbose, context::packets) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << endl;
    93     Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
     93    Synchronisable::deleteObject( *(uint32_t*)(this->data_ + _OBJECTIDS + i * sizeof(uint32_t)) );
    9494  }
    9595  delete this;
  • code/branches/mergeFS18/src/libraries/network/packet/FunctionCalls.cc

    r11071 r12027  
    6363  assert(isDataENetAllocated());
    6464 
    65   uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
    66   uint32_t nrOfCalls = *(uint32_t*)temp;
     65  uint8_t* temp = data_ + sizeof(uint32_t); //skip packetid
     66  uint32_t nrOfCalls = *(uint32_t*) temp;
    6767  temp += sizeof(uint32_t);
    68   this->minGamestateID_ = *(uint32_t*)temp;
     68  this->minGamestateID_ = *(uint32_t*) temp;
    6969  temp += sizeof(uint32_t);
    70   for( unsigned int i = 0; i<nrOfCalls; i++ )
     70  for( unsigned int i = 0; i < nrOfCalls; i++ )
    7171  {
    7272    FunctionCall fctCall;
     
    9595  this->minGamestateID_ = host->getCurrentGamestateID();
    9696  assert(this->functionCalls_.size());
    97   data_=new uint8_t[ currentSize_ ];
    98   *(Type *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
    99   *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
    100   *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
    101   uint8_t* temp = data_+3*sizeof(uint32_t);
     97  this->data_ = new uint8_t[ currentSize_ ];
     98  *(Type *)(this->data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
     99  *(uint32_t*)(this->data_ + sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
     100  *(uint32_t*)(this->data_ + 2 * sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
     101  uint8_t* temp = this->data_ + 3 * sizeof(uint32_t);
    102102 
    103103  while( this->functionCalls_.size() )
     
    107107  }
    108108 
    109   assert( temp==data_+currentSize_ );
     109  assert( temp == this->data_ + currentSize_ );
    110110 
    111111  Packet::send(host);
  • code/branches/mergeFS18/src/libraries/network/packet/Gamestate.cc

    r11083 r12027  
    4646#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
    4747
    48 // #define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
    4948#define PACKET_FLAG_GAMESTATE  0
    5049
     
    102101}
    103102
    104 
     103//AV: This takes all synchronisables and packs it in a GameState, to be sent over the network
    105104bool Gamestate::collectData(int id, uint8_t mode)
    106105{
     
    128127  for(it = ObjectList<Synchronisable>().begin(); it; ++it)
    129128  {
    130 
    131 //     tempsize=it->getSize(id, mode);
    132129
    133130    tempsize = it->getData(mem, this->sizes_, id, mode);
     
    173170}
    174171
    175 
     172//AV: This takes the Gamestate received from the network and "unpacks" it back to a list of Objects/Synchronisables, thus updating the data
    176173bool Gamestate::spreadData(uint8_t mode)
    177174{
     
    196193      else
    197194      {
    198 //         orxout(verbose, context::packets) << "not creating object of classid " << objectheader.getClassID() << endl;
    199195        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
    200196      }
     
    202198    else
    203199    {
    204 //       orxout(verbose, context::packets) << "updating object of classid " << objectheader.getClassID() << endl;
    205200      OrxVerify(s->updateData(mem, mode), "ERROR: could not update Synchronisable with Gamestate data");
    206201    }
    207202  }
    208203  assert((uintptr_t)(mem-data_) == GamestateHeader::getSize()+header_.getDataSize());
    209  
    210    // In debug mode, check first, whether there are no duplicate objectIDs
    211 #ifndef NDEBUG
    212   if(this->getID()%1000==1)
    213   {
    214     std::list<uint32_t> v1;
    215     for (Synchronisable* synchronisable : ObjectList<Synchronisable>())
    216     {
    217       if (synchronisable->getObjectID() == OBJECTID_UNKNOWN)
    218       {
    219         if (synchronisable->objectMode_ != 0x0)
    220         {
    221           orxout(user_error, context::packets) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << endl;
    222           orxout(user_error, context::packets) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << endl;
    223           orxout(user_error, context::packets) << "Objects class: " << synchronisable->getIdentifier()->getName() << endl;
    224           assert(false);
    225         }
    226       }
    227       else
    228       {
    229         for (uint32_t id : v1)
    230         {
    231           if (synchronisable->getObjectID() == id)
    232           {
    233             orxout(user_error, context::packets) << "Found duplicate objectIDs on the client!" << endl
    234                                                  << "Are you sure you don't create a Sychnronisable objcect with 'new' \
    235                                                      that doesn't have objectMode = 0x0?" << endl;
    236             assert(false);
    237           }
    238         }
    239         v1.push_back(synchronisable->getObjectID());
    240       }
    241     }
    242   }
    243 #endif
    244204  return true;
    245205}
     
    276236}
    277237
    278 
     238//AV: This function takes the Gamestate and compresses it for transmission over the network
    279239bool Gamestate::compressData()
    280240{
     
    313273}
    314274
    315 
     275//AV: This function takes the compressed Gamestate received from the network and decompresses it for further unpacking
    316276bool Gamestate::decompressData()
    317277{
     
    374334  if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 )
    375335  {
    376 //     orxout(verbose, context::packets) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;
    377336    origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object
    378337    baseDataPtr += objectOffset + objectHeader.getDataSize();
     
    430389inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
    431390{
    432   //       orxout(verbose, context::packets) << "docopy" << endl;
    433391  // Just copy over the whole Object
    434392  memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() );
     
    437395  newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
    438396  origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
    439 //   SynchronisableHeader baseHeader( baseData );
    440 //   baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
    441   //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << endl;
    442   //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << ":";
     397
    443398  sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
    444 //   for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i )
    445 //   {
    446 //     //         orxout(verbose, context::packets) << " " << *sizes;
    447 //     ++sizes;
    448 //   }
    449     //       orxout(verbose, context::packets) << endl;
     399
    450400}
    451401
     
    501451  uint8_t *origDataEnd = origDataPtr + header_.getDataSize();
    502452  uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize();
    503 //   uint32_t origLength = header_.getDataSize();
    504 //   uint32_t baseLength = base->header_.getDataSize();
     453
    505454
    506455  // Allocate new space for diffed gamestate
     
    595544
    596545
    597 /*Gamestate* Gamestate::diffData(Gamestate *base)
    598 {
    599   assert(this && base); assert(data_ && base->data_);
    600   assert(!header_.isCompressed() && !base->header_.isCompressed());
    601   assert(!header_.isDiffed());
    602 
    603   uint8_t *basep = GAMESTATE_START(base->data_);
    604   uint8_t *gs = GAMESTATE_START(this->data_);
    605   uint32_t dest_length = header_.getDataSize();
    606 
    607   if(dest_length==0)
    608     return nullptr;
    609 
    610   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
    611   uint8_t *dest = GAMESTATE_START(ndata);
    612 
    613   rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    614 #ifndef NDEBUG
    615   uint8_t *dest2 = new uint8_t[dest_length];
    616   rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );
    617   assert( memcmp( dest2, gs, dest_length) == 0 );
    618   delete dest2;
    619 #endif
    620 
    621   Gamestate *g = new Gamestate(ndata, getClientID());
    622   assert(g->header_);
    623   *(g->header_) = *header_;
    624   g->header_.setDiffed( true );
    625   g->header_.setBaseID( base->getID() );
    626   g->flags_=flags_;
    627   g->packetDirection_ = packetDirection_;
    628   assert(g->isDiffed());
    629   assert(!g->isCompressed());
    630   return g;
    631 }
    632 
    633 
    634 Gamestate* Gamestate::undiff(Gamestate *base)
    635 {
    636   assert(this && base); assert(data_ && base->data_);
    637   assert(!header_.isCompressed() && !base->header_.isCompressed());
    638   assert(header_.isDiffed());
    639 
    640   uint8_t *basep = GAMESTATE_START(base->data_);
    641   uint8_t *gs = GAMESTATE_START(this->data_);
    642   uint32_t dest_length = header_.getDataSize();
    643 
    644   if(dest_length==0)
    645     return nullptr;
    646 
    647   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
    648   uint8_t *dest = ndata + GamestateHeader::getSize();
    649 
    650   rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    651 
    652   Gamestate *g = new Gamestate(ndata, getClientID());
    653   assert(g->header_);
    654   *(g->header_) = *header_;
    655   g->header_.setDiffed( false );
    656   g->flags_=flags_;
    657   g->packetDirection_ = packetDirection_;
    658   assert(!g->isDiffed());
    659   assert(!g->isCompressed());
    660   return g;
    661 }
    662 
    663 
    664 void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
    665 {
    666   uint64_t* gd = (uint64_t*)data;
    667   uint64_t* bd = (uint64_t*)basedata;
    668   uint64_t* nd = (uint64_t*)newdata;
    669 
    670   unsigned int i;
    671   for( i=0; i<datalength/8; i++ )
    672   {
    673     if( i<baselength/8 )
    674       *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
    675     else
    676       *(nd+i) = *(gd+i); // just copy over the data
    677   }
    678   unsigned int j;
    679   // now process the rest (when datalength isn't a multiple of 4)
    680   for( j = 8*(datalength/8); j<datalength; j++ )
    681   {
    682     if( j<baselength )
    683       *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
    684     else
    685       *(newdata+j) = *(data+j); // just copy
    686   }
    687   assert(j==datalength);
    688 }*/
    689 
    690 
    691 /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
    692   assert(data_);
    693   std::list<obj>::iterator it;
    694 
    695   // allocate memory for new data
    696   uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];
    697   // create a gamestate out of it
    698   Gamestate *gs = new Gamestate(gdata);
    699   uint8_t *newdata = gdata + GamestateHeader::getSize();
    700   uint8_t *origdata = GAMESTATE_START(data_);
    701 
    702   //copy the GamestateHeader
    703   assert(gs->header_);
    704   *(gs->header_) = *header_;
    705 
    706   uint32_t objectOffset;
    707   unsigned int objectsize, destsize=0;
    708   // TODO: Why is this variable not used?
    709   //Synchronisable *object;
    710 
    711   //call TrafficControl
    712   TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );
    713 
    714   //copy in the zeros
    715 //   std::list<obj>::iterator itt;
    716 //   orxout() << "myvector contains:";
    717 //   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
    718 //     orxout() << " " << (*itt).objID;
    719 //   orxout() << endl;
    720   for(it=dataVector_.begin(); it!=dataVector_.end();){
    721     SynchronisableHeader oldobjectheader(origdata);
    722     SynchronisableHeader newobjectheader(newdata);
    723     if ( (*it).objSize == 0 )
    724     {
    725       ++it;
    726       continue;
    727     }
    728     objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
    729     objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
    730     if ( (*it).objID == oldobjectheader.getObjectID() ){
    731       memcpy(newdata, origdata, objectsize);
    732       ++it;
    733     }else{
    734       newobjectheader = oldobjectheader;
    735       memset(newdata+objectOffset, 0, objectsize-objectOffset);
    736     }
    737     newdata += objectsize;
    738     origdata += objectsize;
    739     destsize += objectsize;
    740   }
    741 #ifndef NDEBUG
    742   uint32_t origsize = destsize;
    743   while ( origsize < header_.getDataSize() )
    744   {
    745     SynchronisableHeader oldobjectheader(origdata);
    746     objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
    747     origdata += objectsize;
    748     origsize += objectsize;
    749   }
    750   assert(origsize==header_.getDataSize());
    751   assert(destsize!=0);
    752 #endif
    753   gs->header_.setDataSize( destsize );
    754   return gs;
    755 }*/
    756 
    757 
    758546uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode)
    759547{
  • code/branches/mergeFS18/src/libraries/network/packet/Gamestate.h

    r11071 r12027  
    5757    GamestateHeader(uint8_t* data)
    5858      { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
    59     /*GamestateHeader(uint8_t* data, GamestateHeader* h)
    60       { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/
    6159    void setData(uint8_t* data)
    6260      { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
     
    127125    inline uint32_t getDataSize() const { return header_.getDataSize(); }
    128126    Gamestate* diffVariables(Gamestate *base);
    129 //     Gamestate* diffData(Gamestate *base);
    130 //     Gamestate *undiff(Gamestate *base);
    131 //     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
     127
    132128    bool compressData();
    133129    bool decompressData();
     
    136132    // Packet functions
    137133  private:
    138 //     void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);
    139 //     inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 );
     134
    140135    virtual uint32_t getSize() const override;
    141136    virtual bool process(orxonox::Host* host) override;
    142137    uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0);
    143 //     inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
    144 //     inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
    145    
     138   
    146139    std::list<obj>          dataVector_;
    147140    GamestateHeader         header_;
  • code/branches/mergeFS18/src/libraries/network/packet/Packet.cc

    r11071 r12027  
    129129}
    130130
     131/**
     132 * Send the Packet.
     133 * @param host The host which sends the packet
     134 */
    131135bool Packet::send(orxonox::Host* host)
    132136{
     137  // Deny sending incoming packets
    133138  if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional )
    134139  {
     
    136141    return false;
    137142  }
     143
    138144  if(!enetPacket_)
    139145  {
    140     if(!data_){
     146    // Deny sending empty packets
     147    if(!data_) {
    141148      assert(0);
    142149      return false;
     
    152159      // without having a reference in the packetMap_
    153160      Packet::packetMapMutex_.lock();
    154       packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
     161      Packet::packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
    155162      Packet::packetMapMutex_.unlock();
    156163    }
     
    173180  }
    174181#endif
    175 //  ENetPacket *temp = enetPacket_;
    176 //  enetPacket_ = nullptr; // otherwise we have a double free because enet already handles the deallocation of the packet
     182
     183  // Send via reliable or standard channel respectively
    177184  if( this->flags_ & PacketFlag::Reliable )
    178185    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT);
    179186  else
    180187    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE);
     188
    181189  return true;
    182190}
    183191
     192/**
     193 * Given an ENetPacket, create an Orxonox packet
     194 * @param packet The ENetPacket
     195 * @param peerID The sender
     196 */
    184197Packet *Packet::createPacket(ENetPacket* packet, uint32_t peerID)
    185198{
    186199  uint8_t *data = packet->data;
    187 //   assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());
    188 //   unsigned int peerID = ClientInformation::findClient(&peer->address)->getID();
    189   // HACK
    190 //   if( peerID==static_cast<unsigned int>(-2))
    191 //     peerID = NETWORK_PEER_ID_SERVER;
    192200  Packet *p = nullptr;
    193 //   orxout(verbose_ultra, context::packets) << "packet type: " << *(Type *)&data[_PACKETID] << endl;
    194201  switch( *(Type *)(data + _PACKETID) )
    195202  {
    196203    case Type::Acknowledgement:
    197 //       orxout(verbose_more, context::packets) << "ack" << endl;
    198     p = new Acknowledgement( data, peerID );
     204      p = new Acknowledgement( data, peerID );
    199205      break;
    200206    case Type::Chat:
    201 //       orxout(verbose_more, context::packets) << "chat" << endl;
    202207      p = new Chat( data, peerID );
    203208      break;
    204209    case Type::ClassID:
    205 //       orxout(verbose_more, context::packets) << "classid" << endl;
    206210      p = new ClassID( data, peerID );
    207211      break;
    208212    case Type::Gamestate:
    209 //       orxout(verbose_more, context::packets) << "gamestate" << endl;
    210213      p = new Gamestate( data, peerID );
    211214      break;
    212215    case Type::Welcome:
    213 //       orxout(verbose_more, context::packets) << "welcome" << endl;
    214216      p = new Welcome( data, peerID );
    215217      break;
    216218    case Type::DeleteObjects:
    217 //       orxout(verbose_more, context::packets) << "deleteobjects" << endl;
    218219      p = new DeleteObjects( data, peerID );
    219220      break;
    220221    case Type::FunctionCalls:
    221 //       orxout(verbose_more, context::packets) << "functionCalls" << endl;
    222222      p = new FunctionCalls( data, peerID );
    223223      break;
    224224    case Type::FunctionIDs:
    225 //       orxout(verbose_more, context::packets) << "functionIDs" << endl;
    226225      p = new FunctionIDs( data, peerID );
    227226      break;
     
    247246  // Get our Packet from a global map with all Packets created in the send() method of Packet.
    248247  Packet::packetMapMutex_.lock();
    249   std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
     248
     249  std::map<size_t, Packet*>::iterator it = Packet::packetMap_.find(reinterpret_cast<size_t>(enetPacket));
    250250  assert(it != packetMap_.end());
     251
    251252  // Make sure we don't delete it again in the destructor
    252253  it->second->enetPacket_ = nullptr;
    253254  delete it->second;
    254255  packetMap_.erase(it);
     256
    255257  Packet::packetMapMutex_.unlock();
    256 //   orxout(verbose_ultra, context::packets) << "PacketMap size: " << packetMap_.size() << endl;
    257258}
    258259
  • code/branches/mergeFS18/src/libraries/network/packet/Packet.h

    r11071 r12027  
    6868
    6969    virtual unsigned char* getData(){ return data_; };
    70     virtual unsigned int getSize() const =0;
    71     virtual bool process(orxonox::Host* host)=0;
     70    virtual unsigned int getSize() const = 0;
     71
     72    // Invoke some sort of action associated with the packet
     73    virtual bool process(orxonox::Host* host) = 0;
     74   
    7275    inline uint32_t getFlags()
    7376      { return flags_; }
     
    8285
    8386    virtual bool send(orxonox::Host* host);
     87
    8488  protected:
    8589    Packet();
    8690    Packet(uint8_t *data, unsigned int peerID);
    87 //    Packet(ENetPacket *packet, ENetPeer *peer);
    8891    inline bool isDataENetAllocated() const
    8992      { return bDataENetAllocated_; }
     
    100103        data_ might no correlate with enetPacket_->data. */
    101104    bool bDataENetAllocated_;
     105
    102106  private:
     107    // All Packets are contained in this map
    103108    static std::map<size_t, Packet *> packetMap_;
    104109    static boost::mutex               packetMapMutex_;
  • code/branches/mergeFS18/src/libraries/network/packet/ServerInformation.cc

    r11083 r12027  
    5050      // Save Server Round Trip Time
    5151      this->serverRTT_ = event->peer->roundTripTime;
     52
    5253      // Save Server Address, leave some space for scope ID
    5354      enet_address_get_host_ip(&event->peer->address, serverIP, 64);
     55
    5456      this->serverIP_ = std::string(serverIP);
    5557      // Save ACK
     
    5759      char* ack = nullptr;
    5860      loadAndIncrease((char*&)ack, temp);
    59 
    60       /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
    61       // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
    62 //       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
    63 
    6461      // Save Server Name
    6562      loadAndIncrease(this->serverName_, temp);
     
    7471    void ServerInformation::send(ENetPeer* peer)
    7572    {
    76       std::string payload = this->serverName_ + Ogre::StringConverter::toString(this->clientNumber_);
     73      std::string payload = this->serverName_ + std::to_string(this->clientNumber_);
    7774      uint32_t size = returnSize(LAN_DISCOVERY_ACK) + returnSize(payload);
    7875      uint8_t* temp = new uint8_t[size];
  • code/branches/mergeFS18/src/libraries/network/synchronisable/Synchronisable.cc

    r11103 r12027  
    4343  std::queue<uint32_t> Synchronisable::deletedObjects_;
    4444
    45   uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
     45  uint8_t Synchronisable::state_=0x1; // determines wheter we are server (default) or client
    4646
    4747  RegisterAbstractClass(Synchronisable).inheritsFrom<OrxonoxInterface>();
     
    5353  Synchronisable::Synchronisable(Context* context)
    5454  {
    55       RegisterObject(Synchronisable);
     55    RegisterObject(Synchronisable);
    5656    static uint32_t idCounter=0;
    5757    objectMode_=0x1; // by default do not send data to server
     
    179179    assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() );
    180180    no->setObjectID(header.getObjectID());
    181     //no->contextID=header.getContextID(); //TODO: remove this
    182181    no->setClassID(header.getClassID());
    183182    assert(no->contextID_ == header.getContextID());
    184     //assert(no->classID_ == header.getClassID());
    185183    orxout(verbose, context::network) << "fabricate objectID_: " << no->objectID_ << " classID_: " << no->classID_ << endl;
    186184          // update data and create object/entity...
     
    189187    if (b)
    190188    {
    191 //        b = no->create();
    192189        assert(b);
    193190    }
     
    273270
    274271    orxout(verbose_more, context::network) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << endl;
    275 //     orxout(verbose, context::network) << "objectid: " << this->objectID_ << ":";
    276272    // copy to location
    277273    for(SynchronisableVariableBase* variable : syncList_)
    278274    {
    279275      uint32_t varsize = variable->getData( mem, mode );
    280 //       orxout(verbose, context::network) << " " << varsize;
    281276      tempsize += varsize;
    282277      sizes.push_back(varsize);
    283278      ++test;
    284       //tempsize += (*i)->getSize( mode );
    285279    }
    286280    assert(tempsize!=0);  // if this happens an empty object (with no variables) would be transmitted
    287 //     orxout(verbose, context::network) << endl;
    288281
    289282    header.setObjectID( this->objectID_ );
     
    340333    }
    341334
    342     //orxout(verbose_more, context::network) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << endl;
    343335    if( !syncHeaderLight.isDiffed() )
    344336    {
     
    357349    {
    358350      mem += SynchronisableHeaderLight::getSize();
    359 //       orxout(debug_output, context::network) << "objectID: " << this->objectID_ << endl;
    360351      while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() )
    361352      {
    362353        VariableID varID = *(VariableID*)mem;
    363 //         orxout(debug_output, context::network) << "varID: " << varID << endl;
    364354        assert( varID < syncList_.size() );
    365355        mem += sizeof(VariableID);
     
    400390  bool Synchronisable::doSync(/*int32_t id,*/ uint8_t mode)
    401391  {
    402 //     if(mode==0x0)
    403 //       mode=state_;
    404392    assert(mode!=0x0);
    405393    return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) );
  • code/branches/mergeFS18/src/libraries/network/synchronisable/Synchronisable.h

    r11071 r12027  
    134134        { memcpy(data_, h.data_, getSize()); }
    135135  };
    136  
    137 //   inline void operator=(SynchronisableHeaderLight& h1, SynchronisableHeader& h2)
    138 //   {
    139 //     memcpy(h1.data_, h2.data_, h1.getSize());
    140 //   }
    141136
    142137  /**
  • code/branches/mergeFS18/src/modules/overlays/hud/HUDWeaponSystem.cc

    r11071 r12027  
    126126        destroyHUDChilds();
    127127
     128        // TODO:
     129        // On network clients, owner_->getWeaponSystem() returns a nullpointer
     130        // This causes the overlay to not be shown on clients.
    128131        if (owner_->getWeaponSystem())
    129132        {
  • code/branches/mergeFS18/src/orxonox/graphics/Model.cc

    r11795 r12027  
    109109        registerVariable(this->meshSrc_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh));
    110110        registerVariable(this->renderQueueGroup_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup));
    111         registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
     111        //Commented out, since this causes texture load failures on client side
     112        //registerVariable(this->materialName_,    VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial));
    112113        registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows));
    113114    }
     
    130131                this->detachOgreObject(this->mesh_.getEntity());
    131132
    132             this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
     133                // Refuse to set empty mesh sources
     134                if (this->meshSrc_ != "")
     135                    this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
    133136
    134137            if (this->mesh_.getEntity())
Note: See TracChangeset for help on using the changeset viewer.