Changeset 9b5d30b in network-game
- Timestamp:
- Jul 14, 2013, 9:22:38 PM (12 years ago)
- Branches:
- master
- Children:
- 10f6fc2
- Parents:
- 5a64bea
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Message.cpp
r5a64bea r9b5d30b 15 15 using namespace std; 16 16 17 /* 17 18 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) 18 19 { … … 33 34 return ret; 34 35 } 36 */ -
common/Message.h
r5a64bea r9b5d30b 26 26 } NETWORK_MSG; 27 27 28 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);28 //int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest); 29 29 30 int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source);30 //int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source); 31 31 32 32 #endif -
common/MessageProcessor.cpp
r5a64bea r9b5d30b 11 11 12 12 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) { 13 msg->id = ++lastUsedId; 13 14 MessageContainer message(*msg, *dest); 14 message.id = ++lastUsedId; 15 sentMessages[message.id] = message; 15 sentMessages[msg->id] = message; 16 16 17 17 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in)); -
common/MessageProcessor.h
r5a64bea r9b5d30b 59 59 60 60 MessageContainer(const MessageContainer& mc) { 61 this-> id = mc.id;61 this->msg = mc.msg; 62 62 this->clientAddr = mc.clientAddr; 63 this->msg = mc.msg;64 this->ackReceived = mc.ackReceived;65 63 } 66 64 … … 73 71 } 74 72 75 int id;73 NETWORK_MSG msg; 76 74 struct sockaddr_in clientAddr; 77 NETWORK_MSG msg;78 bool ackReceived;79 75 }; 80 76 -
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.