source: network-game/common/MessageProcessor.cpp@ 8ed6c8a

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

Debug message acks

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[1a3c42d]1#include "MessageProcessor.h"
2
[5a64bea]3#include <iostream>
[d05086b]4#include <fstream>
[5a64bea]5
[e6c26b8]6#include "Compiler.h"
7
8#if defined WINDOWS
9 #include <ws2tcpip.h>
10#endif
11
12#include "Common.h"
13
[5a64bea]14MessageProcessor::MessageProcessor() {
[68d94de]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;
[5a64bea]23 lastUsedId = 0;
24}
25
26MessageProcessor::~MessageProcessor() {
27}
28
[68d94de]29int MessageProcessor::sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest) {
[46d6469]30 cout << "Sending message of type " << msg->type << endl;
31
[9b5d30b]32 msg->id = ++lastUsedId;
[5a64bea]33 MessageContainer message(*msg, *dest);
[8ed6c8a]34 message.setTimeSent(getCurrentMillis());
[d05086b]35
36 if (outputLog)
37 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[5a64bea]38
[46d6469]39 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
[bd2502a]40
[5a64bea]41 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
42
[8554263]43 if (ret < 0)
44 error("sendMessage");
[0678d60]45
46 return ret;
[1a3c42d]47}
48
[68d94de]49int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
[5a64bea]50 socklen_t socklen = sizeof(struct sockaddr_in);
51
52 // assume we don't care about the value of socklen
[ea17281]53 //cout << "Waiting for message from server" << endl;
[5a64bea]54 int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
[ea17281]55 //cout << "Returned from wait" << endl;
[5a64bea]56
[6b641af]57 if (ret == -1)
58 return ret;
59
[5a64bea]60 // add id to the NETWORK_MSG struct
61 if (msg->type == MSG_TYPE_ACK) {
[46d6469]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());
[d05086b]65 if (outputLog)
66 (*outputLog) << "Received ack for message id " << msg->id << endl;
[6b641af]67 }
[198cf2d]68
69 return -1; // don't do any further processing
[5a64bea]70 }else {
[bace57b]71 bool isDuplicate = false;
[f9cb9fb]72 map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
[bace57b]73
[f9cb9fb]74 if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
[bace57b]75 isDuplicate = true;
[934ab53]76 cout << "Got duplicate of type " << msg->type << endl;
[f9cb9fb]77 if (outputLog)
78 (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[d05086b]79 }else {
[934ab53]80 cout << "Got message of type " << msg->type << endl;
[d05086b]81 if (outputLog)
82 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
83 }
[bace57b]84
[f9cb9fb]85 ackedPlayerMessages[msg->id] = getCurrentMillis();
[855f153]86
[bace57b]87 NETWORK_MSG ack;
88 ack.id = msg->id;
89 ack.type = MSG_TYPE_ACK;
[bd2502a]90
[bace57b]91 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]92
[bace57b]93 if (isDuplicate)
[3794f6d]94 return -1;
[5a64bea]95 }
96
97 return ret;
[1a3c42d]98}
99
[d05c484]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
[68d94de]107void MessageProcessor::resendUnackedMessages() {
[b35b2b2]108 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
[bace57b]109 map<unsigned long, MessageContainer>::iterator it2;
[8ed6c8a]110 map<unsigned long, MessageContainer>* sentMsg;
[bace57b]111
112 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
[8ed6c8a]113
114 unsigned long long maxAge = 0;
115 sentMsg = &(it->second);
116
117 for (it2 = sentMsg->begin(); it2 != sentMsg->end(); it2++) {
[46d6469]118 if (!(it2->second.getAcked())) {
[8ed6c8a]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
[9fe1807]136 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]137 }
[bace57b]138 }
[8ed6c8a]139
140 if (maxAge > 10000) {
141 cout << "Connection lost" << endl;
142 }
[5a64bea]143 }
[1a3c42d]144}
145
[68d94de]146void MessageProcessor::cleanAckedMessages() {
[b35b2b2]147 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
[bace57b]148 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]149
[af713bc]150 while (it != sentMessages.end()) {
[bace57b]151 it2 = it->second.begin();
[46d6469]152 while (it2 != it->second.end()) {
153 if (it2->second.getAcked()) {
[d05086b]154 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
155 if (outputLog)
156 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
[bace57b]157 it->second.erase(it2++);
[d05086b]158 }else
[bace57b]159 it2++;
[6b641af]160 }else
[bace57b]161 it2++;
162 }
163
164 if (it->second.size() == 0)
165 sentMessages.erase(it++);
166 else
[af713bc]167 it++;
[198cf2d]168 }
[4dbac87]169
[f9cb9fb]170 map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
171 map<unsigned int, unsigned long long>::iterator it4;
[4dbac87]172
[f9cb9fb]173 // somehow want to delete the inner map once that player logs out
[bace57b]174 while (it3 != ackedMessages.end()) {
[f9cb9fb]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++;
[4dbac87]183 }
[b35b2b2]184}
185
186map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
187 return this->sentMessages;
188}
189
[f9cb9fb]190map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
[b35b2b2]191 return this->ackedMessages;
[1a3c42d]192}
Note: See TracBrowser for help on using the repository browser.