source: network-game/common/MessageProcessor.cpp@ 68d94de

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

MessageProcessor now takes a socket and optional output log file as constructor arguments instead of accepting them as parameters in each method

  • Property mode set to 100644
File size: 4.9 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() {
[68d94de]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;
[5a64bea]23 lastUsedId = 0;
24}
25
26MessageProcessor::~MessageProcessor() {
27}
28
[68d94de]29int MessageProcessor::sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest) {
[46d6469]30 cout << "Sending message of type " << msg->type << endl;
31
[9b5d30b]32 msg->id = ++lastUsedId;
[5a64bea]33 MessageContainer message(*msg, *dest);
[d05086b]34
35 if (outputLog)
36 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[5a64bea]37
[46d6469]38 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
[bd2502a]39
[5a64bea]40 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
41
42 return ret;
[1a3c42d]43}
44
[68d94de]45int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
[5a64bea]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
[6b641af]51 if (ret == -1)
52 return ret;
53
[5a64bea]54 // add id to the NETWORK_MSG struct
55 if (msg->type == MSG_TYPE_ACK) {
[46d6469]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());
[d05086b]59 if (outputLog)
60 (*outputLog) << "Received ack for message id " << msg->id << endl;
[6b641af]61 }
[198cf2d]62
63 return -1; // don't do any further processing
[5a64bea]64 }else {
[bace57b]65 bool isDuplicate = false;
[f9cb9fb]66 map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
[bace57b]67
[f9cb9fb]68 if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
[bace57b]69 isDuplicate = true;
[934ab53]70 cout << "Got duplicate of type " << msg->type << endl;
[f9cb9fb]71 if (outputLog)
72 (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
[d05086b]73 }else {
[934ab53]74 cout << "Got message of type " << msg->type << endl;
[d05086b]75 if (outputLog)
76 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
77 }
[bace57b]78
[f9cb9fb]79 ackedPlayerMessages[msg->id] = getCurrentMillis();
[855f153]80
[bace57b]81 NETWORK_MSG ack;
82 ack.id = msg->id;
83 ack.type = MSG_TYPE_ACK;
[bd2502a]84
[bace57b]85 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]86
[bace57b]87 if (isDuplicate)
[3794f6d]88 return -1;
[5a64bea]89 }
90
91 return ret;
[1a3c42d]92}
93
[68d94de]94void MessageProcessor::resendUnackedMessages() {
[b35b2b2]95 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
[bace57b]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++) {
[46d6469]102 if (!(it2->second.getAcked())) {
[9fe1807]103 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]104 }
[bace57b]105 }
[5a64bea]106 }
[1a3c42d]107}
108
[68d94de]109void MessageProcessor::cleanAckedMessages() {
[b35b2b2]110 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
[bace57b]111 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]112
[af713bc]113 while (it != sentMessages.end()) {
[bace57b]114 it2 = it->second.begin();
[46d6469]115 while (it2 != it->second.end()) {
116 if (it2->second.getAcked()) {
[d05086b]117 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
118 if (outputLog)
119 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
[bace57b]120 it->second.erase(it2++);
[d05086b]121 }else
[bace57b]122 it2++;
[6b641af]123 }else
[bace57b]124 it2++;
125 }
126
127 if (it->second.size() == 0)
128 sentMessages.erase(it++);
129 else
[af713bc]130 it++;
[198cf2d]131 }
[4dbac87]132
[f9cb9fb]133 map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
134 map<unsigned int, unsigned long long>::iterator it4;
[4dbac87]135
[f9cb9fb]136 // somehow want to delete the inner map once that player logs out
[bace57b]137 while (it3 != ackedMessages.end()) {
[f9cb9fb]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++;
[4dbac87]146 }
[b35b2b2]147}
148
149map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
150 return this->sentMessages;
151}
152
[f9cb9fb]153map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
[b35b2b2]154 return this->ackedMessages;
[1a3c42d]155}
Note: See TracBrowser for help on using the repository browser.