source: network-game/common/MessageProcessor.cpp@ 483a2cb

Last change on this file since 483a2cb 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
RevLine 
[1a3c42d]1#include "MessageProcessor.h"
2
[5a64bea]3#include <iostream>
[d05086b]4#include <fstream>
[5a64bea]5
[e6c26b8]6#include "Compiler.h"
7
8#if defined WINDOWS
9 #include <ws2tcpip.h>
10#endif
11
12#include "Common.h"
13
[5a64bea]14MessageProcessor::MessageProcessor() {
15 lastUsedId = 0;
16}
17
18MessageProcessor::~MessageProcessor() {
19}
20
[d05086b]21int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) {
[46d6469]22 cout << "Sending message of type " << msg->type << endl;
23
[9b5d30b]24 msg->id = ++lastUsedId;
[5a64bea]25 MessageContainer message(*msg, *dest);
[d05086b]26
27 if (outputLog)
28 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[5a64bea]29
[46d6469]30 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
[bd2502a]31
[5a64bea]32 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
33
34 return ret;
[1a3c42d]35}
36
[d05086b]37int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) {
[5a64bea]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
[6b641af]43 if (ret == -1)
44 return ret;
45
[5a64bea]46 // add id to the NETWORK_MSG struct
47 if (msg->type == MSG_TYPE_ACK) {
[46d6469]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());
[d05086b]51 if (outputLog)
52 (*outputLog) << "Received ack for message id " << msg->id << endl;
[6b641af]53 }
[198cf2d]54
55 return -1; // don't do any further processing
[5a64bea]56 }else {
[bace57b]57 bool isDuplicate = false;
[f9cb9fb]58 map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
[bace57b]59
[f9cb9fb]60 if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
[bace57b]61 isDuplicate = true;
[934ab53]62 cout << "Got duplicate of type " << msg->type << endl;
[f9cb9fb]63 if (outputLog)
64 (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[d05086b]65 }else {
[934ab53]66 cout << "Got message of type " << msg->type << endl;
[d05086b]67 if (outputLog)
68 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
69 }
[bace57b]70
[f9cb9fb]71 ackedPlayerMessages[msg->id] = getCurrentMillis();
[855f153]72
[bace57b]73 NETWORK_MSG ack;
74 ack.id = msg->id;
75 ack.type = MSG_TYPE_ACK;
[bd2502a]76
[bace57b]77 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]78
[bace57b]79 if (isDuplicate)
[3794f6d]80 return -1;
[5a64bea]81 }
82
83 return ret;
[1a3c42d]84}
85
[d05086b]86void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) {
[b35b2b2]87 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
[bace57b]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++) {
[46d6469]94 if (!(it2->second.getAcked())) {
[9fe1807]95 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]96 }
[bace57b]97 }
[5a64bea]98 }
[1a3c42d]99}
100
[d05086b]101void MessageProcessor::cleanAckedMessages(ofstream* outputLog) {
[b35b2b2]102 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
[bace57b]103 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]104
[af713bc]105 while (it != sentMessages.end()) {
[bace57b]106 it2 = it->second.begin();
[46d6469]107 while (it2 != it->second.end()) {
108 if (it2->second.getAcked()) {
[d05086b]109 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
110 if (outputLog)
111 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
[bace57b]112 it->second.erase(it2++);
[d05086b]113 }else
[bace57b]114 it2++;
[6b641af]115 }else
[bace57b]116 it2++;
117 }
118
119 if (it->second.size() == 0)
120 sentMessages.erase(it++);
121 else
[af713bc]122 it++;
[198cf2d]123 }
[4dbac87]124
[f9cb9fb]125 map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
126 map<unsigned int, unsigned long long>::iterator it4;
[4dbac87]127
[f9cb9fb]128 // somehow want to delete the inner map once that player logs out
[bace57b]129 while (it3 != ackedMessages.end()) {
[f9cb9fb]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++;
[4dbac87]138 }
[b35b2b2]139}
140
141map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
142 return this->sentMessages;
143}
144
[f9cb9fb]145map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
[b35b2b2]146 return this->ackedMessages;
[1a3c42d]147}
Note: See TracBrowser for help on using the repository browser.