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
Line 
1#include "MessageProcessor.h"
2
3#include <iostream>
4
5#include "Common.h"
6
7MessageProcessor::MessageProcessor() {
8 lastUsedId = 0;
9}
10
11MessageProcessor::~MessageProcessor() {
12}
13
14int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
15 msg->id = ++lastUsedId;
16 MessageContainer message(*msg, *dest);
17 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
18
19 cout << "Sending message" << endl;
20 cout << "id: " << msg->id << endl;
21 cout << "type: " << msg->type << endl;
22 cout << "buffer: " << msg->buffer << endl;
23
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;
29}
30
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
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].isAcked) {
43 cout << "Received new ack" << endl;
44 sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true;
45 sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis();
46 }
47
48 return -1; // don't do any further processing
49 }else {
50 bool isDuplicate = false;
51
52 cout << "Received message" << endl;
53 cout << "id: " << msg->id << endl;
54 cout << "type: " << msg->type << endl;
55 cout << "buffer: " << msg->buffer << endl;
56
57 if (ackedMessages.find(msg->id) == ackedMessages.end())
58 isDuplicate = true;
59
60 ackedMessages[msg->id] = getCurrentMillis();
61
62 NETWORK_MSG ack;
63 ack.id = msg->id;
64 ack.type = MSG_TYPE_ACK;
65
66 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
67
68 if (isDuplicate)
69 return -1;
70 }
71
72 return ret;
73}
74
75void MessageProcessor::resendUnackedMessages(int sock) {
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 }
85 }
86}
87
88void MessageProcessor::cleanAckedMessages() {
89 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
90 map<unsigned long, MessageContainer>::iterator it2;
91
92 while (it != sentMessages.end()) {
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++;
100 }else
101 it2++;
102 }
103
104 if (it->second.size() == 0)
105 sentMessages.erase(it++);
106 else
107 it++;
108 }
109
110 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
111
112 while (it3 != ackedMessages.end()) {
113 if ((getCurrentMillis() - it3->second) > 500) {
114 ackedMessages.erase(it3++);
115 cout << "Deleting ack record" << endl;
116 }else
117 it3++;
118 }
119}
Note: See TracBrowser for help on using the repository browser.