[1a3c42d] | 1 | #include "MessageProcessor.h"
|
---|
| 2 |
|
---|
[5a64bea] | 3 | #include <iostream>
|
---|
| 4 |
|
---|
[198cf2d] | 5 | #include "Common.h"
|
---|
| 6 |
|
---|
[5a64bea] | 7 | MessageProcessor::MessageProcessor() {
|
---|
| 8 | lastUsedId = 0;
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | MessageProcessor::~MessageProcessor() {
|
---|
| 12 | }
|
---|
| 13 |
|
---|
[1a3c42d] | 14 | int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
|
---|
[9b5d30b] | 15 | msg->id = ++lastUsedId;
|
---|
[5a64bea] | 16 | MessageContainer message(*msg, *dest);
|
---|
[bace57b] | 17 | sentMessages[msg->id][dest->sin_addr.s_addr] = message;
|
---|
[5a64bea] | 18 |
|
---|
[bd2502a] | 19 | cout << "Sending message" << endl;
|
---|
| 20 | cout << "id: " << msg->id << endl;
|
---|
| 21 | cout << "type: " << msg->type << endl;
|
---|
| 22 | cout << "buffer: " << msg->buffer << endl;
|
---|
| 23 |
|
---|
[5a64bea] | 24 | int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
|
---|
| 25 |
|
---|
| 26 | cout << "Send a message of type " << msg->type << endl;
|
---|
| 27 |
|
---|
| 28 | return ret;
|
---|
[1a3c42d] | 29 | }
|
---|
| 30 |
|
---|
[5a64bea] | 31 | int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
|
---|
| 32 | socklen_t socklen = sizeof(struct sockaddr_in);
|
---|
| 33 |
|
---|
| 34 | // assume we don't care about the value of socklen
|
---|
| 35 | int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
|
---|
| 36 |
|
---|
[6b641af] | 37 | if (ret == -1)
|
---|
| 38 | return ret;
|
---|
| 39 |
|
---|
[64d22ac] | 40 | cout << "Received message" << endl;
|
---|
| 41 |
|
---|
[5a64bea] | 42 | // add id to the NETWORK_MSG struct
|
---|
| 43 | if (msg->type == MSG_TYPE_ACK) {
|
---|
[64d22ac] | 44 | cout << "Received ack" << endl;
|
---|
[bace57b] | 45 | if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) {
|
---|
[5755e68] | 46 | cout << "Received new ack" << endl;
|
---|
[bace57b] | 47 | sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true;
|
---|
| 48 | sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis();
|
---|
[6b641af] | 49 | }
|
---|
[198cf2d] | 50 |
|
---|
| 51 | return -1; // don't do any further processing
|
---|
[5a64bea] | 52 | }else {
|
---|
[bace57b] | 53 | bool isDuplicate = false;
|
---|
| 54 |
|
---|
[64d22ac] | 55 | cout << "Received real message" << endl;
|
---|
[4dbac87] | 56 | cout << "id: " << msg->id << endl;
|
---|
| 57 | cout << "type: " << msg->type << endl;
|
---|
| 58 | cout << "buffer: " << msg->buffer << endl;
|
---|
| 59 |
|
---|
[09ddba7] | 60 | if (ackedMessages.find(msg->id) != ackedMessages.end())
|
---|
[bace57b] | 61 | isDuplicate = true;
|
---|
| 62 |
|
---|
| 63 | ackedMessages[msg->id] = getCurrentMillis();
|
---|
[855f153] | 64 |
|
---|
[bace57b] | 65 | NETWORK_MSG ack;
|
---|
| 66 | ack.id = msg->id;
|
---|
| 67 | ack.type = MSG_TYPE_ACK;
|
---|
[bd2502a] | 68 |
|
---|
[bace57b] | 69 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
---|
[5a64bea] | 70 |
|
---|
[bace57b] | 71 | if (isDuplicate)
|
---|
[3794f6d] | 72 | return -1;
|
---|
[5a64bea] | 73 | }
|
---|
| 74 |
|
---|
| 75 | return ret;
|
---|
[1a3c42d] | 76 | }
|
---|
| 77 |
|
---|
[5a64bea] | 78 | void MessageProcessor::resendUnackedMessages(int sock) {
|
---|
[bace57b] | 79 | map<int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
| 80 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
| 81 | map<unsigned long, MessageContainer> sentMsg;
|
---|
| 82 |
|
---|
| 83 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
| 84 | sentMsg = it->second;
|
---|
| 85 | for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
|
---|
| 86 | sendto(sock, (char*)&it2->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
|
---|
| 87 | }
|
---|
[5a64bea] | 88 | }
|
---|
[1a3c42d] | 89 | }
|
---|
| 90 |
|
---|
| 91 | void MessageProcessor::cleanAckedMessages() {
|
---|
[bace57b] | 92 | map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
|
---|
| 93 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
[198cf2d] | 94 |
|
---|
[af713bc] | 95 | while (it != sentMessages.end()) {
|
---|
[bace57b] | 96 | it2 = it->second.begin();
|
---|
| 97 | while (it2 != it->second.begin()) {
|
---|
| 98 | if (it2->second.isAcked) {
|
---|
| 99 | if ((getCurrentMillis() - it2->second.timeAcked) > 1000)
|
---|
| 100 | it->second.erase(it2++);
|
---|
| 101 | else
|
---|
| 102 | it2++;
|
---|
[6b641af] | 103 | }else
|
---|
[bace57b] | 104 | it2++;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (it->second.size() == 0)
|
---|
| 108 | sentMessages.erase(it++);
|
---|
| 109 | else
|
---|
[af713bc] | 110 | it++;
|
---|
[198cf2d] | 111 | }
|
---|
[4dbac87] | 112 |
|
---|
[bace57b] | 113 | map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
|
---|
[4dbac87] | 114 |
|
---|
[bace57b] | 115 | while (it3 != ackedMessages.end()) {
|
---|
| 116 | if ((getCurrentMillis() - it3->second) > 500) {
|
---|
| 117 | ackedMessages.erase(it3++);
|
---|
[855f153] | 118 | cout << "Deleting ack record" << endl;
|
---|
| 119 | }else
|
---|
[bace57b] | 120 | it3++;
|
---|
[4dbac87] | 121 | }
|
---|
[1a3c42d] | 122 | }
|
---|