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