[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] | 14 | MessageProcessor::MessageProcessor() {
|
---|
[68d94de] | 15 | this->sock = 0;
|
---|
| 16 | this->outputLog = NULL;
|
---|
| 17 | lastUsedId = 0;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | MessageProcessor::MessageProcessor(int sock, ofstream* outputLog) {
|
---|
| 21 | this->sock = sock;
|
---|
| 22 | this->outputLog = outputLog;
|
---|
[5a64bea] | 23 | lastUsedId = 0;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | MessageProcessor::~MessageProcessor() {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[68d94de] | 29 | int 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] | 48 | int 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 |
|
---|
[d05c484] | 97 | void 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 |
|
---|
[68d94de] | 104 | void MessageProcessor::resendUnackedMessages() {
|
---|
[b35b2b2] | 105 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
[bace57b] | 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++) {
|
---|
[46d6469] | 112 | if (!(it2->second.getAcked())) {
|
---|
[9fe1807] | 113 | sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
|
---|
[46d6469] | 114 | }
|
---|
[bace57b] | 115 | }
|
---|
[5a64bea] | 116 | }
|
---|
[1a3c42d] | 117 | }
|
---|
| 118 |
|
---|
[68d94de] | 119 | void MessageProcessor::cleanAckedMessages() {
|
---|
[b35b2b2] | 120 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
|
---|
[bace57b] | 121 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
[198cf2d] | 122 |
|
---|
[af713bc] | 123 | while (it != sentMessages.end()) {
|
---|
[bace57b] | 124 | it2 = it->second.begin();
|
---|
[46d6469] | 125 | while (it2 != it->second.end()) {
|
---|
| 126 | if (it2->second.getAcked()) {
|
---|
[d05086b] | 127 | if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
|
---|
| 128 | if (outputLog)
|
---|
| 129 | (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
|
---|
[bace57b] | 130 | it->second.erase(it2++);
|
---|
[d05086b] | 131 | }else
|
---|
[bace57b] | 132 | it2++;
|
---|
[6b641af] | 133 | }else
|
---|
[bace57b] | 134 | it2++;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (it->second.size() == 0)
|
---|
| 138 | sentMessages.erase(it++);
|
---|
| 139 | else
|
---|
[af713bc] | 140 | it++;
|
---|
[198cf2d] | 141 | }
|
---|
[4dbac87] | 142 |
|
---|
[f9cb9fb] | 143 | map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
|
---|
| 144 | map<unsigned int, unsigned long long>::iterator it4;
|
---|
[4dbac87] | 145 |
|
---|
[f9cb9fb] | 146 | // somehow want to delete the inner map once that player logs out
|
---|
[bace57b] | 147 | while (it3 != ackedMessages.end()) {
|
---|
[f9cb9fb] | 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++;
|
---|
[4dbac87] | 156 | }
|
---|
[b35b2b2] | 157 | }
|
---|
| 158 |
|
---|
| 159 | map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
|
---|
| 160 | return this->sentMessages;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[f9cb9fb] | 163 | map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
|
---|
[b35b2b2] | 164 | return this->ackedMessages;
|
---|
[1a3c42d] | 165 | }
|
---|