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
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
[09ddba7]48 if (ackedMessages.find(msg->id) != ackedMessages.end())
[bace57b]49 isDuplicate = true;
50
51 ackedMessages[msg->id] = getCurrentMillis();
[855f153]52
[bace57b]53 NETWORK_MSG ack;
54 ack.id = msg->id;
55 ack.type = MSG_TYPE_ACK;
[bd2502a]56
[bace57b]57 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]58
[bace57b]59 if (isDuplicate)
[3794f6d]60 return -1;
[5a64bea]61 }
62
63 return ret;
[1a3c42d]64}
65
[5a64bea]66void MessageProcessor::resendUnackedMessages(int sock) {
[bace57b]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++) {
[46d6469]74 if (!(it2->second.getAcked())) {
[9fe1807]75 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]76 }
[bace57b]77 }
[5a64bea]78 }
[1a3c42d]79}
80
81void MessageProcessor::cleanAckedMessages() {
[bace57b]82 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
83 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]84
[af713bc]85 while (it != sentMessages.end()) {
[bace57b]86 it2 = it->second.begin();
[46d6469]87 while (it2 != it->second.end()) {
88 if (it2->second.getAcked()) {
89 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
[bace57b]90 it->second.erase(it2++);
91 else
92 it2++;
[6b641af]93 }else
[bace57b]94 it2++;
95 }
96
97 if (it->second.size() == 0)
98 sentMessages.erase(it++);
99 else
[af713bc]100 it++;
[198cf2d]101 }
[4dbac87]102
[bace57b]103 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
[4dbac87]104
[bace57b]105 while (it3 != ackedMessages.end()) {
[46d6469]106 if ((getCurrentMillis() - it3->second) > 500)
[bace57b]107 ackedMessages.erase(it3++);
[46d6469]108 else
[bace57b]109 it3++;
[4dbac87]110 }
[1a3c42d]111}
Note: See TracBrowser for help on using the repository browser.