Changeset 1008
- Timestamp:
- Apr 10, 2008, 12:55:35 PM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/Client.cc
r913 r1008 204 204 if(id!=NULL) 205 205 id->setNetworkID(clid->clid); 206 COUT(4) << " received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;206 COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl; 207 207 return; 208 208 } -
code/branches/network/src/network/ClientInformation.cc
r790 r1008 189 189 * This function should only be applied to the head of the list 190 190 * @param clientID id to look for 191 * @return pointer to the element in the list or 0 if the search was unsuccessfull191 * @return pointer to the last element in the list or 0 if the search was unsuccessfull 192 192 */ 193 193 ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { … … 195 195 if (temp->head) 196 196 temp=temp->next(); 197 while(temp!=0 && temp->getID()!=clientID){ 197 //bugfix: temp to temp->next(), get last elem if not found, not segflt 198 while(temp->next()!=0 && temp->getID()!=clientID){ 198 199 temp = temp->next(); 199 200 } … … 210 211 ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { 211 212 ClientInformation *temp = this; 212 while(temp!=0){ 213 //bugfix: temp to temp->next(), get last elem if not found, not segflt 214 while(temp->next()!=0){ 213 215 if(temp->head){ 214 216 temp = temp->next(); -
code/branches/network/src/network/ClientInformation.h
r790 r1008 47 47 bool removeClient(int clientID); 48 48 bool removeClient(ENetPeer *peer); 49 //## add bool mask-function eventually 49 50 ClientInformation *findClient(int clientID, bool look_backwards=false); 51 //## add bool mask-function eventually 50 52 ClientInformation *findClient(ENetAddress *address, bool look_backwards=false); 51 53 -
code/branches/network/src/network/ConnectionManager.cc
r988 r1008 59 59 { 60 60 boost::thread_group network_threads; 61 61 62 ConnectionManager::ConnectionManager(){} 63 62 64 ConnectionManager::ConnectionManager(ClientInformation *head) { 63 65 quit=false; … … 87 89 return NULL; 88 90 } 89 91 /** 92 This function only pops the first element in PacketBuffer (first in first out) 93 used by processQueue in Server.cc 94 */ 90 95 ENetPacket *ConnectionManager::getPacket(int &clientID) { 91 96 ENetAddress address; … … 173 178 case ENET_EVENT_TYPE_CONNECT: 174 179 addClient(&event); 180 COUT(5) << "Con.Man: connection event has occured" << std::endl; 175 181 break; 176 182 case ENET_EVENT_TYPE_RECEIVE: 177 183 //std::cout << "received data" << std::endl; 184 COUT(5) << "Con.Man: receive event has occured" << std::endl; 178 185 processData(&event); 179 186 break; … … 190 197 enet_host_destroy(server); 191 198 } 192 199 200 //### added some bugfixes here, but we cannot test them because 201 //### the server crashes everytime because of some gamestates 202 //### (trying to resolve that now) 193 203 void ConnectionManager::disconnectClients() { 194 204 ENetEvent event; … … 198 208 temp = temp->next(); 199 209 } 200 temp = temp->next(); 210 //bugfix: might be the reason why server crashes when clients disconnects 211 //temp = temp->next(); 212 temp = head_->next(); 201 213 while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 202 214 switch (event.type) 203 215 { 204 case ENET_EVENT_TYPE_NONE: 205 case ENET_EVENT_TYPE_CONNECT: 216 case ENET_EVENT_TYPE_NONE: break; 217 case ENET_EVENT_TYPE_CONNECT: break; 206 218 case ENET_EVENT_TYPE_RECEIVE: 207 219 enet_packet_destroy(event.packet); 208 220 break; 209 221 case ENET_EVENT_TYPE_DISCONNECT: 210 COUT(4) << "disconnecting client" << std::endl;222 COUT(4) << "disconnecting all clients" << std::endl; 211 223 delete head_->findClient(&(event.peer->address)); 224 //maybe needs bugfix: might also be a reason for the server to crash 212 225 temp = temp->next(); 213 226 break; … … 231 244 return head_->removeClient(peer); 232 245 } 233 246 /** 247 This function adds a client that connects to the clientlist of the server 248 NOTE: if you change this, don't forget to change the test function 249 addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid 250 */ 234 251 bool ConnectionManager::addClient(ENetEvent *event) { 235 252 ClientInformation *temp = head_->insertBack(new ClientInformation); 236 if(temp->prev()->head) 253 if(temp->prev()->head) { //not good if you use anything else than insertBack 254 temp->prev()->setID(0); //bugfix: not necessary but usefull 237 255 temp->setID(1); 256 } 238 257 else 239 258 temp->setID(temp->prev()->getID()+1); 240 259 temp->setPeer(event->peer); 241 std::cout << "added client id: " << temp->getID() << std::endl;260 COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl; 242 261 syncClassid(temp->getID()); 243 262 temp->setSynched(true); … … 268 287 classname = id->getName(); 269 288 network_id = id->getNetworkID(); 270 COUT(4) << " network_id: " << network_id << ", classname: " << classname << std::endl;289 COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; 271 290 272 291 addPacket(packet_gen.clid( (int)network_id, classname ), clientID); … … 275 294 } 276 295 sendPackets(); 296 COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; 277 297 } 278 298 … … 289 309 290 310 int ConnectionManager::getObjectsClientID( int objectID ) { 291 std::map<int, int>::iterator iter = clientsShip.begin();292 while( iter != clientsShip.end()) {311 std::map<int, int>::iterator iter; 312 for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { 293 313 if( iter->second == objectID ) return iter->first; 294 314 } … … 302 322 void ConnectionManager::deleteObjectIDReg( int objectID ) { 303 323 std::map<int, int>::iterator iter = clientsShip.begin(); 304 while( iter != clientsShip.end()) {324 for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { 305 325 if( iter->second == objectID ) break; 306 326 } 307 327 clientsShip.erase( iter->first ); 308 328 } 309 329 int ConnectionManager::getNumberOfClients() { 330 return clientsShip.size(); 331 } 310 332 } -
code/branches/network/src/network/ConnectionManager.h
r888 r1008 55 55 bool sendPackets(ENetEvent *event); 56 56 bool sendPackets(); 57 58 //##### for testing purpose only ##### 59 ConnectionManager(); 60 std::map<int, int> testGetClientsShip() { 61 return clientsShip; 62 } 63 void testAddClientsShipID( int clientID, int objectID ) { 64 addClientsObjectID( clientID, objectID ); 65 } 66 int testGetClientsShipID( int clientID ) { 67 return getClientsShipID( clientID ); 68 } 69 int testGetObjectsClientID( int objectID ) { 70 return getObjectsClientID( objectID ); 71 } 72 void testDeleteClientsIDReg( int clientID ) { 73 deleteClientIDReg( clientID ); 74 } 75 void testDeleteObjectIDReg( int objectID ) { 76 deleteObjectIDReg( objectID ); 77 } 78 //##### for testing purpose only ##### 57 79 private: 58 80 bool clientDisconnect(ENetPeer *peer); … … 82 104 void deleteClientIDReg( int clientID ); 83 105 void deleteObjectIDReg( int objectID ); 106 int getNumberOfClients(); 84 107 }; 85 86 87 88 89 90 91 92 108 93 109 } -
code/branches/network/src/network/GameStateClient.cc
r1005 r1008 109 109 110 110 if(!it){ 111 COUT( 5) << "classid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;111 COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl; 112 112 Synchronisable *no = (Synchronisable*)(ID((unsigned int) sync.classID)->fabricate()); 113 113 no->objectID=sync.objectID; … … 197 197 case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL; 198 198 case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL; 199 case Z_DATA_ERROR: COUT(2) << "data corrupted " << std::endl; return NULL;199 case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL; 200 200 } 201 201 -
code/branches/network/src/network/GameStateManager.cc
r1007 r1008 93 93 94 94 GameStateCompressed *GameStateManager::popGameState(int clientID) { 95 //why are we searching the same client's gamestate id as we searched in 96 //Server::sendGameState? 95 97 int gID = head_->findClient(clientID)->getGamestateID(); 96 COUT(4) << "popgamestate: sending gstate id: " << id << "diffed from: " << gID << std::endl; 97 if(gID!=GAMESTATEID_INITIAL){ 98 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl; 99 100 //chose wheather the next gamestate is the first or not 101 if(gID != GAMESTATEID_INITIAL){ 98 102 GameState *client = gameStateMap[gID]; 99 103 GameState *server = reference; … … 115 119 GameState *GameStateManager::getSnapshot(int id) 116 120 { 121 //std::cout << "begin getSnapshot" << std::endl; 117 122 //the size of the gamestate 118 123 int totalsize=0; … … 127 132 GameState *retval=new GameState; //return value 128 133 retval->id=id++; 129 COUT(4) << " producing gamestate with id: " << retval->id << std::endl;134 COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl; 130 135 // reserve a little memory and increase it later on 131 COUT(5) << " mallocing"<< std::endl;136 COUT(5) << "G.ST.Man: mallocing: " << memsize << std::endl; 132 137 retval->data = (unsigned char*)malloc(memsize); 133 COUT(5) << " malloced"<< std::endl;138 COUT(5) << "G.ST.Man: malloced: " << memsize << std::endl; 134 139 135 140 // offset of memory functions … … 137 142 // go through all Synchronisables 138 143 for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){ 144 //std::cout << "begin inner loop" << std::endl; 139 145 //std::cout << "gamestatemanager: in for loop" << std::endl; 140 146 //get size of the synchronisable 141 147 tempsize=it->getSize(); 142 //COUT(5) << "size of temp gamestate: " << tempsize << std::endl;148 //COUT(5) << "size of temp gamestate: " << tempsize << std::endl; 143 149 //COUT(2) << "size of synchronisable: " << tempsize << std::endl; 144 150 // add place for data and 3 ints (length,classid,objectid) 145 151 totalsize+=tempsize+3*sizeof(int); 146 152 //std::cout << "totalsize: " << totalsize << std::endl; 153 //COUT(5) << "G.St.Man: current totalsize=" << totalsize << std::endl; 154 //COUT(5) << "G.St.Man: current it->classID=" << it->classID << " it->objectID=" << it->objectID << std::endl; 147 155 // allocate additional space 148 if(totalsize+tempsize>memsize){ 149 if(tempsize<1000){ 156 if((totalsize+tempsize) > memsize){ 157 COUT(5) << "G.St.Man: need additional memory" << std::endl; 158 if(tempsize < 1000){ 150 159 retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); 151 160 memsize+=1000; … … 154 163 memsize+=tempsize+1000; 155 164 } 165 COUT(5) << "G.St.Man: additional space allocation finished" << std::endl; 156 166 } 157 167 … … 166 176 // increase data pointer 167 177 offset+=tempsize+3*sizeof(int); 168 }169 COUT(5) << "Gamestate size: " << totalsize << std::endl;178 //std::cout << "end inner loop" << std::endl; 179 } 170 180 retval->size=totalsize; 171 181 //#### bugfix 172 182 retval->diffed = false; 183 //std::cout << "end snapShot" << std::endl; 184 COUT(5) << "G.ST.Man: Gamestate size: " << totalsize << std::endl; 173 185 return retval; 174 186 } … … 185 197 186 198 GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) { 199 COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl; 187 200 //GameState r = diff(a,b); 188 201 //r.diffed = true; … … 193 206 194 207 GameStateCompressed *GameStateManager::encode(GameState *a) { 208 COUT(5) << "G.St.Man: this will be a not diffed gamestate" << std::endl; 195 209 a->diffed=false; 196 210 return compress_(a); … … 235 249 236 250 GameStateCompressed *GameStateManager::compress_(GameState *a) { 237 COUT(5) << " compressing gamestate" << std::endl;251 COUT(5) << "G.St.Man: compressing gamestate" << std::endl; 238 252 int size = a->size; 239 253 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; … … 245 259 246 260 switch ( retval ) { 247 case Z_OK: COUT(5) << " successfully compressed" << std::endl; break;248 case Z_MEM_ERROR: COUT(1) << " not enough memory available in gamestate.compress" << std::endl;261 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; 262 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 249 263 return NULL; 250 case Z_BUF_ERROR: COUT(2) << " not enough memory available in the buffer in gamestate.compress" << std::endl;264 case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; 251 265 return NULL; 252 case Z_DATA_ERROR: COUT(2) << " decompress: data corrupted in gamestate.compress" << std::endl;266 case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; 253 267 return NULL; 254 268 } … … 263 277 compressedGamestate->diffed = a->diffed; 264 278 compressedGamestate->base_id = a->base_id; 265 279 COUT(5) << "G.St.Man: saved compressed data in GameStateCompressed" << std::endl; 266 280 return compressedGamestate; 267 281 } -
code/branches/network/src/network/PacketBuffer.cc
r790 r1008 60 60 last=first; 61 61 last->next=NULL; 62 // change this!!!!!!! 62 // change this!!!!!!! 63 63 last->packet = ev->packet; 64 64 last->address = ev->peer->address; -
code/branches/network/src/network/PacketDecoder.cc
r986 r1008 52 52 { 53 53 int client = clientId; 54 COUT(5) << " clientId: " << client << std::endl; //control cout, not important, just debugging info54 COUT(5) << "PacketDecoder: clientId: " << client << std::endl; //control cout, not important, just debugging info 55 55 int id = (int)*packet->data; //the first 4 bytes are always the enet packet id 56 COUT(5) << "packet id: " << id << std::endl; 57 // COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl; 56 COUT(5) << "PacketDecoder: packet id: " << id << std::endl; 57 //COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl; 58 59 if ( packet == NULL ) { 60 COUT(4) << "PacketDecoder: no packets->packetbuffer queue is empty" << std::endl; 61 return false; 62 } 58 63 switch( id ) { 59 64 case ACK: … … 93 98 94 99 95 COUT(5) << " got ack id: " << a->id << std::endl;100 COUT(5) << "PacketDecoder: got ack id: " << a->id << std::endl; 96 101 processAck( a, clientId ); //debug info 97 102 … … 147 152 currentState = new GameStateCompressed; 148 153 if(currentState == NULL){ 149 COUT(3) << " could not generate new GameStateCompressed" << std::endl;154 COUT(3) << "PacketDecoder: could not generate new GameStateCompressed" << std::endl; 150 155 return; 151 156 } … … 156 161 //currentState->id = *((int *)packet->data+sizeof(int)); 157 162 memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) ); 158 COUT(5) << " decoder: received gs id: " << currentState->id << std::endl;163 COUT(5) << "PacketDecoder: received gs id: " << currentState->id << std::endl; 159 164 // std::cout << "id: " << currentState->id << std::endl; 160 165 //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th … … 174 179 //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream 175 180 if(currentState->compsize==0) 176 COUT(2) << " compsize is 0" << std::endl;181 COUT(2) << "PacketDecoder: compsize is 0" << std::endl; 177 182 currentState->data = (unsigned char*)(malloc( currentState->compsize )); 178 183 if(currentState->data==NULL) 179 COUT(2) << " Gamestatepacket-decoder: memory leak" << std::endl;184 COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl; 180 185 //copy the GameStateCompressed data 181 186 //std::cout << "packet size (enet): " << packet->dataLength << std::endl; … … 197 202 void *data = (void *)cid->message; 198 203 memcpy(data, (const void*)(packet->data+3*sizeof(int)), cid->length); 199 COUT(4) << " classid: " << cid->clid << ", name: " << cid->message << std::endl;204 COUT(4) << "PacketDecoder: classid: " << cid->clid << ", name: " << cid->message << std::endl; 200 205 enet_packet_destroy( packet ); 201 206 processClassid(cid); -
code/branches/network/src/network/PacketGenerator.cc
r986 r1008 50 50 ENetPacket* PacketGenerator::acknowledgement( int state, int reliable ) 51 51 { 52 COUT(4) << " generating new acknowledgement, id: " << state << std::endl;52 COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl; 53 53 ack* ackreq = new ack; 54 54 ackreq->id = ACK; … … 63 63 ENetPacket* PacketGenerator::mousem( double x, double y, int reliable ) 64 64 { 65 COUT(4) << " generating new mouse" << std::endl;65 COUT(4) << "PacketGenerator: generating new mouse" << std::endl; 66 66 mouse* mousemove = new mouse; 67 67 mousemove->id = MOUSE; … … 77 77 ENetPacket* PacketGenerator::keystrike( char press, int reliable ) 78 78 { 79 COUT(4) << " generating new keyboard" << std::endl;79 COUT(4) << "PacketGenerator: generating new keyboard" << std::endl; 80 80 keyboard* key = new keyboard; 81 81 key->id = KEYBOARD; … … 126 126 { 127 127 unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1); 128 std::cout << " classid: " << classid << ", name: " << classname << std::endl;128 std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl; 129 129 *(int *)data = CLASSID; 130 130 *((int *)data+1) = classname.length()+1; -
code/branches/network/src/network/Server.cc
r1007 r1008 95 95 bool Server::sendMSG(const char *msg) { 96 96 ENetPacket *packet = packet_gen.chatMessage(msg); 97 std::cout <<"adding Packets" << std::endl;97 COUT(4) <<"Server: adding Packets" << std::endl; 98 98 connection->addPacketAll(packet); 99 99 //std::cout <<"added packets" << std::endl; 100 100 if (connection->sendPackets()){ 101 std::cout << "Sucessfully" << std::endl;101 COUT(4) << "Server: Sucessfully" << std::endl; 102 102 return true; 103 103 } … … 126 126 while(!connection->queueEmpty()){ 127 127 //std::cout << "Client " << clientID << " sent: " << std::endl; 128 //clientID here is a reference to grab clientID from ClientInformation 128 129 packet = connection->getPacket(clientID); 129 elaborate(packet, clientID); 130 //if statement to catch case that packetbuffer is empty 131 if( !elaborate(packet, clientID) ) 132 COUT(4) << "Server: PacketBuffer empty" << std::endl; 130 133 } 131 134 } … … 136 139 void Server::updateGamestate() { 137 140 gamestates->update(); 141 COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; 138 142 //std::cout << "updated gamestate, sending it" << std::endl; 139 143 //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) 140 sendGameState(); 144 sendGameState(); 145 COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; 141 146 //std::cout << "sent gamestate" << std::endl; 142 147 } … … 146 151 */ 147 152 bool Server::sendGameState() { 148 COUT(5) << " startingsendGameState" << std::endl;153 COUT(5) << "Server: starting function sendGameState" << std::endl; 149 154 ClientInformation *temp = clients; 150 155 bool added=false; 151 while(temp !=NULL){156 while(temp != NULL){ 152 157 if(temp->head){ 153 158 temp=temp->next(); 159 //think this works without continue 154 160 continue; 155 161 } 156 162 if( !(temp->getSynched()) ){ 157 COUT(5) << " not sending gamestate" << std::endl;163 COUT(5) << "Server: not sending gamestate" << std::endl; 158 164 temp=temp->next(); 165 //think this works without continue 159 166 continue; 160 167 } 161 COUT(5) << " doing gamestate gamestate preparation" << std::endl;162 int gid = temp->getGamestateID(); 163 int cid = temp->getID(); 164 COUT(5) << " server, got acked (gamestate) ID: " << gid << std::endl;168 COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; 169 int gid = temp->getGamestateID(); //get gamestate id 170 int cid = temp->getID(); //get client id 171 COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; 165 172 GameStateCompressed *gs = gamestates->popGameState(cid); 166 173 if(gs==NULL){ 167 COUT(2) << " could not generate gamestate" << std::endl;174 COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; 168 175 return false; 169 176 } 170 177 //std::cout << "adding gamestate" << std::endl; 171 connection->addPacket(packet_gen.gstate(gs), cid); 178 if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ) 179 COUT(4) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl; 172 180 //std::cout << "added gamestate" << std::endl; 173 181 added=true; 174 182 temp=temp->next(); 175 183 } 176 if(added) 184 if(added) { 185 //std::cout << "send gamestates from server.cc in sendGameState" << std::endl; 177 186 return connection->sendPackets(); 178 COUT(5) << "had no gamestates to send" << std::endl; 187 } 188 COUT(5) << "Server: had no gamestates to send" << std::endl; 179 189 return false; 180 190 } 181 191 182 192 void Server::processAck( ack *data, int clientID) { 183 COUT(5) << " processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;193 COUT(5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl; 184 194 clients->findClient(clientID)->setGamestateID(data->a); 185 195 } -
code/branches/network/src/network/Synchronisable.cc
r871 r1008 44 44 // increase datasize 45 45 datasize+=sizeof(int)+size; 46 // push temp to syncList (at the bottom)46 //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; 47 47 syncList.push_back(temp); 48 48 } … … 104 104 */ 105 105 syncData Synchronisable::getData(unsigned char *mem){ 106 std::list<synchronisableVariable>::iterator i; 106 //std::cout << "inside getData" << std::endl; 107 std::list<SYNCVAR>::iterator i; 107 108 syncData retVal; 108 109 retVal.objectID=this->objectID; … … 113 114 int n=0; 114 115 for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ 115 //COUT(2) << "size of variable: " << i->size << std::endl;116 116 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); 117 117 memcpy( (void *)(retVal.data+n), (const void*)&(i->size), sizeof(int) ); … … 166 166 int Synchronisable::getSize(){ 167 167 int tsize=0; 168 std::list< synchronisableVariable>::iterator i;168 std::list<SYNCVAR>::iterator i; 169 169 for(i=syncList.begin(); i!=syncList.end(); i++){ 170 170 switch(i->type){ 171 case DATA:172 tsize+=sizeof(int);173 tsize+=i->size;174 break;175 case STRING:176 tsize+=sizeof(int);177 tsize+=((std::string *)i->var)->length()+1;178 break;171 case DATA: 172 tsize+=sizeof(int); 173 tsize+=i->size; 174 break; 175 case STRING: 176 tsize+=sizeof(int); 177 tsize+=((std::string *)i->var)->length()+1; 178 break; 179 179 } 180 180 } -
code/branches/network/src/network/Synchronisable.h
r927 r1008 47 47 public: 48 48 49 49 virtual ~Synchronisable(); 50 50 int objectID; 51 51 int classID; -
code/branches/network/src/network/diffTest.cc
r984 r1008 1 #include "enet/enet.h" 2 #include "Orxonox.h" 3 #include "NetworkPrereqs.h" 4 #include "PacketTypes.h" 1 5 #include "GameStateManager.h" 2 6 #include "Synchronisable.h" 3 7 #include "GameStateClient.h" 4 #include "NetworkPrereqs.h"5 #include "PacketTypes.h"6 8 #include "iostream" 7 9 #include "core/CoreIncludes.h" 8 10 #include "time.h" 11 #include "ConnectionManager.h" 12 #include "ClientInformation.h" 13 #include <boost/thread/thread.hpp> 14 #include <boost/bind.hpp> 15 #include "util/Sleep.h" 9 16 10 17 using namespace network; … … 17 24 18 25 void printGameStateCompressed( GameStateCompressed* gc ) { 19 std::cout << "=================================================" << std::endl;26 //std::cout << "=================================================" << std::endl; 20 27 std::cout << "GameStateCompressed id:\t\t" << gc->id << std::endl; 21 28 std::cout << "GameStateCompressed normsize:\t" << gc->normsize << std::endl; … … 24 31 //std::cout << "GameState data:\t" << gc->data << std::endl; 25 32 std::cout << "GameStateCompressed compressing rate:\t" << 100.0-((100.0/(gc->normsize))*(gc->compsize)) << "%" << std::endl; 26 //std::cout << "=================================================" << std::endl;33 std::cout << "=================================================" << std::endl; 27 34 return; 28 35 } … … 30 37 bool compareData( GameState* g1, GameState* g2 ) { 31 38 if ( g1->id != g2->id ) { 32 std::cout << " GameStates are not comparable -> not same id" << std::endl;39 std::cout << "\t--> GameStates are not comparable -> not same id" << std::endl; 33 40 return 1; 34 41 } 35 42 else if ( g1->size != g2->size ) { 36 std::cout << " GameStates are not the same size!!" << std::endl;43 std::cout << "\t--> GameStates are not the same size!!" << std::endl; 37 44 std::cout << g1->size << " != " << g2->size << std::endl; 38 45 } … … 40 47 for ( int i=0; i<length; i++ ) { 41 48 if ( g1->data[i] != g2->data[i] ) { 42 std::cout << " data of both GameStates are not identical" << std::endl;49 std::cout << "\t--> data of both GameStates are not identical" << std::endl; 43 50 return false; 44 51 } 45 52 } 46 std::cout << " GameStates are identical" << std::endl;53 std::cout << "\t--> GameStates are identical (compareData)" << std::endl; 47 54 return true; 48 55 } … … 50 57 bool compareGameStates( GameState* g1, GameState* g2 ) { 51 58 if ( g1->id != g2->id ) { 52 std::cout << "GameState id's not identical" << std::endl; 59 std::cout << "\t==> GameState id's not identical (GameStateCompare)" << std::endl; 60 } 61 if( g1->size != g2->size ) { 62 std::cout << "\t==> GameState sizes are not identical (GameStateCompare)" << std::endl; 53 63 return false; 54 64 } 55 else if ( g1->size != g2->size) {56 std::cout << " GameState sizes are not identical" << std::endl;65 else if ( g1->diffed != g2->diffed ) { 66 std::cout << "\t==> GameState diffed params not identical (GameStateCompare)" << std::endl; 57 67 return false; 58 68 } 59 else if ( g1->diffed != g2->diffed) {60 std::cout << " GameState diffed params not identical" << std::endl;69 else if ( !compareData( g1, g2 ) ) { 70 std::cout << "\t==> GameState data are not identical (GameStateCompare)" << std::endl; 61 71 return false; 62 72 } 63 else if ( !compareData( g1, g2 ) ) { 64 std::cout << "GameState data are not identical" << std::endl; 65 return false; 66 } 67 std::cout << "==>GameStates are identical (GameStateCompare)" << std::endl; 73 std::cout << "\t==> GameStates are identical (GameStateCompare)" << std::endl; 68 74 return true; 69 75 } 70 76 71 77 void printGameState( GameState* gstate ) { 72 std::cout << "=================================================" << std::endl;78 //std::cout << "=================================================" << std::endl; 73 79 std::cout << "GameState id:\t\t" << gstate->id << std::endl; 74 80 std::cout << "GameState size:\t\t" << gstate->size << std::endl; 75 81 std::cout << "GameState diffed:\t" << gstate->diffed << std::endl; 76 82 //std::cout << "GameState data:\t" << gstate->data << std::endl; 77 //std::cout << "=================================================" << std::endl;83 std::cout << "=================================================" << std::endl; 78 84 return; 79 85 } … … 81 87 unsigned char* createData( int length, int mode ) { 82 88 char* data = new char[ length ]; 83 84 89 if ( mode == 1 ) { 85 90 for ( int i=0; i<length; i++ ) … … 93 98 } 94 99 else if ( mode == 3 ) { 95 for ( int i=0; i<length; i++ ) {96 data[i] = (char)(i%255);97 }98 }99 else if ( mode == 4 ) {100 100 for ( int i=0; i<length; i++ ){ 101 101 data[i] = (char)(rand()%255); 102 102 } 103 103 } 104 else if ( mode == 5) {104 else if ( mode == 4 ) { 105 105 for ( int i=0; i<length; i++ ){ 106 106 data[i] = (char)(rand()%127); … … 121 121 122 122 if ( mode == 1 ) { 123 b->data = new unsigned char[ a->size];123 b->data = new unsigned char[length]; 124 124 b->size = a->size; 125 125 for ( int i=0; i<length; i++ ) { … … 129 129 } 130 130 else if ( mode == 2 ) { 131 b->data = new unsigned char[ a->size];132 b->size = a->size;131 b->data = new unsigned char[length]; 132 b->size = length; 133 133 for ( int i=0; i<length; i++ ) { 134 if ( i% 5== 0 ) b->data[i] = rand()%255;134 if ( i%(rand()%((length)/11)) == 0 ) b->data[i] = rand()%255; 135 135 else b->data[i] = a->data[i]; 136 136 } 137 137 } 138 138 else if ( mode == 3 ) { 139 int s = a->size + (a->size)/3;139 int s = length + (rand()%(length)); 140 140 b->data = new unsigned char[s]; 141 141 b->size = s; … … 148 148 } 149 149 } 150 else if ( mode == 4 ) { 151 int s = length + (rand()%(length)); 152 b->data = new unsigned char[s]; 153 b->size = s; 154 for ( int i=0; i<length; i++ ) { 155 if ( i%(rand()%(length)) == 0 ) b->data[i] = rand()%255; 156 else b->data[i] = a->data[i]; 157 } 158 for( int i=length; i<s; i++ ) { 159 b->data[i] = rand()%255; 160 } 161 } 162 else if ( mode == 5 ) { 163 int s = (length)/2; 164 b->data = new unsigned char[s]; 165 b->size = s; 166 for ( int i=0; i<s; i++ ) { 167 if ( i%10 == 0 ) b->data[i] = rand()%255; 168 else b->data[i] = a->data[i]; 169 } 170 } 150 171 151 172 return b; … … 182 203 << modeCreateData << " modeChangeData = " << modeChangeData << std::endl; 183 204 GameStateClient* g_client; 184 GameStateManager* g_manager; ;205 GameStateManager* g_manager; 185 206 186 207 GameState* g_undiff1 = new GameState; … … 267 288 } 268 289 290 void printClientObjectMapping( ConnectionManager* cmanager, int clients ) { 291 std::map<int, int>::iterator iter; 292 std::map<int, int> clientsmap = cmanager->testGetClientsShip(); 293 for( iter = clientsmap.begin(); iter != clientsmap.end(); iter++ ) { 294 std::cout << "clientID: " << iter->first << "\t-> objectID: " << iter->second << std::endl; 295 } 296 return; 297 } 298 299 bool is( int a[], int b, int size ) { 300 for ( int i=0; i<size; i++ ) { 301 if ( a[i] == b ) return true; 302 } 303 return false; 304 } 305 306 void testClientObjectMapping( int clients ) { 307 ConnectionManager* cmanager = new ConnectionManager(); 308 int shift = 2; 309 std::cout << "create a map length [clients]" << std::endl; 310 for ( int i=0; i<clients; i++ ) { 311 cmanager->testAddClientsShipID( i, i+shift ); 312 } 313 printClientObjectMapping( cmanager, clients ); 314 315 std::cout << "get random client's ship id" << std::endl; 316 int id; 317 for ( int i=0; i<(clients/3); i++ ) { 318 id = rand()%clients; 319 std::cout << "client: " << id << "\t-> ship: " << cmanager->testGetClientsShipID( id ) << std::endl; 320 } 321 322 std::cout <<"get random ship's client id" << std::endl; 323 for ( int i=0; i<(clients/3); i++ ) { 324 id = (rand()%clients)+shift; 325 std::cout << "ship: " << id << "\t-> client: " << cmanager->testGetObjectsClientID( id ) << std::endl; 326 } 327 328 std::cout << "delete random client from map" << std::endl; 329 int deleted[clients/3]; 330 for ( int i=0; i<(clients/3); i++ ) { 331 id = rand()%clients; 332 if ( !is( deleted, id, clients/3 ) ) { 333 std::cout << "delete client " << id << std::endl; 334 cmanager->testDeleteClientsIDReg( id ); 335 } 336 deleted[i] = id; 337 } 338 std::cout << "resulting list:" << std::endl; 339 printClientObjectMapping( cmanager, clients-(clients/3)); 340 341 std::cout << "delete random object from map" << std::endl; 342 int jap = 0; 343 while( jap < 3 ) { 344 id = (rand()%clients) + shift; 345 if ( !is( deleted, id, clients/3 ) ) { 346 std::cout << "delete object: " << id << std::endl; 347 cmanager->testDeleteObjectIDReg( id ); 348 jap++; 349 } 350 } 351 std::cout << "resulting list:" << std::endl; 352 printClientObjectMapping( cmanager, clients-(clients/3)-3); 353 } 354 355 bool addClientTest( ENetEvent* event, ClientInformation*& head ) { 356 ClientInformation *temp = head->insertBack(new ClientInformation); 357 if(temp->prev()->head) { 358 temp->prev()->setID(0); 359 temp->setID(1); 360 } 361 else 362 temp->setID(temp->prev()->getID()+1); 363 temp->setPeer(event->peer); 364 std::cout << "added client id: " << temp->getID() << std::endl; 365 366 temp->setSynched(true); 367 return true; 368 } 369 370 void printClientInformationBox( ClientInformation* box ) { 371 std::cout << "ClientList: id: " << box->getID() << "\t"; 372 std::cout << "g_id: " << box->getGamestateID() << " \t"; 373 std::cout << "synched: " << box->getSynched() << "\t"; 374 std::cout << "is head: " << box->head << std::endl; 375 } 376 377 void printClientInformationList( ClientInformation* list ) { 378 printClientInformationBox( list ); 379 list = list->next(); 380 381 while( list != 0 ) { 382 printClientInformationBox( list ); 383 list = list->next(); 384 } 385 return; 386 } 387 388 void testClientInformation( int numberOfClients ) { 389 ClientInformation* head = new ClientInformation( true ); 390 ConnectionManager* connectionManager; 391 ENetEvent event; 392 393 for ( int i=0; i<numberOfClients; i++ ) { 394 if ( !addClientTest( &event, head ) ) { 395 std::cout << "addClientTest didn't work with: " << i << std::endl; 396 } 397 } 398 std::cout << "(now id should be synched, since that works and this is test of list, this step is left out)" << std::endl; 399 400 printClientInformationList( head ); 401 402 std::cout << "remove some clients" << std::endl; 403 if ( head->removeClient( numberOfClients/3 ) ) std::cout << "client " << numberOfClients/3 << " removed" << std::endl; 404 else std::cout << "could not remove client: " << numberOfClients/3 << std::endl; 405 if ( head->removeClient( numberOfClients ) ) std::cout << "client " << numberOfClients << " removed" << std::endl; 406 else std::cout << "could not remove client: " << numberOfClients << std::endl; 407 if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed" << std::endl; 408 else std::cout << "could not remove client: " << 1 << std::endl; 409 if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed a second time" << std::endl; 410 else std::cout << "could not remove client: " << 1 << std::endl; 411 if ( head->removeClient( numberOfClients + 100 ) ) std::cout << "client " << numberOfClients + 100 << " removed a second time" << std::endl; 412 else std::cout << "could not remove client: " << numberOfClients + 100 << std::endl; 413 414 printClientInformationList( head ); 415 416 std::cout << "try to find some clients with findClient(..., false)" << std::endl; 417 ClientInformation* temp = head->findClient( 2 ); 418 printClientInformationBox( temp ); 419 temp = head->findClient( numberOfClients/3 ); 420 printClientInformationBox( temp ); 421 temp = head->findClient( 0 ); 422 printClientInformationBox( temp ); 423 temp = head->findClient( 8 ); 424 printClientInformationBox( temp ); 425 426 std::cout << "find the same, output should be identical with above but with findClient(..., TRUE)" << std::endl; 427 temp = head->findClient( 2, true ); 428 printClientInformationBox( temp ); 429 temp = head->findClient( numberOfClients/3, true ); 430 printClientInformationBox( temp ); 431 temp = head->findClient( 0, true ); 432 printClientInformationBox( temp ); 433 temp = head->findClient( 8, true ); 434 printClientInformationBox( temp ); 435 436 std::cout << "test setGamestateID" << std::endl; 437 temp->setGamestateID( 8 ); 438 printClientInformationBox( temp ); 439 440 std::cout << "test insertAfter() and insertBefore()" << std::endl; 441 ClientInformation* newInfo = new ClientInformation; 442 ClientInformation* toool = new ClientInformation; 443 newInfo->setGamestateID( 200 ); 444 newInfo->setID( numberOfClients+2); 445 newInfo->setSynched( true ); 446 newInfo->setPeer( NULL ); 447 toool->setGamestateID( 199 ); 448 toool->setID( numberOfClients+1); 449 toool->setSynched( true ); 450 toool->setPeer( NULL ); 451 452 //never use the same ClientInformation box in this situation 453 //-> results in endless loop 454 temp->insertAfter( newInfo ); 455 temp->insertBefore( toool ); 456 457 printClientInformationList( head ); 458 return; 459 } 460 461 //### following stuff is to test buffer, took from PacketBufferTestExt.cc ### 462 463 void write(PacketBuffer *test){ 464 ENetEvent event; 465 ENetPacket *packet; 466 if(test->isEmpty()) 467 std::cout << "buffer is empty" << std::endl; 468 for(int i=0; i<10; i++){ 469 std::string temp = "packet "; 470 packet = enet_packet_create("packet", strlen("packet ")+1, 471 ENET_PACKET_FLAG_RELIABLE); 472 std::cout << i << ": pushing " << packet->data << std::endl; 473 event.packet=packet; 474 test->push(&event); 475 if(i==5) 476 usleep(200000); 477 } 478 test->setClosed(true); 479 return; 480 } 481 482 void read(PacketBuffer *test){ 483 //test->print(); 484 // exit if the queue is closed and empty 485 while(!test->isClosed() || !test->isEmpty()){ 486 // only pop if the queue isn't empty 487 while(!test->isEmpty()){ 488 std::cout << "We popped the value " << test->pop()->data << std::endl; 489 } 490 } 491 return; 492 } 493 494 void testPacketBuffer() { 495 PacketBuffer test = PacketBuffer(); 496 boost::thread thrd1(boost::bind(&write, &test)); 497 boost::thread thrd2(boost::bind(&read, &test)); 498 499 thrd1.join(); 500 thrd2.join(); 501 return; 502 } 503 504 //### end packetbuffer test stuff ### 505 506 void displayModes() { 507 std::cout << "mode datalength: length of data array to create" << std::endl; 508 std::cout << "mode Data:" << std::endl; 509 std::cout << "\t-1 -> array[length] with numbers length%255" << std::endl; 510 std::cout << "\t-2 -> array[length] with numbers length%255, every %98 is != 0" << std::endl; 511 std::cout << "\t-3 -> array[length] with random numbers (-126:127) no modulo zeros" << std::endl; 512 std::cout << "\t-4 -> array[length] with random numbers (0:127) no modulo zeros" << std::endl; 513 std::cout << "---------------------------------------------------------------------------------" << std::endl; 514 std::cout << "mode Change:" << std::endl; 515 std::cout << "\t-1 -> every %10 == 0 index is different from original" << std::endl; 516 std::cout << "\t-2 -> every %(rand()%(length/11)) is different from original" << std::endl; 517 std::cout << "\t-3 -> every %10 == 0 index is different and randomly longer till 2xlonger" << std::endl; 518 std::cout << "\t-4 -> random differences and randomly longer" << std::endl; 519 std::cout << "\t-5 -> only half as long and ever %10 == 0 index is different" << std::endl; 520 } 521 269 522 int main( int argc, char* argv[] ) { 270 523 int a,b,c; 271 524 std::string dec = "nothing"; 272 525 std::cout << "############### START TEST (quit q) ###############" << std::endl; 526 std::cout << "possible tests: " << std::endl; 527 std::cout << "displayModes:\t\t modes" << std::endl; 528 std::cout << "testCompression:\t tc [datalength] [mode Data]" << std::endl; 529 std::cout << "testDifferentiation:\t td [datalength] [mode Data] [mode Change]" << std::endl; 530 std::cout << "testCompressWithDiff:\t tcd [datalength] [mode Data] [mode Change]" << std::endl; 531 std::cout << "testClientObjectMapping: tcom [#clients]" << std::endl; 532 std::cout << "testClientInformation:\t tci [#clients] (call with >10)" << std::endl; 533 std::cout << "testPacketBuffer:\t tbuf (comment address assignements in PacketBuffer.cc!)" << std::endl; 273 534 while ( dec.compare("q") != 0 ) { 274 std::cout << "possible tests: testcompression [datalength] [mode]" << std::endl;275 std::cout << "testdiff [datalength] [mode Data] [mode Change]" << std::endl;276 std::cout << "tcd [datalength] [mode Data] [mode Change]" << std::endl;277 535 std::cin >> dec; 278 if ( dec.compare("t estcompression") == 0 ) {536 if ( dec.compare("tc") == 0 ) { 279 537 std::cin >> a; std::cin >> b; 280 538 testCompression( a, b ); 281 539 } 282 else if ( dec.compare("t estdiff") == 0 ) {540 else if ( dec.compare("td") == 0 ) { 283 541 std::cin>> a; std::cin >> b; std::cin >> c; 284 542 testDifferentiation( a, b, c ); … … 288 546 testCompressWithDiff( a, b, c ); 289 547 } 548 else if ( dec.compare("modes") == 0 ) 549 displayModes(); 550 else if ( dec.compare("tcom") == 0 ) { 551 std::cin>> a; 552 testClientObjectMapping( a ); 553 } 554 else if ( dec.compare("tci") == 0 ) { 555 std::cin >> a; 556 testClientInformation( a ); 557 } 558 else if ( dec.compare("tbuf") == 0 ) { 559 testPacketBuffer(); 560 } 561 std::cout << "################## END ONE TURN ##################@" << std::endl; 290 562 } 291 563 return 0; … … 294 566 int main() { 295 567 std::cout << "############### START TEST (quit q) ###############" << std::endl; 296 testC ompressWithDiff( 5000, 5, 1);568 testClientInformation( 10 ); 297 569 }*/
Note: See TracChangeset
for help on using the changeset viewer.