Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1666


Ignore:
Timestamp:
Aug 26, 2008, 12:52:43 AM (16 years ago)
Author:
scheusso
Message:

a hole lot of changes:

  • changes in structure concerning client/server (they inherit from Host now)
  • some small changes in spaceship
  • delete unused files
  • modified clientinformation (is a singleton now)
  • modified gamestatemanager (adopted clientinformation changes)
  • modified connectionmanager (adopted CI changes, is a singleton now)
Location:
code/branches/network/src/network
Files:
3 added
7 deleted
11 edited

Legend:

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

    r1505 r1666  
    1111  Server.cc
    1212  Synchronisable.cc
     13  Host.cc
     14  packet/Packet.cc
     15  packet/Chat.cc
     16  packet/ClassID.cc
     17  packet/Acknowledgement.cc
     18  packet/Gamestate.cc
     19  Host.cc
    1320)
    1421
  • code/branches/network/src/network/Client.cc

    r1534 r1666  
    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"
    4647
    4748namespace network
    4849{
    49   SetConsoleCommandShortcut(Client, Chat);
    50  
    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   }
     50//   SetConsoleCommandShortcut(Client, chat);
     51 
    8252 
    8353  /**
     
    144114  }
    145115
    146  
    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;
    155   }
    156  
     116  bool Client::queuePacket(ENetPacket *packet, int clientID){
     117    return client_connection.addPacket(packet);
     118  }
     119 
     120  bool Client::processChat(packet::Chat *message, unsigned int clientID){
     121    return message->process();
     122  }
     123 
     124  bool Client::sendChat(packet::Chat *chat){
     125    chat->process();
     126    packet::Packet *p = new packet::Packet(chat);
     127    return p->send();
     128  }
    157129
    158130  /**
     
    236208  }
    237209
    238   void Client::processChat( chat *data, int clientId){
    239     COUT(1) << data->message << std::endl;
    240     delete[] data->message;
    241     delete data;
    242   }
     210//   void Client::processChat( chat *data, int clientId){
     211//     COUT(1) << data->message << std::endl;
     212//     delete[] data->message;
     213//     delete data;
     214//   }
    243215 
    244216  bool Client::processWelcome( welcome *w ){
  • code/branches/network/src/network/Client.h

    r1535 r1666  
    4646#include <string>
    4747
     48#include "Host.h"
     49#include "packet/Chat.h"
    4850#include "ClientConnection.h"
    4951#include "PacketManager.h"
     
    6264  *
    6365  */
    64   class _NetworkExport Client : PacketDecoder{
     66  class _NetworkExport Client : PacketDecoder, public Host{
    6567  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:
    8468    Client();
    8569    Client(std::string address, int port);
     
    8771    ~Client();
    8872   
    89     static Client* _sClient;
     73    bool establishConnection();
     74    bool closeConnection();
     75    bool queuePacket(ENetPacket *packet, int clientID);
     76    bool processChat(packet::Chat *message, unsigned int clientID);
     77    bool sendChat(packet::Chat *chat);
     78   
     79//    static void Chat( std::string message );
     80   
     81    int shipID(){return shipID_;}
     82    int playerID(){return clientID_;}
     83
     84    void tick(float time);
     85
     86  private:
    9087   
    9188    ClientConnection client_connection;
     
    10097    void processGamestate( GameStateCompressed *data, int clientID);
    10198    void processClassid(classid *clid);
    102     void processChat( chat *data, int clientId );
     99//     void processChat( chat *data, int clientId );
    103100    bool processWelcome( welcome *w );
    104101    int clientID_;     // this is the id the server gave to us
  • code/branches/network/src/network/ClientInformation.cc

    r1534 r1666  
    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  }
     
    174151  }
    175152 
    176   bool ClientInformation::getHead(){
    177     return head_;
    178   }
    179  
    180   void ClientInformation::setHead(bool h){
    181     head_=h;
    182   }
    183  
    184153  int ClientInformation::getFailures(){
    185154    return failures_;
     
    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/branches/network/src/network/ClientInformation.h

    r1534 r1666  
    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
     
    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);
     
    116114    int ShipID_;   // this is the unique objectID
    117115    bool synched_;
    118     bool head_;
    119116    unsigned short failures_;
    120117   
  • code/branches/network/src/network/ConnectionManager.cc

    r1534 r1666  
    3939
    4040#include <iostream>
     41#include <assert.h>
    4142// boost.thread library for multithreading support
    4243#include <boost/bind.hpp>
     
    6566  //boost::thread_group network_threads;
    6667 
    67   ConnectionManager::ConnectionManager():receiverThread_(0){}
    68   boost::recursive_mutex ConnectionManager::enet_mutex_;
    69  
    70   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     68  ConnectionManager *ConnectionManager::instance_=0;
     69 
     70  ConnectionManager::ConnectionManager():receiverThread_(0){
     71    assert(instance_==0);
     72    instance_=this;
    7173    quit=false;
    7274    bindAddress.host = ENET_HOST_ANY;
    7375    bindAddress.port = NETWORK_PORT;
    74     head_ = head;
    75   }
    76  
    77   ConnectionManager::ConnectionManager(ClientInformation *head, int port){
     76  }
     77  boost::recursive_mutex ConnectionManager::enet_mutex_;
     78 
     79//   ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
     80//     assert(instance_==0);
     81//     instance_=this;
     82//     quit=false;
     83//     bindAddress.host = ENET_HOST_ANY;
     84//     bindAddress.port = NETWORK_PORT;
     85//   }
     86 
     87  ConnectionManager::ConnectionManager(int port){
     88    assert(instance_==0);
     89    instance_=this;
    7890    quit=false;
    7991    bindAddress.host = ENET_HOST_ANY;
    8092    bindAddress.port = port;
    81     head_ = head;
    82   }
    83 
    84   ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) {
     93  }
     94
     95  ConnectionManager::ConnectionManager(int port, std::string address) :receiverThread_(0) {
     96    assert(instance_==0);
     97    instance_=this;
    8598    quit=false;
    8699    enet_address_set_host (& bindAddress, address.c_str());
    87100    bindAddress.port = NETWORK_PORT;
    88     head_ = head;
    89   }
    90 
    91   ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) : receiverThread_(0) {
     101  }
     102
     103  ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) {
     104    assert(instance_==0);
     105    instance_=this;
    92106    quit=false;
    93107    enet_address_set_host (& bindAddress, address);
    94108    bindAddress.port = NETWORK_PORT;
    95     head_ = head;
     109  }
     110 
     111  ConnectionManager::~ConnectionManager(){
     112    instance_=0;
     113    if(!quit)
     114      quitListener();
    96115  }
    97116
     
    140159    return true;
    141160  }
    142 
     161 
     162//   bool ConnectionManager::addPacket(Packet::Packet *packet){
     163//     ClientInformation *temp = instance_->head_->findClient(packet->getClientID());
     164//     if(!temp){
     165//       COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
     166//       return false;
     167//     }
     168//     ENetPacket *packet = new ENetPacket;
     169//     //  TODO: finish implementation
     170//   }
     171//   
     172 
    143173  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)
     174    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_);
     175    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
    149176      return false;
    150177    return true;
     
    152179
    153180  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
    154     ClientInformation *temp = head_->findClient(clientID);
     181    ClientInformation *temp = ClientInformation::findClient(clientID);
    155182    if(!temp){
    156183      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
    157184      return false;
    158185    }
    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;
     186    return addPacket(packet, temp->getPeer());
    165187  }
    166188
    167189  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()){
     190    if(!instance_)
     191      return false;
     192    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_);
     193    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
    170194      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
    171195      if(enet_peer_send(i->getPeer(), 0, packet)!=0)
     
    177201  // we actually dont need that function, because host_service does that for us
    178202  bool ConnectionManager::sendPackets() {
    179     if(server==NULL)
     203    if(server==NULL || !instance_)
    180204      return false;
    181205    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     
    257281  void ConnectionManager::disconnectClients() {
    258282    ENetEvent event;
    259     ClientInformation *temp = head_->next();
     283    ClientInformation *temp = ClientInformation::getBegin()->next();
    260284    while(temp!=0){
    261285      {
     
    267291    }
    268292    //bugfix: might be the reason why server crashes when clients disconnects
    269     temp = head_->next();
     293    temp = ClientInformation::getBegin()->next();
    270294    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    271295    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
     
    279303      case ENET_EVENT_TYPE_DISCONNECT:
    280304        COUT(4) << "disconnecting all clients" << std::endl;
    281         if(head_->findClient(&(event.peer->address)))
    282           delete head_->findClient(&(event.peer->address));
     305        if(ClientInformation::findClient(&(event.peer->address)))
     306          delete ClientInformation::findClient(&(event.peer->address));
    283307        //maybe needs bugfix: might also be a reason for the server to crash
    284308        temp = temp->next();
     
    302326
    303327  int ConnectionManager::getClientID(ENetAddress address) {
    304     return head_->findClient(&address)->getID();
     328    return ClientInformation::findClient(&address)->getID();
    305329  }
    306330
    307331  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
    308     return head_->findClient(clientID)->getPeer();
     332    return ClientInformation::findClient(clientID)->getPeer();
    309333  }
    310334
  • code/branches/network/src/network/ConnectionManager.h

    r1505 r1666  
    5252#include "PacketBuffer.h"
    5353#include "PacketManager.h"
     54#include "packet/Packet.h"
    5455
    5556namespace std
     
    6364#define NETWORK_MAX_CONNECTIONS 50
    6465#define NETWORK_WAIT_TIMEOUT 1
     66#define NETWORK_DEFAULT_CHANNEL 0
    6567
    6668  struct ClientList{
     
    7375  public:
    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();
     
    113117
    114118    bool quit; // quit-variable (communication with threads)
    115     ClientInformation *head_;
    116119
    117120    boost::thread *receiverThread_;
    118121    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 );*/
     122    static ConnectionManager *instance_;
     123
    127124  };
    128125
  • code/branches/network/src/network/GameStateManager.cc

    r1534 r1666  
    5353namespace network
    5454{
    55   GameStateManager::GameStateManager(ClientInformation *head) {
     55  GameStateManager::GameStateManager() {
    5656    id_=0;
    57     head_=head;
    5857  }
    5958
     
    130129    //why are we searching the same client's gamestate id as we searched in
    131130    //Server::sendGameState?
    132     int gID = head_->findClient(clientID)->getGamestateID();
     131    int gID = ClientInformation::findClient(clientID)->getGamestateID();
    133132    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
    134133//     COUT(3) << "gamestatemap: " << &gameStateMap << std::endl;
     
    467466
    468467  void GameStateManager::ackGameState(int clientID, int gamestateID) {
    469     ClientInformation *temp = head_->findClient(clientID);
     468    ClientInformation *temp = ClientInformation::findClient(clientID);
    470469    if(temp==0)
    471470      return;
     
    524523    if(client->getGamestateID()>=0)
    525524      gameStateUsed[client->getGamestateID()]--;
    526     head_->removeClient(client->getID());
     525    ClientInformation::removeClient(client->getID());
    527526  }
    528527
  • code/branches/network/src/network/GameStateManager.h

    r1505 r1666  
    6969  class GameStateManager{
    7070  public:
    71     GameStateManager(ClientInformation *head);
     71    GameStateManager();
    7272    ~GameStateManager();
    7373   
     
    9696    std::map<int, GameStateCompressed*> gameStateQueue;
    9797    GameState *reference;
    98     ClientInformation *head_;
    9998    int id_;
    10099   
  • code/branches/network/src/network/Server.cc

    r1556 r1666  
    4848#include "GameStateManager.h"
    4949#include "ClientInformation.h"
    50 //#include "NetworkFrameListener.h"
    5150#include "util/Sleep.h"
    5251#include "objects/SpaceShip.h"
    5352#include "core/ConsoleCommand.h"
     53#include "packet/Chat.h"
     54#include "packet/Packet.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
     
    9366    timeSinceLastUpdate_=0;
    9467    packet_gen = PacketGenerator();
    95     clients = new ClientInformation(true);
    96     connection = new ConnectionManager(clients);
    97     gamestates = new GameStateManager(clients);
     68    connection = new ConnectionManager();
     69    gamestates = new GameStateManager();
    9870  }
    9971 
     
    10173    timeSinceLastUpdate_=0;
    10274    packet_gen = PacketGenerator();
    103     clients = new ClientInformation(true);
    104     connection = new ConnectionManager(clients, port);
    105     gamestates = new GameStateManager(clients);
     75    connection = new ConnectionManager(port);
     76    gamestates = new GameStateManager();
    10677  }
    10778
     
    11485    timeSinceLastUpdate_=0;
    11586    packet_gen = PacketGenerator();
    116     clients = new ClientInformation();
    117     connection = new ConnectionManager(port, bindAddress, clients);
    118     gamestates = new GameStateManager(clients);
     87    connection = new ConnectionManager(port, bindAddress);
     88    gamestates = new GameStateManager();
    11989  }
    12090
     
    12797    timeSinceLastUpdate_=0;
    12898    packet_gen = PacketGenerator();
    129     clients = new ClientInformation();
    130     connection = new ConnectionManager(port, bindAddress, clients);
    131     gamestates = new GameStateManager(clients);
     99    connection = new ConnectionManager(port, bindAddress);
     100    gamestates = new GameStateManager();
    132101  }
    133102
     
    148117  }
    149118
     119  bool Server::processChat(packet::Chat *message, unsigned int clientID){
     120    ClientInformation *temp = ClientInformation::getBegin();
     121    packet::Packet *pkt;
     122    while(temp){
     123      pkt = new packet::Packet(message);
     124      pkt->setClientID(temp->getID());
     125      if(!pkt->send())
     126        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     127    }
     128    return message->process();
     129  }
     130 
    150131  /**
    151132  * This function sends out a message to all clients
     
    153134  * @return true/false
    154135  */
    155   bool Server::sendChat(std::string msg) {
    156     return sendChat(msg.c_str());
     136  bool Server::sendChat(packet::Chat *chat) {
     137    //TODO: change this (no informations about who wrote a message)
     138    ClientInformation *temp = ClientInformation::getBegin();
     139    packet::Packet *pkt;
     140    while(temp){
     141      pkt = new packet::Packet(chat);
     142      pkt->setClientID(temp->getID());
     143      if(!pkt->send())
     144        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     145    }
     146    return chat->process();
    157147  }
    158148
     
    162152  * @return true/false
    163153  */
    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   }
     154//   bool Server::sendChat(const char *msg) {
     155//     char *message = new char [strlen(msg)+10+1];
     156//     sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
     157//     COUT(1) << message << std::endl;
     158//     ENetPacket *packet = packet_gen.chatMessage(message);
     159//     COUT(5) <<"Server: adding Packets" << std::endl;
     160//     return connection->addPacketAll(packet);
     161//   }
    172162
    173163  /**
     
    190180//     usleep(5000); // TODO remove
    191181    return;
     182  }
     183 
     184  bool Server::queuePacket(ENetPacket *packet, int clientID){
     185    return connection->addPacket(packet, clientID);
    192186  }
    193187
     
    211205        break;
    212206      case ENET_EVENT_TYPE_DISCONNECT:
    213         if(clients->findClient(&event->peer->address))
     207        if(ClientInformation::findClient(&event->peer->address))
    214208          disconnectClient(event);
    215209        break;
    216210      case ENET_EVENT_TYPE_RECEIVE:
    217         if(clients->findClient(&event->peer->address)){
    218           clientID = clients->findClient(&event->peer->address)->getID();
     211        if(ClientInformation::findClient(&event->peer->address)){
     212          clientID = ClientInformation::findClient(&event->peer->address)->getID();
    219213          if( !elaborate(event->packet, clientID) )
    220214            COUT(3) << "Server: could not elaborate" << std::endl;
     
    247241  bool Server::sendGameState() {
    248242    COUT(5) << "Server: starting function sendGameState" << std::endl;
    249     ClientInformation *temp = clients;
     243    ClientInformation *temp = ClientInformation::getBegin();
    250244    bool added=false;
    251245    while(temp != NULL){
    252       if(temp->getHead()){
    253         temp=temp->next();
    254         //think this works without continue
    255         continue;
    256       }
    257246      if( !(temp->getSynched()) ){
    258247        COUT(5) << "Server: not sending gamestate" << std::endl;
    259248        temp=temp->next();
     249        if(!temp)
     250          break;
    260251        //think this works without continue
    261252        continue;
     
    320311  }
    321312 
    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   }
     313//   void Server::processChat( chat *data, int clientId){
     314//     char *message = new char [strlen(data->message)+10+1];
     315//     sprintf(message, "Player %d: %s", clientId, data->message);
     316//     COUT(1) << message << std::endl;
     317//     ENetPacket *pck = packet_gen.chatMessage(message);
     318//     connection->addPacketAll(pck);
     319//     delete[] data->message;
     320//     delete data;
     321//   }
    331322 
    332323  bool Server::addClient(ENetEvent *event){
    333     ClientInformation *temp = clients->insertBack(new ClientInformation);
     324    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    334325    if(!temp){
    335326      COUT(2) << "Server: could not add client" << std::endl;
    336327      return false;
    337328    }
    338     if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
     329    if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack
    339330      temp->prev()->setID(0); //bugfix: not necessary but usefull
    340331      temp->setID(1);
     
    348339 
    349340  bool Server::createClient(int clientID){
    350     ClientInformation *temp = clients->findClient(clientID);
     341    ClientInformation *temp = ClientInformation::findClient(clientID);
    351342    if(!temp){
    352343      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
     
    376367    }
    377368    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
     369    no->classID = id->getNetworkID();
     370    client->setShipID(no->objectID);
    378371    no->setPosition(orxonox::Vector3(0,0,80));
    379372    no->setScale(10);
     
    388381    no->setRotDamp(1.0);
    389382    no->setCamera("cam_"+client->getID());
    390     no->classID = id->getNetworkID();
    391383    no->create();
     384    no->setBacksync(true);
    392385   
    393     client->setShipID(no->objectID);
    394386    return true;
    395387  }
     
    401393    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
    402394    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
    403     ClientInformation *client = clients->findClient(&event->peer->address);
     395    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    404396    if(!client)
    405397      return false;
     
    412404      ++it;
    413405      delete  *temp;
    414       return clients->removeClient(event->peer);
     406      return ClientInformation::removeClient(event->peer);
    415407    }
    416408    return false;
     
    418410
    419411  void Server::disconnectClient(int clientID){
    420     ClientInformation *client = clients->findClient(clientID);
     412    ClientInformation *client = ClientInformation::findClient(clientID);
    421413    if(client)
    422414      disconnectClient(client);
  • code/branches/network/src/network/Server.h

    r1535 r1666  
    4646#include <string>
    4747
     48#include "Host.h"
    4849#include "PacketManager.h"
    4950
     
    5859  * It implements all functions necessary for a Server
    5960  */
    60   class _NetworkExport Server : public PacketDecoder{
     61  class _NetworkExport Server : public PacketDecoder, public Host{
    6162  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();
     63    Server();
     64    Server(int port);
     65    Server(int port, std::string bindAddress);
     66    Server(int port, const char *bindAddress);
    6867   
    6968    void open();
    7069    void close();
    71     bool sendChat(std::string msg);
    72     bool sendChat(const char *msg);
     70    bool processChat(packet::Chat *message, unsigned int clientID);
     71    bool sendChat(packet::Chat *chat);
     72    bool queuePacket(ENetPacket *packet, int clientID);
    7373    void tick(float time);
    7474  protected:
     
    7676    void updateGamestate();
    7777  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_;
     78    int shipID(){return 0;}
     79    int playerID(){return 0;}
    8480   
    8581    bool addClient(ENetEvent *event);
     
    9389    bool processConnectRequest( connectRequest *con, int clientID );
    9490    void processGamestate( GameStateCompressed *data, int clientID);
    95     void processChat( chat *data, int clientId);
     91    //void processChat( chat *data, int clientId);
    9692    ConnectionManager *connection;
    9793    GameStateManager *gamestates;
    9894    PacketGenerator packet_gen;
    9995
    100     ClientInformation *clients;
     96   
    10197    float timeSinceLastUpdate_;
    10298  };
Note: See TracChangeset for help on using the changeset viewer.