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