[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] | 35 | using namespace std;
|
---|
| 36 |
|
---|
[60017fc] | 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);
|
---|
[01d0d00] | 38 |
|
---|
[1106210] | 39 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
---|
[8e540f4] | 40 |
|
---|
[73f75c1] | 41 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 42 | void error(const char *msg)
|
---|
| 43 | {
|
---|
| 44 | perror(msg);
|
---|
| 45 | exit(0);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[01d0d00] | 48 | Player *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] | 61 | Player *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] | 75 | void 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] | 94 | int 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 |
|
---|
[66906aa] | 126 | /*
|
---|
[60017fc] | 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 |
|
---|
[f401cac] | 135 | cout << "Loc before: (" << testP.pos.x << ", " << testP.pos.y << ")" << endl;
|
---|
| 136 |
|
---|
[60017fc] | 137 | testP.move();
|
---|
| 138 |
|
---|
[f401cac] | 139 | cout << "Loc after: (" << testP.pos.x << ", " << testP.pos.y << ")" << endl;
|
---|
[66906aa] | 140 | */
|
---|
[f401cac] | 141 |
|
---|
[da692b9] | 142 | bool broadcastResponse;
|
---|
[cb1f288] | 143 | while (true) {
|
---|
[371ce29] | 144 |
|
---|
| 145 | usleep(5000);
|
---|
| 146 |
|
---|
[e084950] | 147 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 148 |
|
---|
[371ce29] | 149 | if (n >= 0) {
|
---|
| 150 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 151 |
|
---|
[60017fc] | 152 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
|
---|
[371ce29] | 153 |
|
---|
[b128109] | 154 | // probably replace this with a function that prints based on the
|
---|
| 155 | // message type
|
---|
[371ce29] | 156 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
[b128109] | 157 | cout << "broadcastResponse: " << broadcastResponse << endl;
|
---|
[da692b9] | 158 | if (broadcastResponse)
|
---|
[3b1efcc] | 159 | {
|
---|
[da692b9] | 160 | cout << "Should be broadcasting the message" << endl;
|
---|
| 161 |
|
---|
[01d0d00] | 162 | map<unsigned int, Player>::iterator it;
|
---|
| 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 |
|
---|
[66906aa] | 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 |
|
---|
[01d0d00] | 184 | broadcastPlayerPositions(mapPlayers, sock);
|
---|
[edfd1d0] | 185 | }
|
---|
[8e540f4] | 186 | }
|
---|
[371ce29] | 187 |
|
---|
[8e540f4] | 188 | return 0;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[60017fc] | 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)
|
---|
[8e540f4] | 192 | {
|
---|
[41ad8ed] | 193 | DataAccess da;
|
---|
| 194 |
|
---|
[8e540f4] | 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 |
|
---|
[da692b9] | 200 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 201 | bool broadcastResponse = false;
|
---|
| 202 |
|
---|
[8e540f4] | 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:
|
---|
[d2b411a] | 208 | {
|
---|
[8e540f4] | 209 | string username(clientMsg.buffer);
|
---|
| 210 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 211 |
|
---|
[8e540f4] | 212 | cout << "username: " << username << endl;
|
---|
| 213 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 214 |
|
---|
[3b1efcc] | 215 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 216 |
|
---|
[3b1efcc] | 217 | if (!error)
|
---|
| 218 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 219 | else
|
---|
| 220 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 221 |
|
---|
| 222 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 223 |
|
---|
[8e540f4] | 224 | break;
|
---|
| 225 | }
|
---|
| 226 | case MSG_TYPE_LOGIN:
|
---|
| 227 | {
|
---|
[60017fc] | 228 | cout << "Got login message" << endl;
|
---|
| 229 |
|
---|
[8e540f4] | 230 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 231 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 232 |
|
---|
[41ad8ed] | 233 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 234 |
|
---|
[b128109] | 235 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 236 | {
|
---|
| 237 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 238 | }
|
---|
[01d0d00] | 239 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 240 | {
|
---|
| 241 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
[8e540f4] | 244 | {
|
---|
[01d0d00] | 245 | p->setAddr(from);
|
---|
[1106210] | 246 | updateUnusedId(unusedId, mapPlayers);
|
---|
[01d0d00] | 247 | p->id = unusedId;
|
---|
| 248 | mapPlayers[unusedId] = *p;
|
---|
[59061f6] | 249 |
|
---|
[594d2e9] | 250 | // sendd back the new player info to the user
|
---|
| 251 | p->serialize(serverMsg.buffer);
|
---|
[07028b9] | 252 | }
|
---|
| 253 |
|
---|
[8e540f4] | 254 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 255 |
|
---|
| 256 | delete(p);
|
---|
[07028b9] | 257 |
|
---|
[8e540f4] | 258 | break;
|
---|
| 259 | }
|
---|
| 260 | case MSG_TYPE_LOGOUT:
|
---|
| 261 | {
|
---|
| 262 | string name(clientMsg.buffer);
|
---|
| 263 | cout << "Player logging out: " << name << endl;
|
---|
| 264 |
|
---|
[01d0d00] | 265 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 266 |
|
---|
[8e540f4] | 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.");
|
---|
[8a3ef42] | 270 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 271 | }
|
---|
[01d0d00] | 272 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 273 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 274 | {
|
---|
[8e540f4] | 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.");
|
---|
[8a3ef42] | 276 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 277 | }
|
---|
[8e540f4] | 278 | else
|
---|
[2488852] | 279 | {
|
---|
[01d0d00] | 280 | if (p->id < unusedId)
|
---|
| 281 | unusedId = p->id;
|
---|
| 282 | mapPlayers.erase(p->id);
|
---|
[41ad8ed] | 283 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8a3ef42] | 284 | cout << "Player logged out successfuly" << endl;
|
---|
[8e540f4] | 285 | }
|
---|
[07028b9] | 286 |
|
---|
[8a3ef42] | 287 | // should really be serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
| 288 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 289 |
|
---|
[8e540f4] | 290 | break;
|
---|
| 291 | }
|
---|
| 292 | case MSG_TYPE_CHAT:
|
---|
| 293 | {
|
---|
[da692b9] | 294 | cout << "Got a chat message" << endl;
|
---|
| 295 |
|
---|
[01d0d00] | 296 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 297 |
|
---|
[8e540f4] | 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.");
|
---|
[2488852] | 301 | }
|
---|
[8e540f4] | 302 | else
|
---|
| 303 | {
|
---|
[3b1efcc] | 304 | broadcastResponse = true;
|
---|
| 305 |
|
---|
[b128109] | 306 | ostringstream oss;
|
---|
| 307 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 308 |
|
---|
[b128109] | 309 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 310 | }
|
---|
| 311 |
|
---|
| 312 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 313 |
|
---|
| 314 | break;
|
---|
[e084950] | 315 | }
|
---|
[b128109] | 316 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 317 | {
|
---|
[60017fc] | 318 | cout << "Got a move message" << endl;
|
---|
| 319 |
|
---|
[b128109] | 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 | {
|
---|
[60017fc] | 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;
|
---|
[b128109] | 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 | }
|
---|
[8e540f4] | 379 | default:
|
---|
| 380 | {
|
---|
| 381 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 382 |
|
---|
[8e540f4] | 383 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 384 |
|
---|
[8e540f4] | 385 | break;
|
---|
| 386 | }
|
---|
[e3535b3] | 387 | }
|
---|
[da692b9] | 388 |
|
---|
[60017fc] | 389 | cout << "Got to the end of the switch" << endl;
|
---|
| 390 |
|
---|
[da692b9] | 391 | return broadcastResponse;
|
---|
[e3535b3] | 392 | }
|
---|
[da692b9] | 393 |
|
---|
[1106210] | 394 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
[01d0d00] | 395 | {
|
---|
[1106210] | 396 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 397 | id++;
|
---|
[01d0d00] | 398 | }
|
---|