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

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

All server warnings have been fixed and the WorldMap class has a new method to create flags (and, in the future, other objects) based on structures present on the map

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