Changeset d05086b in network-game for server


Ignore:
Timestamp:
Aug 1, 2013, 1:56:17 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
8271c78
Parents:
b35b2b2
Message:

Support for logging to a textfile

Location:
server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified server/.gitignore

    rb35b2b2 rd05086b  
    11server
    22*.o
     3*.log
  • TabularUnified server/server.cpp

    rb35b2b2 rd05086b  
    55#include <iostream>
    66#include <sstream>
     7#include <fstream>
    78#include <cstring>
    89#include <cmath>
     
    1011#include <vector>
    1112#include <map>
     13
     14#include <csignal>
    1215
    1316#include <sys/time.h>
     
    3740using namespace std;
    3841
     42bool done;
     43
    3944// from used to be const. Removed that so I could take a reference
    4045// 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);
     46bool 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);
    4247
    4348void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    4550void damagePlayer(Player *p, int damage);
    4651
    47 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock);
     52void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
    4853
    4954// this should probably go somewhere in the common folder
     
    7984
    8085   return NULL;
     86}
     87
     88void quit(int sig) {
     89   done = true;
    8190}
    8291
     
    92101   unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    93102   int scoreBlue, scoreRed;
     103   ofstream outputLog;
     104
     105   done = false;
    94106
    95107   scoreBlue = 0;
    96108   scoreRed = 0;
     109
     110   signal(SIGINT, quit);
    97111
    98112   //SSL_load_error_strings();
     
    104118      exit(1);
    105119   }
     120
     121   outputLog.open("server.log", ios::app);
     122   outputLog << "Started server on " << getCurrentDateTimeString() << endl;
    106123
    107124   WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
     
    138155   timespec ts;
    139156   int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
    140    while (true) {
     157   while (!done) {
    141158
    142159      usleep(5000);
     
    150167         timeLastUpdated = curTime;
    151168
    152          msgProcessor.cleanAckedMessages();
    153          msgProcessor.resendUnackedMessages(sock);
     169         msgProcessor.cleanAckedMessages(&outputLog);
     170         msgProcessor.resendUnackedMessages(sock, &outputLog);
    154171
    155172         map<unsigned int, Player>::iterator it;
     
    191208                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    192209                  {
    193                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     210                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    194211                        error("sendMessage");
    195212                  }
     
    206223               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    207224               {
    208                   if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     225                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    209226                     error("sendMessage");
    210227               }
     
    319336                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    320337                  {
    321                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     338                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    322339                        error("sendMessage");
    323340                  }
     
    329346                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    330347                  {
    331                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     348                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    332349                        error("sendMessage");
    333350                  }
     
    370387                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    371388                  {
    372                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     389                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    373390                        error("sendMessage");
    374391                  }
     
    383400                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    384401                  {
    385                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     402                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    386403                        error("sendMessage");
    387404                  }
     
    404421               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    405422               {
    406                   if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     423                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    407424                     error("sendMessage");
    408425               }
     
    422439
    423440                     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);
    425442                     }
    426443                  }
     
    452469               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    453470               {
    454                   if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     471                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    455472                     error("sendMessage");
    456473               }
     
    474491               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    475492               {
    476                   if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     493                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    477494                     error("sendMessage");
    478495               }
     
    492509
    493510                  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);
    495512                  }
    496513               }
     
    502519               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    503520               {
    504                   if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     521                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
    505522                     error("sendMessage");
    506523               }
     
    510527      }
    511528
    512       n = msgProcessor.receiveMessage(&clientMsg, sock, &from);
     529      n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
    513530
    514531      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);
    516533
    517534         if (broadcastResponse)
     
    523540            {
    524541               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 )
    526543                  error("sendMessage");
    527544            }
     
    531548            cout << "Should be sending back the message" << endl;
    532549
    533             if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
     550            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    534551               error("sendMessage");
    535552         }
    536553      }
    537554   }
     555
     556   outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
     557   outputLog.close();
    538558
    539559   return 0;
    540560}
    541561
    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)
     562bool 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)
    543563{
    544564   DataAccess da;
     
    630650               cout << "sending info about " << it->second.name  << endl;
    631651               cout << "sending id " << it->second.id  << endl;
    632                if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
     652               if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    633653                  error("sendMessage");
    634654            }
     
    643663               itObjects->serialize(serverMsg.buffer);
    644664               cout << "sending item id " << itObjects->id  << endl;
    645                if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
     665               if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    646666                  error("sendMessage");
    647667            }
     
    651671            memcpy(serverMsg.buffer, &scoreBlue, 4);
    652672            memcpy(serverMsg.buffer+4, &scoreRed, 4);
    653             if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
     673            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
    654674               error("sendMessage");
    655675
     
    661681            {
    662682               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 )
    664684                  error("sendMessage");
    665685            }
     
    701721
    702722               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);
    704724               }
    705725            }
     
    826846                  for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    827847                  {
    828                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     848                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
    829849                        error("sendMessage");
    830850                  }
     
    862882            flagType = WorldMap::OBJECT_RED_FLAG;
    863883
    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);
    865885
    866886         mapPlayers[id].hasBlueFlag = false;
     
    942962}
    943963
    944 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock) {
     964void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
    945965   NETWORK_MSG serverMsg;
    946966
     
    954974   for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    955975   {
    956       if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     976      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
    957977         error("sendMessage");
    958978   }
Note: See TracChangeset for help on using the changeset viewer.