Changeset 1793
- Timestamp:
- Sep 17, 2008, 5:24:56 PM (16 years ago)
- Location:
- code/branches/network
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/TODO
r1775 r1793 1 1 todo: 2 2 !!! 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 5 6 -
code/branches/network/src/network/Client.cc
r1775 r1793 43 43 #include "Synchronisable.h" 44 44 #include "core/CoreIncludes.h" 45 #include "core/ConsoleCommand.h"46 45 #include "packet/Packet.h" 47 46 // #include "packet/Acknowledgement.h" … … 118 117 } 119 118 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; 122 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 }*/ 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 } 129 133 130 134 131 135 /** 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 */ 148 139 void Client::tick(float time){ 149 140 // COUT(3) << "."; -
code/branches/network/src/network/Client.h
r1735 r1793 73 73 bool closeConnection(); 74 74 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); 76 77 //bool sendChat(packet::Chat *chat); 77 78 78 79 // static void Chat( std::string message ); 79 80 80 unsigned int shipID(){return shipID_;}81 int playerID(){return clientID_;}82 81 //static void setShipID( unsigned int shipID){ dynamic_cast<Client *>(instance_)->shipID_=shipID; } 83 82 static void setClientID( unsigned int clientID){ dynamic_cast<Client *>(instance_)->clientID_=clientID; } … … 91 90 bool isConnected; 92 91 bool isSynched_; 93 94 bool sendChat( std::string message );95 92 96 // implement data processing functions of PacketDecoder97 // void processChat( chat *data, int clientId );98 int clientID_; // this is the id the server gave to us99 int shipID_;100 93 bool gameStateFailure_; 101 94 }; -
code/branches/network/src/network/Host.cc
r1751 r1793 30 30 31 31 #include "Host.h" 32 #include "core/ConsoleCommand.h" 32 33 #include "packet/Packet.h" 33 34 34 35 namespace network { 35 36 37 SetConsoleCommandShortcut(Host, Chat); 38 36 39 Host *Host::instance_=0; 37 40 41 /** 42 * @brief Constructor: assures that only one reference will be created and sets the pointer 43 */ 38 44 Host::Host() 39 45 { 46 clientID_=0; 40 47 assert(instance_==0); 41 48 instance_=this; … … 43 50 44 51 52 /** 53 * @brief Destructor: resets the instance pointer to 0 54 */ 45 55 Host::~Host() 46 56 { … … 48 58 } 49 59 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 */ 50 66 bool Host::addPacket(ENetPacket *packet, int clientID){ 51 67 if(instance_) … … 70 86 // } 71 87 72 int Host::getPlayerID(){ 88 /** 89 * This function returns the ID of the player 90 * @return playerID 91 */ 92 unsigned int Host::getPlayerID(){ 73 93 if(!instance_) 74 94 return 0; 75 return instance_-> playerID();95 return instance_->clientID_; 76 96 } 77 97 78 // unsigned int Host::getShipID(){ 79 // if(!instance_) 80 // assert(0); 81 // return instance_->shipID(); 82 // } 98 bool Host::Chat(std::string message){ 99 if(!instance_) 100 return false; 101 return instance_->chat(message); 102 } 103 104 bool Host::incomingChat(std::string message, unsigned int playerID){ 105 return instance_->processChat(message, playerID); 106 } 83 107 84 108 }//namespace network -
code/branches/network/src/network/Host.h
r1751 r1793 37 37 38 38 /** 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 40 45 */ 41 46 class Host{ … … 45 50 //virtual bool sendChat(packet::Chat *chat)=0; 46 51 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; 49 54 50 55 … … 55 60 static Host *instance_; 56 61 bool isServer_; 62 unsigned int clientID_; 63 unsigned int shipID_; 57 64 58 65 public: … … 61 68 //static bool chat(std::string& message); 62 69 // static bool receiveChat(packet::Chat *message, unsigned int clientID); 63 static int getPlayerID();70 static unsigned int getPlayerID(); 64 71 static unsigned int getShipID(){return instance_->shipID_;} 65 72 static void setClientID(unsigned int id){ instance_->clientID_ = id; } 66 73 static void setShipID(unsigned int id){ instance_->shipID_ = id; } 67 74 static bool isServer(){ return instance_->isServer_; } 75 static bool Chat(std::string message); 76 static bool incomingChat(std::string message, unsigned int playerID); 68 77 private: 69 unsigned int clientID_;70 unsigned int shipID_;71 78 }; 72 79 -
code/branches/network/src/network/Server.cc
r1775 r1793 122 122 } 123 123 124 bool Server::processChat( packet::Chat *message, unsigned int clientID){124 bool Server::processChat(std::string message, unsigned int playerID){ 125 125 ClientInformation *temp = ClientInformation::getBegin(); 126 packet::Chat *chat; 126 127 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); 145 129 chat->setClientID(temp->getID()); 146 130 if(!chat->send()) 147 131 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 165 138 166 139 /** … … 175 148 if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){ 176 149 timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_; 177 // timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;178 150 gamestates_->processGamestates(); 179 151 updateGamestate(); 180 152 } 181 /*while(timeSinceLastUpdate_>1./NETWORK_FREQUENCY)182 timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;*/183 // usleep(5000); // TODO remove184 return;185 153 } 186 154 … … 303 271 return true; 304 272 } 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 315 274 316 275 bool Server::addClient(ENetEvent *event){ … … 417 376 gamestates_->removeClient(client); 418 377 } 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 } 419 392 420 393 } -
code/branches/network/src/network/Server.h
r1775 r1793 66 66 void open(); 67 67 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); 70 69 bool queuePacket(ENetPacket *packet, int clientID); 71 70 void tick(float time); … … 75 74 private: 76 75 unsigned int shipID(){return 0;} 77 int playerID(){return 0;}76 unsigned int playerID(){return 0;} 78 77 79 78 bool addClient(ENetEvent *event); … … 86 85 bool sendGameState(); 87 86 bool sendObjectDeletes(); 87 virtual bool chat(std::string message); 88 88 89 89 //void processChat( chat *data, int clientId); -
code/branches/network/src/network/Synchronisable.cc
r1775 r1793 51 51 namespace network 52 52 { 53 53 54 54 55 std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_; … … 59 60 /** 60 61 * Constructor: 61 * calls registarAllVariables, that has to be implemented by the inheriting classID62 * Initializes all Variables and sets the right objectID 62 63 */ 63 64 Synchronisable::Synchronisable(){ … … 71 72 } 72 73 74 /** 75 * Destructor: 76 * Delete all callback objects and remove objectID from the objectMap_ 77 */ 73 78 Synchronisable::~Synchronisable(){ 74 79 // delete callback function objects … … 81 86 } 82 87 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 */ 83 93 bool Synchronisable::create(){ 84 94 objectMap_[objectID]=this; … … 90 100 91 101 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 */ 92 106 void Synchronisable::setClient(bool b){ 93 107 if(b) // client … … 97 111 } 98 112 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 */ 99 120 Synchronisable *Synchronisable::fabricate(unsigned char*& mem, int mode) 100 121 { … … 119 140 120 141 142 /** 143 * Finds and deletes the Synchronisable with the appropriate objectID 144 * @param objectID objectID of the Synchronisable 145 * @return true/false 146 */ 121 147 bool Synchronisable::deleteObject(unsigned int objectID){ 122 148 assert(getSynchronisable(objectID)); … … 126 152 } 127 153 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 */ 128 159 Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){ 129 160 std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID); … … 140 171 * @param var pointer to the variable 141 172 * @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 142 176 */ 143 177 void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){ … … 158 192 159 193 /** 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 162 195 * takes a pointer to already allocated memory (must have at least getSize bytes length) 163 196 * 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 168 206 */ 169 207 bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){ … … 224 262 225 263 /** 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 228 267 * @return true/false 229 268 */ … … 288 327 /** 289 328 * 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 290 331 * @return amount of bytes 291 332 */ … … 316 357 317 358 /** 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 321 362 */ 322 363 bool Synchronisable::isMyTick(unsigned int id){ … … 325 366 } 326 367 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 */ 327 372 bool Synchronisable::isMyData(unsigned char* mem) 328 373 { … … 339 384 } 340 385 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 */ 341 393 void Synchronisable::setObjectMode(int mode){ 342 394 assert(mode==0x1 || mode==0x2 || mode==0x3); -
code/branches/network/src/network/Synchronisable.h
r1775 r1793 39 39 #include "NetworkCallback.h" 40 40 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 41 46 namespace network 42 47 { 48 namespace direction{ 49 enum syncdirection{ 50 toclient=0x1, 51 toserver=0x2, 52 bidirectional=0x3 53 }; 54 55 } 56 43 57 enum variableType{ 44 58 DATA, … … 63 77 /** 64 78 * 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. 67 80 * @author Oliver Scheuss 68 81 */ … … 74 87 unsigned int classID; 75 88 76 void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);77 89 bool getData(unsigned char*& men, unsigned int id, int mode=0x0); 78 90 int getSize(unsigned int id, int mode=0x0); … … 95 107 protected: 96 108 Synchronisable(); 109 void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0); 97 110 private: 98 111 bool isMyTick(unsigned int id); -
code/branches/network/src/network/packet/Chat.cc
r1763 r1793 29 29 #include "Chat.h" 30 30 #include <assert.h> 31 #include "network/Host.h" 31 32 32 33 namespace network { 33 34 namespace packet { 34 35 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 38 const int _PLAYERID = _PACKETID + sizeof(ENUM::Type); 39 #define _MESSAGELENGTH _PLAYERID + sizeof(unsigned int) 40 #define _MESSAGE _MESSAGELENGTH + sizeof(unsigned int) 39 41 40 Chat::Chat( std::string & message, int clientID )42 Chat::Chat( std::string message, unsigned int playerID ) 41 43 : Packet() 42 44 { … … 44 46 messageLength_ = message.length()+1; 45 47 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_ ); 50 52 } 51 53 … … 65 67 66 68 bool Chat::process(){ 67 //TODO: change this !!! 68 assert(0); 69 bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(unsigned int *)(data_+_PLAYERID)); 69 70 delete this; 70 return true;71 return b; 71 72 } 72 73 73 74 unsigned char *Chat::getMessage(){ 74 return &data_[ _MESSAGE ];75 return data_ + _MESSAGE; 75 76 } 76 77 -
code/branches/network/src/network/packet/Chat.h
r1763 r1793 15 15 { 16 16 public: 17 Chat( std::string & message, int clientID );17 Chat( std::string message, unsigned int playerID ); 18 18 Chat( unsigned char* data, int clientID ); 19 19 ~Chat(); -
code/branches/network/src/orxonox/objects/Model.cc
r1775 r1793 86 86 } 87 87 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 89 89 return true; 90 90 }
Note: See TracChangeset
for help on using the changeset viewer.