source: network-game/server/server.cpp@ f401cac

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

Fixed some bugs in the player movement code

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