source: network-game/common/MessageProcessor.cpp@ 8271c78

Last change on this file since 8271c78 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
Line 
1#include "MessageProcessor.h"
2
3#include <iostream>
4#include <fstream>
5
6#include "Common.h"
7
8MessageProcessor::MessageProcessor() {
9 lastUsedId = 0;
10}
11
12MessageProcessor::~MessageProcessor() {
13}
14
15int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) {
16 cout << "Sending message of type " << msg->type << endl;
17
18 msg->id = ++lastUsedId;
19 MessageContainer message(*msg, *dest);
20
21 if (outputLog)
22 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
23
24 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
25
26 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
27
28 return ret;
29}
30
31int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) {
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
37 if (ret == -1)
38 return ret;
39
40 // add id to the NETWORK_MSG struct
41 if (msg->type == MSG_TYPE_ACK) {
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());
45 if (outputLog)
46 (*outputLog) << "Received ack for message id " << msg->id << endl;
47 }
48
49 return -1; // don't do any further processing
50 }else {
51 bool isDuplicate = false;
52
53 if (ackedMessages.find(msg->id) != ackedMessages.end()) {
54 isDuplicate = true;
55 cout << "Got duplicate of type " << msg->type << endl;
56 }else {
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 }
61
62 ackedMessages[msg->id] = MessageContainer(*msg, *source);
63 ackedMessages[msg->id].setAcked(true);
64 ackedMessages[msg->id].setTimeAcked(getCurrentMillis());
65
66 NETWORK_MSG ack;
67 ack.id = msg->id;
68 ack.type = MSG_TYPE_ACK;
69
70 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
71
72 if (isDuplicate)
73 return -1;
74 }
75
76 return ret;
77}
78
79void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) {
80 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
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++) {
87 if (!(it2->second.getAcked())) {
88 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
89 }
90 }
91 }
92}
93
94void MessageProcessor::cleanAckedMessages(ofstream* outputLog) {
95 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
96 map<unsigned long, MessageContainer>::iterator it2;
97
98 while (it != sentMessages.end()) {
99 it2 = it->second.begin();
100 while (it2 != it->second.end()) {
101 if (it2->second.getAcked()) {
102 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
103 if (outputLog)
104 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
105 it->second.erase(it2++);
106 }else
107 it2++;
108 }else
109 it2++;
110 }
111
112 if (it->second.size() == 0)
113 sentMessages.erase(it++);
114 else
115 it++;
116 }
117
118 /*
119 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
120
121 while (it3 != ackedMessages.end()) {
122 if ((getCurrentMillis() - it3->second) > 500)
123 ackedMessages.erase(it3++);
124 else
125 it3++;
126 }
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;
136}
Note: See TracBrowser for help on using the repository browser.