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 |
|
---|
14 | MessageProcessor::MessageProcessor() {
|
---|
15 | this->sock = 0;
|
---|
16 | this->outputLog = NULL;
|
---|
17 | lastUsedId = 0;
|
---|
18 | }
|
---|
19 |
|
---|
20 | MessageProcessor::MessageProcessor(int sock, ofstream* outputLog) {
|
---|
21 | this->sock = sock;
|
---|
22 | this->outputLog = outputLog;
|
---|
23 | lastUsedId = 0;
|
---|
24 | }
|
---|
25 |
|
---|
26 | MessageProcessor::~MessageProcessor() {
|
---|
27 | }
|
---|
28 |
|
---|
29 | int 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 |
|
---|
35 | if (outputLog)
|
---|
36 | (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
---|
37 |
|
---|
38 | sentMessages[msg->id][dest->sin_addr.s_addr] = message;
|
---|
39 |
|
---|
40 | int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
|
---|
41 |
|
---|
42 | return ret;
|
---|
43 | }
|
---|
44 |
|
---|
45 | int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
|
---|
46 | socklen_t socklen = sizeof(struct sockaddr_in);
|
---|
47 |
|
---|
48 | // assume we don't care about the value of socklen
|
---|
49 | int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
|
---|
50 |
|
---|
51 | if (ret == -1)
|
---|
52 | return ret;
|
---|
53 |
|
---|
54 | // add id to the NETWORK_MSG struct
|
---|
55 | if (msg->type == MSG_TYPE_ACK) {
|
---|
56 | if (!sentMessages[msg->id][source->sin_addr.s_addr].getAcked()) {
|
---|
57 | sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true);
|
---|
58 | sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis());
|
---|
59 | if (outputLog)
|
---|
60 | (*outputLog) << "Received ack for message id " << msg->id << endl;
|
---|
61 | }
|
---|
62 |
|
---|
63 | return -1; // don't do any further processing
|
---|
64 | }else {
|
---|
65 | bool isDuplicate = false;
|
---|
66 | map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
|
---|
67 |
|
---|
68 | if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
|
---|
69 | isDuplicate = true;
|
---|
70 | cout << "Got duplicate of type " << msg->type << endl;
|
---|
71 | if (outputLog)
|
---|
72 | (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
---|
73 | }else {
|
---|
74 | cout << "Got message of type " << msg->type << endl;
|
---|
75 | if (outputLog)
|
---|
76 | (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
---|
77 | }
|
---|
78 |
|
---|
79 | ackedPlayerMessages[msg->id] = getCurrentMillis();
|
---|
80 |
|
---|
81 | NETWORK_MSG ack;
|
---|
82 | ack.id = msg->id;
|
---|
83 | ack.type = MSG_TYPE_ACK;
|
---|
84 |
|
---|
85 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
---|
86 |
|
---|
87 | if (isDuplicate)
|
---|
88 | return -1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return ret;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void MessageProcessor::resendUnackedMessages() {
|
---|
95 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
96 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
97 | map<unsigned long, MessageContainer> sentMsg;
|
---|
98 |
|
---|
99 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
100 | sentMsg = it->second;
|
---|
101 | for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
|
---|
102 | if (!(it2->second.getAcked())) {
|
---|
103 | sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void MessageProcessor::cleanAckedMessages() {
|
---|
110 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
|
---|
111 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
112 |
|
---|
113 | while (it != sentMessages.end()) {
|
---|
114 | it2 = it->second.begin();
|
---|
115 | while (it2 != it->second.end()) {
|
---|
116 | if (it2->second.getAcked()) {
|
---|
117 | if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
|
---|
118 | if (outputLog)
|
---|
119 | (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
|
---|
120 | it->second.erase(it2++);
|
---|
121 | }else
|
---|
122 | it2++;
|
---|
123 | }else
|
---|
124 | it2++;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (it->second.size() == 0)
|
---|
128 | sentMessages.erase(it++);
|
---|
129 | else
|
---|
130 | it++;
|
---|
131 | }
|
---|
132 |
|
---|
133 | map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
|
---|
134 | map<unsigned int, unsigned long long>::iterator it4;
|
---|
135 |
|
---|
136 | // somehow want to delete the inner map once that player logs out
|
---|
137 | while (it3 != ackedMessages.end()) {
|
---|
138 | it4 = it3->second.begin();
|
---|
139 | while (it4 != it3->second.end()) {
|
---|
140 | if ((getCurrentMillis() - it4->second) > 500)
|
---|
141 | it3->second.erase(it4++);
|
---|
142 | else
|
---|
143 | it4++;
|
---|
144 | }
|
---|
145 | it3++;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
|
---|
150 | return this->sentMessages;
|
---|
151 | }
|
---|
152 |
|
---|
153 | map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
|
---|
154 | return this->ackedMessages;
|
---|
155 | }
|
---|