source: network-game/common/MessageProcessor.cpp@ 9b5d30b

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

Moved server message sending/receiving into MessageProcessor

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[1a3c42d]1#include "MessageProcessor.h"
2
[5a64bea]3#include <iostream>
4
5MessageProcessor::MessageProcessor() {
6 lastUsedId = 0;
7}
8
9MessageProcessor::~MessageProcessor() {
10}
11
[1a3c42d]12int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
[9b5d30b]13 msg->id = ++lastUsedId;
[5a64bea]14 MessageContainer message(*msg, *dest);
[9b5d30b]15 sentMessages[msg->id] = message;
[5a64bea]16
17 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
18
19 cout << "Send a message of type " << msg->type << endl;
20
21 return ret;
[1a3c42d]22}
23
[5a64bea]24int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
25 socklen_t socklen = sizeof(struct sockaddr_in);
26
27 // assume we don't care about the value of socklen
28 int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
29
30 // add id to the NETWORK_MSG struct
31 if (msg->type == MSG_TYPE_ACK) {
32 sentMessages.erase(msg->id);
33 }else {
34 NETWORK_MSG ack;
35 ack.id = msg->id;
36
37 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
38 }
39
40 return ret;
[1a3c42d]41}
42
[5a64bea]43void MessageProcessor::resendUnackedMessages(int sock) {
44 map<int, MessageContainer>::iterator it;
45
46 for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
47 sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
48 }
[1a3c42d]49}
50
51void MessageProcessor::cleanAckedMessages() {
[5a64bea]52 // shouldn't be needed since I can just remove messages when I get their ACKs
[1a3c42d]53}
Note: See TracBrowser for help on using the repository browser.