source: network-game/common/MessageProcessor.cpp@ b35b2b2

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

Added a basic ingame debug console

  • Property mode set to 100644
File size: 3.7 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
[b35b2b2]54 ackedMessages[msg->id] = MessageContainer(*msg, *source);
55 ackedMessages[msg->id].setAcked(true);
56 ackedMessages[msg->id].setTimeAcked(getCurrentMillis());
[855f153]57
[bace57b]58 NETWORK_MSG ack;
59 ack.id = msg->id;
60 ack.type = MSG_TYPE_ACK;
[bd2502a]61
[bace57b]62 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
[5a64bea]63
[bace57b]64 if (isDuplicate)
[3794f6d]65 return -1;
[5a64bea]66 }
67
68 return ret;
[1a3c42d]69}
70
[5a64bea]71void MessageProcessor::resendUnackedMessages(int sock) {
[b35b2b2]72 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
[bace57b]73 map<unsigned long, MessageContainer>::iterator it2;
74 map<unsigned long, MessageContainer> sentMsg;
75
76 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
77 sentMsg = it->second;
78 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
[46d6469]79 if (!(it2->second.getAcked())) {
[9fe1807]80 sendto(sock, (const char*)it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
[46d6469]81 }
[bace57b]82 }
[5a64bea]83 }
[1a3c42d]84}
85
86void MessageProcessor::cleanAckedMessages() {
[b35b2b2]87 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
[bace57b]88 map<unsigned long, MessageContainer>::iterator it2;
[198cf2d]89
[af713bc]90 while (it != sentMessages.end()) {
[bace57b]91 it2 = it->second.begin();
[46d6469]92 while (it2 != it->second.end()) {
93 if (it2->second.getAcked()) {
94 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
[bace57b]95 it->second.erase(it2++);
96 else
97 it2++;
[6b641af]98 }else
[bace57b]99 it2++;
100 }
101
102 if (it->second.size() == 0)
103 sentMessages.erase(it++);
104 else
[af713bc]105 it++;
[198cf2d]106 }
[4dbac87]107
[b35b2b2]108 /*
[bace57b]109 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
[4dbac87]110
[bace57b]111 while (it3 != ackedMessages.end()) {
[46d6469]112 if ((getCurrentMillis() - it3->second) > 500)
[bace57b]113 ackedMessages.erase(it3++);
[46d6469]114 else
[bace57b]115 it3++;
[4dbac87]116 }
[b35b2b2]117 */
118}
119
120map<unsigned int, map<unsigned long, MessageContainer> >& MessageProcessor::getSentMessages() {
121 return this->sentMessages;
122}
123
124map<unsigned int, MessageContainer>& MessageProcessor::getAckedMessages() {
125 return this->ackedMessages;
[1a3c42d]126}
Note: See TracBrowser for help on using the repository browser.