[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 |
|
---|
| 37 | // add id to the NETWORK_MSG struct
|
---|
| 38 | if (msg->type == MSG_TYPE_ACK) {
|
---|
[198cf2d] | 39 | if (!sentMessages[msg->id].isAcked) {
|
---|
| 40 | sentMessages[msg->id].isAcked = true;
|
---|
| 41 | sentMessages[msg->id].timeAcked = getCurrentMillis();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | return -1; // don't do any further processing
|
---|
[5a64bea] | 45 | }else {
|
---|
[bd2502a] | 46 | cout << "Received message" << endl;
|
---|
| 47 | cout << "id: " << msg->id << endl;
|
---|
| 48 | cout << "type: " << msg->type << endl;
|
---|
| 49 | cout << "buffer: " << msg->buffer << endl;
|
---|
| 50 |
|
---|
[5a64bea] | 51 | NETWORK_MSG ack;
|
---|
| 52 | ack.id = msg->id;
|
---|
| 53 |
|
---|
| 54 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | return ret;
|
---|
[1a3c42d] | 58 | }
|
---|
| 59 |
|
---|
[5a64bea] | 60 | void MessageProcessor::resendUnackedMessages(int sock) {
|
---|
| 61 | map<int, MessageContainer>::iterator it;
|
---|
| 62 |
|
---|
| 63 | for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
| 64 | sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
|
---|
| 65 | }
|
---|
[1a3c42d] | 66 | }
|
---|
| 67 |
|
---|
| 68 | void MessageProcessor::cleanAckedMessages() {
|
---|
[af713bc] | 69 | map<int, MessageContainer>::iterator it = sentMessages.begin();
|
---|
[198cf2d] | 70 |
|
---|
[af713bc] | 71 | while (it != sentMessages.end()) {
|
---|
[198cf2d] | 72 | if (it->second.isAcked && (getCurrentMillis() - it->second.timeAcked) > 1000)
|
---|
[9557f92] | 73 | sentMessages.erase(it++);
|
---|
[af713bc] | 74 | else
|
---|
| 75 | it++;
|
---|
[198cf2d] | 76 | }
|
---|
[1a3c42d] | 77 | }
|
---|