source: network-game/common/MessageProcessor.cpp@ 934ab53

Last change on this file since 934ab53 was 934ab53, checked in by dportnoy <dmp1488@…>, 11 years ago

Added debug messages

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