Changeset 1360 for code/trunk/src/network
- Timestamp:
- May 22, 2008, 2:02:06 PM (17 years ago)
- Location:
- code/trunk/src/network
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Client.cc
r1293 r1360 120 120 isConnected=client_connection.createConnection(); 121 121 if(isConnected){ 122 COUT( 4) << "sending connectrequest" << std::endl;123 client_connection.addPacket(pck_gen.generateConnectRequest());124 client_connection.sendPackets();122 COUT(3) << "sending connectrequest" << std::endl; 123 if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets()) 124 COUT(1) << "could not create connection" << std::endl; 125 125 }else 126 126 COUT(1) << "could not create connection" << std::endl; … … 229 229 */ 230 230 void Client::tick(float time){ 231 // COUT(3) << "."; 231 232 if(client_connection.isConnected() && isSynched_){ 232 233 COUT(4) << "popping partial gamestate: " << std::endl; … … 260 261 if(!isSynched_) 261 262 isSynched_=true; 262 client_connection.addPacket(pck_gen.acknowledgement(id)); 263 if(!client_connection.addPacket(pck_gen.acknowledgement(id))) 264 return; 263 265 // we do this at the end of a tick 264 265 266 if(!client_connection.sendPackets()) 267 COUT(2) << "Could not send acknowledgment" << std::endl; 266 268 } 267 269 } -
code/trunk/src/network/ClientInformation.cc
r1293 r1360 45 45 namespace network 46 46 { 47 boost::recursive_mutex ClientInformation::mutex_; 48 47 49 ClientInformation::ClientInformation() { 48 50 gamestateID_=GAMESTATEID_INITIAL; 49 51 preve=0; 50 52 nexte=0; 51 this->head=false; 53 partialGamestateID_=GAMESTATEID_INITIAL-1; 54 this->head_=false; 52 55 synched_=false; 53 56 } … … 57 60 preve=0; 58 61 nexte=0; 59 this->head=head; 62 partialGamestateID_=GAMESTATEID_INITIAL-1; 63 this->head_=head; 60 64 synched_=false; 61 65 } … … 80 84 81 85 ClientInformation::~ClientInformation() { 86 boost::recursive_mutex::scoped_lock lock(mutex_); 82 87 if(preve!=0) 83 88 preve->setNext(this->nexte); … … 87 92 88 93 ClientInformation *ClientInformation::next() { 94 boost::recursive_mutex::scoped_lock lock(mutex_); 89 95 if(this!=0) 90 96 return this->nexte; … … 93 99 } 94 100 ClientInformation *ClientInformation::prev() { 101 boost::recursive_mutex::scoped_lock lock(mutex_); 95 102 if(this!=0) 96 103 return this->preve; … … 100 107 101 108 bool ClientInformation::setPrev(ClientInformation *prev) { 102 if(!head) 109 boost::recursive_mutex::scoped_lock lock(mutex_); 110 if(!head_) 103 111 this->preve = prev; 104 112 else … … 108 116 109 117 bool ClientInformation::setNext(ClientInformation *next) { 118 boost::recursive_mutex::scoped_lock lock(mutex_); 110 119 this->nexte = next; 111 120 return true; … … 113 122 114 123 ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) { 124 boost::recursive_mutex::scoped_lock lock(mutex_); 115 125 this->nexte->setPrev(ins); 116 126 ins->setNext(this->nexte); … … 121 131 122 132 ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ 133 boost::recursive_mutex::scoped_lock lock(mutex_); 134 if(!this) 135 return NULL; 123 136 this->prev()->setNext(ins); 124 137 ins->setPrev(this->preve); … … 129 142 130 143 void ClientInformation::setID(int clientID){ 144 boost::recursive_mutex::scoped_lock lock(mutex_); 131 145 clientID_ = clientID; 132 146 } 133 147 134 void ClientInformation::setPeer(ENetPeer *peer){ 148 bool ClientInformation::setPeer(ENetPeer *peer){ 149 boost::recursive_mutex::scoped_lock lock(mutex_); 150 if(!this) 151 return false; 135 152 peer_ = peer; 136 } 137 138 void ClientInformation::setGamestateID(int id){ 153 return true; 154 } 155 156 bool ClientInformation::setGamestateID(int id){ 157 boost::recursive_mutex::scoped_lock lock(mutex_); 158 if(!this) 159 return false; 139 160 gamestateID_=id; 161 return true; 162 } 163 164 bool ClientInformation::setPartialGamestateID(int id){ 165 boost::recursive_mutex::scoped_lock lock(mutex_); 166 if(!this) 167 return false; 168 partialGamestateID_=id; 169 return true; 140 170 } 141 171 142 172 int ClientInformation::getID() { 143 return clientID_; 173 boost::recursive_mutex::scoped_lock lock(mutex_); 174 if(!this) 175 return CLIENTID_UNKNOWN; 176 else 177 return clientID_; 144 178 } 145 179 146 180 ENetPeer *ClientInformation::getPeer() { 147 return peer_; 181 boost::recursive_mutex::scoped_lock lock(mutex_); 182 if(this) 183 return peer_; 184 else 185 return NULL; 186 } 187 188 bool ClientInformation::getHead(){ 189 boost::recursive_mutex::scoped_lock lock(mutex_); 190 return head_; 191 } 192 193 void ClientInformation::setHead(bool h){ 194 boost::recursive_mutex::scoped_lock lock(mutex_); 195 head_=h; 196 } 197 198 int ClientInformation::getFailures(){ 199 boost::recursive_mutex::scoped_lock lock(mutex_); 200 return failures_; 201 } 202 void ClientInformation::addFailure(){ 203 boost::recursive_mutex::scoped_lock lock(mutex_); 204 failures_++; 205 } 206 void ClientInformation::resetFailures(){ 207 boost::recursive_mutex::scoped_lock lock(mutex_); 208 failures_=0; 148 209 } 149 210 150 211 int ClientInformation::getGamestateID() { 151 return gamestateID_; 212 boost::recursive_mutex::scoped_lock lock(mutex_); 213 if(this) 214 return gamestateID_; 215 else 216 return -1; 217 } 218 219 int ClientInformation::getPartialGamestateID() { 220 boost::recursive_mutex::scoped_lock lock(mutex_); 221 if(this) 222 return partialGamestateID_; 223 else 224 return -1; 152 225 } 153 226 154 227 ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { 228 boost::recursive_mutex::scoped_lock lock(mutex_); 229 if(!this) 230 return NULL; 155 231 ClientInformation *temp = this; 156 232 while(temp->next()!=0){ … … 163 239 164 240 bool ClientInformation::removeClient(int clientID) { 241 boost::recursive_mutex::scoped_lock lock(mutex_); 242 if(!this || clientID==CLIENTID_UNKNOWN) 243 return false; 165 244 ClientInformation *temp = this; 166 245 while(temp!=0 && temp->getID()!=clientID) … … 173 252 174 253 bool ClientInformation::removeClient(ENetPeer *peer) { 254 boost::recursive_mutex::scoped_lock lock(mutex_); 255 if(!this || !peer) 256 return false; 175 257 ClientInformation *temp = this; 176 258 while(temp!=0){ 177 if(!temp->head )259 if(!temp->head_) 178 260 if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) 179 261 break; … … 193 275 */ 194 276 ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { 195 ClientInformation *temp = this; 196 if (temp->head) 277 boost::recursive_mutex::scoped_lock lock(mutex_); 278 ClientInformation *temp = this; 279 if (temp->head_) 197 280 temp=temp->next(); 198 //bugfix: temp to temp->next(), get last elem if not found, not segflt 199 while(temp->next()!=0 && temp->getID()!=clientID){ 281 while(temp!=0 && temp->getID()!=clientID){ 200 282 temp = temp->next(); 201 283 } … … 211 293 */ 212 294 ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { 213 ClientInformation *temp = this;214 //bugfix: temp to temp->next(), get last elem if not found, not segflt215 while(temp ->next()!=0){216 if(temp->head ){295 boost::recursive_mutex::scoped_lock lock(mutex_); 296 ClientInformation *temp = this; 297 while(temp!=0){ 298 if(temp->head_){ 217 299 temp = temp->next(); 218 300 continue; … … 226 308 } 227 309 228 void ClientInformation::setSynched(bool s) { 310 bool ClientInformation::setSynched(bool s) { 311 boost::recursive_mutex::scoped_lock lock(mutex_); 312 if(!this) 313 return false; 229 314 synched_=s; 315 return true; 230 316 } 231 317 232 318 bool ClientInformation::getSynched() { 233 return synched_; 319 boost::recursive_mutex::scoped_lock lock(mutex_); 320 if(this) 321 return synched_; 322 else 323 return false; 234 324 } 235 325 -
code/trunk/src/network/ClientInformation.h
r1293 r1360 44 44 45 45 #include <enet/enet.h> 46 #include <boost/thread/recursive_mutex.hpp> 46 47 47 48 #define GAMESTATEID_INITIAL -1 49 #define CLIENTID_UNKNOWN -2 48 50 49 51 namespace network … … 62 64 ClientInformation *next(); 63 65 ClientInformation *prev(); 64 bool setNext(ClientInformation *next);65 bool setPrev(ClientInformation *prev);66 ClientInformation *insertAfter(ClientInformation *ins);67 ClientInformation *insertBefore(ClientInformation *ins);68 66 ClientInformation *insertBack(ClientInformation *ins); 69 67 70 68 // set functions 71 69 void setID(int clientID); 72 void setPeer(ENetPeer *peer); 73 void setGamestateID(int id); 70 bool setPeer(ENetPeer *peer); 71 bool setGamestateID(int id); 72 bool setPartialGamestateID(int id); 74 73 inline void setShipID(int id){ShipID_=id;} 75 74 … … 78 77 int getID(); 79 78 int getGamestateID(); 79 int getPartialGamestateID(); 80 80 ENetPeer *getPeer(); 81 bool getHead(); 82 void setHead(bool h); 81 83 84 int getFailures(); 85 void addFailure(); 86 void resetFailures(); 82 87 83 88 bool removeClient(int clientID); … … 88 93 ClientInformation *findClient(ENetAddress *address, bool look_backwards=false); 89 94 90 voidsetSynched(bool s);95 bool setSynched(bool s); 91 96 bool getSynched(); 92 97 93 bool head;94 unsigned short failures_;95 98 96 private: 99 private: 100 bool setNext(ClientInformation *next); 101 bool setPrev(ClientInformation *prev); 102 ClientInformation *insertAfter(ClientInformation *ins); 103 ClientInformation *insertBefore(ClientInformation *ins); 104 97 105 ClientInformation *preve; 98 106 ClientInformation *nexte; … … 101 109 int clientID_; 102 110 int gamestateID_; 111 int partialGamestateID_; 103 112 int ShipID_; // this is the unique objectID 104 113 bool synched_; 114 bool head_; 115 unsigned short failures_; 116 static boost::recursive_mutex mutex_; 117 105 118 }; 106 119 -
code/trunk/src/network/ConnectionManager.cc
r1293 r1360 101 101 ENetPacket *packet=getPacket(address); 102 102 ClientInformation *temp =head_->findClient(&address); 103 if(!temp) 104 return NULL; 103 105 clientID=temp->getID(); 104 106 return packet; … … 124 126 125 127 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { 126 if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0) 128 ClientInformation *temp = head_->findClient(&(peer->address)); 129 if(!temp) 130 return false; 131 if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0) 127 132 return false; 128 133 return true; … … 130 135 131 136 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { 132 if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0) 137 ClientInformation *temp = head_->findClient(clientID); 138 if(!temp) 139 return false; 140 if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0) 133 141 return false; 134 142 return true; … … 185 193 addClient(event); 186 194 //this is a workaround to ensure thread safety 187 /*if(!addFakeConnectRequest(&event))188 COUT(3) << "Problem pushing fakeconnectRequest to queue" << std::endl;*/189 195 COUT(5) << "Con.Man: connection event has occured" << std::endl; 190 196 break; … … 195 201 if(head_->findClient(&event->peer->address)) 196 202 processData(event); 203 else 204 COUT(3) << "received a packet from a client we don't know" << std::endl; 197 205 break; 198 206 case ENET_EVENT_TYPE_DISCONNECT: … … 200 208 break; 201 209 case ENET_EVENT_TYPE_NONE: 210 receiverThread_->yield(); 202 211 break; 203 212 } … … 233 242 case ENET_EVENT_TYPE_DISCONNECT: 234 243 COUT(4) << "disconnecting all clients" << std::endl; 235 delete head_->findClient(&(event.peer->address)); 244 if(head_->findClient(&(event.peer->address))) 245 delete head_->findClient(&(event.peer->address)); 236 246 //maybe needs bugfix: might also be a reason for the server to crash 237 247 temp = temp->next(); … … 259 269 bool ConnectionManager::addClient(ENetEvent *event) { 260 270 ClientInformation *temp = head_->insertBack(new ClientInformation); 261 if(temp->prev()->head) { //not good if you use anything else than insertBack 271 if(!temp){ 272 COUT(2) << "Conn.Man. could not add client" << std::endl; 273 return false; 274 } 275 if(temp->prev()->getHead()) { //not good if you use anything else than insertBack 262 276 temp->prev()->setID(0); //bugfix: not necessary but usefull 263 277 temp->setID(1); … … 266 280 temp->setID(temp->prev()->getID()+1); 267 281 temp->setPeer(event->peer); 268 COUT( 4) << "Con.Man: added client id: " << temp->getID() << std::endl;282 COUT(3) << "Con.Man: added client id: " << temp->getID() << std::endl; 269 283 return true; 270 284 } … … 283 297 284 298 void ConnectionManager::syncClassid(int clientID) { 285 unsigned int network_id=0 ;299 unsigned int network_id=0, failures=0; 286 300 std::string classname; 287 301 orxonox::Identifier *id; … … 293 307 classname = id->getName(); 294 308 network_id = id->getNetworkID(); 309 if(network_id==0) 310 COUT(3) << "we got a null class id: " << id->getName() << std::endl; 295 311 COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; 296 312 297 addPacket(packet_gen.clid( (int)network_id, classname ), clientID); 298 313 while(!addPacket(packet_gen.clid( (int)network_id, classname ), clientID) && failures < 10){ 314 failures++; 315 } 299 316 ++it; 300 317 } … … 305 322 bool ConnectionManager::createClient(int clientID){ 306 323 ClientInformation *temp = head_->findClient(clientID); 324 if(!temp){ 325 COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; 326 return false; 327 } 307 328 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 308 329 syncClassid(temp->getID()); 309 330 COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; 310 331 // TODO: this is only a hack, untill we have a possibility to define default player-join actions 311 createShip(temp); 312 COUT(4) << "created spaceship" << std::endl; 332 if(!createShip(temp)) 333 COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; 334 else 335 COUT(3) << "created spaceship" << std::endl; 313 336 temp->setSynched(true); 314 COUT( 4) << "sending welcome" << std::endl;337 COUT(3) << "sending welcome" << std::endl; 315 338 sendWelcome(temp->getID(), temp->getShipID(), true); 316 339 return true; … … 319 342 bool ConnectionManager::removeClient(int clientID){ 320 343 orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); 344 ClientInformation *client = head_->findClient(clientID); 345 if(!client) 346 return false; 321 347 while(it){ 322 if(it->objectID!= head_->findClient(clientID)->getShipID()){348 if(it->objectID!=client->getShipID()){ 323 349 ++it; 324 350 continue; … … 333 359 334 360 bool ConnectionManager::createShip(ClientInformation *client){ 361 if(!client) 362 return false; 335 363 orxonox::Identifier* id = ID("SpaceShip"); 336 364 if(!id){ … … 351 379 no->setRotDamp(1.0); 352 380 no->setCamera("cam_"+client->getID()); 381 no->classID = id->getNetworkID(); 353 382 no->create(); 354 383 … … 369 398 370 399 bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){ 371 addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID); 372 sendPackets(); 373 return true; 400 if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){ 401 sendPackets(); 402 return true; 403 }else 404 return false; 374 405 } 375 406 -
code/trunk/src/network/GameStateClient.cc
r1293 r1360 47 47 COUT(5) << "this: " << this << std::endl; 48 48 last_diff_=0; 49 last_gamestate_=GAMESTATEID_INITIAL-1; 49 50 } 50 51 … … 56 57 printGameStateMap(); 57 58 GameState *gs, *reference; 59 /*if(compstate->id<last_gamestate_){ 60 // network packets got messed up 61 COUT(3) << "received an obsolete gamestate" << std::endl; 62 return false; 63 }*/ 58 64 if(compstate->diffed && compstate->base_id!=GAMESTATEID_INITIAL){ 59 65 std::map<int, GameState*>::iterator it = gameStateMap.find(compstate->base_id); … … 77 83 COUT(4) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl; 78 84 last_diff_=gs->base_id; 85 //last_gamestate_=gs->id; 79 86 return true; 80 87 }else{ … … 145 152 if(!id){ 146 153 COUT(4) << "We could not identify a new object; classid: " << sync.classID << std::endl; 147 continue;154 return false; 148 155 } 149 156 Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate()); 150 157 COUT(4) << "loadsnapshot: classid: " << sync.classID << " objectID: " << sync.objectID << " length: " << sync.length << std::endl; 158 if(!no){ 159 COUT(2) << "coudl not frabricate classid: " << sync.classID << " objectID: " << sync.objectID << " identifier: " << id << std::endl; 160 break; 161 } 151 162 no->objectID=sync.objectID; 152 163 no->classID=sync.classID; 153 164 // update data and create object/entity... 154 if( !no->updateData(sync) ) 165 if( !no->updateData(sync) ){ 155 166 COUT(1) << "We couldn't update the object: " << sync.objectID << std::endl; 167 return false; 168 } 156 169 if( !no->create() ) 157 170 COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl; … … 349 362 COUT(4) << "using diffed gamestate" << std::endl; 350 363 GameState *t = decode(diff); 364 if(!t) 365 return NULL; 351 366 GameState *r = undiff(old, t); 352 367 delete[] t->data; -
code/trunk/src/network/GameStateClient.h
r1293 r1360 71 71 72 72 int last_diff_; 73 int last_gamestate_; 73 74 std::map<int, GameState *> gameStateMap; 74 75 -
code/trunk/src/network/GameStateManager.cc
r1293 r1360 117 117 client = it->second; 118 118 GameState *server = reference; 119 //head_->findClient(clientID)->setGamestateID(id);120 119 COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl; 121 120 if(client) … … 127 126 GameState *server = reference; 128 127 // ackGameState(clientID, reference->id); 129 //head_->findClient(clientID)->setGamestateID(id);130 128 return encode(server); 131 129 // return an undiffed gamestate and set appropriate flags … … 223 221 orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start(); 224 222 syncData sync; 223 /*ClientInformation *client = head_->findClient(clientID); 224 if(client) 225 if(client->getPartialGamestateID()>state->id){ 226 COUT(3) << "we received an obsolete partial gamestate" << std::endl; 227 return false; 228 } 229 else;*/ 230 //what should we do now ?? 225 231 // loop as long as we have some data ;) 226 232 while(data < state->data+state->size){ … … 270 276 ++it; 271 277 } 272 278 //client->setPartialGamestateID(state->id); 273 279 return true; 274 280 } … … 427 433 void GameStateManager::ackGameState(int clientID, int gamestateID) { 428 434 ClientInformation *temp = head_->findClient(clientID); 435 if(temp==0) 436 return; 429 437 int curid = temp->getGamestateID(); 438 if(curid > gamestateID) 439 // the network packets got messed up 440 return; 430 441 COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; 431 442 // decrease usage of gamestate and save it 432 443 // deleteUnusedGameState(curid); 433 444 //increase gamestateused 434 --(gameStateUsed.find(curid)->second); 445 if(curid!=GAMESTATEID_INITIAL) 446 --(gameStateUsed.find(curid)->second); 435 447 ++(gameStateUsed.find(gamestateID)->second); 436 448 temp->setGamestateID(gamestateID); … … 458 470 459 471 void GameStateManager::removeClient(ClientInformation* client){ 460 gameStateUsed[client->getGamestateID()]--; 472 if(!client) 473 return; 474 if(client->getGamestateID()>=0) 475 gameStateUsed[client->getGamestateID()]--; 461 476 head_->removeClient(client->getID()); 462 477 } -
code/trunk/src/network/NetworkPrereqs.h
r1293 r1360 56 56 #endif 57 57 58 //----------------------------------------------------------------------- 59 // fixed width integers 60 //----------------------------------------------------------------------- 61 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC 62 typedef __int8 int8_t; 63 typedef __int16 int16_t; 64 typedef __int32 int32_t; 65 typedef __int64 int64_t; 66 typedef unsigned __int8 uint8_t; 67 typedef unsigned __int16 uint16_t; 68 typedef unsigned __int32 uint32_t; 69 typedef unsigned __int64 uint64_t; 70 #else 71 # include "inttypes.h" 72 #endif 58 73 59 74 //----------------------------------------------------------------------- -
code/trunk/src/network/PacketBuffer.cc
r1293 r1360 41 41 namespace network 42 42 { 43 boost::mutex networkPacketBufferMutex;43 boost::recursive_mutex PacketBuffer::mutex_; 44 44 45 45 PacketBuffer::PacketBuffer() { … … 52 52 53 53 bool PacketBuffer::push(ENetEvent *ev) { 54 boost:: mutex::scoped_lock lock(networkPacketBufferMutex);54 boost::recursive_mutex::scoped_lock lock(mutex_); 55 55 //std::cout << "event size inside packetbuffer " << ev->packet->dataLength << std::endl; 56 56 // if(closed) … … 61 61 last=first; 62 62 last->next=NULL; 63 // change this!!!!!!! 63 // change this!!!!!!! ---- we are not doing stl so we won't change this 64 64 last->packet = ev->packet; 65 last->address = ev->peer->address; 65 66 //last->address = ev->peer->address; 66 67 } else { … … 72 73 // save the packet to the new element 73 74 last->packet = ev->packet; 75 last->address = ev->peer->address; 74 76 //last->address = ev->peer->address; 75 77 } 76 // pseudo bugfix: added a return false statement for error handling 77 if ( last->packet == ev->packet ) return true; 78 return false; 78 lock.unlock(); 79 return true; 79 80 } 80 81 … … 82 83 //moving first pointer to next element 83 84 ENetPacket *PacketBuffer::pop() { 84 boost::mutex::scoped_lock lock(networkPacketBufferMutex); 85 //std::cout << "packetbuffer pop" << std::endl; 86 if(first!=NULL /*&& !closed*/){ 87 QueueItem *temp = first; 88 // get packet 89 ENetPacket *pck=first->packet; 90 // remove first element 91 first = first->next; 92 delete temp; 93 //std::cout << "pop size of packet " << pck->dataLength << std::endl; 94 return pck; 95 } else{ 96 //std::cout << "nothing to return" << std::endl; 97 return NULL; 98 } 85 ENetAddress address; 86 return pop(address); 99 87 } 100 88 101 89 ENetPacket *PacketBuffer::pop(ENetAddress &address) { 102 boost:: mutex::scoped_lock lock(networkPacketBufferMutex);90 boost::recursive_mutex::scoped_lock lock(mutex_); 103 91 //std::cout << "packetbuffer pop(address)" << std::endl; 104 92 if(first!=NULL /*&& !closed*/){ … … 110 98 first = first->next; 111 99 delete temp; 100 lock.unlock(); 112 101 //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; 113 102 return pck; 114 103 } else{ 104 lock.unlock(); 115 105 return NULL; 116 106 } -
code/trunk/src/network/PacketBuffer.h
r1293 r1360 45 45 46 46 #include <enet/enet.h> 47 #include <boost/thread/recursive_mutex.hpp> 47 48 48 49 namespace network … … 76 77 QueueItem *last; 77 78 bool closed; 78 79 static boost::recursive_mutex mutex_; 79 80 }; 80 81 -
code/trunk/src/network/PacketDecoder.cc
r1293 r1360 91 91 } 92 92 93 bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){ 94 uint32_t submittetcrc; 95 int dataLength = packet->dataLength; 96 // get the submittet crc code 97 memcpy(&submittetcrc, &packet->data[dataLength-sizeof(uint32_t)], sizeof(uint32_t)); 98 unsigned char *data = packet->data; 99 uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t)); 100 // now append the crc to the packet data 101 if(crc32==submittetcrc){ 102 dataLength-=sizeof(uint32_t); 103 enet_packet_resize(packet, dataLength); 104 return true; 105 } 106 COUT(3) << "gamestate crc: " << crc32 << std::endl; 107 COUT(3) << "submitted crc: " << submittetcrc << std::endl; 108 return false; 109 } 110 93 111 // ATTENTION: TODO watch, that arguments we pass over to the processFunction gets deleted in THE PROCESSXXX function 94 112 … … 111 129 void *data = (void *)new unsigned char[length]; 112 130 memcpy(data, (void *)(packet->data+2*sizeof(int)), length); 131 enet_packet_destroy( packet ); 113 132 return true; 114 133 } … … 156 175 void PacketDecoder::gstate( ENetPacket* packet, int clientID ) 157 176 { 177 if(!testAndRemoveCRC(packet)){ 178 COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl; 179 return; 180 } 158 181 GameStateCompressed* currentState = NULL; 159 182 currentState = new GameStateCompressed; -
code/trunk/src/network/PacketGenerator.cc
r1293 r1360 46 46 namespace network 47 47 { 48 void calcCRCBit(uint32_t &crc32, int bit){ 49 int hbit; 50 51 hbit=(crc32 & 0x80000000) ? 1 : 0; 52 if (hbit != bit) 53 crc32=(crc32<<1) ^ NETWORK_CRC32POLY; 54 else 55 crc32=crc32<<1; 56 } 57 58 uint32_t calcCRC(unsigned char *data, unsigned int dataLength){ 59 uint32_t crc32=0; 60 for(unsigned int i=0; i<dataLength; i++){ 61 calcCRCBit(crc32, (data[i]&0x1)>>0); // 1st bit 62 calcCRCBit(crc32, (data[i]&0x2)>>1); // 2nd bit 63 calcCRCBit(crc32, (data[i]&0x3)>>2); // 3rd bit 64 calcCRCBit(crc32, (data[i]&0x4)>>3); // 4th bit 65 calcCRCBit(crc32, (data[i]&0x5)>>4); // 5th bit 66 calcCRCBit(crc32, (data[i]&0x6)>>5); // 6th bit 67 calcCRCBit(crc32, (data[i]&0x7)>>6); // 7th bit 68 calcCRCBit(crc32, (data[i]&0x8)>>7); // 8th bit 69 } 70 return crc32; 71 } 72 48 73 PacketGenerator::PacketGenerator() { } 49 74 … … 64 89 ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE ) 65 90 { 66 void *stream = newchar[dataLength + 2*sizeof(int)];91 unsigned char *stream = new unsigned char[dataLength + 2*sizeof(int)]; 67 92 if(!stream) 68 93 return NULL; … … 135 160 memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) ); 136 161 memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize ); 162 137 163 //create an enet packet with the generated bytestream 138 164 COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl; 139 165 ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); 140 166 delete[] data; 167 if(!addCRC(packet)) 168 COUT(3) << "could not add crc to gamestate packet" << std::endl; 141 169 return packet; 142 170 } … … 174 202 return packet; 175 203 } 204 205 206 bool PacketGenerator::addCRC( ENetPacket *packet){ 207 unsigned char *data = packet->data; 208 uint32_t crc32=calcCRC(data, packet->dataLength); 209 // now append the crc to the packet data 210 int oldlength = packet->dataLength; 211 if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){ 212 memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t)); 213 return true; 214 }else{ 215 COUT(3) << "could not add crc to gamestate" << std::endl; 216 return false; 217 } 218 } 176 219 177 220 } -
code/trunk/src/network/PacketManager.h
r1293 r1360 38 38 39 39 #define CLIENTID_CLIENT -1 40 #define NETWORK_CRC32POLY 0x04C11DB7 /* CRC-32 Polynom */ 40 41 41 42 //enum netowk generally used to set the type ID of a packet … … 48 49 * 49 50 */ 51 //void calcCRC(uint32_t &crc32, int bit); 52 uint32_t calcCRC(unsigned char *data, unsigned int dataLength); 53 50 54 class PacketGenerator 51 55 { … … 53 57 PacketGenerator(); 54 58 //call one of this functions out of an instance of PacketGenerator to create a packet 55 ENetPacket* acknowledgement( int state, int reliable = ENET_PACKET_FLAG_RELIABLE );59 ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability 56 60 ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE ); 57 61 ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE ); 58 62 ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE ); 59 63 ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE ); 60 ENetPacket* gstate( GameStateCompressed *states, int reliable = ENET_PACKET_FLAG_RELIABLE );64 ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates 61 65 ENetPacket* clid( int classid, std::string classname, int reliable = ENET_PACKET_FLAG_RELIABLE ); 62 66 ENetPacket* generateWelcome( int clientID,int shipID, bool allowed, int reliable = ENET_PACKET_FLAG_RELIABLE ); 63 67 ENetPacket* generateConnectRequest( int reliable = ENET_PACKET_FLAG_RELIABLE ); 64 68 private: 69 bool addCRC( ENetPacket *packet); 65 70 }; 66 71 … … 84 89 85 90 private: 86 91 bool testAndRemoveCRC(ENetPacket *packet); 87 92 88 93 -
code/trunk/src/network/Server.cc
r1293 r1360 117 117 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 118 118 //std::cout <<"adding packets" << std::endl; 119 connection->addPacketAll(packet);119 if(connection->addPacketAll(packet)) 120 120 //std::cout <<"added packets" << std::endl; 121 return connection->sendPackets(); 121 return connection->sendPackets(); 122 else 123 return false; 122 124 } 123 125 … … 147 149 processQueue(); 148 150 updateGamestate(); 149 150 151 // usleep(500000); // TODO remove 151 152 return; … … 162 163 //clientID here is a reference to grab clientID from ClientInformation 163 164 packet = connection->getPacket(clientID); 165 if(!packet) 166 continue; 164 167 //if statement to catch case that packetbuffer is empty 165 168 if( !elaborate(packet, clientID) ) 166 COUT( 4) << "Server: PacketBuffer empty" << std::endl;169 COUT(3) << "Server: could not elaborate" << std::endl; 167 170 } 168 171 } … … 189 192 bool added=false; 190 193 while(temp != NULL){ 191 if(temp-> head){194 if(temp->getHead()){ 192 195 temp=temp->next(); 193 196 //think this works without continue … … 211 214 //std::cout << "adding gamestate" << std::endl; 212 215 if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){ 213 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp-> failures_<< std::endl;214 temp-> failures_++;215 if(temp-> failures_> 20 )216 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 217 temp->addFailure(); 218 if(temp->getFailures() > 20 ) 216 219 disconnectClient(temp); 217 220 //std::cout << "added gamestate" << std::endl; … … 238 241 239 242 bool Server::processConnectRequest( connectRequest *con, int clientID ){ 240 COUT( 4) << "processing connectRequest " << std::endl;243 COUT(3) << "processing connectRequest " << std::endl; 241 244 //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID); 242 245 connection->createClient(clientID); … … 250 253 COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl; 251 254 else 252 clients->findClient(clientID)->failures_=0; 255 if(clients->findClient(clientID)) 256 clients->findClient(clientID)->resetFailures(); 253 257 } 254 258 255 259 void Server::disconnectClient(int clientID){ 256 260 ClientInformation *client = clients->findClient(clientID); 257 disconnectClient(client); 261 if(client) 262 disconnectClient(client); 258 263 } 259 264 void Server::disconnectClient( ClientInformation *client){ -
code/trunk/src/network/Synchronisable.cc
r1293 r1360 153 153 syncData Synchronisable::getData(unsigned char *mem){ 154 154 //std::cout << "inside getData" << std::endl; 155 classID=this->getIdentifier()->getNetworkID(); 155 if(classID==0) 156 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; 157 this->classID=this->getIdentifier()->getNetworkID(); 156 158 std::list<synchronisableVariable *>::iterator i; 157 159 syncData retVal; -
code/trunk/src/network/diffTest.cc
r1293 r1360 466 466 bool addClientTest( ENetEvent* event, ClientInformation*& head ) { 467 467 ClientInformation *temp = head->insertBack(new ClientInformation); 468 if(!temp) 469 return false; 468 470 if(temp->prev()->head) { 469 471 temp->prev()->setID(0); … … 475 477 std::cout << "added client id: " << temp->getID() << std::endl; 476 478 477 temp->setSynched(true); 478 return true; 479 return temp->setSynched(true); 479 480 } 480 481 481 482 void printClientInformationBox( ClientInformation* box ) { 483 if(!box) 484 return; 482 485 std::cout << "ClientList: id: " << box->getID() << "\t"; 483 486 std::cout << "g_id: " << box->getGamestateID() << " \t";
Note: See TracChangeset
for help on using the changeset viewer.