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

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

MessageProcessor stores the recipient address along with each sent message, so it can properly resend messages

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[1a3c42d]1#include "MessageProcessor.h"
2
[5a64bea]3#include <iostream>
4
[198cf2d]5#include "Common.h"
6
[5a64bea]7MessageProcessor::MessageProcessor() {
8 lastUsedId = 0;
9}
10
11MessageProcessor::~MessageProcessor() {
12}
13
[1a3c42d]14int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
[9b5d30b]15 msg->id = ++lastUsedId;
[5a64bea]16 MessageContainer message(*msg, *dest);
[bace57b]17 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
[5a64bea]18
[bd2502a]19 cout << "Sending message" << endl;
20 cout << "id: " << msg->id << endl;
21 cout << "type: " << msg->type << endl;
22 cout << "buffer: " << msg->buffer << endl;
23
[5a64bea]24 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
25
26 cout << "Send a message of type " << msg->type << endl;
27
28 return ret;
[1a3c42d]29}
30
[5a64bea]31int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
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) {
[bace57b]42 if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) {
[5755e68]43 cout << "Received new ack" << endl;
[bace57b]44 sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true;
45 sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis();
[6b641af]46 }
[198cf2d]47
48 return -1; // don't do any further processing
[5a64bea]49 }else {
[bace57b]50 bool isDuplicate = false;
51
[4dbac87]52 cout << "Received message" << endl;
53 cout << "id: " << msg->id << endl;
54 cout << "type: " << msg->type << endl;
55 cout << "buffer: " << msg->buffer << endl;
56
[bace57b]57 if (ackedMessages.find(msg->id) == ackedMessages.end())
58 isDuplicate = true;
59
60 ackedMessages[msg->id] = getCurrentMillis();
[855f153]61
[bace57b]62 NETWORK_MSG ack;
63 ack.id = msg->id;
64 ack.type = MSG_TYPE_ACK;
[bd2502a]65
[bace57b]66 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]67
[bace57b]68 if (isDuplicate)
[3794f6d]69 return -1;
[5a64bea]70 }
71
72 return ret;
[1a3c42d]73}
74
[5a64bea]75void MessageProcessor::resendUnackedMessages(int sock) {
[bace57b]76 map<int, map<unsigned long, MessageContainer> >::iterator it;
77 map<unsigned long, MessageContainer>::iterator it2;
78 map<unsigned long, MessageContainer> sentMsg;
79
80 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
81 sentMsg = it->second;
82 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
83 sendto(sock, (char*)&it2->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
84 }
[5a64bea]85 }
[1a3c42d]86}
87
88void MessageProcessor::cleanAckedMessages() {
[bace57b]89 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
90 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]91
[af713bc]92 while (it != sentMessages.end()) {
[bace57b]93 it2 = it->second.begin();
94 while (it2 != it->second.begin()) {
95 if (it2->second.isAcked) {
96 if ((getCurrentMillis() - it2->second.timeAcked) > 1000)
97 it->second.erase(it2++);
98 else
99 it2++;
[6b641af]100 }else
[bace57b]101 it2++;
102 }
103
104 if (it->second.size() == 0)
105 sentMessages.erase(it++);
106 else
[af713bc]107 it++;
[198cf2d]108 }
[4dbac87]109
[bace57b]110 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
[4dbac87]111
[bace57b]112 while (it3 != ackedMessages.end()) {
113 if ((getCurrentMillis() - it3->second) > 500) {
114 ackedMessages.erase(it3++);
[855f153]115 cout << "Deleting ack record" << endl;
116 }else
[bace57b]117 it3++;
[4dbac87]118 }
[1a3c42d]119}
Note: See TracBrowser for help on using the repository browser.