Changeset d05086b in network-game for common/MessageProcessor.cpp


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.