source: network-game/common/MessageProcessor.cpp@ 1df6bec

Last change on this file since 1df6bec was 9fe1807, checked in by dportnoy <dmp1488@…>, 11 years ago

Added MessageContainer to the VC++ project

  • Property mode set to 100644
File size: 3.2 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
51 ackedMessages[msg->id] = getCurrentMillis();
52
53 NETWORK_MSG ack;
54 ack.id = msg->id;
55 ack.type = MSG_TYPE_ACK;
56
57 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
58
59 if (isDuplicate)
60 return -1;
61 }
62
63 return ret;
64}
65
66void MessageProcessor::resendUnackedMessages(int sock) {
67 map<int, map<unsigned long, MessageContainer> >::iterator it;
68 map<unsigned long, MessageContainer>::iterator it2;
69 map<unsigned long, MessageContainer> sentMsg;
70
71 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
72 sentMsg = it->second;
73 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
74 if (!(it2->second.getAcked())) {
75 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
76 }
77 }
78 }
79}
80
81void MessageProcessor::cleanAckedMessages() {
82 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
83 map<unsigned long, MessageContainer>::iterator it2;
84
85 while (it != sentMessages.end()) {
86 it2 = it->second.begin();
87 while (it2 != it->second.end()) {
88 if (it2->second.getAcked()) {
89 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
90 it->second.erase(it2++);
91 else
92 it2++;
93 }else
94 it2++;
95 }
96
97 if (it->second.size() == 0)
98 sentMessages.erase(it++);
99 else
100 it++;
101 }
102
103 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
104
105 while (it3 != ackedMessages.end()) {
106 if ((getCurrentMillis() - it3->second) > 500)
107 ackedMessages.erase(it3++);
108 else
109 it3++;
110 }
111}
Note: See TracBrowser for help on using the repository browser.