Changeset 5a64bea in network-game for common/MessageProcessor.cpp
- Timestamp:
- Jul 14, 2013, 6:44:29 PM (12 years ago)
- Branches:
- master
- Children:
- 9b5d30b
- Parents:
- 1f1eb58
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
common/MessageProcessor.cpp
r1f1eb58 r5a64bea 1 1 #include "MessageProcessor.h" 2 2 3 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) { 4 return 0; 3 #include <iostream> 4 5 MessageProcessor::MessageProcessor() { 6 lastUsedId = 0; 5 7 } 6 8 7 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) { 8 return 0; 9 MessageProcessor::~MessageProcessor() { 9 10 } 10 11 11 void MessageProcessor::resendUnackedMessages() { 12 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) { 13 MessageContainer message(*msg, *dest); 14 message.id = ++lastUsedId; 15 sentMessages[message.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 24 int 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 43 void 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 } 12 49 } 13 50 14 51 void MessageProcessor::cleanAckedMessages() { 52 // shouldn't be needed since I can just remove messages when I get their ACKs 15 53 }
Note:
See TracChangeset
for help on using the changeset viewer.