source: network-game/common/MessageProcessor.cpp@ 10f6fc2

Last change on this file since 10f6fc2 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
Line 
1#include "MessageProcessor.h"
2
3#include <iostream>
4
5MessageProcessor::MessageProcessor() {
6 lastUsedId = 0;
7}
8
9MessageProcessor::~MessageProcessor() {
10}
11
12int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
13 msg->id = ++lastUsedId;
14 MessageContainer message(*msg, *dest);
15 sentMessages[msg->id] = message;
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;
22}
23
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;
41}
42
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 }
49}
50
51void MessageProcessor::cleanAckedMessages() {
52 // shouldn't be needed since I can just remove messages when I get their ACKs
53}
Note: See TracBrowser for help on using the repository browser.