Changeset 46d6469 in network-game


Ignore:
Timestamp:
Jul 26, 2013, 6:28:39 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
9fe1807
Parents:
411c1ae
Message:

Some bug fixes for MessageProcessor. MessageContainer is now in its own files.

Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • common/MessageProcessor.cpp

    r411c1ae r46d6469  
    1313
    1414int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
     15   cout << "Sending message of type " << msg->type << endl;
     16
    1517   msg->id = ++lastUsedId;
    1618   MessageContainer message(*msg, *dest);
    1719   sentMessages[msg->id][dest->sin_addr.s_addr] = message;
    1820
    19    cout << "Sending message" << endl;
    20    cout << "id: " << msg->id << endl;
    21    cout << "type: " << msg->type << endl;
    22    cout << "buffer: " << msg->buffer << endl;
     21   sentMessages[msg->id][dest->sin_addr.s_addr] = message;
    2322
    2423   int ret =  sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
    25 
    26    cout << "Send a message of type " << msg->type << endl;
    2724
    2825   return ret;
     
    3835      return ret;
    3936
    40    cout << "Received message" << endl;
    41 
    4237   // add id to the NETWORK_MSG struct
    4338   if (msg->type == MSG_TYPE_ACK) {
    44       cout << "Received ack" << endl;
    45       if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) {
    46          cout << "Received new ack" << endl;
    47          sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true;
    48          sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis();
     39      if (!sentMessages[msg->id][source->sin_addr.s_addr].getAcked()) {
     40         sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true);
     41         sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis());
    4942      }
    5043
     
    5245   }else {
    5346      bool isDuplicate = false;
    54 
    55       cout << "Received real message" << endl;
    56       cout << "id: " << msg->id << endl;
    57       cout << "type: " << msg->type << endl;
    58       cout << "buffer: " << msg->buffer << endl;
    5947
    6048      if (ackedMessages.find(msg->id) != ackedMessages.end())
     
    8472      sentMsg = it->second;
    8573      for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) {
    86          sendto(sock, (char*)&it2->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
     74         if (!(it2->second.getAcked())) {
     75            sendto(sock, it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in));
     76         }
    8777      }
    8878   }
     
    9585   while (it != sentMessages.end()) {
    9686      it2 = it->second.begin();
    97       while (it2 != it->second.begin()) {
    98          if (it2->second.isAcked) {
    99             if ((getCurrentMillis() - it2->second.timeAcked) > 1000)
     87      while (it2 != it->second.end()) {
     88         if (it2->second.getAcked()) {
     89            if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000)
    10090               it->second.erase(it2++);
    10191            else
     
    114104
    115105   while (it3 != ackedMessages.end()) {
    116       if ((getCurrentMillis() - it3->second) > 500) {
     106      if ((getCurrentMillis() - it3->second) > 500)
    117107         ackedMessages.erase(it3++);
    118          cout << "Deleting ack record" << endl;
    119       }else
     108      else
    120109         it3++;
    121110   }
  • common/MessageProcessor.h

    r411c1ae r46d6469  
    44#include <map>
    55
    6 #include "Compiler.h"
    7 
    8 #if defined WINDOWS
    9    #include <winsock2.h>
    10    #include <WS2tcpip.h>
    11 #elif defined LINUX
    12    #include <netinet/in.h>
    13 #endif
    14 
    15 #define MSG_TYPE_ACK               1
    16 #define MSG_TYPE_REGISTER          2
    17 #define MSG_TYPE_LOGIN             3
    18 #define MSG_TYPE_LOGOUT            4
    19 #define MSG_TYPE_CHAT              5
    20 #define MSG_TYPE_PLAYER            6  // server sends this to update player positions
    21 #define MSG_TYPE_PLAYER_MOVE       7  // client sends this when a player wants to move
    22 #define MSG_TYPE_OBJECT            8
    23 #define MSG_TYPE_REMOVE_OBJECT     9
    24 #define MSG_TYPE_PICKUP_FLAG       10
    25 #define MSG_TYPE_DROP_FLAG         11
    26 #define MSG_TYPE_SCORE             12
    27 #define MSG_TYPE_START_ATTACK      13
    28 #define MSG_TYPE_ATTACK            14
    29 #define MSG_TYPE_PROJECTILE        15
    30 #define MSG_TYPE_REMOVE_PROJECTILE 16
    31 
    32 typedef struct
    33 {
    34    unsigned int id;
    35    unsigned short type;
    36    char buffer[256];
    37 } NETWORK_MSG;
     6#include "MessageContainer.h"
    387
    398using namespace std;
    409
    4110class MessageProcessor {
     11private:
     12   int lastUsedId;
     13
     14   // map from message ids to maps from player addresses to message info
     15   map<int, map<unsigned long, MessageContainer> > sentMessages;
     16
     17   // map from message ids to the time each mesage was acked
     18   map<unsigned int, unsigned long long> ackedMessages;
     19
     20   unsigned long pid;
     21
    4222public:
    4323   MessageProcessor();
     
    4828   void resendUnackedMessages(int sock);
    4929   void cleanAckedMessages();
    50 
    51 private:
    52    // this should eventually just replace the Message struct
    53    class MessageContainer {
    54    public:
    55       MessageContainer() {
    56       }
    57 
    58       MessageContainer(const MessageContainer& mc) {
    59          this->msg = mc.msg;
    60          this->clientAddr = mc.clientAddr;
    61       }
    62 
    63       MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr) {
    64          this->clientAddr = clientAddr;
    65          this->msg = msg;
    66       }
    67 
    68       ~MessageContainer() {
    69       }
    70 
    71       NETWORK_MSG msg;
    72       struct sockaddr_in clientAddr;
    73       bool isAcked;
    74       unsigned long long timeAcked;
    75    };
    76 
    77    int lastUsedId;
    78 
    79    // map from message ids to maps from player addresses to message info
    80    map<int, map<unsigned long, MessageContainer> > sentMessages;
    81 
    82    // map from message ids to the time each mesage was acked
    83    map<unsigned int, unsigned long long> ackedMessages;
    8430};
    8531
  • server/makefile

    r411c1ae r46d6469  
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Common.o MessageProcessor.o Player.o WorldMap.o DataAccess.o Projectile.o
     5DEPENDENCIES = Common.o MessageContainer.o MessageProcessor.o Player.o WorldMap.o DataAccess.o Projectile.o
    66
    77server : server.cpp $(DEPENDENCIES)
     
    99
    1010Common.o : $(COMMON_PATH)/Common.cpp
     11        $(CC) -c -o $@ $?
     12
     13MessageContainer.o : $(COMMON_PATH)/MessageContainer.cpp
    1114        $(CC) -c -o $@ $?
    1215
Note: See TracChangeset for help on using the changeset viewer.