Changeset 1534 for code/trunk/src/network
- Timestamp:
- Jun 4, 2008, 8:53:10 PM (17 years ago)
- Location:
- code/trunk/src/network
- Files:
-
- 15 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Client.cc
r1505 r1534 42 42 #include "Synchronisable.h" 43 43 #include "core/CoreIncludes.h" 44 #include "core/ConsoleCommand.h" 45 #include "Server.h" 44 46 45 47 namespace network 46 48 { 49 SetConsoleCommandShortcut(Client, Chat); 50 47 51 Client* Client::_sClient = 0; 48 52 … … 142 146 143 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 } 144 156 145 157 … … 224 236 } 225 237 226 void Client::processChat( chat *data ){227 COUT( 0) << "Server: "<< data->message << std::endl;238 void Client::processChat( chat *data, int clientId){ 239 COUT(1) << data->message << std::endl; 228 240 delete[] data->message; 229 241 delete data; -
code/trunk/src/network/Client.h
r1505 r1534 75 75 bool closeConnection(); 76 76 77 bool sendChat( std::string message );77 static void Chat( std::string message ); 78 78 79 79 int getShipID(){return shipID_;} … … 96 96 bool isSynched_; 97 97 98 bool sendChat( std::string message ); 99 98 100 // implement data processing functions of PacketDecoder 99 101 void processGamestate( GameStateCompressed *data, int clientID); 100 102 void processClassid(classid *clid); 101 void processChat( chat *data );103 void processChat( chat *data, int clientId ); 102 104 bool processWelcome( welcome *w ); 103 105 int clientID_; // this is the id the server gave to us -
code/trunk/src/network/ClientConnection.cc
r1505 r1534 57 57 server=NULL; 58 58 enet_address_set_host(&serverAddress, address.c_str()); 59 serverAddress.port = NETWORK_PORT;59 serverAddress.port = port; 60 60 established=false; 61 61 } … … 65 65 server=NULL; 66 66 enet_address_set_host(&serverAddress, address); 67 serverAddress.port = NETWORK_PORT;67 serverAddress.port = port; 68 68 established=false; 69 69 } … … 107 107 //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); 108 108 // wait 10 seconds for the connection to be established 109 return waitEstablished( 3000);109 return waitEstablished(NETWORK_CLIENT_CONNECT_TIMEOUT); 110 110 } 111 111 … … 169 169 { 170 170 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 171 if(enet_host_service(client, event, NETWORK_CLIENT_ TIMEOUT)<0){171 if(enet_host_service(client, event, NETWORK_CLIENT_WAIT_TIME)<0){ 172 172 // we should never reach this point 173 173 quit=true; … … 211 211 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 212 212 enet_peer_disconnect(server, 0); 213 while(enet_host_service(client, &event, NETWORK_CLIENT_ TIMEOUT) > 0){213 while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME) > 0){ 214 214 switch (event.type) 215 215 { … … 238 238 } 239 239 // handshake 240 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){241 established=true;242 returntrue;243 }244 else {245 COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;246 return false;247 }240 while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit){ 241 if( event.type == ENET_EVENT_TYPE_CONNECT ){ 242 established=true; 243 return true; 244 } 245 } 246 COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl; 247 return false; 248 248 } 249 249 -
code/trunk/src/network/ClientConnection.h
r1505 r1534 54 54 #define NETWORK_PORT 55556 55 55 #define NETWORK_CLIENT_MAX_CONNECTIONS 5 56 #define NETWORK_CLIENT_TIMEOUT 1 56 #define NETWORK_CLIENT_WAIT_TIME 1 57 #define NETWORK_CLIENT_CONNECT_TIMEOUT 3000 // miliseconds 57 58 #define NETWORK_CLIENT_CHANNELS 2 58 59 -
code/trunk/src/network/ClientInformation.cc
r1505 r1534 191 191 failures_=0; 192 192 } 193 194 enet_uint32 ClientInformation::getRTT(){ 195 return peer_->roundTripTime; 196 } 197 198 enet_uint32 ClientInformation::getPacketLoss(){ 199 return peer_->packetLoss; 200 } 193 201 194 202 int ClientInformation::getGamestateID() { -
code/trunk/src/network/ClientInformation.h
r1505 r1534 87 87 void addFailure(); 88 88 void resetFailures(); 89 enet_uint32 getRTT(); 90 enet_uint32 getPacketLoss(); 89 91 90 92 bool removeClient(int clientID); -
code/trunk/src/network/ConnectionManager.cc
r1505 r1534 168 168 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 169 169 for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ 170 if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0) 170 COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl; 171 if(enet_peer_send(i->getPeer(), 0, packet)!=0) 171 172 return false; 172 173 } -
code/trunk/src/network/GameStateClient.cc
r1505 r1534 82 82 if (loadSnapshot(gs)){ 83 83 gameStateMap.insert(std::pair<int, GameState*>(gs->id, gs)); 84 COUT( 4) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl;84 COUT(5) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl; 85 85 last_diff_=gs->base_id; 86 86 //last_gamestate_=gs->id; … … 241 241 size+=it->getSize(); // size of the actual data of the synchronisable 242 242 size+=3*sizeof(int); // size of datasize, classID and objectID 243 COUT( 4) << "getpartialsnapshot: size: " << size << std::endl;243 COUT(5) << "getpartialsnapshot: size: " << size << std::endl; 244 244 } 245 245 //retval->data = (unsigned char*)malloc(size); … … 380 380 //std::cout << "length " << length << std::endl; 381 381 switch ( retval ) { 382 case Z_OK: COUT( 4) << "successfully decompressed" << std::endl; break;382 case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; 383 383 case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL; 384 384 case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL; … … 399 399 400 400 GameState *GameStateClient::decode(GameState *old, GameStateCompressed *diff) { 401 COUT( 4) << "using diffed gamestate" << std::endl;401 COUT(5) << "using diffed gamestate" << std::endl; 402 402 GameState *t = decode(diff); 403 403 if(!t) -
code/trunk/src/network/GameStateManager.cc
r1505 r1534 44 44 #include <iostream> 45 45 #include <zlib.h> 46 #include <assert.h> 46 47 47 48 #include "core/CoreIncludes.h" … … 106 107 break; 107 108 if( (*it).second <= 0 ){ 108 COUT( 4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;109 COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl; 109 110 std::map<int, GameState *>::iterator tempit = gameStateMap.find((*it).first); 110 111 if( tempit != gameStateMap.end() ){ … … 140 141 client = it->second; 141 142 GameState *server = reference; 142 COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl; 143 //COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl; 144 COUT(4) << "client: " << (client!=0 ? client->id : (int)client) << " server: " << server->id << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl; 143 145 if(client) 144 146 return encode(client, server); … … 267 269 sync.data = data; 268 270 data+=sync.length; 269 COUT( 4) << "objectID: " << sync.objectID << " classID: " << sync.classID << std::endl;271 COUT(5) << "objectID: " << sync.objectID << " classID: " << sync.classID << std::endl; 270 272 while(it && it->objectID!=sync.objectID) 271 273 ++it; … … 319 321 320 322 GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) { 321 COUT( 4) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;323 COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl; 322 324 GameState *r = diff(a,b); 323 325 GameStateCompressed *c = compress_(r); … … 472 474 if(gamestateID == GAMESTATEID_INITIAL){ 473 475 temp->setGameStateID(GAMESTATEID_INITIAL); 474 if(curid!=GAMESTATEID_INITIAL) 476 if(curid!=GAMESTATEID_INITIAL){ 477 assert(gameStateUsed.find(curid)!=gameStateUsed.end()); 475 478 --(gameStateUsed.find(curid)->second); 479 } 476 480 return; 477 481 } … … 483 487 // deleteUnusedGameState(curid); 484 488 //increase gamestateused 485 if(curid!=GAMESTATEID_INITIAL) 486 --(gameStateUsed.find(curid)->second); 487 ++(gameStateUsed.find(gamestateID)->second); 488 temp->setGameStateID(gamestateID); 489 std::map<int, int>::iterator it = gameStateUsed.find(curid); 490 if(curid!=GAMESTATEID_INITIAL){ 491 if(it!=gameStateUsed.end()) 492 --(it->second); 493 } 494 it = gameStateUsed.find(gamestateID); 495 if(it!=gameStateUsed.end()){ 496 ++(it->second); 497 temp->setGameStateID(gamestateID); 498 } 489 499 /* 490 500 GameState *old = clientGameState[clientID]; -
code/trunk/src/network/PacketDecoder.cc
r1505 r1534 151 151 void PacketDecoder::gstate( ENetPacket* packet, int clientID ) 152 152 { 153 if(!testAndRemoveCRC(packet)){154 155 return;156 }153 // if(!testAndRemoveCRC(packet)){ 154 // COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl; 155 // return; 156 // } 157 157 GameStateCompressed* currentState = NULL; 158 158 currentState = new GameStateCompressed; -
code/trunk/src/network/PacketGenerator.cc
r1505 r1534 144 144 COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl; 145 145 //delete[] data; 146 if(!addCRC(packet))147 COUT(3) << "could not add crc to gamestate packet" << std::endl;146 // if(!addCRC(packet)) 147 // COUT(3) << "could not add crc to gamestate packet" << std::endl; 148 148 return packet; 149 149 } -
code/trunk/src/network/Server.cc
r1505 r1534 51 51 #include "util/Sleep.h" 52 52 #include "objects/SpaceShip.h" 53 53 #include "core/ConsoleCommand.h" 54 54 55 55 namespace network 56 56 { 57 58 59 #define MAX_FAILURES 20; 60 #define NETWORK_FREQUENCY 30 57 #define MAX_FAILURES 20; 58 #define NETWORK_FREQUENCY 30 59 60 Server *Server::instance_=0; 61 62 Server *Server::createSingleton(){ 63 if(!instance_) 64 instance_ = new Server(); 65 return instance_; 66 } 67 Server *Server::createSingleton(int port){ 68 if(!instance_) 69 instance_ = new Server(port); 70 return instance_; 71 } 72 Server *Server::createSingleton(int port, std::string bindAddress){ 73 if(!instance_) 74 instance_ = new Server(port, bindAddress); 75 return instance_; 76 } 77 Server *Server::createSingleton(int port, const char *bindAddress){ 78 if(!instance_) 79 instance_ = new Server(port, bindAddress); 80 return instance_; 81 } 82 83 Server *Server::getSingleton(){ 84 return instance_; 85 } 86 61 87 62 88 /** … … 127 153 * @return true/false 128 154 */ 129 bool Server::sendMSG(std::string msg) { 130 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 131 //std::cout <<"adding packets" << std::endl; 132 return connection->addPacketAll(packet); 155 bool Server::sendChat(std::string msg) { 156 return sendChat(msg.c_str()); 133 157 } 134 158 … … 138 162 * @return true/false 139 163 */ 140 bool Server::sendMSG(const char *msg) { 141 ENetPacket *packet = packet_gen.chatMessage(msg); 142 COUT(4) <<"Server: adding Packets" << std::endl; 164 bool Server::sendChat(const char *msg) { 165 char *message = new char [strlen(msg)+10+1]; 166 sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg); 167 COUT(1) << message << std::endl; 168 ENetPacket *packet = packet_gen.chatMessage(message); 169 COUT(5) <<"Server: adding Packets" << std::endl; 143 170 return connection->addPacketAll(packet); 144 171 } … … 202 229 void Server::updateGamestate() { 203 230 gamestates->update(); 204 COUT( 4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;231 COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; 205 232 //std::cout << "updated gamestate, sending it" << std::endl; 206 233 //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) 207 234 sendGameState(); 208 COUT( 4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;235 COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; 209 236 //std::cout << "sent gamestate" << std::endl; 210 237 } … … 229 256 continue; 230 257 } 258 COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; 231 259 COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; 232 260 int gid = temp->getGamestateID(); //get gamestate id … … 287 315 } 288 316 317 void Server::processChat( chat *data, int clientId){ 318 char *message = new char [strlen(data->message)+10+1]; 319 sprintf(message, "Player %d: %s", clientId, data->message); 320 COUT(1) << message << std::endl; 321 ENetPacket *pck = packet_gen.chatMessage(message); 322 connection->addPacketAll(pck); 323 delete[] data->message; 324 delete data; 325 } 326 289 327 bool Server::addClient(ENetEvent *event){ 290 328 ClientInformation *temp = clients->insertBack(new ClientInformation); … … 312 350 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 313 351 connection->syncClassid(temp->getID()); 314 COUT( 4) << "creating spaceship for clientid: " << temp->getID() << std::endl;352 COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl; 315 353 // TODO: this is only a hack, untill we have a possibility to define default player-join actions 316 354 if(!createShip(temp)) -
code/trunk/src/network/Server.h
r1505 r1534 53 53 namespace network 54 54 { 55 #define CLIENTID_SERVER 0 56 55 57 /** 56 58 * This class is the root class of the network module for a server. … … 59 61 class _NetworkExport Server : public PacketDecoder, public orxonox::Tickable{ 60 62 public: 61 Server(); 62 Server(int port); 63 Server(int port, std::string bindAddress); 64 Server(int port, const char *bindAddress); 63 static Server *createSingleton(); 64 static Server *createSingleton(int port); 65 static Server *createSingleton(int port, std::string bindAddress); 66 static Server *createSingleton(int port, const char *bindAddress); 67 68 static Server *getSingleton(); 69 65 70 void open(); 66 71 void close(); 67 bool send MSG(std::string msg);68 bool send MSG(const char *msg);72 bool sendChat(std::string msg); 73 bool sendChat(const char *msg); 69 74 void tick(float time); 70 75 protected: … … 72 77 void updateGamestate(); 73 78 private: 79 Server(); 80 Server(int port); 81 Server(int port, std::string bindAddress); 82 Server(int port, const char *bindAddress); 83 84 static Server *instance_; 85 74 86 bool addClient(ENetEvent *event); 75 87 bool createClient(int clientID); … … 82 94 bool processConnectRequest( connectRequest *con, int clientID ); 83 95 void processGamestate( GameStateCompressed *data, int clientID); 96 void processChat( chat *data, int clientId); 84 97 ConnectionManager *connection; 85 98 GameStateManager *gamestates; -
code/trunk/src/network/Synchronisable.cc
r1505 r1534 45 45 46 46 #include "core/CoreIncludes.h" 47 // #include "core/Identifier.h" 47 48 48 49 namespace network … … 66 67 67 68 Synchronisable::~Synchronisable(){ 69 // delete callback function objects 70 if(!orxonox::Identifier::isCreatingHierarchy()) 71 for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++) 72 delete (*it)->callback; 68 73 } 69 74 … … 87 92 * @param size size of the datatype the variable consists of 88 93 */ 89 void Synchronisable::registerVar(void *var, int size, variableType t, int mode ){94 void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){ 90 95 // create temporary synch.Var struct 91 96 synchronisableVariable *temp = new synchronisableVariable; … … 94 99 temp->mode = mode; 95 100 temp->type = t; 101 temp->callback = cb; 96 102 COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; 97 103 // increase datasize … … 218 224 } 219 225 COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; 226 bool callback=false; 220 227 switch((*i)->type){ 221 228 case DATA: 229 if((*i)->callback) // check whether this variable changed (but only if callback was set) 230 if(strncmp((char *)(*i)->var, (char *)data, (*i)->size)!=0) 231 callback=true; 222 232 memcpy((void*)(*i)->var, data, (*i)->size); 223 233 data+=(*i)->size; … … 227 237 COUT(5) << "string size: " << (*i)->size << std::endl; 228 238 data+=sizeof(int); 239 if((*i)->callback) // check whether this string changed 240 if( *(std::string *)((*i)->var) != std::string((char *)data) ) 241 callback=true; 229 242 *((std::string *)((*i)->var)) = std::string((const char*)data); 230 243 COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl; … … 232 245 break; 233 246 } 247 // call the callback function, if defined 248 if(callback) 249 (*i)->callback->call(); 234 250 } 235 251 return true; -
code/trunk/src/network/Synchronisable.h
r1505 r1534 27 27 */ 28 28 29 //30 // C++ Interface: synchronisable31 //32 // Description:33 //34 //35 // Author: Oliver Scheuss, (C) 200736 //37 // Copyright: See COPYING file that comes with this distribution38 //39 //40 29 #ifndef _Synchronisable_H__ 41 30 #define _Synchronisable_H__ … … 45 34 #include <list> 46 35 #include "core/OrxonoxClass.h" 36 #include "NetworkCallback.h" 47 37 48 38 namespace network … … 65 55 void *var; 66 56 variableType type; 57 NetworkCallbackBase *callback; 67 58 }SYNCVAR; 68 59 … … 81 72 int classID; 82 73 83 void registerVar(void *var, int size, variableType t, int mode=1 );74 void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0); 84 75 // syncData getData(); 85 76 syncData getData(unsigned char *mem, int mode=0x0);
Note: See TracChangeset
for help on using the changeset viewer.