| 1 | #include "MessageProcessor.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <fstream>
|
|---|
| 5 |
|
|---|
| 6 | #include "Compiler.h"
|
|---|
| 7 |
|
|---|
| 8 | #if defined WINDOWS
|
|---|
| 9 | #include <ws2tcpip.h>
|
|---|
| 10 | #endif
|
|---|
| 11 |
|
|---|
| 12 | #include "Common.h"
|
|---|
| 13 |
|
|---|
| 14 | MessageProcessor::MessageProcessor() {
|
|---|
| 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;
|
|---|
| 23 | lastUsedId = 0;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | MessageProcessor::~MessageProcessor() {
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | int MessageProcessor::sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest) {
|
|---|
| 30 | cout << "Sending message of type " << msg->type << endl;
|
|---|
| 31 |
|
|---|
| 32 | msg->id = ++lastUsedId;
|
|---|
| 33 | MessageContainer message(*msg, *dest);
|
|---|
| 34 |
|
|---|
| 35 | if (outputLog)
|
|---|
| 36 | (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
|---|
| 37 |
|
|---|
| 38 | sentMessages[msg->id][dest->sin_addr.s_addr] = message;
|
|---|
| 39 |
|
|---|
| 40 | int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
|
|---|
| 41 |
|
|---|
| 42 | if (ret < 0)
|
|---|
| 43 | error("sendMessage");
|
|---|
| 44 | else
|
|---|
| 45 | return ret;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
|
|---|
| 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 |
|
|---|
| 54 | if (ret == -1)
|
|---|
| 55 | return ret;
|
|---|
| 56 |
|
|---|
| 57 | // add id to the NETWORK_MSG struct
|
|---|
| 58 | if (msg->type == MSG_TYPE_ACK) {
|
|---|
| 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());
|
|---|
| 62 | if (outputLog)
|
|---|
| 63 | (*outputLog) << "Received ack for message id " << msg->id << endl;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return -1; // don't do any further processing
|
|---|
| 67 | }else {
|
|---|
| 68 | bool isDuplicate = false;
|
|---|
| 69 | map<unsigned int, unsigned long long>& ackedPlayerMessages = ackedMessages[source->sin_addr.s_addr];
|
|---|
| 70 |
|
|---|
| 71 | if (ackedPlayerMessages.find(msg->id) != ackedPlayerMessages.end()) {
|
|---|
| 72 | isDuplicate = true;
|
|---|
| 73 | cout << "Got duplicate of type " << msg->type << endl;
|
|---|
| 74 | if (outputLog)
|
|---|
| 75 | (*outputLog) << "Received duplicate (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
|---|
| 76 | }else {
|
|---|
| 77 | cout << "Got message of type " << msg->type << endl;
|
|---|
| 78 | if (outputLog)
|
|---|
| 79 | (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | ackedPlayerMessages[msg->id] = getCurrentMillis();
|
|---|
| 83 |
|
|---|
| 84 | NETWORK_MSG ack;
|
|---|
| 85 | ack.id = msg->id;
|
|---|
| 86 | ack.type = MSG_TYPE_ACK;
|
|---|
| 87 |
|
|---|
| 88 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
|---|
| 89 |
|
|---|
| 90 | if (isDuplicate)
|
|---|
| 91 | return -1;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return ret;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void MessageProcessor::resendUnackedMessages() {
|
|---|
| 98 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
|---|
| 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++) {
|
|---|
| 105 | if (!(it2->second.getAcked())) {
|
|---|
| 106 | sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void MessageProcessor::cleanAckedMessages() {
|
|---|
| 113 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
|
|---|
| 114 | map<unsigned long, MessageContainer>::iterator it2;
|
|---|
| 115 |
|
|---|
| 116 | while (it != sentMessages.end()) {
|
|---|
| 117 | it2 = it->second.begin();
|
|---|
| 118 | while (it2 != it->second.end()) {
|
|---|
| 119 | if (it2->second.getAcked()) {
|
|---|
| 120 | if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) {
|
|---|
| 121 | if (outputLog)
|
|---|
| 122 | (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl;
|
|---|
| 123 | it->second.erase(it2++);
|
|---|
| 124 | }else
|
|---|
| 125 | it2++;
|
|---|
| 126 | }else
|
|---|
| 127 | it2++;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (it->second.size() == 0)
|
|---|
| 131 | sentMessages.erase(it++);
|
|---|
| 132 | else
|
|---|
| 133 | it++;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | map<unsigned long, map<unsigned int, unsigned long long> >::iterator it3 = ackedMessages.begin();
|
|---|
| 137 | map<unsigned int, unsigned long long>::iterator it4;
|
|---|
| 138 |
|
|---|
| 139 | // somehow want to delete the inner map once that player logs out
|
|---|
| 140 | while (it3 != ackedMessages.end()) {
|
|---|
| 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++;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
|
|---|
| 153 | return this->sentMessages;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | map<unsigned long, map<unsigned int, unsigned long long> >& MessageProcessor::getAckedMessages() {
|
|---|
| 157 | return this->ackedMessages;
|
|---|
| 158 | }
|
|---|