Changeset d05086b in network-game


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

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • common/Common.cpp

    rb35b2b2 rd05086b  
    11#include "Common.h"
    22
    3 #include <iostream>
     3#include <sstream>
    44#include <cmath>
    55
     
    77   #include <Windows.h>
    88#elif defined LINUX
    9    #include <time.h>
     9   #include <ctime>
    1010#endif
    1111
    1212using namespace std;
    13 
    14 /*
    15 FLOAT_POSITION POSITION::toFloat() {
    16    FLOAT_POSITION floatPosition;
    17    floatPosition.x = x;
    18    floatPosition.y = y;
    19 
    20    return floatPosition;
    21 }
    22 */
    2313
    2414void set_nonblock(int sock)
     
    5141}
    5242
     43string getCurrentDateTimeString() {
     44   time_t millis = time(NULL);
     45   struct tm *time = localtime(&millis);
     46
     47   ostringstream timeString;
     48   timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
     49
     50   return timeString.str();
     51}
     52
    5353float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
    5454   float xDiff = pos2.x - pos1.x;
  • common/Common.h

    rb35b2b2 rd05086b  
    1212#endif
    1313
     14#include <string>
     15
     16using namespace std;
     17
    1418typedef struct
    1519{
     
    2226   int x;
    2327   int y;
    24    //FLOAT_POSITION toFloat();
    2528   FLOAT_POSITION toFloat() {
    2629      FLOAT_POSITION floatPosition;
     
    3437void set_nonblock(int sock);
    3538unsigned long long getCurrentMillis();
     39string getCurrentDateTimeString();
    3640float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2);
    3741
  • common/MessageProcessor.cpp

    rb35b2b2 rd05086b  
    22
    33#include <iostream>
     4#include <fstream>
    45
    56#include "Common.h"
     
    1213}
    1314
    14 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
     15int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) {
    1516   cout << "Sending message of type " << msg->type << endl;
    1617
    1718   msg->id = ++lastUsedId;
    1819   MessageContainer message(*msg, *dest);
    19    sentMessages[msg->id][dest->sin_addr.s_addr] = message;
     20
     21   if (outputLog)
     22      (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
    2023
    2124   sentMessages[msg->id][dest->sin_addr.s_addr] = message;
     
    2629}
    2730
    28 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
     31int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) {
    2932   socklen_t socklen = sizeof(struct sockaddr_in);
    3033
     
    4043         sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true);
    4144         sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis());
     45         if (outputLog)
     46            (*outputLog) << "Received ack for message id " << msg->id << endl;
    4247      }
    4348
     
    4954         isDuplicate = true;
    5055         cout << "Got duplicate of type " << msg->type << endl;
    51       }else
     56      }else {
    5257         cout << "Got message of type " << msg->type << endl;
     58         if (outputLog)
     59            (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
     60      }
    5361
    5462      ackedMessages[msg->id] = MessageContainer(*msg, *source);
     
    6977}
    7078
    71 void MessageProcessor::resendUnackedMessages(int sock) {
     79void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) {
    7280   map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
    7381   map<unsigned long, MessageContainer>::iterator it2;
     
    8492}
    8593
    86 void MessageProcessor::cleanAckedMessages() {
     94void MessageProcessor::cleanAckedMessages(ofstream* outputLog) {
    8795   map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
    8896   map<unsigned long, MessageContainer>::iterator it2;
     
    92100      while (it2 != it->second.end()) {
    93101         if (it2->second.getAcked()) {
    94             if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
     102            if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
     103               if (outputLog)
     104                  (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
    95105               it->second.erase(it2++);
    96             else
     106            }else
    97107               it2++;
    98108         }else
  • common/MessageProcessor.h

    rb35b2b2 rd05086b  
    2424   ~MessageProcessor();
    2525
    26    int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
    27    int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
    28    void resendUnackedMessages(int sock);
    29    void cleanAckedMessages();
     26   int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL);
     27   int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL);
     28   void resendUnackedMessages(int sock, ofstream* outputLog = NULL);
     29   void cleanAckedMessages(ofstream* outputLog = NULL);
    3030
    3131   map<unsigned int, map<unsigned long, MessageContainer> >& getSentMessages();
  • server/.gitignore

    rb35b2b2 rd05086b  
    11server
    22*.o
     3*.log
  • 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.