source: network-game/common/MessageProcessor.cpp@ 855f153

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

Added debug info

  • Property mode set to 100644
File size: 3.3 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 msg->id = ++lastUsedId;
16 MessageContainer message(*msg, *dest);
17 sentMessages[msg->id] = message;
18
19 cout << "Sending message" << endl;
20 cout << "id: " << msg->id << endl;
21 cout << "type: " << msg->type << endl;
22 cout << "buffer: " << msg->buffer << endl;
23
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;
29}
30
31int 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 if (ret == -1)
38 return ret;
39
40 // add id to the NETWORK_MSG struct
41 if (msg->type == MSG_TYPE_ACK) {
42 if (!sentMessages[msg->id].isAcked) {
43 cout << "Received new ack" << endl;
44 sentMessages[msg->id].isAcked = true;
45 sentMessages[msg->id].timeAcked = getCurrentMillis();
46 }
47
48 return -1; // don't do any further processing
49 }else {
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 cout << "Not a duplicate" << endl;
57
58 ackedMessages[msg->id] = getCurrentMillis();
59
60 NETWORK_MSG ack;
61 ack.id = msg->id;
62 ack.type = MSG_TYPE_ACK;
63
64 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
65 }else
66 cout << "Got duplicate ack" << endl;
67 }
68
69 return ret;
70}
71
72void MessageProcessor::resendUnackedMessages(int sock) {
73 map<int, MessageContainer>::iterator it;
74
75 for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
76 sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
77 }
78}
79
80void MessageProcessor::cleanAckedMessages() {
81 map<int, MessageContainer>::iterator it = sentMessages.begin();
82
83 while (it != sentMessages.end()) {
84 if (it->second.isAcked) {
85// cout << "Found acked message" << endl;
86// cout << "time acked" << it->second.timeAcked << endl;
87// cout << "cur time" << getCurrentMillis() << endl;
88 if ((getCurrentMillis() - it->second.timeAcked) > 1000) {
89 cout << "Message was acked. time to delete it" << endl;
90 cout << "old map size" << sentMessages.size() << endl;
91 sentMessages.erase(it++);
92 cout << "new map size" << sentMessages.size() << endl;
93 }else
94 it++;
95 }else
96 it++;
97 }
98
99 map<unsigned int, unsigned long long>::iterator it2 = ackedMessages.begin();
100
101 while (it2 != ackedMessages.end()) {
102 if ((getCurrentMillis() - it2->second) > 5000) {
103 ackedMessages.erase(it2++);
104 cout << "Deleting ack record" << endl;
105 }else
106 it2++;
107 }
108}
Note: See TracBrowser for help on using the repository browser.