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