Changeset d05086b in network-game for common


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:
common
Files:
4 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();
Note: See TracChangeset for help on using the changeset viewer.