Changeset 68d94de in network-game


Ignore:
Timestamp:
Dec 24, 2013, 3:02:22 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
8554263
Parents:
e1af80c
Message:

MessageProcessor now takes a socket and optional output log file as constructor arguments instead of accepting them as parameters in each method

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    re1af80c r68d94de  
    130130
    131131MessageProcessor msgProcessor;
    132 ofstream outputLog;
    133132
    134133int main(int argc, char **argv)
     
    141140   map<unsigned int, Projectile> mapProjectiles;
    142141   unsigned int curPlayerId = -1;
     142   ofstream outputLog;
     143
    143144   int scoreBlue, scoreRed;
    144145
     
    341342   memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
    342343   server.sin_port = htons(atoi(argv[2]));
     344
     345   msgProcessor = MessageProcessor(sock, &outputLog);
    343346
    344347   al_start_timer(timer);
     
    370373                  msgTo.type = MSG_TYPE_PICKUP_FLAG;
    371374                  memcpy(msgTo.buffer, &curPlayerId, 4);
    372                   msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     375                  msgProcessor.sendMessage(&msgTo, &server);
    373376               }
    374377               break;
     
    391394                        msgTo.type = MSG_TYPE_DROP_FLAG;
    392395                        memcpy(msgTo.buffer, &curPlayerId, 4);
    393                         msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     396                        msgProcessor.sendMessage(&msgTo, &server);
    394397                     }
    395398                  }
     
    414417                  memcpy(msgTo.buffer+8, &pos.y, 4);
    415418
    416                   msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     419                  msgProcessor.sendMessage(&msgTo, &server);
    417420               }
    418421               else
     
    449452                     memcpy(msgTo.buffer+4, &target->id, 4);
    450453
    451                      msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     454                     msgProcessor.sendMessage(&msgTo, &server);
    452455                  }
    453456               }
     
    456459      }
    457460
    458       if (msgProcessor.receiveMessage(&msgFrom, sock, &from, &outputLog) >= 0)
     461      if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
    459462         processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
    460463
     
    463466         redraw = false;
    464467
    465          msgProcessor.resendUnackedMessages(sock, &outputLog);
    466          //msgProcessor.cleanAckedMessages(&outputLog);
     468         msgProcessor.resendUnackedMessages();
    467469
    468470         if (debugging && wndCurrent == wndGame)
     
    981983               strcpy(msgTo.buffer, gameName.c_str());
    982984
    983                msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     985               msgProcessor.sendMessage(&msgTo, &server);
    984986
    985987               break;
     
    13621364   memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
    13631365
    1364    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1366   msgProcessor.sendMessage(&msgTo, &server);
    13651367}
    13661368
     
    13791381   strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
    13801382
    1381    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1383   msgProcessor.sendMessage(&msgTo, &server);
    13821384
    13831385   state = STATE_LOBBY;
     
    14041406   strcpy(msgTo.buffer, username.c_str());
    14051407
    1406    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1408   msgProcessor.sendMessage(&msgTo, &server);
    14071409}
    14081410
     
    14201422   strcpy(msgTo.buffer, msg.c_str());
    14211423
    1422    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1424   msgProcessor.sendMessage(&msgTo, &server);
    14231425}
    14241426
     
    14381440   strcpy(msgTo.buffer, msg.c_str());
    14391441
    1440    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1442   msgProcessor.sendMessage(&msgTo, &server);
    14411443}
    14421444
     
    14531455   strcpy(msgTo.buffer, msg.c_str());
    14541456
    1455    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1457   msgProcessor.sendMessage(&msgTo, &server);
    14561458}
    14571459
     
    14671469   msgTo.type = MSG_TYPE_LEAVE_GAME;
    14681470
    1469    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1471   msgProcessor.sendMessage(&msgTo, &server);
    14701472}
    14711473
  • common/MessageProcessor.cpp

    re1af80c r68d94de  
    1313
    1414MessageProcessor::MessageProcessor() {
     15   this->sock = 0;
     16   this->outputLog = NULL;
     17   lastUsedId = 0;
     18}
     19
     20MessageProcessor::MessageProcessor(int sock, ofstream* outputLog) {
     21   this->sock = sock;
     22   this->outputLog = outputLog;
    1523   lastUsedId = 0;
    1624}
     
    1927}
    2028
    21 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) {
     29int MessageProcessor::sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest) {
    2230   cout << "Sending message of type " << msg->type << endl;
    2331
     
    3543}
    3644
    37 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) {
     45int MessageProcessor::receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source) {
    3846   socklen_t socklen = sizeof(struct sockaddr_in);
    3947
     
    8492}
    8593
    86 void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) {
     94void MessageProcessor::resendUnackedMessages() {
    8795   map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
    8896   map<unsigned long, MessageContainer>::iterator it2;
     
    99107}
    100108
    101 void MessageProcessor::cleanAckedMessages(ofstream* outputLog) {
     109void MessageProcessor::cleanAckedMessages() {
    102110   map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin();
    103111   map<unsigned long, MessageContainer>::iterator it2;
  • common/MessageProcessor.h

    re1af80c r68d94de  
    1010class MessageProcessor {
    1111private:
     12   int sock;
     13   ofstream* outputLog;
    1214   int lastUsedId;
    1315
     
    1820   map<unsigned long, map<unsigned int, unsigned long long> > ackedMessages;
    1921
    20    unsigned long pid;
     22   //unsigned long pid;
    2123
    2224public:
    2325   MessageProcessor();
     26   MessageProcessor(int sock, ofstream* outputLog = NULL);
    2427   ~MessageProcessor();
    2528
    26    int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL);
    27    int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL);
    28    void resendUnackedMessages(int sock, ofstream* outputLog = NULL);
    29    void cleanAckedMessages(ofstream* outputLog = NULL);
     29   int sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest);
     30   int receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source);
     31   void resendUnackedMessages();
     32   void cleanAckedMessages();
    3033
    3134   map<unsigned int, map<unsigned long, MessageContainer> >& getSentMessages();
Note: See TracChangeset for help on using the changeset viewer.