Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1534 for code/trunk


Ignore:
Timestamp:
Jun 4, 2008, 8:53:10 PM (17 years ago)
Author:
rgrieder
Message:

merged network branch back to trunk

Location:
code/trunk
Files:
20 edited
1 copied

Legend:

Unmodified
Added
Removed
  • code/trunk/TODO

    r1505 r1534  
    11- should we use enet_peer_ping to test if a client is still alive ?
    22- enet_host_broadcast ? (to all peers)
    3 - enet_host_check_events ? instead of enet_host_service
  • code/trunk/src/core/ConsoleCommand.h

    r1505 r1534  
    4242
    4343#define SetConsoleCommandGeneric(fakevariable, classname, command, bCreateShortcut) \
    44     ConsoleCommand& fakevariable = ClassManager<classname>::getIdentifier()->addConsoleCommand(command, bCreateShortcut)
     44    orxonox::ConsoleCommand& fakevariable = orxonox::ClassManager<classname>::getIdentifier()->addConsoleCommand(command, bCreateShortcut)
    4545
    4646
     
    5252
    5353#define SetConsoleCommandShortcutGeneric(fakevariable, command) \
    54     ConsoleCommand& fakevariable = CommandExecutor::addConsoleCommandShortcut(command)
     54    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command)
    5555
    5656
  • code/trunk/src/network/Client.cc

    r1505 r1534  
    4242#include "Synchronisable.h"
    4343#include "core/CoreIncludes.h"
     44#include "core/ConsoleCommand.h"
     45#include "Server.h"
    4446
    4547namespace network
    4648{
     49  SetConsoleCommandShortcut(Client, Chat);
     50 
    4751  Client* Client::_sClient = 0;
    4852 
     
    142146 
    143147
     148  void Client::Chat( std::string message ){
     149    if(Client::getSingleton())
     150      Client::getSingleton()->sendChat(message);
     151    else if(Server::getSingleton())
     152      Server::getSingleton()->sendChat(message);
     153    else
     154      COUT(1) << "do you want to monologize ??" << std::endl;
     155  }
    144156 
    145157
     
    224236  }
    225237
    226   void Client::processChat( chat *data){
    227     COUT(0) << "Server: " << data->message << std::endl;
     238  void Client::processChat( chat *data, int clientId){
     239    COUT(1) << data->message << std::endl;
    228240    delete[] data->message;
    229241    delete data;
  • code/trunk/src/network/Client.h

    r1505 r1534  
    7575    bool closeConnection();
    7676
    77     bool sendChat( std::string message );
     77    static void Chat( std::string message );
    7878   
    7979    int getShipID(){return shipID_;}
     
    9696    bool isSynched_;
    9797
     98    bool sendChat( std::string message );
     99   
    98100    // implement data processing functions of PacketDecoder
    99101    void processGamestate( GameStateCompressed *data, int clientID);
    100102    void processClassid(classid *clid);
    101     void processChat( chat *data);
     103    void processChat( chat *data, int clientId );
    102104    bool processWelcome( welcome *w );
    103105    int clientID_;     // this is the id the server gave to us
  • code/trunk/src/network/ClientConnection.cc

    r1505 r1534  
    5757    server=NULL;
    5858    enet_address_set_host(&serverAddress, address.c_str());
    59     serverAddress.port = NETWORK_PORT;
     59    serverAddress.port = port;
    6060    established=false;
    6161  }
     
    6565    server=NULL;
    6666    enet_address_set_host(&serverAddress, address);
    67     serverAddress.port = NETWORK_PORT;
     67    serverAddress.port = port;
    6868    established=false;
    6969  }
     
    107107    //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
    108108    // wait 10 seconds for the connection to be established
    109     return waitEstablished(3000);
     109    return waitEstablished(NETWORK_CLIENT_CONNECT_TIMEOUT);
    110110  }
    111111
     
    169169      {
    170170        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    171         if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
     171        if(enet_host_service(client, event, NETWORK_CLIENT_WAIT_TIME)<0){
    172172          // we should never reach this point
    173173          quit=true;
     
    211211    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    212212    enet_peer_disconnect(server, 0);
    213     while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
     213    while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME) > 0){
    214214      switch (event.type)
    215215      {
     
    238238    }
    239239    // handshake
    240     if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){
    241       established=true;
    242       return true;
    243     }
    244     else {
    245       COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
    246       return false;
    247     }
     240    while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit){
     241      if( event.type == ENET_EVENT_TYPE_CONNECT ){
     242        established=true;
     243        return true;
     244      }
     245    }
     246    COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
     247    return false;
    248248  }
    249249
  • code/trunk/src/network/ClientConnection.h

    r1505 r1534  
    5454#define NETWORK_PORT 55556
    5555#define NETWORK_CLIENT_MAX_CONNECTIONS 5
    56 #define NETWORK_CLIENT_TIMEOUT 1
     56#define NETWORK_CLIENT_WAIT_TIME 1
     57#define NETWORK_CLIENT_CONNECT_TIMEOUT 3000 // miliseconds
    5758#define NETWORK_CLIENT_CHANNELS 2
    5859
  • code/trunk/src/network/ClientInformation.cc

    r1505 r1534  
    191191    failures_=0;
    192192  }
     193 
     194  enet_uint32 ClientInformation::getRTT(){
     195    return peer_->roundTripTime;
     196  }
     197 
     198  enet_uint32 ClientInformation::getPacketLoss(){
     199    return peer_->packetLoss;
     200  }
    193201
    194202  int ClientInformation::getGamestateID() {
  • code/trunk/src/network/ClientInformation.h

    r1505 r1534  
    8787    void addFailure();
    8888    void resetFailures();
     89    enet_uint32 getRTT();
     90    enet_uint32 getPacketLoss();
    8991   
    9092    bool removeClient(int clientID);
  • code/trunk/src/network/ConnectionManager.cc

    r1505 r1534  
    168168    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    169169    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
    170       if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
     170      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
     171      if(enet_peer_send(i->getPeer(), 0, packet)!=0)
    171172        return false;
    172173    }
  • code/trunk/src/network/GameStateClient.cc

    r1505 r1534  
    8282      if (loadSnapshot(gs)){
    8383        gameStateMap.insert(std::pair<int, GameState*>(gs->id, gs));
    84         COUT(4) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl;
     84        COUT(5) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl;
    8585        last_diff_=gs->base_id;
    8686        //last_gamestate_=gs->id;
     
    241241      size+=it->getSize(); // size of the actual data of the synchronisable
    242242      size+=3*sizeof(int); // size of datasize, classID and objectID
    243       COUT(4) << "getpartialsnapshot: size: " << size << std::endl;
     243      COUT(5) << "getpartialsnapshot: size: " << size << std::endl;
    244244    }
    245245    //retval->data = (unsigned char*)malloc(size);
     
    380380    //std::cout << "length " << length << std::endl;
    381381    switch ( retval ) {
    382       case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
     382      case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
    383383      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
    384384      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
     
    399399
    400400  GameState *GameStateClient::decode(GameState *old, GameStateCompressed *diff) {
    401     COUT(4) << "using diffed gamestate" << std::endl;
     401    COUT(5) << "using diffed gamestate" << std::endl;
    402402    GameState *t = decode(diff);
    403403    if(!t)
  • code/trunk/src/network/GameStateManager.cc

    r1505 r1534  
    4444#include <iostream>
    4545#include <zlib.h>
     46#include <assert.h>
    4647
    4748#include "core/CoreIncludes.h"
     
    106107        break;
    107108      if( (*it).second <= 0 ){
    108         COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
     109        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
    109110        std::map<int, GameState *>::iterator tempit = gameStateMap.find((*it).first);
    110111        if( tempit != gameStateMap.end() ){
     
    140141        client = it->second;
    141142      GameState *server = reference;
    142       COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl;
     143      //COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl;
     144      COUT(4) << "client: " << (client!=0 ? client->id : (int)client) << " server: " << server->id << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl;
    143145      if(client)
    144146        return encode(client, server);
     
    267269      sync.data = data;
    268270      data+=sync.length;
    269       COUT(4) << "objectID: " << sync.objectID << " classID: " << sync.classID << std::endl;
     271      COUT(5) << "objectID: " << sync.objectID << " classID: " << sync.classID << std::endl;
    270272      while(it && it->objectID!=sync.objectID)
    271273        ++it;
     
    319321
    320322  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
    321     COUT(4) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
     323    COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
    322324    GameState *r = diff(a,b);
    323325    GameStateCompressed *c = compress_(r);
     
    472474    if(gamestateID == GAMESTATEID_INITIAL){
    473475      temp->setGameStateID(GAMESTATEID_INITIAL);
    474       if(curid!=GAMESTATEID_INITIAL)
     476      if(curid!=GAMESTATEID_INITIAL){
     477        assert(gameStateUsed.find(curid)!=gameStateUsed.end());
    475478        --(gameStateUsed.find(curid)->second);
     479      }
    476480      return;
    477481    }
     
    483487//     deleteUnusedGameState(curid);
    484488    //increase gamestateused
    485     if(curid!=GAMESTATEID_INITIAL)
    486       --(gameStateUsed.find(curid)->second);
    487     ++(gameStateUsed.find(gamestateID)->second);
    488     temp->setGameStateID(gamestateID);
     489    std::map<int, int>::iterator it = gameStateUsed.find(curid);
     490    if(curid!=GAMESTATEID_INITIAL){
     491      if(it!=gameStateUsed.end())
     492        --(it->second);
     493    }
     494    it = gameStateUsed.find(gamestateID);
     495    if(it!=gameStateUsed.end()){
     496      ++(it->second);
     497      temp->setGameStateID(gamestateID);
     498    }
    489499    /*
    490500    GameState *old = clientGameState[clientID];
  • code/trunk/src/network/PacketDecoder.cc

    r1505 r1534  
    151151  void PacketDecoder::gstate( ENetPacket* packet, int clientID )
    152152  {
    153     if(!testAndRemoveCRC(packet)){
    154       COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl;
    155       return;
    156     }
     153//    if(!testAndRemoveCRC(packet)){
     154//     COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl;
     155//      return;
     156//    }
    157157    GameStateCompressed* currentState = NULL;
    158158    currentState = new GameStateCompressed;
  • code/trunk/src/network/PacketGenerator.cc

    r1505 r1534  
    144144    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
    145145    //delete[] data;
    146     if(!addCRC(packet))
    147       COUT(3) << "could not add crc to gamestate packet" << std::endl;
     146//    if(!addCRC(packet))
     147//      COUT(3) << "could not add crc to gamestate packet" << std::endl;
    148148    return packet;
    149149  }
  • code/trunk/src/network/Server.cc

    r1505 r1534  
    5151#include "util/Sleep.h"
    5252#include "objects/SpaceShip.h"
    53 
     53#include "core/ConsoleCommand.h"
    5454
    5555namespace network
    5656{
    57  
    58  
    59 #define MAX_FAILURES 20;
    60 #define NETWORK_FREQUENCY 30
     57  #define MAX_FAILURES 20;
     58  #define NETWORK_FREQUENCY 30
     59 
     60  Server *Server::instance_=0;
     61 
     62  Server *Server::createSingleton(){
     63    if(!instance_)
     64      instance_ = new Server();
     65    return instance_;
     66  }
     67  Server *Server::createSingleton(int port){
     68    if(!instance_)
     69      instance_ = new Server(port);
     70    return instance_;
     71  }
     72  Server *Server::createSingleton(int port, std::string bindAddress){
     73    if(!instance_)
     74      instance_ = new Server(port, bindAddress);
     75    return instance_;
     76  }
     77  Server *Server::createSingleton(int port, const char *bindAddress){
     78    if(!instance_)
     79      instance_ = new Server(port, bindAddress);
     80    return instance_;
     81  }
     82 
     83  Server *Server::getSingleton(){
     84    return instance_;
     85  }
     86 
    6187 
    6288  /**
     
    127153  * @return true/false
    128154  */
    129   bool Server::sendMSG(std::string msg) {
    130     ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
    131     //std::cout <<"adding packets" << std::endl;
    132     return connection->addPacketAll(packet);
     155  bool Server::sendChat(std::string msg) {
     156    return sendChat(msg.c_str());
    133157  }
    134158
     
    138162  * @return true/false
    139163  */
    140   bool Server::sendMSG(const char *msg) {
    141     ENetPacket *packet = packet_gen.chatMessage(msg);
    142     COUT(4) <<"Server: adding Packets" << std::endl;
     164  bool Server::sendChat(const char *msg) {
     165    char *message = new char [strlen(msg)+10+1];
     166    sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
     167    COUT(1) << message << std::endl;
     168    ENetPacket *packet = packet_gen.chatMessage(message);
     169    COUT(5) <<"Server: adding Packets" << std::endl;
    143170    return connection->addPacketAll(packet);
    144171  }
     
    202229  void Server::updateGamestate() {
    203230    gamestates->update();
    204     COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
     231    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
    205232    //std::cout << "updated gamestate, sending it" << std::endl;
    206233    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    207234    sendGameState();
    208     COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
     235    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
    209236    //std::cout << "sent gamestate" << std::endl;
    210237  }
     
    229256        continue;
    230257      }
     258      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
    231259      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
    232260      int gid = temp->getGamestateID(); //get gamestate id
     
    287315  }
    288316 
     317  void Server::processChat( chat *data, int clientId){
     318    char *message = new char [strlen(data->message)+10+1];
     319    sprintf(message, "Player %d: %s", clientId, data->message);
     320    COUT(1) << message << std::endl;
     321    ENetPacket *pck = packet_gen.chatMessage(message);
     322    connection->addPacketAll(pck);
     323    delete[] data->message;
     324    delete data;
     325  }
     326 
    289327  bool Server::addClient(ENetEvent *event){
    290328    ClientInformation *temp = clients->insertBack(new ClientInformation);
     
    312350    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
    313351    connection->syncClassid(temp->getID());
    314     COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
     352    COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl;
    315353    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
    316354    if(!createShip(temp))
  • code/trunk/src/network/Server.h

    r1505 r1534  
    5353namespace network
    5454{
     55  #define CLIENTID_SERVER 0
     56 
    5557  /**
    5658  * This class is the root class of the network module for a server.
     
    5961  class _NetworkExport Server : public PacketDecoder, public orxonox::Tickable{
    6062  public:
    61     Server();
    62     Server(int port);
    63     Server(int port, std::string bindAddress);
    64     Server(int port, const char *bindAddress);
     63    static Server *createSingleton();
     64    static Server *createSingleton(int port);
     65    static Server *createSingleton(int port, std::string bindAddress);
     66    static Server *createSingleton(int port, const char *bindAddress);
     67   
     68    static Server *getSingleton();
     69   
    6570    void open();
    6671    void close();
    67     bool sendMSG(std::string msg);
    68     bool sendMSG(const char *msg);
     72    bool sendChat(std::string msg);
     73    bool sendChat(const char *msg);
    6974    void tick(float time);
    7075  protected:
     
    7277    void updateGamestate();
    7378  private:
     79    Server();
     80    Server(int port);
     81    Server(int port, std::string bindAddress);
     82    Server(int port, const char *bindAddress);
     83   
     84    static Server *instance_;
     85   
    7486    bool addClient(ENetEvent *event);
    7587    bool createClient(int clientID);
     
    8294    bool processConnectRequest( connectRequest *con, int clientID );
    8395    void processGamestate( GameStateCompressed *data, int clientID);
     96    void processChat( chat *data, int clientId);
    8497    ConnectionManager *connection;
    8598    GameStateManager *gamestates;
  • code/trunk/src/network/Synchronisable.cc

    r1505 r1534  
    4545
    4646#include "core/CoreIncludes.h"
     47// #include "core/Identifier.h"
    4748
    4849namespace network
     
    6667
    6768  Synchronisable::~Synchronisable(){
     69    // delete callback function objects
     70    if(!orxonox::Identifier::isCreatingHierarchy())
     71      for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++)
     72        delete (*it)->callback;
    6873  }
    6974 
     
    8792  * @param size size of the datatype the variable consists of
    8893  */
    89   void Synchronisable::registerVar(void *var, int size, variableType t, int mode){
     94  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
    9095    // create temporary synch.Var struct
    9196    synchronisableVariable *temp = new synchronisableVariable;
     
    9499    temp->mode = mode;
    95100    temp->type = t;
     101    temp->callback = cb;
    96102    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
    97103    // increase datasize
     
    218224      }
    219225      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
     226      bool callback=false;
    220227      switch((*i)->type){
    221228      case DATA:
     229        if((*i)->callback) // check whether this variable changed (but only if callback was set)
     230          if(strncmp((char *)(*i)->var, (char *)data, (*i)->size)!=0)
     231            callback=true;
    222232        memcpy((void*)(*i)->var, data, (*i)->size);
    223233        data+=(*i)->size;
     
    227237        COUT(5) << "string size: " << (*i)->size << std::endl;
    228238        data+=sizeof(int);
     239        if((*i)->callback) // check whether this string changed
     240          if( *(std::string *)((*i)->var) != std::string((char *)data) )
     241            callback=true;
    229242        *((std::string *)((*i)->var)) = std::string((const char*)data);
    230243        COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
     
    232245        break;
    233246      }
     247      // call the callback function, if defined
     248      if(callback)
     249        (*i)->callback->call();
    234250    }
    235251    return true;
  • code/trunk/src/network/Synchronisable.h

    r1505 r1534  
    2727 */
    2828
    29 //
    30 // C++ Interface: synchronisable
    31 //
    32 // Description:
    33 //
    34 //
    35 // Author:  Oliver Scheuss, (C) 2007
    36 //
    37 // Copyright: See COPYING file that comes with this distribution
    38 //
    39 //
    4029#ifndef _Synchronisable_H__
    4130#define _Synchronisable_H__
     
    4534#include <list>
    4635#include "core/OrxonoxClass.h"
     36#include "NetworkCallback.h"
    4737
    4838namespace network
     
    6555    void *var;
    6656    variableType type;
     57    NetworkCallbackBase *callback;
    6758  }SYNCVAR;
    6859
     
    8172    int classID;
    8273
    83     void registerVar(void *var, int size, variableType t, int mode=1);
     74    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    8475    //  syncData getData();
    8576    syncData getData(unsigned char *mem, int mode=0x0);
  • code/trunk/src/orxonox/Orxonox.cc

    r1511 r1534  
    127127      network::Client::destroySingleton();
    128128    if (server_g)
    129       delete server_g;
     129      delete network::Server::getSingleton();
    130130  }
    131131
     
    303303    COUT(2) << "Loading level in server mode" << std::endl;
    304304
    305     server_g = new network::Server(serverPort_);
     305    //server_g = new network::Server(serverPort_);
     306    server_g = network::Server::createSingleton(serverPort_);
    306307
    307308    if (!loadScene())
  • code/trunk/src/orxonox/objects/SpaceShip.cc

    r1505 r1534  
    6969    SpaceShip* SpaceShip::instance_s;
    7070
     71   
    7172    SpaceShip *SpaceShip::getLocalShip(){
    7273      Iterator<SpaceShip> it;
     
    163164
    164165    void SpaceShip::registerAllVariables(){
    165       registerVar( &camName_, camName_.length()+1, network::STRING, 0x1);
     166      registerVar( &camName_, camName_.length()+1, network::STRING, 0x1 );
    166167      registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1);
    167168      registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1);
     
    469470        if( myShip_ )
    470471        {
    471           COUT(4) << "steering our ship: " << objectID << std::endl;
     472          COUT(5) << "steering our ship: " << objectID << std::endl;
    472473          this->acceleration_.x = 0;
    473474          this->acceleration_.y = 0;
  • code/trunk/src/orxonox/objects/SpaceShip.h

    r1505 r1534  
    4444    {
    4545        public:
     46         
    4647
    4748            static SpaceShip *getLocalShip();
Note: See TracChangeset for help on using the changeset viewer.