Changeset 01d0d00 in network-game for server


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.