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

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

Restructuring and code cleanup

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