Changeset 01d0d00 in network-game
- Timestamp:
- Dec 27, 2012, 7:04:49 PM (12 years ago)
- Branches:
- master
- Children:
- 0333211, 3a79253
- Parents:
- 5066e27
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Player.cpp
r5066e27 r01d0d00 7 7 using namespace std; 8 8 9 Player::Player() 10 { 11 this->id = 0; 12 this->name = ""; 13 this->password = ""; 14 this->pos.x = 0; 15 this->pos.y = 0; 16 } 17 18 Player::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 9 28 Player::Player(string name, string password) 10 29 { 30 this->id = 0; 11 31 this->name = name; 12 32 this->password = password; 13 33 this->pos.x = 200; 14 34 this->pos.y = 200; 15 16 cout << "Created new player: " << this->name << endl;17 35 } 18 36 19 37 Player::Player(string name, sockaddr_in addr) 20 38 { 39 this->id = 0; 21 40 this->name = name; 22 41 this->password = ""; … … 24 43 this->pos.y = 200; 25 44 this->addr = addr; 26 27 cout << "Created new played: " << this->name << endl;28 45 } 29 46 … … 36 53 ostringstream oss; 37 54 55 oss.write((char*)&(this->id), sizeof(int)); 38 56 oss << this->name; 39 57 oss.write("\0", 1); … … 49 67 iss.str(buffer); 50 68 69 iss.read((char*)&(this->id), sizeof(int)); 51 70 iss >> this->name; 52 71 iss.read((char*)&(this->pos.x), sizeof(int)); 53 72 iss.read((char*)&(this->pos.y), sizeof(int)); 73 } 74 75 void Player::setId(int id) 76 { 77 this->id = id; 54 78 } 55 79 -
common/Player.h
r5066e27 r01d0d00 19 19 class Player { 20 20 public: 21 Player(); 22 Player(const Player& p); 21 23 Player(string name, string password); 22 24 Player(string name, sockaddr_in addr); // this will be deleted 25 23 26 ~Player(); 24 27 … … 26 29 void deserialize(char* buffer); 27 30 31 void setId(int id); 28 32 void setAddr(sockaddr_in addr); 29 33 34 int id; 30 35 string name; 31 36 string password; -
server/server.cpp
r5066e27 r01d0d00 5 5 #include <iostream> 6 6 #include <sstream> 7 #include <cstring> 8 7 9 #include <vector> 8 #include <algorithm> 9 #include <cstring> 10 #include <map> 10 11 11 12 #include <sys/socket.h> … … 29 30 using namespace std; 30 31 31 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg); 32 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg); 33 34 void updateUnusedId(unsigned int& id); 32 35 33 36 // this should probably go somewhere in the common folder … … 38 41 } 39 42 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);43 Player *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); 48 51 } 49 52 … … 51 54 } 52 55 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);56 Player *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); 62 65 } 63 66 … … 65 68 } 66 69 67 void broadcastPlayerPositions( vector<Player> &vec, int sock)68 { 69 vector<Player>::iterator it, it2;70 void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock) 71 { 72 map<unsigned int, Player>::iterator it, it2; 70 73 NETWORK_MSG serverMsg; 71 74 72 75 serverMsg.type = MSG_TYPE_PLAYER; 73 76 74 for (it = vec.begin(); it != vec.end(); it++)75 { 76 it->se rialize(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 ) 81 84 error("sendMessage"); 82 85 } … … 90 93 struct sockaddr_in from; // info of client sending the message 91 94 NETWORK_MSG clientMsg, serverMsg; 92 vector<Player> vctPlayers; 95 map<unsigned int, Player> mapPlayers; 96 unsigned int unusedId = 0; 93 97 94 98 //SSL_load_error_strings(); … … 123 127 cout << "Got a message" << endl; 124 128 125 broadcastResponse = processMessage(clientMsg, from, vctPlayers, serverMsg);129 broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg); 126 130 127 131 cout << "msg: " << serverMsg.buffer << endl; … … 131 135 cout << "Should be broadcasting the message" << endl; 132 136 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++) 136 140 { 137 if ( sendMessage(&serverMsg, sock, &(it-> addr)) < 0 )141 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 138 142 error("sendMessage"); 139 143 } … … 147 151 } 148 152 149 broadcastPlayerPositions( vctPlayers, sock);153 broadcastPlayerPositions(mapPlayers, sock); 150 154 } 151 155 } … … 154 158 } 155 159 156 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)160 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg) 157 161 { 158 162 DataAccess da; … … 201 205 strcpy(serverMsg.buffer, "Incorrect username or password"); 202 206 } 203 else if(findPlayerByName( vctPlayers, username) != NULL)207 else if(findPlayerByName(mapPlayers, username) != NULL) 204 208 { 205 209 strcpy(serverMsg.buffer, "Player has already logged in."); … … 207 211 else 208 212 { 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 213 218 strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players."); 214 219 } … … 225 230 cout << "Player logging out: " << name << endl; 226 231 227 Player *p = findPlayerByName( vctPlayers, name);232 Player *p = findPlayerByName(mapPlayers, name); 228 233 229 234 if (p == NULL) … … 231 236 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server."); 232 237 } 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 ) 235 240 { 236 241 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."); … … 238 243 else 239 244 { 240 vctPlayers.erase((vector<Player>::iterator)p); 245 if (p->id < unusedId) 246 unusedId = p->id; 247 mapPlayers.erase(p->id); 241 248 strcpy(serverMsg.buffer, "You have successfully logged out."); 242 249 } … … 248 255 cout << "Got a chat message" << endl; 249 256 250 Player *p = findPlayerByAddr( vctPlayers, from);257 Player *p = findPlayerByAddr(mapPlayers, from); 251 258 252 259 if (p == NULL) … … 281 288 } 282 289 290 void updateUnusedId(unsigned int& id) 291 { 292 id = 5; 293 }
Note:
See TracChangeset
for help on using the changeset viewer.