Changeset bace57b in network-game
- Timestamp:
- Jul 20, 2013, 1:24:43 PM (11 years ago)
- Branches:
- master
- Children:
- 64d22ac
- Parents:
- cc6a14a
- Location:
- common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
common/MessageProcessor.cpp
rcc6a14a rbace57b 15 15 msg->id = ++lastUsedId; 16 16 MessageContainer message(*msg, *dest); 17 sentMessages[msg->id] = message;17 sentMessages[msg->id][dest->sin_addr.s_addr] = message; 18 18 19 19 cout << "Sending message" << endl; … … 40 40 // add id to the NETWORK_MSG struct 41 41 if (msg->type == MSG_TYPE_ACK) { 42 if (!sentMessages[msg->id] .isAcked) {42 if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) { 43 43 cout << "Received new ack" << endl; 44 sentMessages[msg->id] .isAcked = true;45 sentMessages[msg->id] .timeAcked = getCurrentMillis();44 sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true; 45 sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis(); 46 46 } 47 47 48 48 return -1; // don't do any further processing 49 49 }else { 50 bool isDuplicate = false; 51 50 52 cout << "Received message" << endl; 51 53 cout << "id: " << msg->id << endl; … … 53 55 cout << "buffer: " << msg->buffer << endl; 54 56 55 if (ackedMessages.find(msg->id) == ackedMessages.end()) {56 cout << "Not a duplicate" << endl;57 if (ackedMessages.find(msg->id) == ackedMessages.end()) 58 isDuplicate = true; 57 59 58 60 ackedMessages[msg->id] = getCurrentMillis(); 59 61 60 61 62 62 NETWORK_MSG ack; 63 ack.id = msg->id; 64 ack.type = MSG_TYPE_ACK; 63 65 64 65 }else { 66 cout << "Got duplicate ack" << endl;66 sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in)); 67 68 if (isDuplicate) 67 69 return -1; 68 }69 70 } 70 71 … … 73 74 74 75 void MessageProcessor::resendUnackedMessages(int sock) { 75 map<int, MessageContainer>::iterator it; 76 map<int, map<unsigned long, MessageContainer> >::iterator it; 77 map<unsigned long, MessageContainer>::iterator it2; 78 map<unsigned long, MessageContainer> sentMsg; 76 79 77 for(it = sentMessages.begin(); it != sentMessages.end(); it++) { 78 sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in)); 80 for (it = sentMessages.begin(); it != sentMessages.end(); it++) { 81 sentMsg = it->second; 82 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) { 83 sendto(sock, (char*)&it2->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in)); 84 } 79 85 } 80 86 } 81 87 82 88 void MessageProcessor::cleanAckedMessages() { 83 map<int, MessageContainer>::iterator it = sentMessages.begin(); 89 map<int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin(); 90 map<unsigned long, MessageContainer>::iterator it2; 84 91 85 92 while (it != sentMessages.end()) { 86 if (it->second.isAcked) { 87 // cout << "Found acked message" << endl; 88 // cout << "time acked" << it->second.timeAcked << endl; 89 // cout << "cur time" << getCurrentMillis() << endl; 90 if ((getCurrentMillis() - it->second.timeAcked) > 1000) { 91 cout << "Message was acked. time to delete it" << endl; 92 cout << "old map size" << sentMessages.size() << endl; 93 sentMessages.erase(it++); 94 cout << "new map size" << sentMessages.size() << endl; 93 it2 = it->second.begin(); 94 while (it2 != it->second.begin()) { 95 if (it2->second.isAcked) { 96 if ((getCurrentMillis() - it2->second.timeAcked) > 1000) 97 it->second.erase(it2++); 98 else 99 it2++; 95 100 }else 96 it++; 97 }else 101 it2++; 102 } 103 104 if (it->second.size() == 0) 105 sentMessages.erase(it++); 106 else 98 107 it++; 99 108 } 100 109 101 map<unsigned int, unsigned long long>::iterator it 2= ackedMessages.begin();110 map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin(); 102 111 103 while (it 2!= ackedMessages.end()) {104 if ((getCurrentMillis() - it 2->second) > 500) {105 ackedMessages.erase(it 2++);112 while (it3 != ackedMessages.end()) { 113 if ((getCurrentMillis() - it3->second) > 500) { 114 ackedMessages.erase(it3++); 106 115 cout << "Deleting ack record" << endl; 107 116 }else 108 it 2++;117 it3++; 109 118 } 110 119 } -
common/MessageProcessor.h
rcc6a14a rbace57b 78 78 79 79 int lastUsedId; 80 map<int, MessageContainer> sentMessages; 80 81 // map from message ids to maps from player addresses to message info 82 map<int, map<unsigned long, MessageContainer> > sentMessages; 83 84 // map from message ids to the time each mesage was acked 81 85 map<unsigned int, unsigned long long> ackedMessages; 82 86 };
Note:
See TracChangeset
for help on using the changeset viewer.