Changeset 5a64bea in network-game
- Timestamp:
- Jul 14, 2013, 6:44:29 PM (11 years ago)
- Branches:
- master
- Children:
- 9b5d30b
- Parents:
- 1f1eb58
- Location:
- common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Message.h
r1f1eb58 r5a64bea 21 21 typedef struct 22 22 { 23 short type; 23 unsigned int id; 24 unsigned short type; 24 25 char buffer[256]; 25 26 } NETWORK_MSG; -
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 } -
common/MessageProcessor.h
r1f1eb58 r5a64bea 2 2 #define _MESSAGE_PROCESSOR_H 3 3 4 #include <map> 5 6 #include "Compiler.h" 7 4 8 #include "Message.h" 9 10 #if defined WINDOWS 11 #include <winsock2.h> 12 #include <WS2tcpip.h> 13 #elif defined LINUX 14 #include <netinet/in.h> 15 #endif 5 16 6 17 /* … … 28 39 */ 29 40 41 using namespace std; 42 30 43 class MessageProcessor { 31 44 public: 45 MessageProcessor(); 46 ~MessageProcessor(); 47 32 48 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest); 33 49 int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest); 34 void resendUnackedMessages( );50 void resendUnackedMessages(int sock); 35 51 void cleanAckedMessages(); 36 52 37 53 private: 54 // this should eventually just replace the Message struct 55 class MessageContainer { 56 public: 57 MessageContainer() { 58 } 59 60 MessageContainer(const MessageContainer& mc) { 61 this->id = mc.id; 62 this->clientAddr = mc.clientAddr; 63 this->msg = mc.msg; 64 this->ackReceived = mc.ackReceived; 65 } 66 67 MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr) { 68 this->clientAddr = clientAddr; 69 this->msg = msg; 70 } 71 72 ~MessageContainer() { 73 } 74 75 int id; 76 struct sockaddr_in clientAddr; 77 NETWORK_MSG msg; 78 bool ackReceived; 79 }; 80 81 int lastUsedId; 82 map<int, MessageContainer> sentMessages; 38 83 }; 39 84
Note:
See TracChangeset
for help on using the changeset viewer.