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

Last change on this file since b4c5b6a was e6c26b8, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

The client dynamically allocates memory for players and passes around a map with player pointers and some includes are now in individual files instead of in Common.h

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