source: network-game/common/MessageProcessor.cpp@ d05086b

Last change on this file since d05086b was d05086b, checked in by dportnoy <dmp1488@…>, 11 years ago

Support for logging to a textfile

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[1a3c42d]1#include "MessageProcessor.h"
2
[5a64bea]3#include <iostream>
[d05086b]4#include <fstream>
[5a64bea]5
[198cf2d]6#include "Common.h"
7
[5a64bea]8MessageProcessor::MessageProcessor() {
9 lastUsedId = 0;
10}
11
12MessageProcessor::~MessageProcessor() {
13}
14
[d05086b]15int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) {
[46d6469]16 cout << "Sending message of type " << msg->type << endl;
17
[9b5d30b]18 msg->id = ++lastUsedId;
[5a64bea]19 MessageContainer message(*msg, *dest);
[d05086b]20
21 if (outputLog)
22 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[5a64bea]23
[46d6469]24 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
[bd2502a]25
[5a64bea]26 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
27
28 return ret;
[1a3c42d]29}
30
[d05086b]31int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) {
[5a64bea]32 socklen_t socklen = sizeof(struct sockaddr_in);
33
34 // assume we don't care about the value of socklen
35 int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
36
[6b641af]37 if (ret == -1)
38 return ret;
39
[5a64bea]40 // add id to the NETWORK_MSG struct
41 if (msg->type == MSG_TYPE_ACK) {
[46d6469]42 if (!sentMessages[msg->id][source->sin_addr.s_addr].getAcked()) {
43 sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true);
44 sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis());
[d05086b]45 if (outputLog)
46 (*outputLog) << "Received ack for message id " << msg->id << endl;
[6b641af]47 }
[198cf2d]48
49 return -1; // don't do any further processing
[5a64bea]50 }else {
[bace57b]51 bool isDuplicate = false;
52
[934ab53]53 if (ackedMessages.find(msg->id) != ackedMessages.end()) {
[bace57b]54 isDuplicate = true;
[934ab53]55 cout << "Got duplicate of type " << msg->type << endl;
[d05086b]56 }else {
[934ab53]57 cout << "Got message of type " << msg->type << endl;
[d05086b]58 if (outputLog)
59 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
60 }
[bace57b]61
[b35b2b2]62 ackedMessages[msg->id] = MessageContainer(*msg, *source);
63 ackedMessages[msg->id].setAcked(true);
64 ackedMessages[msg->id].setTimeAcked(getCurrentMillis());
[855f153]65
[bace57b]66 NETWORK_MSG ack;
67 ack.id = msg->id;
68 ack.type = MSG_TYPE_ACK;
[bd2502a]69
[bace57b]70 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]71
[bace57b]72 if (isDuplicate)
[3794f6d]73 return -1;
[5a64bea]74 }
75
76 return ret;
[1a3c42d]77}
78
[d05086b]79void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) {
[b35b2b2]80 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
[bace57b]81 map<unsigned long, MessageContainer>::iterator it2;
82 map<unsigned long, MessageContainer> sentMsg;
83
84 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
85 sentMsg = it->second;
86 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
[46d6469]87 if (!(it2->second.getAcked())) {
[9fe1807]88 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]89 }
[bace57b]90 }
[5a64bea]91 }
[1a3c42d]92}
93
[d05086b]94void MessageProcessor::cleanAckedMessages(ofstream* outputLog) {
[b35b2b2]95 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
[bace57b]96 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]97
[af713bc]98 while (it != sentMessages.end()) {
[bace57b]99 it2 = it->second.begin();
[46d6469]100 while (it2 != it->second.end()) {
101 if (it2->second.getAcked()) {
[d05086b]102 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
103 if (outputLog)
104 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
[bace57b]105 it->second.erase(it2++);
[d05086b]106 }else
[bace57b]107 it2++;
[6b641af]108 }else
[bace57b]109 it2++;
110 }
111
112 if (it->second.size() == 0)
113 sentMessages.erase(it++);
114 else
[af713bc]115 it++;
[198cf2d]116 }
[4dbac87]117
[b35b2b2]118 /*
[bace57b]119 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
[4dbac87]120
[bace57b]121 while (it3 != ackedMessages.end()) {
[46d6469]122 if ((getCurrentMillis() - it3->second) > 500)
[bace57b]123 ackedMessages.erase(it3++);
[46d6469]124 else
[bace57b]125 it3++;
[4dbac87]126 }
[b35b2b2]127 */
128}
129
130map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
131 return this->sentMessages;
132}
133
134map<unsigned int, MessageContainer>& MessageProcessor::getAckedMessages() {
135 return this->ackedMessages;
[1a3c42d]136}
Note: See TracBrowser for help on using the repository browser.