Changeset 9b5d30b in network-game


Ignore:
Timestamp:
Jul 14, 2013, 9:22:38 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
10f6fc2
Parents:
5a64bea
Message:

Moved server message sending/receiving into MessageProcessor

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • common/Message.cpp

    r5a64bea r9b5d30b  
    1515using namespace std;
    1616
     17/*
    1718int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest)
    1819{
     
    3334   return ret;
    3435}
     36*/
  • common/Message.h

    r5a64bea r9b5d30b  
    2626} NETWORK_MSG;
    2727
    28 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
     28//int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
    2929
    30 int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source);
     30//int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source);
    3131
    3232#endif
  • common/MessageProcessor.cpp

    r5a64bea r9b5d30b  
    1111
    1212int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
     13   msg->id = ++lastUsedId;
    1314   MessageContainer message(*msg, *dest);
    14    message.id = ++lastUsedId;
    15    sentMessages[message.id] = message;
     15   sentMessages[msg->id] = message;
    1616
    1717   int ret =  sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
  • common/MessageProcessor.h

    r5a64bea r9b5d30b  
    5959
    6060      MessageContainer(const MessageContainer& mc) {
    61          this->id = mc.id;
     61         this->msg = mc.msg;
    6262         this->clientAddr = mc.clientAddr;
    63          this->msg = mc.msg;
    64          this->ackReceived = mc.ackReceived;
    6563      }
    6664
     
    7371      }
    7472
    75       int id;
     73      NETWORK_MSG msg;
    7674      struct sockaddr_in clientAddr;
    77       NETWORK_MSG msg;
    78       bool ackReceived;
    7975   };
    8076
  • server/server.cpp

    r5a64bea r9b5d30b  
    2929#include "../common/Common.h"
    3030#include "../common/Message.h"
     31#include "../common/MessageProcessor.h"
    3132#include "../common/WorldMap.h"
    3233#include "../common/Player.h"
     
    3940// from used to be const. Removed that so I could take a reference
    4041// and use it to send messages
    41 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
     42bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
    4243
    4344void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    8586   struct sockaddr_in from; // info of client sending the message
    8687   NETWORK_MSG clientMsg, serverMsg;
     88   MessageProcessor msgProcessor;
    8789   map<unsigned int, Player> mapPlayers;
    8890   map<unsigned int, Projectile> mapProjectiles;
     
    146148      if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
    147149         timeLastUpdated = curTime;
     150
     151         msgProcessor.resendUnackedMessages(sock);
    148152
    149153         map<unsigned int, Player>::iterator it;
     
    185189                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    186190                  {
    187                      if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     191                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    188192                        error("sendMessage");
    189193                  }
     
    200204               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    201205               {
    202                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     206                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    203207                     error("sendMessage");
    204208               }
     
    313317                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    314318                  {
    315                      if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     319                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    316320                        error("sendMessage");
    317321                  }
     
    323327                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    324328                  {
    325                      if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     329                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    326330                        error("sendMessage");
    327331                  }
     
    364368                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    365369                  {
    366                      if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     370                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    367371                        error("sendMessage");
    368372                  }
     
    377381                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    378382                  {
    379                      if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     383                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    380384                        error("sendMessage");
    381385                  }
     
    398402               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    399403               {
    400                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     404                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    401405                     error("sendMessage");
    402406               }
     
    434438               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    435439               {
    436                   if (sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     440                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    437441                     error("sendMessage");
    438442               }
     
    456460               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    457461               {
    458                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     462                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    459463                     error("sendMessage");
    460464               }
     
    472476               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    473477               {
    474                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     478                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    475479                     error("sendMessage");
    476480               }
     
    480484      }
    481485
    482       n = receiveMessage(&clientMsg, sock, &from);
     486      n = msgProcessor.receiveMessage(&clientMsg, sock, &from);
    483487
    484488      if (n >= 0) {
     
    493497            {
    494498               cout << "Sent message back to " << it->second.name << endl;
    495                if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     499               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    496500                  error("sendMessage");
    497501            }
     
    501505            cout << "Should be sending back the message" << endl;
    502506
    503             if ( sendMessage(&serverMsg, sock, &from) < 0 )
     507            if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
    504508               error("sendMessage");
    505509         }
     
    510514}
    511515
    512 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)
     516bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed)
    513517{
    514518   DataAccess da;
     
    595599               cout << "sending info about " << it->second.name  << endl;
    596600               cout << "sending id " << it->second.id  << endl;
    597                if ( sendMessage(&serverMsg, sock, &from) < 0 )
     601               if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
    598602                  error("sendMessage");
    599603            }
     
    608612               itObjects->serialize(serverMsg.buffer);
    609613               cout << "sending item id " << itObjects->id  << endl;
    610                if ( sendMessage(&serverMsg, sock, &from) < 0 )
     614               if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
    611615                  error("sendMessage");
    612616            }
     
    616620            memcpy(serverMsg.buffer, &scoreBlue, 4);
    617621            memcpy(serverMsg.buffer+4, &scoreRed, 4);
    618             if ( sendMessage(&serverMsg, sock, &from) < 0 )
     622            if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
    619623               error("sendMessage");
    620624
     
    626630            {
    627631               cout << "Sent message back to " << it->second.name << endl;
    628                if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     632               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    629633                  error("sendMessage");
    630634            }
     
    779783                  for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    780784                  {
    781                      if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     785                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    782786                        error("sendMessage");
    783787                  }
     
    824828         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    825829         {
    826             if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     830            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    827831               error("sendMessage");
    828832         }
Note: See TracChangeset for help on using the changeset viewer.