Changeset 6214 in orxonox.OLD for branches/network/src
- Timestamp:
- Dec 21, 2005, 9:11:47 AM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/network_game_manager.cc
r6197 r6214 22 22 #include "factory.h" 23 23 #include "network_stream.h" 24 #include "converter.h" 24 25 25 26 /* include your own header */ … … 37 38 /* set the class id for the base object */ 38 39 this->setClassID(CL_ENTITY_MANAGER, "EntityManager"); 40 41 allOutBuffer.length = 0; 42 43 allOutBuffer.maxLength = 10*1024; 44 45 allOutBuffer.buffer = new byte[10*1024]; 46 47 newUniqueID = MAX_CONNECTIONS + 2; 39 48 } 40 49 … … 44 53 NetworkGameManager::~NetworkGameManager() 45 54 { 46 for ( int i = 0; i<inBuffer.size(); i++) 47 { 48 if ( inBuffer[i].buffer ) 49 delete inBuffer[i].buffer; 55 for ( int i = 0; i<outBuffer.size(); i++) 56 { 50 57 if ( outBuffer[i].buffer ) 51 58 delete outBuffer[i].buffer; 52 59 } 60 61 if ( allOutBuffer.buffer ) 62 delete allOutBuffer.buffer; 53 63 } 54 64 … … 56 66 void NetworkGameManager::writeBytes(const byte* data, int length, int sender) 57 67 { 68 int i = 0; 69 byte b; 70 71 while ( i<length ) 72 { 73 b = data[i++]; 74 75 if ( b == REQUEST_CREATE ) 76 { 77 return; 78 } 79 if ( b == REQUEST_REMOVE ) 80 { 81 return; 82 } 83 if ( b == CREATE_ENTITY ) 84 { 85 return; 86 } 87 if ( b == REMOVE_ENTITY ) 88 { 89 return; 90 } 91 if ( b == REQUEST_SYNC ) 92 { 93 return; 94 } 95 if ( b == YOU_ARE_ENTITY ) 96 { 97 return; 98 } 99 if ( b == CREATE_ENTITY_LIST ) 100 { 101 return; 102 } 103 if ( b == REMOVE_ENTITY_LIST ) 104 { 105 return; 106 } 107 } 58 108 } 59 109 60 110 int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) 61 111 { 112 for ( int i = 0; i<outBuffer.size(); i++ ) 113 { 114 if ( outBuffer[i].length>0 ) 115 { 116 int nbytes = outBuffer[i].length; 117 118 if ( nbytes > maxLength ) 119 { 120 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength); 121 return 0; 122 } 123 124 memcpy(data, outBuffer[i].buffer, nbytes); 125 return nbytes; 126 } 127 } 128 129 int nbytes = allOutBuffer.length; 130 131 if ( nbytes <=0 ) 132 return 0; 133 134 if ( nbytes > maxLength ) 135 { 136 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength); 137 return 0; 138 } 139 140 memcpy( data, allOutBuffer.buffer, nbytes ); 141 return nbytes; 62 142 } 63 143 … … 73 153 /*! 74 154 * Checks whether this is connected to a server or a client 75 * and afterwards creates the needed entity if possible155 * and afterwards creates the needed entity 76 156 * @param classID: The ID of the class of which an entity should be created 77 157 */ 78 void NetworkGameManager::createEntity(int classID) 79 { 158 void NetworkGameManager::createEntity(ClassID classID) 159 { 160 if ( this->isServer() ) 161 { 162 if ( newUniqueID < 0 ) 163 { 164 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); 165 return; 166 } 167 168 this->executeCreateEntity( classID, newUniqueID++ ); 169 } 170 else 171 { 172 this->requestCreateEntity( classID); 173 } 80 174 } 81 175 … … 87 181 void NetworkGameManager::removeEntity(int uniqueID) 88 182 { 183 if ( this->isServer() ) 184 { 185 this->executeRemoveEntity( uniqueID ); 186 } 187 else 188 { 189 this->requestRemoveEntity( uniqueID ); 190 } 89 191 } 90 192 … … 95 197 * @param classID: The ID of the class of which an entity should be created 96 198 */ 97 void NetworkGameManager::requestCreateEntity(int classID) 98 { 199 void NetworkGameManager::requestCreateEntity(ClassID classID) 200 { 201 byte* bp; 202 byte b = REQUEST_CREATE; 203 bp = Converter::intToByteArray( classID ); 204 205 if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) ) 206 { 207 PRINTF(1)("Could not write to clientBuffer\n"); 208 return; 209 } 210 211 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 212 { 213 PRINTF(1)("Could not write to clientBuffer\n"); 214 delete bp; 215 return; 216 } 217 delete bp; 99 218 } 100 219 … … 105 224 void NetworkGameManager::requestRemoveEntity(int uniqueID) 106 225 { 226 byte* bp; 227 byte b = REQUEST_REMOVE; 228 bp = Converter::intToByteArray( uniqueID ); 229 230 if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) ) 231 { 232 PRINTF(1)("Could not write to clientBuffer\n"); 233 return; 234 } 235 236 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 237 { 238 PRINTF(1)("Could not write to clientBuffer\n"); 239 delete bp; 240 return; 241 } 242 delete bp; 107 243 } 108 244 … … 112 248 * @param classID: The ID of the class of which an entity should be created 113 249 */ 114 void NetworkGameManager::executeCreateEntity(int classID) 115 { 250 void NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) 251 { 252 byte* bp; 253 byte b = CREATE_ENTITY; 254 255 if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) ) 256 { 257 PRINTF(1)("Could not write to clientBuffer\n"); 258 return; 259 } 260 261 bp = Converter::intToByteArray( uniqueID ); 262 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 263 { 264 PRINTF(1)("Could not write to clientBuffer\n"); 265 delete bp; 266 return; 267 } 268 delete bp; 269 270 bp = Converter::intToByteArray( uniqueID ); 271 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 272 { 273 PRINTF(1)("Could not write to clientBuffer\n"); 274 delete bp; 275 return; 276 } 277 delete bp; 278 279 bp = Converter::intToByteArray( owner ); 280 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 281 { 282 PRINTF(1)("Could not write to clientBuffer\n"); 283 delete bp; 284 return; 285 } 286 delete bp; 287 288 doCreateEntity( classID, uniqueID, owner ); 116 289 } 117 290 … … 123 296 void NetworkGameManager::executeRemoveEntity(int uniqueID) 124 297 { 298 byte* bp; 299 byte b = REMOVE_ENTITY; 300 301 if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) ) 302 { 303 PRINTF(1)("Could not write to clientBuffer\n"); 304 return; 305 } 306 307 bp = Converter::intToByteArray( uniqueID ); 308 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 309 { 310 PRINTF(1)("Could not write to clientBuffer\n"); 311 delete bp; 312 return; 313 } 314 delete bp; 315 316 doRemoveEntity(uniqueID); 125 317 } 126 318 … … 129 321 * @return: true if the entity can be created, false otherwise 130 322 */ 131 bool NetworkGameManager::canCreateEntity( intclassID)323 bool NetworkGameManager::canCreateEntity(ClassID classID) 132 324 { 133 325 return true; … … 140 332 void NetworkGameManager::sendEntityList( int userID ) 141 333 { 334 if ( !isServer() ) 335 return; 336 337 if ( userID > outBuffer.size() ) 338 resizeBufferVector( userID ); 339 340 SynchronizeableList::const_iterator it, e; 341 342 it = this->networkStream->getSyncBegin(); 343 e = this->networkStream->getSyncEnd(); 344 345 byte b = CREATE_ENTITY_LIST; 346 byte* ib; 347 348 if ( !writeToClientBuffer( outBuffer[userID], &b, 1 ) ) 349 { 350 PRINTF(1)("Could not write to clientBuffer\n"); 351 return; 352 } 353 354 ib = Converter::intToByteArray( networkStream->getSyncCount() ); 355 if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) ) 356 { 357 PRINTF(1)("Could not write to clientBuffer\n"); 358 delete ib; 359 return; 360 } 361 delete ib; 362 363 while ( it != e ) 364 { 365 ib = Converter::intToByteArray( (*it)->getClassID() ); 366 if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) ) 367 { 368 PRINTF(1)("Could not write to clientBuffer\n"); 369 delete ib; 370 return; 371 } 372 delete ib; 373 374 ib = Converter::intToByteArray( (*it)->getUniqueID() ); 375 if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) ) 376 { 377 PRINTF(1)("Could not write to clientBuffer\n"); 378 delete ib; 379 return; 380 } 381 delete ib; 382 383 ib = Converter::intToByteArray( (*it)->getOwner() ); 384 if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) ) 385 { 386 PRINTF(1)("Could not write to clientBuffer\n"); 387 delete ib; 388 return; 389 } 390 delete ib; 391 392 it++; 393 } 142 394 } 143 395 … … 148 400 void NetworkGameManager::resizeBufferVector( int n ) 149 401 { 150 for ( int i = inBuffer.size(); i<=n; i++) 151 { 152 clientBuffer inBuf; 402 for ( int i = outBuffer.size(); i<=n; i++) 403 { 153 404 clientBuffer outBuf; 154 405 155 inBuf.length = 0;156 406 outBuf.length = 0; 157 407 158 inBuf.maxLength = 5*1014;159 408 outBuf.maxLength = 5*1024; 160 409 161 inBuf.buffer = new byte[5*1014];162 410 outBuf.buffer = new byte[5*1014]; 163 411 164 inBuffer.push_back(inBuf);165 412 outBuffer.push_back(outBuf); 166 413 } … … 175 422 void NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) 176 423 { 177 //BaseObject * b = Factory::fabricate( classID ); 178 BaseObject * b = NULL; 424 BaseObject * b = Factory::fabricate( classID ); 425 426 if ( !b ) 427 { 428 PRINTF(1)("Could not fabricate Object with classID %d\n", classID); 429 return; 430 } 179 431 180 432 if ( b->isA(CL_SYNCHRONIZEABLE) ) … … 209 461 break; 210 462 } 463 it++; 211 464 } 212 465 } … … 230 483 break; 231 484 } 232 } 233 } 485 it++; 486 } 487 } 488 489 /** 490 * Copies length bytes to the clientBuffer with error checking 491 * @param clientBuffer: the clientBuffer to write to 492 * @param data: buffer to the data 493 * @param length: length of data 494 * @return false on error true else 495 */ 496 bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) 497 { 498 if ( length > cb.maxLength-cb.length ) 499 { 500 PRINTF(1)("No space left in clientBuffer\n"); 501 return false; 502 } 503 504 memcpy( cb.buffer+cb.length, data, length ); 505 return true; 506 } 507 508 /** 509 * Reads data from clientBuffer with error checking 510 * @param clientBuffer: the clientBuffer to read from 511 * @param data: pointer to the buffer 512 * @param length: 513 * @return 514 */ 515 bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) 516 { 517 if ( cb.length < length ) 518 { 519 PRINTF(0)("There is not enough data in clientBuffer\n"); 520 return 0; 521 } 522 523 memcpy( data, cb.buffer+cb.length-length, length ); 524 return true; 525 } 526 527 /** 528 * Tells this client that he has to control this entity 529 * @param uniqueID: the entity's uniqeID 530 */ 531 void NetworkGameManager::doYouAre( int uniqueID ) 532 { 533 //TODO: what has to be done 534 } 535 536 /** 537 * Tells a remote client that he has to control this entity 538 * @param uniqueID: the entity's uniqeID 539 * @param userID: the users ID 540 */ 541 void NetworkGameManager::sendYouAre( int uniqueID, int userID ) 542 { 543 if ( !isServer() ) 544 return; 545 546 byte* bp; 547 byte b = YOU_ARE_ENTITY; 548 549 if ( userID != 0 ) 550 { 551 if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) ) 552 { 553 PRINTF(1)("Could not write to clientBuffer\n"); 554 return; 555 } 556 557 bp = Converter::intToByteArray( uniqueID ); 558 if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) ) 559 { 560 PRINTF(1)("Could not write to clientBuffer\n"); 561 delete bp; 562 return; 563 } 564 delete bp; 565 } 566 else 567 { 568 doYouAre(uniqueID); 569 } 570 } 571 -
branches/network/src/lib/network/network_game_manager.h
r6190 r6214 27 27 * REQUEST_REMOVE: UNIQUE_ID 28 28 * 29 * REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER]30 * REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]29 * //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER] 30 * //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 31 31 * 32 32 * REQUEST_SYNC: UNIQUE_ID 33 * REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]33 * //REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 34 34 * 35 * YOU_ARE_ENTITY: UNIQUE_ID 35 36 * 36 37 */ … … 39 40 CREATE_ENTITY = 0, 40 41 REMOVE_ENTITY, 42 CREATE_ENTITY_LIST, 43 REMOVE_ENTITY_LIST, 41 44 REQUEST_CREATE, 42 REQUEST_SYNC 45 REQUEST_REMOVE, 46 REQUEST_SYNC, 47 YOU_ARE_ENTITY 43 48 }; 44 49 … … 64 69 virtual void readDebug() const; 65 70 66 void createEntity( intclassID);71 void createEntity(ClassID classID); 67 72 void removeEntity(int uniqueID); 73 void sendYouAre( int uniqueID, int userID ); 68 74 69 75 void sync(int uniqueID); … … 72 78 73 79 private: 74 void requestCreateEntity( intclassID);75 void executeCreateEntity( int classID);80 void requestCreateEntity(ClassID classID); 81 void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0); 76 82 77 83 void requestRemoveEntity(int uniqueID); … … 81 87 void doRemoveEntity(int uniqueID); 82 88 void doRequestSync(int uniqueID, int userID); 89 void doYouAre( int uniqueID ); 83 90 84 bool canCreateEntity( intclassID);91 bool canCreateEntity(ClassID classID); 85 92 86 93 void resizeBufferVector(int n); 87 94 95 inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length ); 96 inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); 97 88 98 private: 89 std::vector<clientBuffer> inBuffer;90 99 std::vector<clientBuffer> outBuffer; 100 clientBuffer allOutBuffer; 101 102 int newUniqueID; 91 103 }; 92 104 -
branches/network/src/lib/network/network_stream.cc
r6190 r6214 382 382 return; 383 383 } 384 385 if ( n > MAX_CONNECTIONS ) 386 { 387 PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS); 388 return; 389 } 390 384 391 this->maxConnections = n; 385 392 this->networkGameManager->setUniqueID( n+2 ); -
branches/network/src/lib/network/network_stream.h
r6190 r6214 14 14 #include "server_socket.h" 15 15 #include "handshake.h" 16 17 #define MAX_CONNECTIONS 1000 16 18 17 19 class Synchronizeable; … … 51 53 inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } 52 54 inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } 55 inline int getSyncCount(){ return synchronizeables.size(); } 53 56 54 57 private:
Note: See TracChangeset
for help on using the changeset viewer.