Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1793


Ignore:
Timestamp:
Sep 17, 2008, 5:24:56 PM (16 years ago)
Author:
scheusso
Message:

chat works again now. some doxygen. small change according to playerid in Host

Location:
code/branches/network
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/TODO

    r1775 r1793  
    11todo:
    22        !!! check that enet does not cause a packet traffic jam when a reliable packet gets missed !!!
    3         spaceship of client vanishes on server
    4         new gamestate concept
     3        [implemented] new gamestate concept
     4        check Projectiles
     5        check that we dont have to much registervar because of virtual function calls
    56
  • code/branches/network/src/network/Client.cc

    r1775 r1793  
    4343#include "Synchronisable.h"
    4444#include "core/CoreIncludes.h"
    45 #include "core/ConsoleCommand.h"
    4645#include "packet/Packet.h"
    4746// #include "packet/Acknowledgement.h"
     
    118117  }
    119118
    120   bool Client::processChat(packet::Chat *message, unsigned int clientID){
    121     return message->process();
     119  bool Client::processChat(std::string message, unsigned int playerID){
     120    COUT(1) << "Player " << playerID << ": " << message << std::endl;
     121    return true;
    122122  }
    123 
    124   /*bool Client::sendChat(packet::Chat *chat){
    125     chat->process();
    126     packet::Packet *p = new packet::Packet(chat);
    127     return p->send();
    128   }*/
     123 
     124  /**
     125   * This function implements the method of sending a chat message to the server
     126   * @param message message to be sent
     127   * @return result(true/false)
     128   */
     129  bool Client::chat(std::string message){
     130    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
     131    return m->send();
     132  }
    129133
    130134
    131135  /**
    132   * submits a chat message to the server
    133   * @param message message to send
    134   * @return true/false
    135   */
    136   bool Client::sendChat( std::string message ){
    137     // generate packet and add it to queue
    138     if(!isConnected)
    139       return false;
    140     packet::Chat chat(message, 0);
    141     return chat.send();
    142     // send packets
    143   }
    144 
    145   /**
    146   * Performs a GameState update
    147   */
     136   * Processes incoming packets, sends a gamestate to the server and does the cleanup
     137   * @param time
     138   */
    148139  void Client::tick(float time){
    149140//     COUT(3) << ".";
  • code/branches/network/src/network/Client.h

    r1735 r1793  
    7373    bool closeConnection();
    7474    bool queuePacket(ENetPacket *packet, int clientID);
    75     bool processChat(packet::Chat *message, unsigned int clientID);
     75    bool processChat(std::string message, unsigned int playerID);
     76    virtual bool chat(std::string message);
    7677    //bool sendChat(packet::Chat *chat);
    7778   
    7879//    static void Chat( std::string message );
    7980   
    80     unsigned int shipID(){return shipID_;}
    81     int playerID(){return clientID_;}
    8281    //static void setShipID( unsigned int shipID){ dynamic_cast<Client *>(instance_)->shipID_=shipID; }
    8382    static void setClientID( unsigned int clientID){ dynamic_cast<Client *>(instance_)->clientID_=clientID; }
     
    9190    bool isConnected;
    9291    bool isSynched_;
    93 
    94     bool sendChat( std::string message );
    9592   
    96     // implement data processing functions of PacketDecoder
    97 //     void processChat( chat *data, int clientId );
    98     int clientID_;     // this is the id the server gave to us
    99     int shipID_;
    10093    bool gameStateFailure_;
    10194  };
  • code/branches/network/src/network/Host.cc

    r1751 r1793  
    3030
    3131#include "Host.h"
     32#include "core/ConsoleCommand.h"
    3233#include "packet/Packet.h"
    3334
    3435namespace network {
    3536
     37SetConsoleCommandShortcut(Host, Chat);
     38
    3639Host *Host::instance_=0;
    3740 
     41/**
     42 * @brief Constructor: assures that only one reference will be created and sets the pointer
     43 */
    3844Host::Host()
    3945{
     46  clientID_=0;
    4047  assert(instance_==0);
    4148  instance_=this;
     
    4350
    4451
     52/**
     53 * @brief Destructor: resets the instance pointer to 0
     54 */
    4555Host::~Host()
    4656{
     
    4858}
    4959
     60/**
     61 * This function is used to add an enetpacket to be sent to another peer
     62 * @param packet Packet to be added
     63 * @param clientID ID of the client the packet should be sent to
     64 * @return success?
     65 */
    5066bool Host::addPacket(ENetPacket *packet, int clientID){
    5167  if(instance_)
     
    7086// }
    7187
    72 int Host::getPlayerID(){
     88/**
     89 * This function returns the ID of the player
     90 * @return playerID
     91 */
     92unsigned int Host::getPlayerID(){
    7393  if(!instance_)
    7494    return 0;
    75   return instance_->playerID();
     95  return instance_->clientID_;
    7696}
    7797
    78 // unsigned int Host::getShipID(){
    79 //   if(!instance_)
    80 //     assert(0);
    81 //   return instance_->shipID();
    82 // }
     98bool Host::Chat(std::string message){
     99  if(!instance_)
     100    return false;
     101  return instance_->chat(message);
     102}
     103
     104bool Host::incomingChat(std::string message, unsigned int playerID){
     105  return instance_->processChat(message, playerID);
     106}
    83107
    84108}//namespace network
  • code/branches/network/src/network/Host.h

    r1751 r1793  
    3737
    3838/**
    39         @author Oliver Scheuss
     39*       @brief Base class of Server and Client
     40*       This is the Base class of the Server and Client classes
     41*       - Makes server and client a singleton
     42*       - defines static functions available on both server and client
     43*       - is the interface to be used when communicating with the network
     44*       @author Oliver Scheuss
    4045*/
    4146class Host{
     
    4550    //virtual bool sendChat(packet::Chat *chat)=0;
    4651    virtual bool queuePacket(ENetPacket *packet, int clientID)=0;
    47     virtual unsigned int shipID()=0;
    48     virtual int playerID()=0;
     52    virtual bool chat(std::string message)=0;
     53    virtual bool processChat(std::string message, unsigned int playerID)=0;
    4954
    5055
     
    5560    static Host *instance_;
    5661    bool isServer_;     
     62    unsigned int clientID_;
     63    unsigned int shipID_;
    5764
    5865  public:
     
    6168    //static bool chat(std::string& message);
    6269//     static bool receiveChat(packet::Chat *message, unsigned int clientID);
    63     static int getPlayerID();
     70    static unsigned int getPlayerID();
    6471    static unsigned int getShipID(){return instance_->shipID_;}
    6572    static void setClientID(unsigned int id){ instance_->clientID_ = id; }
    6673    static void setShipID(unsigned int id){ instance_->shipID_ = id; }
    6774    static bool isServer(){ return instance_->isServer_; }             
     75    static bool Chat(std::string message);
     76    static bool incomingChat(std::string message, unsigned int playerID);
    6877  private:
    69     unsigned int clientID_;
    70     unsigned int shipID_;
    7178};
    7279
  • code/branches/network/src/network/Server.cc

    r1775 r1793  
    122122  }
    123123
    124   bool Server::processChat(packet::Chat *message, unsigned int clientID){
     124  bool Server::processChat(std::string message, unsigned int playerID){
    125125    ClientInformation *temp = ClientInformation::getBegin();
     126    packet::Chat *chat;
    126127    while(temp){
    127       message->setClientID(temp->getID());
    128       if(!message->send())
    129         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    130       temp = temp->next();
    131     }
    132     return message->process();
    133   }
    134 
    135   /**
    136   * This function sends out a message to all clients
    137   * @param msg message
    138   * @return true/false
    139   */
    140   bool Server::sendChat(packet::Chat *chat) {
    141     //TODO: change this (no informations about who wrote a message)
    142     assert(0);
    143     ClientInformation *temp = ClientInformation::getBegin();
    144     while(temp){
     128      chat = new packet::Chat(message, playerID);
    145129      chat->setClientID(temp->getID());
    146130      if(!chat->send())
    147131        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    148     }
    149     return chat->process();;
    150   }
    151 
    152   /**
    153   * This function sends out a message to all clients
    154   * @param msg message
    155   * @return true/false
    156   */
    157 //   bool Server::sendChat(const char *msg) {
    158 //     char *message = new char [strlen(msg)+10+1];
    159 //     sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
    160 //     COUT(1) << message << std::endl;
    161 //     ENetPacket *packet = packet_gen.chatMessage(message);
    162 //     COUT(5) <<"Server: adding Packets" << std::endl;
    163 //     return connection->addPacketAll(packet);
    164 //   }
     132      temp = temp->next();
     133    }
     134    COUT(1) << "Player " << playerID << ": " << message << std::endl;
     135    return true;
     136  }
     137
    165138
    166139  /**
     
    175148    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
    176149      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
    177 //      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;
    178150      gamestates_->processGamestates();
    179151      updateGamestate();
    180152    }
    181     /*while(timeSinceLastUpdate_>1./NETWORK_FREQUENCY)
    182       timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;*/
    183 //     usleep(5000); // TODO remove
    184     return;
    185153  }
    186154
     
    303271    return true;
    304272  }
    305  
    306 //   void Server::processChat( chat *data, int clientId){
    307 //     char *message = new char [strlen(data->message)+10+1];
    308 //     sprintf(message, "Player %d: %s", clientId, data->message);
    309 //     COUT(1) << message << std::endl;
    310 //     ENetPacket *pck = packet_gen.chatMessage(message);
    311 //     connection->addPacketAll(pck);
    312 //     delete[] data->message;
    313 //     delete data;
    314 //   }
     273
    315274
    316275  bool Server::addClient(ENetEvent *event){
     
    417376    gamestates_->removeClient(client);
    418377  }
     378 
     379  bool Server::chat(std::string message){
     380    ClientInformation *temp = ClientInformation::getBegin();
     381    packet::Chat *chat;
     382    while(temp){
     383      chat = new packet::Chat(message, Host::getPlayerID());
     384      chat->setClientID(temp->getID());
     385      if(!chat->send())
     386        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     387      temp = temp->next();
     388    }
     389    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
     390    return true;
     391  }
    419392
    420393}
  • code/branches/network/src/network/Server.h

    r1775 r1793  
    6666    void open();
    6767    void close();
    68     bool processChat(packet::Chat *message, unsigned int clientID);
    69     bool sendChat(packet::Chat *chat);
     68    bool processChat(std::string message, unsigned int playerID);
    7069    bool queuePacket(ENetPacket *packet, int clientID);
    7170    void tick(float time);
     
    7574  private:
    7675    unsigned int shipID(){return 0;}
    77     int playerID(){return 0;}
     76    unsigned int playerID(){return 0;}
    7877   
    7978    bool addClient(ENetEvent *event);
     
    8685    bool sendGameState();
    8786    bool sendObjectDeletes();
     87    virtual bool chat(std::string message);
    8888   
    8989    //void processChat( chat *data, int clientId);
  • code/branches/network/src/network/Synchronisable.cc

    r1775 r1793  
    5151namespace network
    5252{
     53 
    5354
    5455  std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_;
     
    5960  /**
    6061  * Constructor:
    61   * calls registarAllVariables, that has to be implemented by the inheriting classID
     62  * Initializes all Variables and sets the right objectID
    6263  */
    6364  Synchronisable::Synchronisable(){
     
    7172  }
    7273
     74  /**
     75   * Destructor:
     76   * Delete all callback objects and remove objectID from the objectMap_
     77   */
    7378  Synchronisable::~Synchronisable(){
    7479    // delete callback function objects
     
    8186  }
    8287
     88  /**
     89   * This function gets called after all neccessary data has been passed to the object
     90   * Overload this function and recall the create function of the parent class
     91   * @return true/false
     92   */
    8393  bool Synchronisable::create(){
    8494    objectMap_[objectID]=this;
     
    90100
    91101 
     102  /**
     103   * This function sets the internal mode for synchronisation
     104   * @param b true if this object is located on a client or on a server
     105   */
    92106  void Synchronisable::setClient(bool b){
    93107    if(b) // client
     
    97111  }
    98112 
     113  /**
     114   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
     115   * After calling this function the mem pointer will be increased by the size of the needed data
     116   * @param mem pointer to where the appropriate data is located
     117   * @param mode defines the mode, how the data should be loaded
     118   * @return pointer to the newly created synchronisable
     119   */
    99120  Synchronisable *Synchronisable::fabricate(unsigned char*& mem, int mode)
    100121  {
     
    119140
    120141 
     142  /**
     143   * Finds and deletes the Synchronisable with the appropriate objectID
     144   * @param objectID objectID of the Synchronisable
     145   * @return true/false
     146   */
    121147  bool Synchronisable::deleteObject(unsigned int objectID){
    122148    assert(getSynchronisable(objectID));
     
    126152  }
    127153 
     154  /**
     155   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
     156   * @param objectID objectID of the Synchronisable
     157   * @return pointer to the Synchronisable with the objectID
     158   */
    128159  Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){
    129160    std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID);
     
    140171  * @param var pointer to the variable
    141172  * @param size size of the datatype the variable consists of
     173  * @param t the type of the variable (network::DATA or network::STRING
     174  * @param mode same as in getData
     175  * @param cb callback object that should get called, if the value of the variable changes
    142176  */
    143177  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
     
    158192 
    159193  /**
    160    * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
    161    * Difference to the above function:
     194   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
    162195   * takes a pointer to already allocated memory (must have at least getSize bytes length)
    163196   * structure of the bitstream:
    164    * (var1_size,var1,var2_size,var2,...)
    165    * varx_size: size = sizeof(int)
    166    * varx: size = varx_size
    167    * @return data containing all variables and their sizes
     197   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
     198   * length of varx: size saved int syncvarlist
     199   * @param mem pointer to allocated memory with enough size
     200   * @param id gamestateid of the gamestate to be saved (important for priorities)
     201   * @param mode defines the direction in which the data will be send/received
     202   *             0x1: server->client
     203   *             0x2: client->server (not recommended)
     204   *             0x3: bidirectional
     205   * @return true: if !isMyTick or if everything was successfully saved
    168206   */
    169207  bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){
     
    224262 
    225263  /**
    226    * This function takes a syncData struct and takes it to update the variables
    227    * @param vars data of the variables
     264   * This function takes a bytestream and loads the data into the registered variables
     265   * @param mem pointer to the bytestream
     266   * @param mode same as in getData
    228267   * @return true/false
    229268   */
     
    288327  /**
    289328  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
     329  * @param id id of the gamestate
     330  * @param mode same as getData
    290331  * @return amount of bytes
    291332  */
     
    316357
    317358  /**
    318    *
    319    * @param id
    320    * @return
     359   * This function determines, wheter the object should be saved to the bytestream (according to its syncfrequency)
     360   * @param id gamestate id
     361   * @return true/false
    321362   */
    322363  bool Synchronisable::isMyTick(unsigned int id){
     
    325366  }
    326367 
     368  /**
     369   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
     370   * @param mem pointer to the bytestream
     371   */
    327372  bool Synchronisable::isMyData(unsigned char* mem)
    328373  {
     
    339384  }
    340385 
     386  /**
     387   * This function sets the synchronisation mode of the object
     388   * If set to 0x1 variables will only be synchronised to the client
     389   * If set to 0x2 variables will only be synchronised to the server
     390   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
     391   * @param mode same as in registerVar
     392   */
    341393  void Synchronisable::setObjectMode(int mode){
    342394    assert(mode==0x1 || mode==0x2 || mode==0x3);
  • code/branches/network/src/network/Synchronisable.h

    r1775 r1793  
    3939#include "NetworkCallback.h"
    4040
     41#define REGISTERDATA(varname) registerVar(&varname, sizeof(varname), network::DATA)
     42#define REGISTERDATA_WITHDIR(varname, mode) registerVar(&varname, sizeof(varname), network::DATA, mode)
     43#define REGISTERSTRING(stringname) registerVar(&stringname, stringname.length()+1, network::STRING)
     44#define REGISTERSTRING_WITHDIR(stringname, mode) registerVar(&stringname, stringname.length()+1, network::STRING, mode)
     45
    4146namespace network
    4247{
     48  namespace direction{
     49    enum syncdirection{
     50      toclient=0x1,
     51      toserver=0x2,
     52      bidirectional=0x3
     53    };
     54   
     55  }
     56 
    4357  enum variableType{
    4458    DATA,
     
    6377  /**
    6478  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
    65    * Every class, t
    66   int mode;hat inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. Additionally it also has to provide a Constructor, that takes exactly the variables in this linked list.
     79   * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
    6780  * @author Oliver Scheuss
    6881  */
     
    7487    unsigned int classID;
    7588
    76     void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    7789    bool getData(unsigned char*& men, unsigned int id, int mode=0x0);
    7890    int getSize(unsigned int id, int mode=0x0);
     
    95107  protected:
    96108    Synchronisable();
     109    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
    97110  private:
    98111    bool isMyTick(unsigned int id);
  • code/branches/network/src/network/packet/Chat.cc

    r1763 r1793  
    2929#include "Chat.h"
    3030#include <assert.h>
     31#include "network/Host.h"
    3132
    3233namespace network {
    3334namespace packet {
    3435 
    35 #define PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
    36 #define _PACKETID         0
    37 #define _MESSAGELENGTH    _PACKETID + sizeof(ENUM::Type)
    38 #define _MESSAGE          _MESSAGELENGTH + sizeof(unsigned int)
     36#define   PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
     37#define   _PACKETID         0
     38const int _PLAYERID     =   _PACKETID + sizeof(ENUM::Type);
     39#define   _MESSAGELENGTH    _PLAYERID + sizeof(unsigned int)
     40#define   _MESSAGE          _MESSAGELENGTH + sizeof(unsigned int)
    3941
    40 Chat::Chat( std::string& message, int clientID )
     42Chat::Chat( std::string message, unsigned int playerID )
    4143 : Packet()
    4244{
     
    4446  messageLength_ = message.length()+1;
    4547  data_=new unsigned char[ getSize() ];
    46   *(ENUM::Type *)&data_[ _PACKETID ] = ENUM::Chat;
    47   *(unsigned int *)&data_[ _MESSAGELENGTH ] = messageLength_;
    48   memcpy( &data_[ _MESSAGE ], (void *)message.c_str(), messageLength_ );
    49   clientID_=clientID;
     48  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Chat;
     49  *(unsigned int *)(data_ + _PLAYERID ) = playerID;
     50  *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
     51  memcpy( data_+_MESSAGE, (void *)message.c_str(), messageLength_ );
    5052}
    5153
     
    6567
    6668bool Chat::process(){
    67   //TODO: change this !!!
    68   assert(0);
     69  bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(unsigned int *)(data_+_PLAYERID));
    6970  delete this;
    70   return true;
     71  return b;
    7172}
    7273
    7374unsigned char *Chat::getMessage(){
    74   return &data_[ _MESSAGE ];
     75  return data_ + _MESSAGE;
    7576}
    7677
  • code/branches/network/src/network/packet/Chat.h

    r1763 r1793  
    1515{
    1616public:
    17   Chat( std::string& message, int clientID );
     17  Chat( std::string message, unsigned int playerID );
    1818  Chat( unsigned char* data, int clientID );
    1919  ~Chat();
  • code/branches/network/src/orxonox/objects/Model.cc

    r1775 r1793  
    8686      }
    8787      if(this->isExactlyA(Class(Model)))
    88         setObjectFrequency(1); //sync all 10 seconds (this only applies to asteroids and other isExactlyA(Model)
     88        setObjectFrequency(300); //sync all 10 seconds (this only applies to asteroids and other isExactlyA(Model)'s
    8989      return true;
    9090    }
Note: See TracChangeset for help on using the changeset viewer.