- Timestamp:
- May 9, 2013, 1:32:42 AM (12 years ago)
- Branches:
- master
- Children:
- db58227
- Parents:
- 227baaa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r227baaa rd211210 11 11 #include <map> 12 12 13 #include <sys/time.h> 14 13 15 #include <sys/socket.h> 14 16 #include <netdb.h> … … 34 36 using namespace std; 35 37 36 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg); 38 // from used to be const. Removed that so I could take a reference 39 // and use it to send messages 40 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock); 37 41 38 42 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers); … … 124 128 125 129 bool broadcastResponse; 130 timespec ts; 131 long timeLastUpdated = 0, curTime = 0; 126 132 while (true) { 127 133 128 134 usleep(5000); 135 136 clock_gettime(CLOCK_REALTIME, &ts); 137 curTime = ts.tv_sec + ts.tv_nsec*1000000000; 138 139 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50000) { 140 timeLastUpdated = curTime; 141 142 // maybe put this in a separate method 143 map<unsigned int, Player>::iterator it, it2; 144 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 145 if (!it->second.move(gameMap)) { 146 cout << "Cenceling move" << endl; 147 //serverMsg.type = MSG_TYPE_PLAYER; 148 //it->second.serialize(serverMsg.buffer); 149 150 cout << "about to send move cencellation" << endl; 151 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 152 { 153 //if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 154 // error("sendMessage"); 155 } 156 } 157 } 158 } 129 159 130 160 n = receiveMessage(&clientMsg, sock, &from); … … 133 163 cout << "Got a message" << endl; 134 164 135 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg );165 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock); 136 166 137 167 // probably replace this with a function that prints based on the … … 146 176 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 147 177 { 178 cout << "Sent message back to " << it->second.name << endl; 148 179 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 149 180 error("sendMessage"); … … 159 190 } 160 191 192 // we don't want to update and broadcast player positions here 193 // when a player sends a position update, we want to check if 194 // it's reasonable and send it out to all other players 195 161 196 // update player positions 197 /* 162 198 map<unsigned int, Player>::iterator it; 163 199 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) … … 167 203 168 204 broadcastPlayerPositions(mapPlayers, sock); 205 */ 169 206 } 170 207 … … 172 209 } 173 210 174 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)211 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock) 175 212 { 176 213 DataAccess da; … … 209 246 cout << "Got login message" << endl; 210 247 248 serverMsg.type = MSG_TYPE_LOGIN; 249 211 250 string username(clientMsg.buffer); 212 251 string password(strchr(clientMsg.buffer, '\0')+1); … … 224 263 else 225 264 { 265 serverMsg.type = MSG_TYPE_PLAYER; 266 226 267 p->setAddr(from); 227 268 updateUnusedId(unusedId, mapPlayers); 228 269 p->id = unusedId; 270 cout << "new player id: " << p->id << endl; 271 272 // tell the new player about all the existing players 273 cout << "Sending other players to new player" << endl; 274 275 map<unsigned int, Player>::iterator it; 276 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 277 { 278 it->second.serialize(serverMsg.buffer); 279 280 cout << "sending info about " << it->second.name << endl; 281 cout << "sending ind " << it->second.id << endl; 282 if ( sendMessage(&serverMsg, sock, &from) < 0 ) 283 error("sendMessage"); 284 } 285 286 p->serialize(serverMsg.buffer); 287 cout << "Should be broadcasting the message" << endl; 288 289 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 290 { 291 cout << "Sent message back to " << it->second.name << endl; 292 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 ) 293 error("sendMessage"); 294 } 295 296 serverMsg.type = MSG_TYPE_LOGIN; 229 297 mapPlayers[unusedId] = *p; 230 231 // sendd back the new player info to the user 232 p->serialize(serverMsg.buffer); 233 } 234 235 serverMsg.type = MSG_TYPE_LOGIN; 236 298 } 299 237 300 delete(p); 238 301 … … 266 329 } 267 330 268 // should really be serverMsg.type = MSG_TYPE_LOGOUT; 269 serverMsg.type = MSG_TYPE_LOGIN; 331 serverMsg.type = MSG_TYPE_LOGOUT; 270 332 271 333 break; … … 321 383 cout << "valid terrain" << endl; 322 384 323 cout << "orig x: " << mapPlayers[id].pos.x << endl;324 cout << "orig y: " << mapPlayers[id].pos.y << endl;385 //cout << "orig x: " << mapPlayers[id].pos.x << endl; 386 //cout << "orig y: " << mapPlayers[id].pos.y << endl; 325 387 // first we get the correct vector 326 388 mapPlayers[id].target.x = x; … … 328 390 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x; 329 391 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y; 330 cout << "xDiff: " << xDiff << endl;331 cout << "yDiff: " << yDiff << endl;392 //cout << "xDiff: " << xDiff << endl; 393 //cout << "yDiff: " << yDiff << endl; 332 394 333 395 // then we get the correct angle … … 344 406 345 407 memcpy(serverMsg.buffer, &id, 4); 346 memcpy(serverMsg.buffer+4, &mapPlayers[id]. pos.x, 4);347 memcpy(serverMsg.buffer+8, &mapPlayers[id]. pos.y, 4);408 memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4); 409 memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4); 348 410 //memcpy(serverMsg.buffer, clientMsg.buffer, 12); 349 411
Note:
See TracChangeset
for help on using the changeset viewer.