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

Last change on this file since 0065962 was 8ed6c8a, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Debug message acks

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#include "MessageProcessor.h"
2
3#include <iostream>
4#include <fstream>
5
6#include "Compiler.h"
7
8#if defined WINDOWS
9 #include <ws2tcpip.h>
10#endif
11
12#include "Common.h"
13
14MessageProcessor::MessageProcessor() {
15 this->sock = 0;
16 this->outputLog = NULL;
17 lastUsedId = 0;
18}
19
20MessageProcessor::MessageProcessor(int sock, ofstream* outputLog) {
21 this->sock = sock;
22 this->outputLog = outputLog;
23 lastUsedId = 0;
24}
25
26MessageProcessor::~MessageProcessor() {
27}
28
29int MessageProcessor::sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest) {
30 cout << "Sending message of type " << msg->type << endl;
31
32 msg->id = ++lastUsedId;
33 MessageContainer message(*msg, *dest);
34 message.setTimeSent(getCurrentMillis());
35
36 if (outputLog)
37 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
38
39 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
40
41 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
42
43 if (ret < 0)
44 error("sendMessage");
45
46 return ret;
47}
48
49int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
50 socklen_t socklen = sizeof(struct sockaddr_in);
51
52 // assume we don't care about the value of socklen
53 //cout << "Waiting for message from server" << endl;
54 int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
55 //cout << "Returned from wait" << endl;
56
57 if (ret == -1)
58 return ret;
59
60 // add id to the NETWORK_MSG struct
61 if (msg->type == MSG_TYPE_ACK) {
62 if (!sentMessages[msg->id][source->sin_addr.s_addr].getAcked()) {
63 sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true);
64 sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis());
65 if (outputLog)
66 (*outputLog) << "Received ack for message id " << msg->id << endl;
67 }
68
69 return -1; // don't do any further processing
70 }else {
71 bool isDuplicate = false;
72 map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
73
74 if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
75 isDuplicate = true;
76 cout << "Got duplicate of type " << msg->type << endl;
77 if (outputLog)
78 (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
79 }else {
80 cout << "Got message of type " << msg->type << endl;
81 if (outputLog)
82 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
83 }
84
85 ackedPlayerMessages[msg->id] = getCurrentMillis();
86
87 NETWORK_MSG ack;
88 ack.id = msg->id;
89 ack.type = MSG_TYPE_ACK;
90
91 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
92
93 if (isDuplicate)
94 return -1;
95 }
96
97 return ret;
98}
99
100void MessageProcessor::broadcastMessage(NETWORK_MSG &msg, map<unsigned int, Player*>& players) {
101 map<unsigned int, Player*>::iterator it;
102 for (it = players.begin(); it != players.end(); it++) {
103 this->sendMessage(&msg, &(it->second->addr));
104 }
105}
106
107void MessageProcessor::resendUnackedMessages() {
108 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
109 map<unsigned long, MessageContainer>::iterator it2;
110 map<unsigned long, MessageContainer>* sentMsg;
111
112 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
113
114 unsigned long long maxAge = 0;
115 sentMsg = &(it->second);
116
117 for (it2 = sentMsg->begin(); it2 != sentMsg->end(); it2++) {
118 if (!(it2->second.getAcked())) {
119 cout << "Maybe resending message" << endl;
120 cout << "time sent: " << it2->second.getTimeSent() << endl;
121 unsigned long long age = getCurrentMillis() - it2->second.getTimeSent();
122
123 cout << "age: " << age << endl;
124 if (maxAge < age) {
125 maxAge = age;
126 cout << "new max age: " << maxAge << endl;
127 }
128
129 if (maxAge > 10000) {
130 cout << "id " << it2->second.getMessage()->id << " is not getting acked" << endl;
131 // this will prevent the message from getting resent anymore
132 it2->second.setAcked(true);
133 cout << "acked after being set: " << it2->second.getAcked() << endl;
134 }
135
136 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
137 }
138 }
139
140 if (maxAge > 10000) {
141 cout << "Connection lost" << endl;
142 }
143 }
144}
145
146void MessageProcessor::cleanAckedMessages() {
147 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
148 map<unsigned long, MessageContainer>::iterator it2;
149
150 while (it != sentMessages.end()) {
151 it2 = it->second.begin();
152 while (it2 != it->second.end()) {
153 if (it2->second.getAcked()) {
154 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
155 if (outputLog)
156 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
157 it->second.erase(it2++);
158 }else
159 it2++;
160 }else
161 it2++;
162 }
163
164 if (it->second.size() == 0)
165 sentMessages.erase(it++);
166 else
167 it++;
168 }
169
170 map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
171 map<unsigned int, unsigned long long>::iterator it4;
172
173 // somehow want to delete the inner map once that player logs out
174 while (it3 != ackedMessages.end()) {
175 it4 = it3->second.begin();
176 while (it4 != it3->second.end()) {
177 if ((getCurrentMillis() - it4->second) > 500)
178 it3->second.erase(it4++);
179 else
180 it4++;
181 }
182 it3++;
183 }
184}
185
186map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
187 return this->sentMessages;
188}
189
190map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
191 return this->ackedMessages;
192}
Note: See TracBrowser for help on using the repository browser.