Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 7, 2008, 5:03:10 PM (16 years ago)
Author:
scheusso
Message:

network branch merged into trunk

Location:
code/trunk/src/network
Files:
15 deleted
12 edited
21 copied

Legend:

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

    r1505 r1735  
    44  ClientInformation.cc
    55  ConnectionManager.cc
    6   GameStateManager.cc
    7   GameStateClient.cc
     6  GamestateManager.cc
     7  GamestateClient.cc
     8  GamestateHandler.cc
    89  PacketBuffer.cc
    9   PacketDecoder.cc
    10   PacketGenerator.cc
    1110  Server.cc
    1211  Synchronisable.cc
     12  Host.cc
     13  packet/Packet.cc
     14  packet/Chat.cc
     15  packet/ClassID.cc
     16  packet/Acknowledgement.cc
     17  packet/Gamestate.cc
     18  packet/Welcome.cc
     19  Host.cc
    1320)
    1421
     
    3138  ##### test for gamestate stuff #####
    3239  SET( TEST_SRC_FILES
    33     ConnectionManager.cc
    34     GameStateManager.cc
    35     PacketBuffer.cc
    36     PacketDecoder.cc
    37     PacketGenerator.cc
    38     ClientConnection.cc
    39     ClientInformation.cc
    40     diffTest.cc
    41     GameStateClient.cc
    42     Server.cc
    43     Client.cc 
    44     Synchronisable.cc 
     40    ${NETWORK_SRC_FILES}
    4541  )
    46 
    4742  ADD_EXECUTABLE(networktest ${TEST_SRC_FILES})
    4843  TARGET_LINK_LIBRARIES( networktest
     
    5550  ##### end test for gamestate stuff #####
    5651ENDIF(NETWORK_TESTING_ENABLED)
    57 # build this if you want dummyserver4 and dummyclient4
    58 IF(NETWORKTRAFFIC_TESTING_ENABLED)
    59   ##### test network traffic #####
    60   SET( DUMMYSERVER4_SRC_FILES
    61     ConnectionManager.cc
    62     GameStateManager.cc
    63     PacketBuffer.cc
    64     PacketDecoder.cc
    65     PacketGenerator.cc
    66     ClientConnection.cc
    67     ClientInformation.cc
    68     GameStateClient.cc
    69     Server.cc
    70     Client.cc 
    71     Synchronisable.cc
    72     dummyserver4.cc
    73   )
    74 
    75   SET( DUMMYCLIENT4_SRC_FILES
    76     ConnectionManager.cc
    77     GameStateManager.cc
    78     PacketBuffer.cc
    79     PacketDecoder.cc
    80     PacketGenerator.cc
    81     ClientConnection.cc
    82     ClientInformation.cc
    83     GameStateClient.cc
    84     Server.cc
    85     Client.cc 
    86     Synchronisable.cc
    87     dummyclient4.cc
    88   )
    89 
    90   ADD_EXECUTABLE(dummyserver4 ${DUMMYSERVER4_SRC_FILES})
    91   TARGET_LINK_LIBRARIES( dummyserver4
    92     network
    93     ${ENet_LIBRARY}
    94     ${ZLIB_LIBRARY}
    95     ${WINDOWS_ENET_DEPENDENCIES}
    96     ${Boost_thread_LIBRARIES}
    97     core
    98     orxonoxs
    99     audio
    100   )
    101 
    102   ADD_EXECUTABLE(dummyclient4 ${DUMMYCLIENT4_SRC_FILES})
    103   TARGET_LINK_LIBRARIES( dummyclient4
    104     network
    105     ${ENet_LIBRARY}
    106     ${ZLIB_LIBRARY}
    107     ${WINDOWS_ENET_DEPENDENCIES}
    108     ${Boost_thread_LIBRARIES}
    109     core
    110     orxonoxs
    111     audio
    112   )
    113 ENDIF(NETWORKTRAFFIC_TESTING_ENABLED)
    114 
    115 IF(BLABLA)
    116   SET( CHATCLIENT_SRC_FILES
    117     ClientConnection.cc
    118     Client.cc
    119     PacketBuffer.cc
    120     PacketDecoder.cc
    121     PacketGenerator.cc
    122     dummyclient3.cc
    123   )
    124 
    125   SET( CHATSERVER_SRC_FILES
    126     ConnectionManager.cc
    127     GameStateManager.cc
    128     PacketBuffer.cc
    129     PacketDecoder.cc
    130     PacketGenerator.cc
    131     dummyserver3.cc
    132   )
    133 
    134   # chatserver for testing purposes
    135   ADD_EXECUTABLE(chatserver ${CHATSERVER_SRC_FILES})
    136   TARGET_LINK_LIBRARIES( chatserver
    137     ${OGRE_LIBRARIES}
    138     network
    139     core
    140     #objects
    141     ${ENet_LIBRARY}
    142     ${ZLIB_LIBRARY}
    143     ${WINDOWS_ENET_DEPENDENCIES}
    144   )
    145 
    146   # chatclient for testing purpose
    147   ADD_EXECUTABLE(chatclient ${CHATCLIENT_SRC_FILES})
    148   TARGET_LINK_LIBRARIES( chatclient
    149     ${OGRE_LIBRARIES}
    150     network
    151     core
    152     #objects
    153     ${ENet_LIBRARY}
    154     ${ZLIB_LIBRARY}
    155     ${WINDOWS_ENET_DEPENDENCIES}
    156   )
    157 
    158   # client for testing purpose
    159   ADD_EXECUTABLE(client dummyclient2.cc)
    160   TARGET_LINK_LIBRARIES( client
    161     ${OGRE_LIBRARIES}
    162     network
    163     core
    164     #objects
    165     ${ENet_LIBRARY}
    166     ${ZLIB_LIBRARY}
    167     ${WINDOWS_ENET_DEPENDENCIES}
    168   )
    169 
    170   # server for testing purpose
    171   ADD_EXECUTABLE(server dummyserver.cc)
    172   TARGET_LINK_LIBRARIES( server
    173     ${OGRE_LIBRARIES}
    174     network
    175     core
    176     #objects
    177     ${ENet_LIBRARY}
    178     ${ZLIB_LIBRARY}
    179     ${WINDOWS_ENET_DEPENDENCIES}
    180   )
    181 ENDIF(BLABLA)
    182 
  • code/trunk/src/network/Client.cc

    r1534 r1735  
    4040
    4141#include "Client.h"
     42#include "Host.h"
    4243#include "Synchronisable.h"
    4344#include "core/CoreIncludes.h"
    4445#include "core/ConsoleCommand.h"
    45 #include "Server.h"
     46#include "packet/Packet.h"
     47#include "packet/Acknowledgement.h"
    4648
    4749namespace network
    4850{
    49   SetConsoleCommandShortcut(Client, Chat);
     51//   SetConsoleCommandShortcut(Client, chat);
    5052 
    51   Client* Client::_sClient = 0;
    52  
    53   Client* Client::createSingleton(){
    54     if(!_sClient){
    55       _sClient = new Client();
    56     }
    57     return _sClient;
    58   }
    59  
    60   Client* Client::createSingleton(std::string address, int port){
    61     if(!_sClient)
    62       _sClient = new Client(address, port);
    63     return _sClient;
    64   }
    65  
    66   Client* Client::createSingleton(const char *address, int port){
    67     if(!_sClient)
    68       _sClient = new Client(address, port);
    69     return _sClient;
    70   }
    71  
    72   void Client::destroySingleton(){
    73     if(_sClient){
    74       delete _sClient;
    75       _sClient = 0;
    76     }
    77   }
    78  
    79   Client* Client::getSingleton(){
    80     return _sClient;
    81   }
    8253 
    8354  /**
     
    144115  }
    145116
     117  bool Client::queuePacket(ENetPacket *packet, int clientID){
     118    return client_connection.addPacket(packet);
     119  }
    146120 
    147 
    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;
     121  bool Client::processChat(packet::Chat *message, unsigned int clientID){
     122    return message->process();
    155123  }
     124 
     125  /*bool Client::sendChat(packet::Chat *chat){
     126    chat->process();
     127    packet::Packet *p = new packet::Packet(chat);
     128    return p->send();
     129  }*/
    156130 
    157131
     
    165139    if(!isConnected)
    166140      return false;
    167     return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
    168       //return client_connection.sendPackets();
     141    packet::Chat chat(message, 0);
     142    return chat.send();
    169143    // send packets
    170     return false;
    171144  }
    172145
     
    178151    if(client_connection.isConnected() && isSynched_){
    179152      COUT(4) << "popping partial gamestate: " << std::endl;
    180       GameStateCompressed *gs = gamestate.popPartialGameState();
     153      /*packet::Gamestate *gs = gamestate.getGamestate();
    181154      if(gs){
    182155        COUT(4) << "client tick: sending gs " << gs << std::endl;
    183         ENetPacket *packet = pck_gen.gstate(gs);
    184         if( packet == NULL || !client_connection.addPacket(packet))
     156        if( !gs->send() )
    185157          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
    186         // now delete it to save memory
    187         delete[] gs->data;
    188         delete gs;
    189       }
     158        // gs gets automatically deleted by enet callback
     159      }*/
    190160    }
    191161    ENetEvent *event;
     
    194164      event = client_connection.getEvent();
    195165      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
    196       elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     166      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
     167      assert(packet->process());
    197168    }
    198     int gameStateID = gamestate.processGameState();
    199     if(gameStateID==GAMESTATEID_INITIAL)
     169    int gameStateID = gamestate.processGamestates();
     170    /*if(gameStateID==GAMESTATEID_INITIAL)
    200171      if(gameStateFailure_){
    201         if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
     172        packet::Acknowledgement ack(GAMESTATEID_INITIAL, 0);
     173        if(!ack.send())
    202174          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
    203175        else
     
    211183        isSynched_=true;
    212184      gameStateFailure_=false;
    213       if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
     185      packet::Acknowledgement ack(gameStateID, 0);
     186      if(!ack.send())
    214187        COUT(3) << "could not ack gamestate" << std::endl;
    215     }// otherwise we had no gamestate to load
     188    }*/// otherwise we had no gamestate to load
    216189    gamestate.cleanup();
    217     /*if(!client_connection.sendPackets())
    218       COUT(3) << "Problem sending packets to server" << std::endl;*/
    219190    return;
    220191  }
    221192
    222   void Client::processGamestate( GameStateCompressed *data, int clientID){
    223     COUT(5) << "received gamestate id: " << data->id << std::endl;
    224     gamestate.addGameState(data);
    225   }
    226 
    227   void Client::processClassid(classid *clid){
    228     orxonox::Identifier *id;
    229     id=ID(std::string(clid->message));
    230     if(id!=NULL)
    231       id->setNetworkID(clid->clid);
    232     COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
    233     COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
    234     delete clid;
    235     return;
    236   }
    237 
    238   void Client::processChat( chat *data, int clientId){
    239     COUT(1) << data->message << std::endl;
    240     delete[] data->message;
    241     delete data;
    242   }
    243  
    244   bool Client::processWelcome( welcome *w ){
    245     COUT(4) << "processing welcome message" << std::endl;
    246     clientID_ = w->clientID;
    247     shipID_ = w->shipID;
    248     delete w;
    249     return true;
    250   }
    251 
    252193}
  • code/trunk/src/network/Client.h

    r1535 r1735  
    4646#include <string>
    4747
     48#include "Host.h"
     49#include "packet/Chat.h"
    4850#include "ClientConnection.h"
    49 #include "PacketManager.h"
    50 #include "GameStateClient.h"
     51#include "GamestateClient.h"
    5152//#include "NetworkFrameListener.h"
    5253
     
    6263  *
    6364  */
    64   class _NetworkExport Client : PacketDecoder{
     65  class _NetworkExport Client : public Host{
    6566  public:
    66    
    67     static Client* createSingleton();
    68     static Client* createSingleton(std::string address, int port);
    69     static Client* createSingleton(const char *address, int port);
    70     static void destroySingleton();
    71     static Client *getSingleton();
    72    
    73     bool establishConnection();
    74     bool closeConnection();
    75 
    76     static void Chat( std::string message );
    77    
    78     int getShipID(){return shipID_;}
    79     int getClientID(){return clientID_;}
    80 
    81     void tick(float time);
    82 
    83   private:
    8467    Client();
    8568    Client(std::string address, int port);
     
    8770    ~Client();
    8871   
    89     static Client* _sClient;
     72    bool establishConnection();
     73    bool closeConnection();
     74    bool queuePacket(ENetPacket *packet, int clientID);
     75    bool processChat(packet::Chat *message, unsigned int clientID);
     76    //bool sendChat(packet::Chat *chat);
     77   
     78//    static void Chat( std::string message );
     79   
     80    unsigned int shipID(){return shipID_;}
     81    int playerID(){return clientID_;}
     82    //static void setShipID( unsigned int shipID){ dynamic_cast<Client *>(instance_)->shipID_=shipID; }
     83    static void setClientID( unsigned int clientID){ dynamic_cast<Client *>(instance_)->clientID_=clientID; }
     84   
     85    void tick(float time);
     86
     87  private:
    9088   
    9189    ClientConnection client_connection;
    92     PacketGenerator pck_gen;
    93     GameStateClient gamestate;
     90    GamestateClient gamestate;
    9491    bool isConnected;
    9592    bool isSynched_;
     
    9895   
    9996    // implement data processing functions of PacketDecoder
    100     void processGamestate( GameStateCompressed *data, int clientID);
    101     void processClassid(classid *clid);
    102     void processChat( chat *data, int clientId );
    103     bool processWelcome( welcome *w );
     97//     void processChat( chat *data, int clientId );
    10498    int clientID_;     // this is the id the server gave to us
    10599    int shipID_;
  • code/trunk/src/network/ClientInformation.cc

    r1534 r1735  
    4646{
    4747 
     48  ClientInformation *ClientInformation::head_=0;
     49 
    4850  ClientInformation::ClientInformation() {
     51    if(!head_)
     52      head_=this;
    4953    gamestateID_=GAMESTATEID_INITIAL;
    5054    preve=0;
    5155    nexte=0;
    5256    partialGamestateID_=GAMESTATEID_INITIAL-1;
    53     this->head_=false;
    5457    synched_=false;
    5558  }
    5659
    57   ClientInformation::ClientInformation(bool head) {
    58     gamestateID_=GAMESTATEID_INITIAL;
    59     preve=0;
    60     nexte=0;
    61     partialGamestateID_=GAMESTATEID_INITIAL-1;
    62     this->head_=head;
    63     synched_=false;
    64   }
    65 
    66   // ClientInformation::ClientInformation(ClientInformation *prev) {
    67   //   if(prev->next()!=0){
    68   //     this->nexte=prev->next();
    69   //     this->nexte->setPrev(this);
    70   //   }
    71   //   else
    72   //     this->nexte = 0;
    73   //   prev->setNext(this);
    74   //   this->preve = pref;
    75   // }
    76   //
    77   // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){
    78   //   this->nexte = next;
    79   //   this->preve = prev;
    80   //   this->preve->setNext(this);
    81   //   this->nexte->setPrev(this);
    82   // }
    83 
    8460  ClientInformation::~ClientInformation() {
     61    if(this==head_)
     62      head_=next();
    8563    if(prev()!=0)
    8664      prev()->setNext(this->next());
     
    10381
    10482  bool ClientInformation::setPrev(ClientInformation *prev) {
    105     if(!head_)
    106       this->preve = prev;
    107     else
    108       return false;
     83    if(this==head_)
     84      head_=prev;
     85    this->preve = prev;
    10986    return true;
    11087  }
     
    146123  }
    147124
    148   bool ClientInformation::setGameStateID(int id){
     125  bool ClientInformation::setGamestateID(int id){
    149126    if(!this)
    150127      return false;
     
    172149    else
    173150      return NULL;
    174   }
    175  
    176   bool ClientInformation::getHead(){
    177     return head_;
    178   }
    179  
    180   void ClientInformation::setHead(bool h){
    181     head_=h;
    182151  }
    183152 
     
    215184
    216185  ClientInformation *ClientInformation::insertBack(ClientInformation *ins) {
    217     if(!this)
    218       return NULL;
    219     ClientInformation *temp = this;
     186    ClientInformation *temp = head_;
     187    if(temp==head_){
     188      return head_;
     189    }
    220190    while(temp->next()!=0){
    221191      temp = temp->next();
     
    227197
    228198  bool ClientInformation::removeClient(int clientID) {
    229     if(!this || clientID==CLIENTID_UNKNOWN)
    230       return false;
    231     ClientInformation *temp = this;
     199    if(clientID==CLIENTID_UNKNOWN)
     200      return false;
     201    ClientInformation *temp = head_;
    232202    while(temp!=0 && temp->getID()!=clientID)
    233203      temp = temp->next();
     
    239209
    240210  bool ClientInformation::removeClient(ENetPeer *peer) {
    241     if(!this || !peer)
    242       return false;
    243     ClientInformation *temp = this;
     211    if(!peer)
     212      return false;
     213    ClientInformation *temp = head_;
    244214    while(temp!=0){
    245       if(!temp->head_)
    246         if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port)
    247           break;
     215      if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port)
     216        break;
    248217      temp = temp->next();
    249218    }
     
    261230  */
    262231  ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
    263     ClientInformation *temp = this;
    264     if (temp->head_)
    265       temp=temp->next();
     232    ClientInformation *temp = head_;
    266233    while(temp!=0 && temp->getID()!=clientID){
    267234      temp = temp->next();
     
    278245  */
    279246  ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
    280     ClientInformation *temp = this;
     247    ClientInformation *temp = head_;
    281248    while(temp!=0){
    282       if(temp->head_){
    283         temp = temp->next();
    284         continue;
    285       }
    286249      if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port)
    287250        break;
  • code/trunk/src/network/ClientInformation.h

    r1534 r1735  
    6060  public:
    6161    ClientInformation();
    62     ClientInformation(bool head);
    6362    //   ClientInformation(ClientInformation *prev, ClientInformation *next);
    6463    //   ClientInformation(ClientInformation *prev);
     
    6665    ClientInformation *next();
    6766    ClientInformation *prev();
    68     ClientInformation *insertBack(ClientInformation *ins);
     67    static ClientInformation *insertBack(ClientInformation *ins);
    6968   
    7069    // set functions
    7170    void setID(int clientID);
    7271    bool setPeer(ENetPeer *peer);
    73     bool setGameStateID(int id);
     72    bool setGamestateID(int id);
    7473    bool setPartialGamestateID(int id);
    75     inline void setShipID(int id){ShipID_=id;}
     74    inline void setShipID(unsigned int id){ShipID_=id;}
    7675   
    7776    // get functions
    78     inline int getShipID(){return ShipID_;}
     77    inline unsigned int getShipID(){return ShipID_;}
    7978    int getID();
    8079    int getGamestateID();
    8180    int getPartialGamestateID();
    8281    ENetPeer *getPeer();
    83     bool getHead();
    84     void setHead(bool h);
    8582   
    8683    int getFailures();
     
    9087    enet_uint32 getPacketLoss();
    9188   
    92     bool removeClient(int clientID);
    93     bool removeClient(ENetPeer *peer);
    94     //## add bool mask-function eventually
    95     ClientInformation *findClient(int clientID, bool look_backwards=false);
    96     //## add bool mask-function eventually
    97     ClientInformation *findClient(ENetAddress *address, bool look_backwards=false);
     89    static bool removeClient(int clientID);
     90    static bool removeClient(ENetPeer *peer);
     91    static ClientInformation *findClient(int clientID, bool look_backwards=false);
     92    static ClientInformation *findClient(ENetAddress *address, bool look_backwards=false);
     93    static ClientInformation *getBegin(){return head_;}
    9894
    9995    bool setSynched(bool s);
     
    10197
    10298
    103     private:
    104       bool setNext(ClientInformation *next);
    105       bool setPrev(ClientInformation *prev);
     99  private:
     100    static ClientInformation *head_;
     101   
     102    bool setNext(ClientInformation *next);
     103    bool setPrev(ClientInformation *prev);
    106104    ClientInformation *insertAfter(ClientInformation *ins);
    107105    ClientInformation *insertBefore(ClientInformation *ins);
     
    114112    int gamestateID_;
    115113    int partialGamestateID_;
    116     int ShipID_;   // this is the unique objectID
     114    unsigned int ShipID_;   // this is the unique objectID
    117115    bool synched_;
    118     bool head_;
    119116    unsigned short failures_;
    120117   
  • code/trunk/src/network/ConnectionManager.cc

    r1534 r1735  
    3939
    4040#include <iostream>
     41#include <assert.h>
    4142// boost.thread library for multithreading support
    4243#include <boost/bind.hpp>
     
    5051#include "ConnectionManager.h"
    5152#include "Synchronisable.h"
     53#include "packet/ClassID.h"
    5254
    5355namespace std
     
    6567  //boost::thread_group network_threads;
    6668 
    67   ConnectionManager::ConnectionManager():receiverThread_(0){}
    68   boost::recursive_mutex ConnectionManager::enet_mutex_;
    69  
    70   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     69  ConnectionManager *ConnectionManager::instance_=0;
     70 
     71  ConnectionManager::ConnectionManager():receiverThread_(0){
     72    assert(instance_==0);
     73    instance_=this;
    7174    quit=false;
    7275    bindAddress.host = ENET_HOST_ANY;
    7376    bindAddress.port = NETWORK_PORT;
    74     head_ = head;
    75   }
    76  
    77   ConnectionManager::ConnectionManager(ClientInformation *head, int port){
     77  }
     78  boost::recursive_mutex ConnectionManager::enet_mutex;
     79 
     80//   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     81//     assert(instance_==0);
     82//     instance_=this;
     83//     quit=false;
     84//     bindAddress.host = ENET_HOST_ANY;
     85//     bindAddress.port = NETWORK_PORT;
     86//   }
     87 
     88  ConnectionManager::ConnectionManager(int port){
     89    assert(instance_==0);
     90    instance_=this;
    7891    quit=false;
    7992    bindAddress.host = ENET_HOST_ANY;
    8093    bindAddress.port = port;
    81     head_ = head;
    82   }
    83 
    84   ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) {
     94  }
     95
     96  ConnectionManager::ConnectionManager(int port, std::string address) :receiverThread_(0) {
     97    assert(instance_==0);
     98    instance_=this;
    8599    quit=false;
    86100    enet_address_set_host (& bindAddress, address.c_str());
    87101    bindAddress.port = NETWORK_PORT;
    88     head_ = head;
    89   }
    90 
    91   ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) : receiverThread_(0) {
     102  }
     103
     104  ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) {
     105    assert(instance_==0);
     106    instance_=this;
    92107    quit=false;
    93108    enet_address_set_host (& bindAddress, address);
    94109    bindAddress.port = NETWORK_PORT;
    95     head_ = head;
     110  }
     111 
     112  ConnectionManager::~ConnectionManager(){
     113    instance_=0;
     114    if(!quit)
     115      quitListener();
    96116  }
    97117
     
    140160    return true;
    141161  }
    142 
     162 
     163//   bool ConnectionManager::addPacket(Packet::Packet *packet){
     164//     ClientInformation *temp = instance_->head_->findClient(packet->getClientID());
     165//     if(!temp){
     166//       COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
     167//       return false;
     168//     }
     169//     ENetPacket *packet = new ENetPacket;
     170//     //  TODO: finish implementation
     171//   }
     172//   
     173 
    143174  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
    144     ClientInformation *temp = head_->findClient(&(peer->address));
    145     if(!temp)
    146       return false;
    147     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    148     if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0)
     175    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
     176    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
    149177      return false;
    150178    return true;
     
    152180
    153181  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
    154     ClientInformation *temp = head_->findClient(clientID);
     182    ClientInformation *temp = ClientInformation::findClient(clientID);
    155183    if(!temp){
    156184      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
    157185      return false;
    158186    }
    159     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    160     if(enet_peer_send(temp->getPeer(), 0, packet)!=0){
    161       COUT(3) << "C.Man: addPacket enet_peer_send failed" << std::endl;
    162       return false;
    163     }
    164     return true;
     187    return addPacket(packet, temp->getPeer());
    165188  }
    166189
    167190  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
    168     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    169     for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
     191    if(!instance_)
     192      return false;
     193    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
     194    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
    170195      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
    171196      if(enet_peer_send(i->getPeer(), 0, packet)!=0)
     
    177202  // we actually dont need that function, because host_service does that for us
    178203  bool ConnectionManager::sendPackets() {
    179     if(server==NULL)
    180       return false;
    181     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     204    if(server==NULL || !instance_)
     205      return false;
     206    boost::recursive_mutex::scoped_lock lock(enet_mutex);
    182207    enet_host_flush(server);
    183208    lock.unlock();
     
    190215    atexit(enet_deinitialize);
    191216    { //scope of the mutex
    192       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     217      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    193218      enet_initialize();
    194219      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     
    204229    while(!quit){
    205230      { //mutex scope
    206         boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     231        boost::recursive_mutex::scoped_lock lock(enet_mutex);
    207232        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    208233          // we should never reach this point
     
    246271    // if we're finishied, destroy server
    247272    {
    248       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     273      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    249274      enet_host_destroy(server);
    250275      lock.unlock();
     
    257282  void ConnectionManager::disconnectClients() {
    258283    ENetEvent event;
    259     ClientInformation *temp = head_->next();
     284    ClientInformation *temp = ClientInformation::getBegin()->next();
    260285    while(temp!=0){
    261286      {
    262         boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     287        boost::recursive_mutex::scoped_lock lock(enet_mutex);
    263288        enet_peer_disconnect(temp->getPeer(), 0);
    264289        lock.unlock();
     
    267292    }
    268293    //bugfix: might be the reason why server crashes when clients disconnects
    269     temp = head_->next();
    270     boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     294    temp = ClientInformation::getBegin()->next();
     295    boost::recursive_mutex::scoped_lock lock(enet_mutex);
    271296    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
    272297      switch (event.type)
     
    279304      case ENET_EVENT_TYPE_DISCONNECT:
    280305        COUT(4) << "disconnecting all clients" << std::endl;
    281         if(head_->findClient(&(event.peer->address)))
    282           delete head_->findClient(&(event.peer->address));
     306        if(ClientInformation::findClient(&(event.peer->address)))
     307          delete ClientInformation::findClient(&(event.peer->address));
    283308        //maybe needs bugfix: might also be a reason for the server to crash
    284309        temp = temp->next();
     
    302327
    303328  int ConnectionManager::getClientID(ENetAddress address) {
    304     return head_->findClient(&address)->getID();
     329    return ClientInformation::findClient(&address)->getID();
    305330  }
    306331
    307332  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
    308     return head_->findClient(clientID)->getPeer();
    309   }
    310 
    311   void ConnectionManager::syncClassid(int clientID) {
     333    return ClientInformation::findClient(clientID)->getPeer();
     334  }
     335
     336  /**
     337   *
     338   * @param clientID
     339   */
     340  void ConnectionManager::syncClassid(unsigned int clientID) {
    312341    unsigned int network_id=0, failures=0;
    313342    std::string classname;
     
    324353      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
    325354
    326       while(!addPacket(packet_gen.clid( (int)network_id, classname ), clientID) && failures < 10){
     355      packet::ClassID *classid = new packet::ClassID( network_id, classname );
     356      classid->setClientID(clientID);
     357      while(!classid->send() && failures < 10){
    327358        failures++;
    328359      }
     
    336367 
    337368  bool ConnectionManager::removeShip(ClientInformation *client){
    338     int id=client->getShipID();
     369    unsigned int id=client->getShipID();
    339370    orxonox::Iterator<orxonox::SpaceShip> it;
    340371    for(it = orxonox::ObjectList<orxonox::SpaceShip>::start(); it; ++it){
     
    346377  }
    347378 
    348   bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){
    349     if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){
    350       //sendPackets();
    351       return true;
    352     }else
    353       return false;
    354   }
    355379 
    356380  void ConnectionManager::disconnectClient(ClientInformation *client){
    357381    {
    358       boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     382      boost::recursive_mutex::scoped_lock lock(enet_mutex);
    359383      enet_peer_disconnect(client->getPeer(), 0);
    360384      lock.unlock();
     
    363387  }
    364388 
    365   bool ConnectionManager::addFakeConnectRequest(ENetEvent *ev){
    366     ENetEvent event;
    367     event.peer=ev->peer;
    368     event.packet = packet_gen.generateConnectRequest();
    369     return buffer.push(&event);
    370   }
    371  
    372  
    373389
    374390}
  • code/trunk/src/network/ConnectionManager.h

    r1505 r1735  
    5151
    5252#include "PacketBuffer.h"
    53 #include "PacketManager.h"
     53#include "packet/Packet.h"
    5454
    5555namespace std
     
    6363#define NETWORK_MAX_CONNECTIONS 50
    6464#define NETWORK_WAIT_TIMEOUT 1
     65#define NETWORK_DEFAULT_CHANNEL 0
    6566
    6667  struct ClientList{
     
    7172
    7273  class ConnectionManager{
    73   public:
     74    public:
     75    static boost::recursive_mutex enet_mutex;
    7476    ConnectionManager();
    75     ConnectionManager(ClientInformation *head);
    76     ConnectionManager(ClientInformation *head, int port);
    77     ConnectionManager(int port, const char *address, ClientInformation *head);
    78     ConnectionManager(int port, std::string address, ClientInformation *head);
     77    //ConnectionManager(ClientInformation *head);
     78    ConnectionManager(int port);
     79    ConnectionManager(int port, const char *address);
     80    ConnectionManager(int port, std::string address);
     81    ~ConnectionManager();
    7982    //ENetPacket *getPacket(ENetAddress &address); // thread1
    8083    //ENetPacket *getPacket(int &clientID);
     
    8386    void createListener();
    8487    bool quitListener();
    85     bool addPacket(ENetPacket *packet, ENetPeer *peer);
    86     bool addPacket(ENetPacket *packet, int ID);
    87     bool addPacketAll(ENetPacket *packet);
     88//     bool addPacket(Packet::Packet *packet);
     89    static bool addPacket(ENetPacket *packet, ENetPeer *peer);
     90    static bool addPacket(ENetPacket *packet, int ID);
     91    static bool addPacketAll(ENetPacket *packet);
    8892  //  bool sendPackets(ENetEvent *event);
    8993    bool sendPackets();
    9094    //bool createClient(int clientID);
    9195    void disconnectClient(ClientInformation *client);
    92     void syncClassid(int clientID);
    93     bool sendWelcome(int clientID, int shipID, bool allowed);
     96    void syncClassid(unsigned int clientID);
    9497
    9598  private:
     
    105108    //bool createShip(ClientInformation *client);
    106109    bool removeShip(ClientInformation *client);
    107     bool addFakeConnectRequest(ENetEvent *ev);
    108110    PacketBuffer buffer;
    109     PacketGenerator packet_gen;
    110111
    111112    ENetHost *server;
     
    113114
    114115    bool quit; // quit-variable (communication with threads)
    115     ClientInformation *head_;
    116116
    117117    boost::thread *receiverThread_;
    118     static boost::recursive_mutex enet_mutex_;
    119 //     int getNumberOfClients();
    120     //functions to map what object every clients uses
    121     /*std::map<int, int> clientsShip;
    122     void addClientsObjectID( int clientID, int objectID );
    123     int getClientsShipID( int clientID );
    124     int getObjectsClientID( int objectID );
    125     void deleteClientIDReg( int clientID );
    126     void deleteObjectIDReg( int objectID );*/
     118    static ConnectionManager *instance_;
     119
    127120  };
    128121
  • code/trunk/src/network/NetworkPrereqs.h

    r1505 r1735  
    6666  class ClientInformation;
    6767  class ConnectionManager;
    68   class GameStateClient;
    69   class GameStateManager;
     68  class GamestateClient;
     69  class GamestateManager;
     70  class GamestateHandler;
    7071  class PacketBuffer;
    71   class PacketDecoder;
    72   class PacketGenerator;
    7372  class Server;
    7473  class ServerFrameListener;
    7574  class Synchronisable;
    76   struct ack;
    77   struct chat;
    78   struct classid;
    79   struct welcome;
    80   struct connectRequest;
    8175  struct ClientList;
    82   struct GameState;
    83   struct GameStateCompressed;
    84   struct keyboard;
    85   struct mouse;
    8676  struct PacketEnvelope;
    8777  struct QueueItem;
    8878  struct syncData;
    8979  struct synchronisableVariable;
     80  namespace packet{
     81    class Gamestate;
     82    class Packet;
     83    class Acknowledgement;
     84    class ClassID;
     85    class Welcome;
     86    class Chat;
     87  }
    9088}
    9189
  • code/trunk/src/network/Server.cc

    r1556 r1735  
    4545
    4646#include "ConnectionManager.h"
    47 #include "PacketTypes.h"
    48 #include "GameStateManager.h"
     47#include "GamestateManager.h"
    4948#include "ClientInformation.h"
    50 //#include "NetworkFrameListener.h"
    5149#include "util/Sleep.h"
    5250#include "objects/SpaceShip.h"
    5351#include "core/ConsoleCommand.h"
     52#include "packet/Chat.h"
     53#include "packet/Packet.h"
     54#include "packet/Welcome.h"
    5455
    5556namespace network
     
    5859  #define NETWORK_FREQUENCY 30
    5960 
    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  
    87  
    8861  /**
    8962  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
     
    9265  Server::Server() {
    9366    timeSinceLastUpdate_=0;
    94     packet_gen = PacketGenerator();
    95     clients = new ClientInformation(true);
    96     connection = new ConnectionManager(clients);
    97     gamestates = new GameStateManager(clients);
     67    connection = new ConnectionManager();
     68    gamestates_ = new GamestateManager();
    9869  }
    9970 
    10071  Server::Server(int port){
    10172    timeSinceLastUpdate_=0;
    102     packet_gen = PacketGenerator();
    103     clients = new ClientInformation(true);
    104     connection = new ConnectionManager(clients, port);
    105     gamestates = new GameStateManager(clients);
     73    connection = new ConnectionManager(port);
     74    gamestates_ = new GamestateManager();
    10675  }
    10776
     
    11382  Server::Server(int port, std::string bindAddress) {
    11483    timeSinceLastUpdate_=0;
    115     packet_gen = PacketGenerator();
    116     clients = new ClientInformation();
    117     connection = new ConnectionManager(port, bindAddress, clients);
    118     gamestates = new GameStateManager(clients);
     84    connection = new ConnectionManager(port, bindAddress);
     85    gamestates_ = new GamestateManager();
    11986  }
    12087
     
    12693  Server::Server(int port, const char *bindAddress) {
    12794    timeSinceLastUpdate_=0;
    128     packet_gen = PacketGenerator();
    129     clients = new ClientInformation();
    130     connection = new ConnectionManager(port, bindAddress, clients);
    131     gamestates = new GameStateManager(clients);
     95    connection = new ConnectionManager(port, bindAddress);
     96    gamestates_ = new GamestateManager();
    13297  }
    13398
     
    148113  }
    149114
     115  bool Server::processChat(packet::Chat *message, unsigned int clientID){
     116    ClientInformation *temp = ClientInformation::getBegin();
     117    while(temp){
     118      message->setClientID(temp->getID());
     119      if(!message->send())
     120        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     121      temp = temp->next();
     122    }
     123    return message->process();
     124  }
     125 
    150126  /**
    151127  * This function sends out a message to all clients
     
    153129  * @return true/false
    154130  */
    155   bool Server::sendChat(std::string msg) {
    156     return sendChat(msg.c_str());
     131  bool Server::sendChat(packet::Chat *chat) {
     132    //TODO: change this (no informations about who wrote a message)
     133    assert(0);
     134    ClientInformation *temp = ClientInformation::getBegin();
     135    while(temp){
     136      chat->setClientID(temp->getID());
     137      if(!chat->send())
     138        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     139    }
     140    return chat->process();;
    157141  }
    158142
     
    162146  * @return true/false
    163147  */
    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;
    170     return connection->addPacketAll(packet);
    171   }
     148//   bool Server::sendChat(const char *msg) {
     149//     char *message = new char [strlen(msg)+10+1];
     150//     sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
     151//     COUT(1) << message << std::endl;
     152//     ENetPacket *packet = packet_gen.chatMessage(message);
     153//     COUT(5) <<"Server: adding Packets" << std::endl;
     154//     return connection->addPacketAll(packet);
     155//   }
    172156
    173157  /**
     
    183167      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
    184168//      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;
    185       gamestates->processGameStates();
     169      gamestates_->processGamestates();
    186170      updateGamestate();
    187171    }
     
    191175    return;
    192176  }
     177 
     178  bool Server::queuePacket(ENetPacket *packet, int clientID){
     179    return connection->addPacket(packet, clientID);
     180  }
    193181
    194182  /**
     
    197185  void Server::processQueue() {
    198186    ENetEvent *event;
    199     int clientID=-1;
    200187    while(!connection->queueEmpty()){
    201188      //std::cout << "Client " << clientID << " sent: " << std::endl;
     
    211198        break;
    212199      case ENET_EVENT_TYPE_DISCONNECT:
    213         if(clients->findClient(&event->peer->address))
     200        if(ClientInformation::findClient(&event->peer->address))
    214201          disconnectClient(event);
    215202        break;
    216203      case ENET_EVENT_TYPE_RECEIVE:
    217         if(clients->findClient(&event->peer->address)){
    218           clientID = clients->findClient(&event->peer->address)->getID();
    219           if( !elaborate(event->packet, clientID) )
    220             COUT(3) << "Server: could not elaborate" << std::endl;
    221         }
     204        if(!processPacket(event->packet, event->peer))
     205          COUT(3) << "processing incoming packet failed" << std::endl;
    222206        break;
    223207      default:
     
    233217  */
    234218  void Server::updateGamestate() {
    235     gamestates->update();
     219    gamestates_->update();
    236220    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
    237221    //std::cout << "updated gamestate, sending it" << std::endl;
     
    242226  }
    243227
     228  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
     229    packet::Packet *p = packet::Packet::createPacket(packet, peer);
     230    return p->process();
     231  }
     232 
    244233  /**
    245234  * sends the gamestate
     
    247236  bool Server::sendGameState() {
    248237    COUT(5) << "Server: starting function sendGameState" << std::endl;
    249     ClientInformation *temp = clients;
     238    ClientInformation *temp = ClientInformation::getBegin();
    250239    bool added=false;
    251240    while(temp != NULL){
    252       if(temp->getHead()){
    253         temp=temp->next();
    254         //think this works without continue
    255         continue;
    256       }
    257241      if( !(temp->getSynched()) ){
    258242        COUT(5) << "Server: not sending gamestate" << std::endl;
    259243        temp=temp->next();
     244        if(!temp)
     245          break;
    260246        //think this works without continue
    261247        continue;
     
    266252      int cid = temp->getID(); //get client id
    267253      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
    268       GameStateCompressed *gs = gamestates->popGameState(cid);
     254      packet::Gamestate *gs = gamestates_->popGameState(cid);
    269255      if(gs==NULL){
    270256        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
     
    272258      }
    273259      //std::cout << "adding gamestate" << std::endl;
    274       ENetPacket *packet = packet_gen.gstate(gs);
    275       if(!packet)
    276         continue;
    277       if ( !(connection->addPacket(packet, cid)) ){
     260      gs->setClientID(cid);
     261      assert(gs->compressData());
     262      if ( !gs->send() ){
    278263        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
    279264        temp->addFailure();
    280         /*if(temp->getFailures() > 0 )
    281           disconnectClient(temp);*/
    282       //std::cout << "added gamestate" << std::endl;
    283265      }else
    284266        temp->resetFailures();
    285267      added=true;
    286268      temp=temp->next();
    287       // now delete gamestate
    288       delete[] gs->data;
    289       delete gs;
     269      // gs gets automatically deleted by enet callback
    290270    }
    291271    /*if(added) {
     
    296276    return true;
    297277  }
    298 
    299   void Server::processAck( ack *data, int clientID) {
    300     COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
    301     gamestates->ackGameState(clientID, data->a);
    302     delete data;
    303   }
    304  
    305   bool Server::processConnectRequest( connectRequest *con, int clientID ){
    306     //(COUT(3) << "processing connectRequest " << std::endl;
    307     //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    308     //createClient(clientID);
    309     delete con;
    310     return true;
    311   }
    312  
    313   void Server::processGamestate( GameStateCompressed *data, int clientID){
    314     COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
    315     gamestates->addGameState(data, clientID);
    316         /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
    317     else
    318       if(clients->findClient(clientID))
    319         clients->findClient(clientID)->resetFailures();*/
    320   }
    321  
    322   void Server::processChat( chat *data, int clientId){
    323     char *message = new char [strlen(data->message)+10+1];
    324     sprintf(message, "Player %d: %s", clientId, data->message);
    325     COUT(1) << message << std::endl;
    326     ENetPacket *pck = packet_gen.chatMessage(message);
    327     connection->addPacketAll(pck);
    328     delete[] data->message;
    329     delete data;
    330   }
     278 
     279//   void Server::processChat( chat *data, int clientId){
     280//     char *message = new char [strlen(data->message)+10+1];
     281//     sprintf(message, "Player %d: %s", clientId, data->message);
     282//     COUT(1) << message << std::endl;
     283//     ENetPacket *pck = packet_gen.chatMessage(message);
     284//     connection->addPacketAll(pck);
     285//     delete[] data->message;
     286//     delete data;
     287//   }
    331288 
    332289  bool Server::addClient(ENetEvent *event){
    333     ClientInformation *temp = clients->insertBack(new ClientInformation);
     290    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    334291    if(!temp){
    335292      COUT(2) << "Server: could not add client" << std::endl;
    336293      return false;
    337294    }
    338     if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
     295    if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack
    339296      temp->prev()->setID(0); //bugfix: not necessary but usefull
    340297      temp->setID(1);
     
    348305 
    349306  bool Server::createClient(int clientID){
    350     ClientInformation *temp = clients->findClient(clientID);
     307    ClientInformation *temp = ClientInformation::findClient(clientID);
    351308    if(!temp){
    352309      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
     
    363320    temp->setSynched(true);
    364321    COUT(3) << "sending welcome" << std::endl;
    365     connection->sendWelcome(temp->getID(), temp->getShipID(), true);
     322    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
     323    w->setClientID(temp->getID());
     324    assert(w->send());
    366325    return true;
    367326  }
     
    376335    }
    377336    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
     337    no->classID = id->getNetworkID();
     338    client->setShipID(no->objectID);
    378339    no->setPosition(orxonox::Vector3(0,0,80));
    379340    no->setScale(10);
     
    388349    no->setRotDamp(1.0);
    389350    no->setCamera("cam_"+client->getID());
    390     no->classID = id->getNetworkID();
    391351    no->create();
     352    no->setBacksync(true);
    392353   
    393     client->setShipID(no->objectID);
    394354    return true;
    395355  }
     
    401361    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
    402362    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
    403     ClientInformation *client = clients->findClient(&event->peer->address);
     363    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    404364    if(!client)
    405365      return false;
     
    412372      ++it;
    413373      delete  *temp;
    414       return clients->removeClient(event->peer);
     374      return ClientInformation::removeClient(event->peer);
    415375    }
    416376    return false;
     
    418378
    419379  void Server::disconnectClient(int clientID){
    420     ClientInformation *client = clients->findClient(clientID);
     380    ClientInformation *client = ClientInformation::findClient(clientID);
    421381    if(client)
    422382      disconnectClient(client);
     
    424384  void Server::disconnectClient( ClientInformation *client){
    425385    connection->disconnectClient(client);
    426     gamestates->removeClient(client);
     386    gamestates_->removeClient(client);
    427387  }
    428388 
  • code/trunk/src/network/Server.h

    r1535 r1735  
    4646#include <string>
    4747
    48 #include "PacketManager.h"
    49 
    50 
     48#include "Host.h"
     49#include "GamestateManager.h"
    5150
    5251namespace network
     
    5857  * It implements all functions necessary for a Server
    5958  */
    60   class _NetworkExport Server : public PacketDecoder{
     59  class _NetworkExport Server : public Host{
    6160  public:
    62     static Server *createSingleton();
    63     static Server *createSingleton(int port);
    64     static Server *createSingleton(int port, std::string bindAddress);
    65     static Server *createSingleton(int port, const char *bindAddress);
    66    
    67     static Server *getSingleton();
     61    Server();
     62    Server(int port);
     63    Server(int port, std::string bindAddress);
     64    Server(int port, const char *bindAddress);
    6865   
    6966    void open();
    7067    void close();
    71     bool sendChat(std::string msg);
    72     bool sendChat(const char *msg);
     68    bool processChat(packet::Chat *message, unsigned int clientID);
     69    bool sendChat(packet::Chat *chat);
     70    bool queuePacket(ENetPacket *packet, int clientID);
    7371    void tick(float time);
    7472  protected:
     
    7674    void updateGamestate();
    7775  private:
    78     Server();
    79     Server(int port);
    80     Server(int port, std::string bindAddress);
    81     Server(int port, const char *bindAddress);
    82    
    83     static Server *instance_;
     76    unsigned int shipID(){return 0;}
     77    int playerID(){return 0;}
    8478   
    8579    bool addClient(ENetEvent *event);
     
    8983    void disconnectClient(int clientID);
    9084    void disconnectClient( ClientInformation *client);
     85    bool processPacket( ENetPacket *packet, ENetPeer *peer );
    9186    bool sendGameState();
    92     void processAck( ack *data, int clientID);
    93     bool processConnectRequest( connectRequest *con, int clientID );
    94     void processGamestate( GameStateCompressed *data, int clientID);
    95     void processChat( chat *data, int clientId);
     87   
     88   
     89    //void processChat( chat *data, int clientId);
    9690    ConnectionManager *connection;
    97     GameStateManager *gamestates;
    98     PacketGenerator packet_gen;
     91    GamestateManager *gamestates_;
    9992
    100     ClientInformation *clients;
     93   
    10194    float timeSinceLastUpdate_;
    10295  };
  • code/trunk/src/network/Synchronisable.cc

    r1639 r1735  
    4343#include <string>
    4444#include <iostream>
    45 #include <string.h>
     45#include <assert.h>
    4646
    4747#include "core/CoreIncludes.h"
     48#include "core/BaseObject.h"
    4849// #include "core/Identifier.h"
    4950
     
    8586    else  // server
    8687      state_=0x1;
     88  }
     89 
     90  bool Synchronisable::fabricate(unsigned char*& mem, int mode)
     91  {
     92    unsigned int size, objectID, classID;
     93    size = *(unsigned int *)mem;
     94    objectID = *(unsigned int*)(mem+sizeof(unsigned int));
     95    classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
     96   
     97    orxonox::Identifier* id = ID(classID);
     98    if(!id){
     99      COUT(3) << "We could not identify a new object; classid: " << classID << " uint: " << (unsigned int)classID << " objectID: " << objectID << " size: " << size << std::endl;
     100      return false; // most probably the gamestate is corrupted
     101    }
     102    orxonox::BaseObject *bo = id->fabricate();
     103    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
     104    assert(no);
     105    no->objectID=objectID;
     106    no->classID=classID;
     107    COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
     108          // update data and create object/entity...
     109    if( !no->updateData(mem, mode) ){
     110      COUT(1) << "We couldn't update the object: " << objectID << std::endl;
     111      return false;
     112    }
     113    if( !no->create() )
     114    {
     115      COUT(1) << "We couldn't manifest (create() ) the object: " << objectID << std::endl;
     116      return false;
     117    }
     118    return true;
    87119  }
    88120
     
    109141  }
    110142
    111   /**
    112   * note: only use this function for debug use, because it's inefficient (in order to produce a gamestate, you have to copy the whole data again to another memory location after this process)
    113   * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
    114   * structure of the bitstream:
    115   * (var1_size,var1,var2_size,var2,...)
    116   * varx_size: size = sizeof(int)
    117   * varx: size = varx_size
    118   * @return data containing all variables and their sizes
    119   */
    120   // syncData Synchronisable::getData(){
    121   //   std::list<synchronisableVariable>::iterator i;
    122   //   int totalsize=0;
    123   //   //figure out size of data to be allocated
    124   //   for(i=syncList->begin(); i!=syncList->end(); i++){
    125   //     // increase size (size of variable and size of size of variable ;)
    126   //     if(i->type == STRING)
    127   //       totalsize+=sizeof(int)+((std::string *)i->var)->length()+1;
    128   //     else
    129   //       totalsize+=sizeof(int)+i->size;
    130   //   }
    131   //   syncData retVal;
    132   //   retVal.objectID=this->objectID;
    133   //   retVal.classID=this->classID;
    134   //   retVal.length=totalsize;
    135   //   // allocate memory
    136   //   retVal.data = (unsigned char *)malloc(totalsize);
    137   //   // copy to location
    138   //   //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
    139   //   int n=0;
    140   //   for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){
    141   //     std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
    142   //     n+=sizeof(int);
    143   //     switch(i->type){
    144   //     case STRING:
    145   //       std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1);
    146   //       n+=((std::string *)i->var)->length()+1;
    147   //       break;
    148   //     case DATA:
    149   //       std::memcpy(retVal.data+n, ((const void*)i->var), i->size);
    150   //       n+=i->size;
    151   //       break;
    152   //     }
    153   //   }
    154   //   return retVal;
    155   // }
     143
    156144  /**
    157145  * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     
    203191    return retVal;
    204192  }
    205 
     193 
     194  /**
     195   * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     196  * Difference to the above function:
     197   * takes a pointer to already allocated memory (must have at least getSize bytes length)
     198  * structure of the bitstream:
     199   * (var1_size,var1,var2_size,var2,...)
     200   * varx_size: size = sizeof(int)
     201   * varx: size = varx_size
     202   * @return data containing all variables and their sizes
     203   */
     204  bool Synchronisable::getData2(unsigned char*& mem, int mode){
     205    //std::cout << "inside getData" << std::endl;
     206    unsigned int tempsize = 0;
     207    if(mode==0x0)
     208      mode=state_;
     209    if(classID==0)
     210      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
     211    this->classID=this->getIdentifier()->getNetworkID();
     212    std::list<synchronisableVariable *>::iterator i;
     213    unsigned int size;
     214    size=getSize2(mode);
     215   
     216    // start copy header
     217    memcpy(mem, &size, sizeof(unsigned int));
     218    mem+=sizeof(unsigned int);
     219    memcpy(mem, &(this->objectID), sizeof(unsigned int));
     220    mem+=sizeof(unsigned int);
     221    memcpy(mem, &(this->classID), sizeof(unsigned int));
     222    mem+=sizeof(unsigned int);
     223    tempsize+=12;
     224    // end copy header
     225   
     226   
     227    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
     228    // copy to location
     229    for(i=syncList->begin(); i!=syncList->end(); ++i){
     230      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
     231      if( ((*i)->mode & mode) == 0 ){
     232        COUT(5) << "not getting data: " << std::endl;
     233        continue;  // this variable should only be received
     234      }
     235      switch((*i)->type){
     236        case DATA:
     237          memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size);
     238          mem+=(*i)->size;
     239          tempsize+=(*i)->size;
     240          break;
     241        case STRING:
     242          memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(int) );
     243          mem+=sizeof(int);
     244          const char *data = ( ( *(std::string *) (*i)->var).c_str());
     245          memcpy( mem, (void*)data, (*i)->size);
     246          COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
     247          mem+=(*i)->size;
     248          tempsize+=(*i)->size + 4;
     249          break;
     250      }
     251    }
     252    assert(tempsize==size);
     253    return true;
     254  }
     255
     256  /*bool Synchronisable::getData(Bytestream& bs, int mode)
     257  {
     258    //std::cout << "inside getData" << std::endl;
     259    if(mode==0x0)
     260      mode=state_;
     261    if(classID==0)
     262      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
     263    this->classID=this->getIdentifier()->getNetworkID();
     264    std::list<synchronisableVariable *>::iterator i;
     265    bs << this->getSize();
     266    bs << this->objectID;
     267    bs << this->classID;
     268    // copy to location
     269    for(i=syncList->begin(); i!=syncList->end(); ++i){
     270      if( ((*i)->mode & mode) == 0 ){
     271        COUT(5) << "not getting data: " << std::endl;
     272        continue;  // this variable should only be received
     273      }
     274      switch((*i)->type){
     275        case DATA:
     276          bs << *(*i)->var;
     277          //std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);
     278          //n+=(*i)->size;
     279          break;
     280        case STRING:
     281          bs << *(String *)((*i)->var);
     282          //memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );
     283          //n+=sizeof(int);
     284          //const char *data = ( ( *(std::string *) (*i)->var).c_str());
     285          //std::memcpy( retVal.data+n, (void*)data, (*i)->size);
     286          //COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
     287          //n+=(*i)->size;
     288          break;
     289      }
     290    }
     291    return true;
     292  }*/
     293
     294 
    206295  /**
    207296  * This function takes a syncData struct and takes it to update the variables
     
    247336      }
    248337      // call the callback function, if defined
    249       if(callback)
     338      if(callback && (*i)->callback)
     339        (*i)->callback->call();
     340    }
     341    return true;
     342  }
     343 
     344  /**
     345   * This function takes a syncData struct and takes it to update the variables
     346   * @param vars data of the variables
     347   * @return true/false
     348   */
     349  bool Synchronisable::updateData(unsigned char*& mem, int mode){
     350    unsigned char *data = mem;
     351    if(mode==0x0)
     352      mode=state_;
     353    std::list<synchronisableVariable *>::iterator i;
     354    if(syncList->empty()){
     355      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
     356      return false;
     357    }
     358   
     359    // start extract header
     360    unsigned int objectID, classID, size;
     361    size = *(int *)mem;
     362    mem+=sizeof(size);
     363    objectID = *(int *)mem;
     364    mem+=sizeof(objectID);
     365    classID = *(int *)mem;
     366    mem+=sizeof(classID);
     367    // stop extract header
     368    assert(this->objectID==objectID);
     369    assert(this->classID==classID);
     370   
     371    COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl;
     372    for(i=syncList->begin(); i!=syncList->end() && mem <= data+size; i++){
     373      if( ((*i)->mode ^ mode) == 0 ){
     374        COUT(5) << "synchronisable: not updating variable " << std::endl;
     375        continue;  // this variable should only be set
     376      }
     377      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
     378      bool callback=false;
     379      switch((*i)->type){
     380        case DATA:
     381          if((*i)->callback) // check whether this variable changed (but only if callback was set)
     382            if(strncmp((char *)(*i)->var, (char *)mem, (*i)->size)!=0)
     383              callback=true;
     384          memcpy((void*)(*i)->var, mem, (*i)->size);
     385          mem+=(*i)->size;
     386          break;
     387        case STRING:
     388          (*i)->size = *(int *)mem;
     389          COUT(5) << "string size: " << (*i)->size << std::endl;
     390          mem+=sizeof(int);
     391          if((*i)->callback) // check whether this string changed
     392            if( *(std::string *)((*i)->var) != std::string((char *)mem) )
     393              callback=true;
     394          *((std::string *)((*i)->var)) = std::string((const char*)mem);
     395          COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl;
     396          mem += (*i)->size;
     397          break;
     398      }
     399      // call the callback function, if defined
     400      if(callback && (*i)->callback)
    250401        (*i)->callback->call();
    251402    }
     
    280431  }
    281432
     433  /**
     434   * This function returns the total amount of bytes needed by getData to save the whole content of the variables
     435   * @return amount of bytes
     436   */
     437  int Synchronisable::getSize2(int mode){
     438    return 3*sizeof(unsigned int) + getSize( mode );
     439  }
     440 
     441  bool Synchronisable::isMyData(unsigned char* mem)
     442  {
     443    unsigned int objectID, classID, size;
     444    size = *(int *)mem;
     445    mem+=sizeof(size);
     446    objectID = *(int *)mem;
     447    mem+=sizeof(objectID);
     448    classID = *(int *)mem;
     449    mem+=sizeof(classID);
     450   
     451    assert(classID == this->classID);
     452    return (objectID == this->objectID);
     453  }
     454 
    282455  void Synchronisable::setBacksync(bool sync){
    283456    backsync_=sync;
  • code/trunk/src/network/Synchronisable.h

    r1534 r1735  
    4444
    4545  struct syncData{
    46     int length;
    47     int objectID;
    48     int classID;
     46    unsigned int length;
     47    unsigned int objectID;
     48    unsigned int classID;
    4949    unsigned char *data;
    5050  };
    5151
    5252  typedef struct synchronisableVariable{
    53     int size;
     53    unsigned int size;
    5454    int mode; // this determines in which direction the variable gets synchronised
    5555    void *var;
     
    6969
    7070    virtual ~Synchronisable();
    71     int objectID;
    72     int classID;
     71    unsigned int objectID;
     72    unsigned int classID;
    7373
    7474    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    7575    //  syncData getData();
    7676    syncData getData(unsigned char *mem, int mode=0x0);
     77    bool getData2(unsigned char*& men, int mode=0x0);
     78    //bool getData(Bytestream& bs, int mode=0x0);
    7779    int getSize(int mode=0x0);
     80    int getSize2(int mode=0x0);
    7881    bool updateData(syncData vars, int mode=0x0);
     82    bool updateData(unsigned char*& mem, int mode=0x0);
     83    bool isMyData(unsigned char* mem);
    7984    void setBacksync(bool sync);
    8085    bool getBacksync();
     
    8287    virtual bool create();
    8388    static void setClient(bool b);
     89   
     90    static bool fabricate(unsigned char*& mem, int mode=0x0);
    8491  protected:
    8592    Synchronisable();
Note: See TracChangeset for help on using the changeset viewer.