source: network-game/server/server.cpp@ 7d91bbe

Last change on this file since 7d91bbe was d211210, checked in by dportnoy <dmp1488@…>, 12 years ago

Add server-side checking of player movement

  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[2488852]1#include <cstdlib>
[371ce29]2#include <cstdio>
[e3535b3]3#include <unistd.h>
[2488852]4#include <string>
[e3535b3]5#include <iostream>
[3b1efcc]6#include <sstream>
[edfd1d0]7#include <cstring>
[60017fc]8#include <cmath>
[371ce29]9
[01d0d00]10#include <vector>
11#include <map>
12
[d211210]13#include <sys/time.h>
14
[73f75c1]15#include <sys/socket.h>
[371ce29]16#include <netdb.h>
[73f75c1]17#include <netinet/in.h>
18#include <arpa/inet.h>
19
[b128109]20#include <crypt.h>
21
[edfd1d0]22/*
[e3535b3]23#include <openssl/bio.h>
24#include <openssl/ssl.h>
25#include <openssl/err.h>
[edfd1d0]26*/
[e3535b3]27
[b53c6b3]28#include "../common/Compiler.h"
[3b1efcc]29#include "../common/Common.h"
[edfd1d0]30#include "../common/Message.h"
[60017fc]31#include "../common/WorldMap.h"
[edfd1d0]32#include "../common/Player.h"
[b53c6b3]33
[36082e8]34#include "DataAccess.h"
[d2b411a]35
[e3535b3]36using namespace std;
37
[d211210]38// from used to be const. Removed that so I could take a reference
39// and use it to send messages
40bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock);
[01d0d00]41
[1106210]42void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
[8e540f4]43
[73f75c1]44// this should probably go somewhere in the common folder
[e3535b3]45void error(const char *msg)
46{
47 perror(msg);
48 exit(0);
49}
50
[01d0d00]51Player *findPlayerByName(map<unsigned int, Player> &m, string name)
[2488852]52{
[01d0d00]53 map<unsigned int, Player>::iterator it;
[2488852]54
[01d0d00]55 for (it = m.begin(); it != m.end(); it++)
[2488852]56 {
[01d0d00]57 if ( it->second.name.compare(name) == 0 )
58 return &(it->second);
[2488852]59 }
60
61 return NULL;
62}
63
[01d0d00]64Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
[73f75c1]65{
[01d0d00]66 map<unsigned int, Player>::iterator it;
[73f75c1]67
[01d0d00]68 for (it = m.begin(); it != m.end(); it++)
[73f75c1]69 {
[01d0d00]70 if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
71 it->second.addr.sin_port == addr.sin_port )
72 return &(it->second);
[73f75c1]73 }
74
75 return NULL;
76}
77
[01d0d00]78void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock)
[edfd1d0]79{
[01d0d00]80 map<unsigned int, Player>::iterator it, it2;
[edfd1d0]81 NETWORK_MSG serverMsg;
82
83 serverMsg.type = MSG_TYPE_PLAYER;
84
[01d0d00]85 for (it = m.begin(); it != m.end(); it++)
[edfd1d0]86 {
[01d0d00]87 it->second.serialize(serverMsg.buffer);
[edfd1d0]88
[01d0d00]89 for (it2 = m.begin(); it2 != m.end(); it2++)
[edfd1d0]90 {
[01d0d00]91 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
[edfd1d0]92 error("sendMessage");
93 }
94 }
95}
96
[e3535b3]97int main(int argc, char *argv[])
98{
99 int sock, length, n;
100 struct sockaddr_in server;
[3b1efcc]101 struct sockaddr_in from; // info of client sending the message
[e084950]102 NETWORK_MSG clientMsg, serverMsg;
[01d0d00]103 map<unsigned int, Player> mapPlayers;
[1106210]104 unsigned int unusedId = 1;
[e084950]105
[edfd1d0]106 //SSL_load_error_strings();
107 //ERR_load_BIO_strings();
108 //OpenSSL_add_all_algorithms();
[e3535b3]109
110 if (argc < 2) {
[73f75c1]111 cerr << "ERROR, no port provided" << endl;
112 exit(1);
[e3535b3]113 }
[60017fc]114
[7b43385]115 WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
[41ad8ed]116
[371ce29]117 sock = socket(AF_INET, SOCK_DGRAM, 0);
[e3535b3]118 if (sock < 0) error("Opening socket");
119 length = sizeof(server);
120 bzero(&server,length);
121 server.sin_family=AF_INET;
122 server.sin_port=htons(atoi(argv[1]));
[2488852]123 server.sin_addr.s_addr=INADDR_ANY;
124 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
[e084950]125 error("binding");
[73f75c1]126
[371ce29]127 set_nonblock(sock);
128
[da692b9]129 bool broadcastResponse;
[d211210]130 timespec ts;
131 long timeLastUpdated = 0, curTime = 0;
[cb1f288]132 while (true) {
[371ce29]133
134 usleep(5000);
135
[d211210]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 }
159
[e084950]160 n = receiveMessage(&clientMsg, sock, &from);
[8e540f4]161
[371ce29]162 if (n >= 0) {
163 cout << "Got a message" << endl;
[8e540f4]164
[d211210]165 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
[371ce29]166
[b128109]167 // probably replace this with a function that prints based on the
168 // message type
[371ce29]169 cout << "msg: " << serverMsg.buffer << endl;
[b128109]170 cout << "broadcastResponse: " << broadcastResponse << endl;
[da692b9]171 if (broadcastResponse)
[3b1efcc]172 {
[da692b9]173 cout << "Should be broadcasting the message" << endl;
174
[01d0d00]175 map<unsigned int, Player>::iterator it;
176 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]177 {
[d211210]178 cout << "Sent message back to " << it->second.name << endl;
[01d0d00]179 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
[3b1efcc]180 error("sendMessage");
181 }
182 }
183 else
184 {
[da692b9]185 cout << "Should be sending back the message" << endl;
186
[3b1efcc]187 if ( sendMessage(&serverMsg, sock, &from) < 0 )
188 error("sendMessage");
189 }
[7b43385]190 }
[8e540f4]191
[d211210]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
[7b43385]196 // update player positions
[d211210]197 /*
[7b43385]198 map<unsigned int, Player>::iterator it;
199 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
200 {
201 it->second.move();
[edfd1d0]202 }
[7b43385]203
204 broadcastPlayerPositions(mapPlayers, sock);
[d211210]205 */
[8e540f4]206 }
[371ce29]207
[8e540f4]208 return 0;
209}
210
[d211210]211bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock)
[8e540f4]212{
[41ad8ed]213 DataAccess da;
214
[8e540f4]215 cout << "MSG: type: " << clientMsg.type << endl;
216 cout << "MSG contents: " << clientMsg.buffer << endl;
217
[da692b9]218 // maybe we should make a message class and have this be a member
[3b1efcc]219 bool broadcastResponse = false;
220
[8e540f4]221 // Check that if an invalid message is sent, the client will correctly
222 // receive and display the response. Maybe make a special error msg type
223 switch(clientMsg.type)
224 {
225 case MSG_TYPE_REGISTER:
[d2b411a]226 {
[8e540f4]227 string username(clientMsg.buffer);
228 string password(strchr(clientMsg.buffer, '\0')+1);
[d2b411a]229
[8e540f4]230 cout << "username: " << username << endl;
231 cout << "password: " << password << endl;
[d2b411a]232
[3b1efcc]233 int error = da.insertPlayer(username, password);
[41ad8ed]234
[3b1efcc]235 if (!error)
236 strcpy(serverMsg.buffer, "Registration successful.");
237 else
238 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[8e540f4]239
240 serverMsg.type = MSG_TYPE_REGISTER;
[d2b411a]241
[8e540f4]242 break;
243 }
244 case MSG_TYPE_LOGIN:
245 {
[60017fc]246 cout << "Got login message" << endl;
247
[d211210]248 serverMsg.type = MSG_TYPE_LOGIN;
249
[8e540f4]250 string username(clientMsg.buffer);
[41ad8ed]251 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]252
[41ad8ed]253 Player* p = da.getPlayer(username);
[d2b411a]254
[b128109]255 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]256 {
257 strcpy(serverMsg.buffer, "Incorrect username or password");
258 }
[01d0d00]259 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]260 {
261 strcpy(serverMsg.buffer, "Player has already logged in.");
262 }
263 else
[8e540f4]264 {
[d211210]265 serverMsg.type = MSG_TYPE_PLAYER;
266
[01d0d00]267 p->setAddr(from);
[1106210]268 updateUnusedId(unusedId, mapPlayers);
[01d0d00]269 p->id = unusedId;
[d211210]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 }
[59061f6]285
[594d2e9]286 p->serialize(serverMsg.buffer);
[d211210]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;
297 mapPlayers[unusedId] = *p;
[07028b9]298 }
299
[41ad8ed]300 delete(p);
[07028b9]301
[8e540f4]302 break;
303 }
304 case MSG_TYPE_LOGOUT:
305 {
306 string name(clientMsg.buffer);
307 cout << "Player logging out: " << name << endl;
308
[01d0d00]309 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]310
[8e540f4]311 if (p == NULL)
312 {
313 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]314 cout << "Player not logged in" << endl;
[07028b9]315 }
[01d0d00]316 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
317 p->addr.sin_port != from.sin_port )
[07028b9]318 {
[8e540f4]319 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.");
[8a3ef42]320 cout << "Player logged in using a different connection" << endl;
[2488852]321 }
[8e540f4]322 else
[2488852]323 {
[01d0d00]324 if (p->id < unusedId)
325 unusedId = p->id;
326 mapPlayers.erase(p->id);
[41ad8ed]327 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8a3ef42]328 cout << "Player logged out successfuly" << endl;
[8e540f4]329 }
[07028b9]330
[d211210]331 serverMsg.type = MSG_TYPE_LOGOUT;
[8a3ef42]332
[8e540f4]333 break;
334 }
335 case MSG_TYPE_CHAT:
336 {
[da692b9]337 cout << "Got a chat message" << endl;
338
[01d0d00]339 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]340
[8e540f4]341 if (p == NULL)
342 {
343 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
[2488852]344 }
[8e540f4]345 else
346 {
[3b1efcc]347 broadcastResponse = true;
348
[b128109]349 ostringstream oss;
350 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]351
[b128109]352 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]353 }
354
355 serverMsg.type = MSG_TYPE_CHAT;
356
357 break;
[e084950]358 }
[b128109]359 case MSG_TYPE_PLAYER_MOVE:
360 {
361 istringstream iss;
362 iss.str(clientMsg.buffer);
363
364 cout << "PLAYER_MOVE" << endl;
365
366 int id, x, y;
367
368 memcpy(&id, clientMsg.buffer, 4);
369 memcpy(&x, clientMsg.buffer+4, 4);
370 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]371
[b128109]372 cout << "x: " << x << endl;
373 cout << "y: " << y << endl;
374 cout << "id: " << id << endl;
[7b43385]375
[b128109]376 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
377 mapPlayers[id].addr.sin_port == from.sin_port )
378 {
[60017fc]379 // we need to make sure the player can move here
380 if (0 <= x && x < 300 && 0 <= y && y < 300 &&
381 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
382 {
[7b43385]383 cout << "valid terrain" << endl;
384
[d211210]385 //cout << "orig x: " << mapPlayers[id].pos.x << endl;
386 //cout << "orig y: " << mapPlayers[id].pos.y << endl;
[7b43385]387 // first we get the correct vector
[60017fc]388 mapPlayers[id].target.x = x;
389 mapPlayers[id].target.y = y;
390 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
391 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
[d211210]392 //cout << "xDiff: " << xDiff << endl;
393 //cout << "yDiff: " << yDiff << endl;
[60017fc]394
395 // then we get the correct angle
396 double angle = atan2(yDiff, xDiff);
[7b43385]397 cout << "angle: " << angle << endl;
[60017fc]398
399 // finally we use the angle to determine
400 // how much the player moves
401 // the player will move 50 pixels in the correct direction
[7b43385]402 //mapPlayers[id].pos.x += cos(angle)*50;
403 //mapPlayers[id].pos.y += sin(angle)*50;
[60017fc]404
405 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
406
407 memcpy(serverMsg.buffer, &id, 4);
[d211210]408 memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
409 memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
[60017fc]410 //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
411
412 broadcastResponse = true;
413 }
414 else
415 cout << "Bad terrain detected" << endl;
[b128109]416 }
417 else // nned to send back a message indicating failure
418 cout << "Player id (" << id << ") doesn't match sender" << endl;
419
420 break;
421 }
[8e540f4]422 default:
423 {
424 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]425
[8e540f4]426 serverMsg.type = MSG_TYPE_CHAT;
[e084950]427
[8e540f4]428 break;
429 }
[e3535b3]430 }
[da692b9]431
[60017fc]432 cout << "Got to the end of the switch" << endl;
433
[da692b9]434 return broadcastResponse;
[e3535b3]435}
[da692b9]436
[1106210]437void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]438{
[1106210]439 while (mapPlayers.find(id) != mapPlayers.end())
440 id++;
[01d0d00]441}
Note: See TracBrowser for help on using the repository browser.