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

Last change on this file since cc1c6c1 was b26229c, checked in by dportnoy <dmp1488@…>, 11 years ago

Some more map debugging

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