Changeset bace57b in network-game


Ignore:
Timestamp:
Jul 20, 2013, 1:24:43 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
64d22ac
Parents:
cc6a14a
Message:

MessageProcessor stores the recipient address along with each sent message, so it can properly resend messages

Location:
common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • common/MessageProcessor.cpp

    rcc6a14a rbace57b  
    1515   msg->id = ++lastUsedId;
    1616   MessageContainer message(*msg, *dest);
    17    sentMessages[msg->id] = message;
     17   sentMessages[msg->id][dest->sin_addr.s_addr] = message;
    1818
    1919   cout << "Sending message" << endl;
     
    4040   // add id to the NETWORK_MSG struct
    4141   if (msg->type == MSG_TYPE_ACK) {
    42       if (!sentMessages[msg->id].isAcked) {
     42      if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) {
    4343         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();
    4646      }
    4747
    4848      return -1; // don't do any further processing
    4949   }else {
     50      bool isDuplicate = false;
     51
    5052      cout << "Received message" << endl;
    5153      cout << "id: " << msg->id << endl;
     
    5355      cout << "buffer: " << msg->buffer << endl;
    5456
    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;
    5759
    58          ackedMessages[msg->id] = getCurrentMillis();
     60      ackedMessages[msg->id] = getCurrentMillis();
    5961
    60          NETWORK_MSG ack;
    61          ack.id = msg->id;
    62          ack.type = MSG_TYPE_ACK;
     62      NETWORK_MSG ack;
     63      ack.id = msg->id;
     64      ack.type = MSG_TYPE_ACK;
    6365
    64          sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
    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)
    6769         return -1;
    68       }
    6970   }
    7071
     
    7374
    7475void 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;
    7679
    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      }
    7985   }
    8086}
    8187
    8288void 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;
    8491
    8592   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++;
    95100         }else
    96             it++;
    97       }else
     101            it2++;
     102      }
     103
     104      if (it->second.size() == 0)
     105         sentMessages.erase(it++);
     106      else
    98107         it++;
    99108   }
    100109
    101    map<unsigned int, unsigned long long>::iterator it2 = ackedMessages.begin();
     110   map<unsigned int, unsigned long long>::iterator it3 = ackedMessages.begin();
    102111
    103    while (it2 != ackedMessages.end()) {
    104       if ((getCurrentMillis() - it2->second) > 500) {
    105          ackedMessages.erase(it2++);
     112   while (it3 != ackedMessages.end()) {
     113      if ((getCurrentMillis() - it3->second) > 500) {
     114         ackedMessages.erase(it3++);
    106115         cout << "Deleting ack record" << endl;
    107116      }else
    108          it2++;
     117         it3++;
    109118   }
    110119}
  • common/MessageProcessor.h

    rcc6a14a rbace57b  
    7878
    7979   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
    8185   map<unsigned int, unsigned long long> ackedMessages;
    8286};
Note: See TracChangeset for help on using the changeset viewer.