[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);
|
---|
[9b5d30b] | 17 | sentMessages[msg->id] = 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 |
|
---|
[5a64bea] | 40 | // add id to the NETWORK_MSG struct
|
---|
| 41 | if (msg->type == MSG_TYPE_ACK) {
|
---|
[198cf2d] | 42 | if (!sentMessages[msg->id].isAcked) {
|
---|
[5755e68] | 43 | cout << "Received new ack" << endl;
|
---|
[198cf2d] | 44 | sentMessages[msg->id].isAcked = true;
|
---|
| 45 | sentMessages[msg->id].timeAcked = getCurrentMillis();
|
---|
[6b641af] | 46 | }
|
---|
[198cf2d] | 47 |
|
---|
| 48 | return -1; // don't do any further processing
|
---|
[5a64bea] | 49 | }else {
|
---|
[4dbac87] | 50 | cout << "Received message" << endl;
|
---|
| 51 | cout << "id: " << msg->id << endl;
|
---|
| 52 | cout << "type: " << msg->type << endl;
|
---|
| 53 | cout << "buffer: " << msg->buffer << endl;
|
---|
| 54 |
|
---|
| 55 | if (ackedMessages.find(msg->id) != ackedMessages.end()) {
|
---|
| 56 | ackedMessages[msg->id] = getCurrentMillis();
|
---|
[bd2502a] | 57 |
|
---|
[4dbac87] | 58 | NETWORK_MSG ack;
|
---|
| 59 | ack.id = msg->id;
|
---|
| 60 | ack.type = MSG_TYPE_ACK;
|
---|
[5a64bea] | 61 |
|
---|
[4dbac87] | 62 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
---|
| 63 | }else
|
---|
| 64 | cout << "Got duplicate ack" << endl;
|
---|
[5a64bea] | 65 | }
|
---|
| 66 |
|
---|
| 67 | return ret;
|
---|
[1a3c42d] | 68 | }
|
---|
| 69 |
|
---|
[5a64bea] | 70 | void MessageProcessor::resendUnackedMessages(int sock) {
|
---|
| 71 | map<int, MessageContainer>::iterator it;
|
---|
| 72 |
|
---|
| 73 | for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
| 74 | sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
|
---|
| 75 | }
|
---|
[1a3c42d] | 76 | }
|
---|
| 77 |
|
---|
| 78 | void MessageProcessor::cleanAckedMessages() {
|
---|
[af713bc] | 79 | map<int, MessageContainer>::iterator it = sentMessages.begin();
|
---|
[198cf2d] | 80 |
|
---|
[af713bc] | 81 | while (it != sentMessages.end()) {
|
---|
[6b641af] | 82 | if (it->second.isAcked) {
|
---|
| 83 | // cout << "Found acked message" << endl;
|
---|
| 84 | // cout << "time acked" << it->second.timeAcked << endl;
|
---|
| 85 | // cout << "cur time" << getCurrentMillis() << endl;
|
---|
| 86 | if ((getCurrentMillis() - it->second.timeAcked) > 1000) {
|
---|
| 87 | cout << "Message was acked. time to delete it" << endl;
|
---|
| 88 | cout << "old map size" << sentMessages.size() << endl;
|
---|
| 89 | sentMessages.erase(it++);
|
---|
| 90 | cout << "new map size" << sentMessages.size() << endl;
|
---|
| 91 | }else
|
---|
| 92 | it++;
|
---|
| 93 | }else
|
---|
[af713bc] | 94 | it++;
|
---|
[198cf2d] | 95 | }
|
---|
[4dbac87] | 96 |
|
---|
| 97 | map<unsigned int, unsigned long long>::iterator it2 = ackedMessages.begin();
|
---|
| 98 |
|
---|
| 99 | while (it2 != ackedMessages.end()) {
|
---|
| 100 | if ((getCurrentMillis() - it2->second) > 500)
|
---|
| 101 | ackedMessages.erase(it2++);
|
---|
| 102 | else
|
---|
| 103 | it2++;
|
---|
| 104 | }
|
---|
[1a3c42d] | 105 | }
|
---|