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
Line 
1#include "MessageProcessor.h"
2
3#include <iostream>
4
5#include "Common.h"
6
7MessageProcessor::MessageProcessor() {
8 lastUsedId = 0;
9}
10
11MessageProcessor::~MessageProcessor() {
12}
13
14int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
15 cout << "Sending message of type " << msg->type << endl;
16
17 msg->id = ++lastUsedId;
18 MessageContainer message(*msg, *dest);
19 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
20
21 sentMessages[msg->id][dest->sin_addr.s_addr] = message;
22
23 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
24
25 return ret;
26}
27
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
34 if (ret == -1)
35 return ret;
36
37 // add id to the NETWORK_MSG struct
38 if (msg->type == MSG_TYPE_ACK) {
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());
42 }
43
44 return -1; // don't do any further processing
45 }else {
46 bool isDuplicate = false;
47
48 if (ackedMessages.find(msg->id) != ackedMessages.end()) {
49 isDuplicate = true;
50 cout << "Got duplicate of type " << msg->type << endl;
51 }else
52 cout << "Got message of type " << msg->type << endl;
53
54 ackedMessages[msg->id] = getCurrentMillis();
55
56 NETWORK_MSG ack;
57 ack.id = msg->id;
58 ack.type = MSG_TYPE_ACK;
59
60 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
61
62 if (isDuplicate)
63 return -1;
64 }
65
66 return ret;
67}
68
69void MessageProcessor::resendUnackedMessages(int sock) {
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++) {
77 if (!(it2->second.getAcked())) {
78 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
79 }
80 }
81 }
82}
83
84void MessageProcessor::cleanAckedMessages() {
85 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
86 map<unsigned long, MessageContainer>::iterator it2;
87
88 while (it != sentMessages.end()) {
89 it2 = it->second.begin();
90 while (it2 != it->second.end()) {
91 if (it2->second.getAcked()) {
92 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
93 it->second.erase(it2++);
94 else
95 it2++;
96 }else
97 it2++;
98 }
99
100 if (it->second.size() == 0)
101 sentMessages.erase(it++);
102 else
103 it++;
104 }
105
106 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
107
108 while (it3 != ackedMessages.end()) {
109 if ((getCurrentMillis() - it3->second) > 500)
110 ackedMessages.erase(it3++);
111 else
112 it3++;
113 }
114}
Note: See TracBrowser for help on using the repository browser.