Changeset d05086b in network-game for common/MessageProcessor.cpp
- Timestamp:
- Aug 1, 2013, 1:56:17 AM (11 years ago)
- Branches:
- master
- Children:
- 8271c78
- Parents:
- b35b2b2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
common/MessageProcessor.cpp
rb35b2b2 rd05086b 2 2 3 3 #include <iostream> 4 #include <fstream> 4 5 5 6 #include "Common.h" … … 12 13 } 13 14 14 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest ) {15 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) { 15 16 cout << "Sending message of type " << msg->type << endl; 16 17 17 18 msg->id = ++lastUsedId; 18 19 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; 20 23 21 24 sentMessages[msg->id][dest->sin_addr.s_addr] = message; … … 26 29 } 27 30 28 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source ) {31 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) { 29 32 socklen_t socklen = sizeof(struct sockaddr_in); 30 33 … … 40 43 sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true); 41 44 sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis()); 45 if (outputLog) 46 (*outputLog) << "Received ack for message id " << msg->id << endl; 42 47 } 43 48 … … 49 54 isDuplicate = true; 50 55 cout << "Got duplicate of type " << msg->type << endl; 51 }else 56 }else { 52 57 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 } 53 61 54 62 ackedMessages[msg->id] = MessageContainer(*msg, *source); … … 69 77 } 70 78 71 void MessageProcessor::resendUnackedMessages(int sock ) {79 void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) { 72 80 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it; 73 81 map<unsigned long, MessageContainer>::iterator it2; … … 84 92 } 85 93 86 void MessageProcessor::cleanAckedMessages( ) {94 void MessageProcessor::cleanAckedMessages(ofstream* outputLog) { 87 95 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin(); 88 96 map<unsigned long, MessageContainer>::iterator it2; … … 92 100 while (it2 != it->second.end()) { 93 101 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; 95 105 it->second.erase(it2++); 96 else106 }else 97 107 it2++; 98 108 }else
Note:
See TracChangeset
for help on using the changeset viewer.