Changeset 01d0d00 in network-game


Ignore:
Timestamp:
Dec 27, 2012, 7:04:49 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
0333211, 3a79253
Parents:
5066e27
Message:

The server now uses a map to store players with player ids as keys

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r5066e27 r01d0d00  
    77using namespace std;
    88
     9Player::Player()
     10{
     11   this->id = 0;
     12   this->name = "";
     13   this->password = "";
     14   this->pos.x = 0;
     15   this->pos.y = 0;
     16}
     17
     18Player::Player(const Player& p)
     19{
     20   this->id = p.id;
     21   this->name = p.name;
     22   this->password = p.password;
     23   this->pos.x = p.pos.x;
     24   this->pos.y = p.pos.y;
     25   this->addr = p.addr;
     26}
     27
    928Player::Player(string name, string password)
    1029{
     30   this->id = 0;
    1131   this->name = name;
    1232   this->password = password;
    1333   this->pos.x = 200;
    1434   this->pos.y = 200;
    15 
    16    cout << "Created new player: " << this->name << endl;
    1735}
    1836
    1937Player::Player(string name, sockaddr_in addr)
    2038{
     39   this->id = 0;
    2140   this->name = name;
    2241   this->password = "";
     
    2443   this->pos.y = 200;
    2544   this->addr = addr;
    26 
    27    cout << "Created new played: " << this->name << endl;
    2845}
    2946
     
    3653   ostringstream oss;
    3754
     55   oss.write((char*)&(this->id), sizeof(int));
    3856   oss << this->name;
    3957   oss.write("\0", 1);
     
    4967   iss.str(buffer);
    5068
     69   iss.read((char*)&(this->id), sizeof(int));
    5170   iss >> this->name;
    5271   iss.read((char*)&(this->pos.x), sizeof(int));
    5372   iss.read((char*)&(this->pos.y), sizeof(int));
     73}
     74
     75void Player::setId(int id)
     76{
     77   this->id = id;
    5478}
    5579
  • common/Player.h

    r5066e27 r01d0d00  
    1919class Player {
    2020public:
     21   Player();
     22   Player(const Player& p);
    2123   Player(string name, string password);
    2224   Player(string name, sockaddr_in addr); // this will be deleted
     25
    2326   ~Player();
    2427
     
    2629   void deserialize(char* buffer);
    2730
     31   void setId(int id);
    2832   void setAddr(sockaddr_in addr);
    2933
     34   int id;
    3035   string name;
    3136   string password;
  • server/server.cpp

    r5066e27 r01d0d00  
    55#include <iostream>
    66#include <sstream>
     7#include <cstring>
     8
    79#include <vector>
    8 #include <algorithm>
    9 #include <cstring>
     10#include <map>
    1011
    1112#include <sys/socket.h>
     
    2930using namespace std;
    3031
    31 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
     32bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     33
     34void updateUnusedId(unsigned int& id);
    3235
    3336// this should probably go somewhere in the common folder
     
    3841}
    3942
    40 Player *findPlayerByName(vector<Player> &vec, string name)
    41 {
    42    vector<Player>::iterator it;
    43 
    44    for (it = vec.begin(); it != vec.end(); it++)
    45    {
    46       if ( it->name.compare(name) == 0 )
    47          return &(*it);
     43Player *findPlayerByName(map<unsigned int, Player> &m, string name)
     44{
     45   map<unsigned int, Player>::iterator it;
     46
     47   for (it = m.begin(); it != m.end(); it++)
     48   {
     49      if ( it->second.name.compare(name) == 0 )
     50         return &(it->second);
    4851   }
    4952
     
    5154}
    5255
    53 Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
    54 {
    55    vector<Player>::iterator it;
    56 
    57    for (it = vec.begin(); it != vec.end(); it++)
    58    {
    59       if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
    60            it->addr.sin_port == addr.sin_port )
    61          return &(*it);
     56Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
     57{
     58   map<unsigned int, Player>::iterator it;
     59
     60   for (it = m.begin(); it != m.end(); it++)
     61   {
     62      if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
     63           it->second.addr.sin_port == addr.sin_port )
     64         return &(it->second);
    6265   }
    6366
     
    6568}
    6669
    67 void broadcastPlayerPositions(vector<Player> &vec, int sock)
    68 {
    69    vector<Player>::iterator it, it2;
     70void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock)
     71{
     72   map<unsigned int, Player>::iterator it, it2;
    7073   NETWORK_MSG serverMsg;
    7174
    7275   serverMsg.type = MSG_TYPE_PLAYER;   
    7376
    74    for (it = vec.begin(); it != vec.end(); it++)
    75    {
    76       it->serialize(serverMsg.buffer);
    77 
    78       for (it2 = vec.begin(); it2 != vec.end(); it2++)
    79       {
    80          if ( sendMessage(&serverMsg, sock, &(it2->addr)) < 0 )
     77   for (it = m.begin(); it != m.end(); it++)
     78   {
     79      it->second.serialize(serverMsg.buffer);
     80
     81      for (it2 = m.begin(); it2 != m.end(); it2++)
     82      {
     83         if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    8184            error("sendMessage");
    8285      }
     
    9093   struct sockaddr_in from; // info of client sending the message
    9194   NETWORK_MSG clientMsg, serverMsg;
    92    vector<Player> vctPlayers;
     95   map<unsigned int, Player> mapPlayers;
     96   unsigned int unusedId = 0;
    9397
    9498   //SSL_load_error_strings();
     
    123127         cout << "Got a message" << endl;
    124128
    125          broadcastResponse = processMessage(clientMsg, from, vctPlayers, serverMsg);
     129         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
    126130
    127131         cout << "msg: " << serverMsg.buffer << endl;
     
    131135            cout << "Should be broadcasting the message" << endl;
    132136
    133             vector<Player>::iterator it;
    134 
    135             for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
     137            map<unsigned int, Player>::iterator it;
     138
     139            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    136140            {
    137                if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
     141               if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    138142                  error("sendMessage");
    139143            }
     
    147151         }
    148152
    149          broadcastPlayerPositions(vctPlayers, sock);
     153         broadcastPlayerPositions(mapPlayers, sock);
    150154      }
    151155   }
     
    154158}
    155159
    156 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
     160bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
    157161{
    158162   DataAccess da;
     
    201205            strcpy(serverMsg.buffer, "Incorrect username or password");
    202206         }
    203          else if(findPlayerByName(vctPlayers, username) != NULL)
     207         else if(findPlayerByName(mapPlayers, username) != NULL)
    204208         {
    205209            strcpy(serverMsg.buffer, "Player has already logged in.");
     
    207211         else
    208212         {
    209             Player newP(username, "");
    210             newP.setAddr(from);
    211 
    212             vctPlayers.push_back(newP);
     213            p->setAddr(from);
     214            p->id = unusedId;
     215            mapPlayers[unusedId] = *p;
     216            updateUnusedId(unusedId);
     217
    213218            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
    214219         }
     
    225230         cout << "Player logging out: " << name << endl;
    226231
    227          Player *p = findPlayerByName(vctPlayers, name);
     232         Player *p = findPlayerByName(mapPlayers, name);
    228233
    229234         if (p == NULL)
     
    231236            strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
    232237         }
    233          else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
    234                   p->addr.sin_port != from.sin_port )
     238         else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
     239                   p->addr.sin_port != from.sin_port )
    235240         {
    236241            strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
     
    238243         else
    239244         {
    240             vctPlayers.erase((vector<Player>::iterator)p);
     245            if (p->id < unusedId)
     246               unusedId = p->id;
     247            mapPlayers.erase(p->id);
    241248            strcpy(serverMsg.buffer, "You have successfully logged out.");
    242249         }
     
    248255         cout << "Got a chat message" << endl;
    249256
    250          Player *p = findPlayerByAddr(vctPlayers, from);
     257         Player *p = findPlayerByAddr(mapPlayers, from);
    251258
    252259         if (p == NULL)
     
    281288}
    282289
     290void updateUnusedId(unsigned int& id)
     291{
     292   id = 5;
     293}
Note: See TracChangeset for help on using the changeset viewer.