Changeset 1106210 in network-game


Ignore:
Timestamp:
Dec 30, 2012, 6:33:03 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
3535088
Parents:
092817a
Message:

Changed the player serialization code

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r092817a r1106210  
    5353   ostringstream oss;
    5454
     55   cout << "Player name: " << this->name << endl;
     56
     57   /*
    5558   oss.write((char*)&(this->id), sizeof(int));
    5659   oss << this->name;
     60   cout << "first oss str: " << oss.str() << endl;
    5761   oss.write("\0", 1);
     62   cout << "second oss str: " << oss.str() << endl;
    5863   oss.write((char*)&(this->pos.x), sizeof(int));
     64   cout << "third oss str: " << oss.str() << endl;
    5965   oss.write((char*)&(this->pos.y), sizeof(int));
     66   */
    6067
    61    memcpy(buffer, oss.str().c_str(), this->name.length()+1+2*sizeof(int));
     68   oss << this->id;
     69   oss << this->name;
     70   css << this->pos.x;
     71   css << this->pos.y;
     72
     73   memcpy(buffer, oss.str().c_str(), oss.str().length);
    6274}
    6375
     
    6779   iss.str(buffer);
    6880
     81   /*
    6982   iss.read((char*)&(this->id), sizeof(int));
    7083   iss >> this->name;
    7184   iss.read((char*)&(this->pos.x), sizeof(int));
    7285   iss.read((char*)&(this->pos.y), sizeof(int));
     86   */
     87
     88   iss >> this.id;
     89   iss >> this->name;
     90   iss >> this->pos.x;
     91   iss >> this->pos.y;
    7392}
    7493
  • server/server.cpp

    r092817a r1106210  
    3232bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
    3333
    34 void updateUnusedId(unsigned int& id);
     34void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
    3535
    3636// this should probably go somewhere in the common folder
     
    9494   NETWORK_MSG clientMsg, serverMsg;
    9595   map<unsigned int, Player> mapPlayers;
    96    unsigned int unusedId = 0;
     96   unsigned int unusedId = 1;
    9797
    9898   //SSL_load_error_strings();
     
    212212         {
    213213            p->setAddr(from);
     214            updateUnusedId(unusedId, mapPlayers);
    214215            p->id = unusedId;
    215216            mapPlayers[unusedId] = *p;
    216             updateUnusedId(unusedId);
    217217
    218218            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
     
    288288}
    289289
    290 void updateUnusedId(unsigned int& id)
    291 {
    292    id = 5;
    293 }
     290void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
     291{
     292   while (mapPlayers.find(id) != mapPlayers.end())
     293      id++;
     294}
Note: See TracChangeset for help on using the changeset viewer.