- Timestamp:
- Aug 1, 2013, 1:56:17 AM (11 years ago)
- Branches:
- master
- Children:
- 8271c78
- Parents:
- b35b2b2
- Location:
- server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified server/.gitignore ¶
rb35b2b2 rd05086b 1 1 server 2 2 *.o 3 *.log -
TabularUnified server/server.cpp ¶
rb35b2b2 rd05086b 5 5 #include <iostream> 6 6 #include <sstream> 7 #include <fstream> 7 8 #include <cstring> 8 9 #include <cmath> … … 10 11 #include <vector> 11 12 #include <map> 13 14 #include <csignal> 12 15 13 16 #include <sys/time.h> … … 37 40 using namespace std; 38 41 42 bool done; 43 39 44 // from used to be const. Removed that so I could take a reference 40 45 // and use it to send messages 41 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 );46 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, ofstream& outputLog); 42 47 43 48 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers); … … 45 50 void damagePlayer(Player *p, int damage); 46 51 47 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock );52 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog); 48 53 49 54 // this should probably go somewhere in the common folder … … 79 84 80 85 return NULL; 86 } 87 88 void quit(int sig) { 89 done = true; 81 90 } 82 91 … … 92 101 unsigned int unusedPlayerId = 1, unusedProjectileId = 1; 93 102 int scoreBlue, scoreRed; 103 ofstream outputLog; 104 105 done = false; 94 106 95 107 scoreBlue = 0; 96 108 scoreRed = 0; 109 110 signal(SIGINT, quit); 97 111 98 112 //SSL_load_error_strings(); … … 104 118 exit(1); 105 119 } 120 121 outputLog.open("server.log", ios::app); 122 outputLog << "Started server on " << getCurrentDateTimeString() << endl; 106 123 107 124 WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt"); … … 138 155 timespec ts; 139 156 int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0; 140 while ( true) {157 while (!done) { 141 158 142 159 usleep(5000); … … 150 167 timeLastUpdated = curTime; 151 168 152 msgProcessor.cleanAckedMessages( );153 msgProcessor.resendUnackedMessages(sock );169 msgProcessor.cleanAckedMessages(&outputLog); 170 msgProcessor.resendUnackedMessages(sock, &outputLog); 154 171 155 172 map<unsigned int, Player>::iterator it; … … 191 208 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 192 209 { 193 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )210 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 194 211 error("sendMessage"); 195 212 } … … 206 223 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 207 224 { 208 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )225 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 209 226 error("sendMessage"); 210 227 } … … 319 336 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 320 337 { 321 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )338 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 322 339 error("sendMessage"); 323 340 } … … 329 346 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 330 347 { 331 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )348 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 332 349 error("sendMessage"); 333 350 } … … 370 387 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 371 388 { 372 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )389 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 373 390 error("sendMessage"); 374 391 } … … 383 400 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 384 401 { 385 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )402 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 386 403 error("sendMessage"); 387 404 } … … 404 421 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 405 422 { 406 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )423 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 407 424 error("sendMessage"); 408 425 } … … 422 439 423 440 if (flagType != WorldMap::OBJECT_NONE) { 424 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock );441 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 425 442 } 426 443 } … … 452 469 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 453 470 { 454 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )471 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 455 472 error("sendMessage"); 456 473 } … … 474 491 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 475 492 { 476 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )493 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 477 494 error("sendMessage"); 478 495 } … … 492 509 493 510 if (flagType != WorldMap::OBJECT_NONE) { 494 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock );511 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 495 512 } 496 513 } … … 502 519 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 503 520 { 504 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr) ) < 0 )521 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 ) 505 522 error("sendMessage"); 506 523 } … … 510 527 } 511 528 512 n = msgProcessor.receiveMessage(&clientMsg, sock, &from );529 n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog); 513 530 514 531 if (n >= 0) { 515 broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed );532 broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog); 516 533 517 534 if (broadcastResponse) … … 523 540 { 524 541 cout << "Sent message back to " << it->second.name << endl; 525 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr) ) < 0 )542 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 ) 526 543 error("sendMessage"); 527 544 } … … 531 548 cout << "Should be sending back the message" << endl; 532 549 533 if ( msgProcessor.sendMessage(&serverMsg, sock, &from ) < 0 )550 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 534 551 error("sendMessage"); 535 552 } 536 553 } 537 554 } 555 556 outputLog << "Stopped server on " << getCurrentDateTimeString() << endl; 557 outputLog.close(); 538 558 539 559 return 0; 540 560 } 541 561 542 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 )562 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, ofstream& outputLog) 543 563 { 544 564 DataAccess da; … … 630 650 cout << "sending info about " << it->second.name << endl; 631 651 cout << "sending id " << it->second.id << endl; 632 if ( msgProcessor.sendMessage(&serverMsg, sock, &from ) < 0 )652 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 633 653 error("sendMessage"); 634 654 } … … 643 663 itObjects->serialize(serverMsg.buffer); 644 664 cout << "sending item id " << itObjects->id << endl; 645 if ( msgProcessor.sendMessage(&serverMsg, sock, &from ) < 0 )665 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 646 666 error("sendMessage"); 647 667 } … … 651 671 memcpy(serverMsg.buffer, &scoreBlue, 4); 652 672 memcpy(serverMsg.buffer+4, &scoreRed, 4); 653 if ( msgProcessor.sendMessage(&serverMsg, sock, &from ) < 0 )673 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 654 674 error("sendMessage"); 655 675 … … 661 681 { 662 682 cout << "Sent message back to " << it->second.name << endl; 663 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr) ) < 0 )683 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 ) 664 684 error("sendMessage"); 665 685 } … … 701 721 702 722 if (flagType != WorldMap::OBJECT_NONE) { 703 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock );723 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 704 724 } 705 725 } … … 826 846 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 827 847 { 828 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr) ) < 0 )848 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 ) 829 849 error("sendMessage"); 830 850 } … … 862 882 flagType = WorldMap::OBJECT_RED_FLAG; 863 883 864 addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock );884 addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 865 885 866 886 mapPlayers[id].hasBlueFlag = false; … … 942 962 } 943 963 944 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock ) {964 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) { 945 965 NETWORK_MSG serverMsg; 946 966 … … 954 974 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 955 975 { 956 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr) ) < 0 )976 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 ) 957 977 error("sendMessage"); 958 978 }
Note:
See TracChangeset
for help on using the changeset viewer.