- Timestamp:
- Jul 14, 2013, 9:22:38 PM (12 years ago)
- Branches:
- master
- Children:
- 10f6fc2
- Parents:
- 5a64bea
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r5a64bea r9b5d30b 29 29 #include "../common/Common.h" 30 30 #include "../common/Message.h" 31 #include "../common/MessageProcessor.h" 31 32 #include "../common/WorldMap.h" 32 33 #include "../common/Player.h" … … 39 40 // from used to be const. Removed that so I could take a reference 40 41 // and use it to send messages 41 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);42 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed); 42 43 43 44 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers); … … 85 86 struct sockaddr_in from; // info of client sending the message 86 87 NETWORK_MSG clientMsg, serverMsg; 88 MessageProcessor msgProcessor; 87 89 map<unsigned int, Player> mapPlayers; 88 90 map<unsigned int, Projectile> mapProjectiles; … … 146 148 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) { 147 149 timeLastUpdated = curTime; 150 151 msgProcessor.resendUnackedMessages(sock); 148 152 149 153 map<unsigned int, Player>::iterator it; … … 185 189 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 186 190 { 187 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )191 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 188 192 error("sendMessage"); 189 193 } … … 200 204 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 201 205 { 202 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )206 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 203 207 error("sendMessage"); 204 208 } … … 313 317 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 314 318 { 315 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )319 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 316 320 error("sendMessage"); 317 321 } … … 323 327 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 324 328 { 325 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )329 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 326 330 error("sendMessage"); 327 331 } … … 364 368 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 365 369 { 366 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )370 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 367 371 error("sendMessage"); 368 372 } … … 377 381 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 378 382 { 379 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )383 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 380 384 error("sendMessage"); 381 385 } … … 398 402 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 399 403 { 400 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )404 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 401 405 error("sendMessage"); 402 406 } … … 434 438 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 435 439 { 436 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )440 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 437 441 error("sendMessage"); 438 442 } … … 456 460 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 457 461 { 458 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )462 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 459 463 error("sendMessage"); 460 464 } … … 472 476 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 473 477 { 474 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )478 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 475 479 error("sendMessage"); 476 480 } … … 480 484 } 481 485 482 n = receiveMessage(&clientMsg, sock, &from);486 n = msgProcessor.receiveMessage(&clientMsg, sock, &from); 483 487 484 488 if (n >= 0) { … … 493 497 { 494 498 cout << "Sent message back to " << it->second.name << endl; 495 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )499 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 496 500 error("sendMessage"); 497 501 } … … 501 505 cout << "Should be sending back the message" << endl; 502 506 503 if ( sendMessage(&serverMsg, sock, &from) < 0 )507 if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 ) 504 508 error("sendMessage"); 505 509 } … … 510 514 } 511 515 512 bool processMessage(const NETWORK_MSG & clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG&serverMsg, int sock, int &scoreBlue, int &scoreRed)516 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed) 513 517 { 514 518 DataAccess da; … … 595 599 cout << "sending info about " << it->second.name << endl; 596 600 cout << "sending id " << it->second.id << endl; 597 if ( sendMessage(&serverMsg, sock, &from) < 0 )601 if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 ) 598 602 error("sendMessage"); 599 603 } … … 608 612 itObjects->serialize(serverMsg.buffer); 609 613 cout << "sending item id " << itObjects->id << endl; 610 if ( sendMessage(&serverMsg, sock, &from) < 0 )614 if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 ) 611 615 error("sendMessage"); 612 616 } … … 616 620 memcpy(serverMsg.buffer, &scoreBlue, 4); 617 621 memcpy(serverMsg.buffer+4, &scoreRed, 4); 618 if ( sendMessage(&serverMsg, sock, &from) < 0 )622 if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 ) 619 623 error("sendMessage"); 620 624 … … 626 630 { 627 631 cout << "Sent message back to " << it->second.name << endl; 628 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )632 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 629 633 error("sendMessage"); 630 634 } … … 779 783 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 780 784 { 781 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )785 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 782 786 error("sendMessage"); 783 787 } … … 824 828 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 825 829 { 826 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )830 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 827 831 error("sendMessage"); 828 832 }
Note:
See TracChangeset
for help on using the changeset viewer.