[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 |
|
---|
| 112 | WorldMap* gameMap = 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 |
|
---|
| 134 | testP.move();
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
[da692b9] | 137 | bool broadcastResponse;
|
---|
[cb1f288] | 138 | while (true) {
|
---|
[371ce29] | 139 |
|
---|
| 140 | usleep(5000);
|
---|
| 141 |
|
---|
[e084950] | 142 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 143 |
|
---|
[371ce29] | 144 | if (n >= 0) {
|
---|
| 145 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 146 |
|
---|
[60017fc] | 147 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
|
---|
[371ce29] | 148 |
|
---|
[b128109] | 149 | // probably replace this with a function that prints based on the
|
---|
| 150 | // message type
|
---|
[371ce29] | 151 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
[b128109] | 152 | cout << "broadcastResponse: " << broadcastResponse << endl;
|
---|
[da692b9] | 153 | if (broadcastResponse)
|
---|
[3b1efcc] | 154 | {
|
---|
[da692b9] | 155 | cout << "Should be broadcasting the message" << endl;
|
---|
| 156 |
|
---|
[01d0d00] | 157 | map<unsigned int, Player>::iterator it;
|
---|
[3b1efcc] | 158 |
|
---|
[01d0d00] | 159 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
[3b1efcc] | 160 | {
|
---|
[01d0d00] | 161 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[3b1efcc] | 162 | error("sendMessage");
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
[da692b9] | 167 | cout << "Should be sending back the message" << endl;
|
---|
| 168 |
|
---|
[3b1efcc] | 169 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 170 | error("sendMessage");
|
---|
| 171 | }
|
---|
[8e540f4] | 172 |
|
---|
[01d0d00] | 173 | broadcastPlayerPositions(mapPlayers, sock);
|
---|
[edfd1d0] | 174 | }
|
---|
[8e540f4] | 175 | }
|
---|
[60017fc] | 176 | */
|
---|
[371ce29] | 177 |
|
---|
[8e540f4] | 178 | return 0;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[60017fc] | 181 | 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] | 182 | {
|
---|
[41ad8ed] | 183 | DataAccess da;
|
---|
| 184 |
|
---|
[8e540f4] | 185 | cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
|
---|
| 186 | cout << "port: " << from.sin_port << endl;
|
---|
| 187 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 188 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 189 |
|
---|
[da692b9] | 190 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 191 | bool broadcastResponse = false;
|
---|
| 192 |
|
---|
[8e540f4] | 193 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 194 | // receive and display the response. Maybe make a special error msg type
|
---|
| 195 | switch(clientMsg.type)
|
---|
| 196 | {
|
---|
| 197 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 198 | {
|
---|
[8e540f4] | 199 | string username(clientMsg.buffer);
|
---|
| 200 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 201 |
|
---|
[8e540f4] | 202 | cout << "username: " << username << endl;
|
---|
| 203 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 204 |
|
---|
[3b1efcc] | 205 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 206 |
|
---|
[3b1efcc] | 207 | if (!error)
|
---|
| 208 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 209 | else
|
---|
| 210 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 211 |
|
---|
| 212 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 213 |
|
---|
[8e540f4] | 214 | break;
|
---|
| 215 | }
|
---|
| 216 | case MSG_TYPE_LOGIN:
|
---|
| 217 | {
|
---|
[60017fc] | 218 | cout << "Got login message" << endl;
|
---|
| 219 |
|
---|
[8e540f4] | 220 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 221 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 222 |
|
---|
[41ad8ed] | 223 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 224 |
|
---|
[b128109] | 225 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 226 | {
|
---|
| 227 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 228 | }
|
---|
[01d0d00] | 229 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 230 | {
|
---|
| 231 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 232 | }
|
---|
| 233 | else
|
---|
[8e540f4] | 234 | {
|
---|
[01d0d00] | 235 | p->setAddr(from);
|
---|
[1106210] | 236 | updateUnusedId(unusedId, mapPlayers);
|
---|
[01d0d00] | 237 | p->id = unusedId;
|
---|
| 238 | mapPlayers[unusedId] = *p;
|
---|
[59061f6] | 239 |
|
---|
[594d2e9] | 240 | // sendd back the new player info to the user
|
---|
| 241 | p->serialize(serverMsg.buffer);
|
---|
[07028b9] | 242 | }
|
---|
| 243 |
|
---|
[8e540f4] | 244 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 245 |
|
---|
| 246 | delete(p);
|
---|
[07028b9] | 247 |
|
---|
[8e540f4] | 248 | break;
|
---|
| 249 | }
|
---|
| 250 | case MSG_TYPE_LOGOUT:
|
---|
| 251 | {
|
---|
| 252 | string name(clientMsg.buffer);
|
---|
| 253 | cout << "Player logging out: " << name << endl;
|
---|
| 254 |
|
---|
[01d0d00] | 255 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 256 |
|
---|
[8e540f4] | 257 | if (p == NULL)
|
---|
| 258 | {
|
---|
| 259 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 260 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 261 | }
|
---|
[01d0d00] | 262 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 263 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 264 | {
|
---|
[8e540f4] | 265 | 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] | 266 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 267 | }
|
---|
[8e540f4] | 268 | else
|
---|
[2488852] | 269 | {
|
---|
[01d0d00] | 270 | if (p->id < unusedId)
|
---|
| 271 | unusedId = p->id;
|
---|
| 272 | mapPlayers.erase(p->id);
|
---|
[41ad8ed] | 273 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8a3ef42] | 274 | cout << "Player logged out successfuly" << endl;
|
---|
[8e540f4] | 275 | }
|
---|
[07028b9] | 276 |
|
---|
[8a3ef42] | 277 | // should really be serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
| 278 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 279 |
|
---|
[8e540f4] | 280 | break;
|
---|
| 281 | }
|
---|
| 282 | case MSG_TYPE_CHAT:
|
---|
| 283 | {
|
---|
[da692b9] | 284 | cout << "Got a chat message" << endl;
|
---|
| 285 |
|
---|
[01d0d00] | 286 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 287 |
|
---|
[8e540f4] | 288 | if (p == NULL)
|
---|
| 289 | {
|
---|
| 290 | 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] | 291 | }
|
---|
[8e540f4] | 292 | else
|
---|
| 293 | {
|
---|
[3b1efcc] | 294 | broadcastResponse = true;
|
---|
| 295 |
|
---|
[b128109] | 296 | ostringstream oss;
|
---|
| 297 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 298 |
|
---|
[b128109] | 299 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 300 | }
|
---|
| 301 |
|
---|
| 302 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 303 |
|
---|
| 304 | break;
|
---|
[e084950] | 305 | }
|
---|
[b128109] | 306 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 307 | {
|
---|
[60017fc] | 308 | cout << "Got a move message" << endl;
|
---|
| 309 |
|
---|
[b128109] | 310 | istringstream iss;
|
---|
| 311 | iss.str(clientMsg.buffer);
|
---|
| 312 |
|
---|
| 313 | cout << "PLAYER_MOVE" << endl;
|
---|
| 314 |
|
---|
| 315 | int id, x, y;
|
---|
| 316 |
|
---|
| 317 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 318 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
| 319 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
| 320 |
|
---|
| 321 | cout << "x: " << x << endl;
|
---|
| 322 | cout << "y: " << y << endl;
|
---|
| 323 | cout << "id: " << id << endl;
|
---|
| 324 |
|
---|
| 325 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
| 326 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
| 327 | {
|
---|
[60017fc] | 328 | // we need to make sure the player can move here
|
---|
| 329 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
| 330 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
| 331 | {
|
---|
| 332 | // first we get the correct vector
|
---|
| 333 | mapPlayers[id].target.x = x;
|
---|
| 334 | mapPlayers[id].target.y = y;
|
---|
| 335 | int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
|
---|
| 336 | int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
|
---|
| 337 | cout << "xDiff: " << xDiff << endl;
|
---|
| 338 | cout << "yDiff: " << yDiff << endl;
|
---|
| 339 |
|
---|
| 340 | // then we get the correct angle
|
---|
| 341 | double angle = atan2(yDiff, xDiff);
|
---|
| 342 | cout << "angle: " << angle << endl;
|
---|
| 343 |
|
---|
| 344 | // finally we use the angle to determine
|
---|
| 345 | // how much the player moves
|
---|
| 346 | // the player will move 50 pixels in the correct direction
|
---|
| 347 | mapPlayers[id].pos.x += cos(angle)*50;
|
---|
| 348 | mapPlayers[id].pos.y += sin(angle)*50;
|
---|
| 349 | cout << "new x: " << mapPlayers[id].pos.x << endl;
|
---|
| 350 | cout << "new y: " << mapPlayers[id].pos.y << endl;
|
---|
| 351 |
|
---|
| 352 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 353 |
|
---|
| 354 | memcpy(serverMsg.buffer, &id, 4);
|
---|
| 355 | memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
|
---|
| 356 | memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
|
---|
| 357 | //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
|
---|
| 358 |
|
---|
| 359 | broadcastResponse = true;
|
---|
| 360 | }
|
---|
| 361 | else
|
---|
| 362 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 363 | }
|
---|
| 364 | else // nned to send back a message indicating failure
|
---|
| 365 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
| 366 |
|
---|
| 367 | break;
|
---|
| 368 | }
|
---|
[8e540f4] | 369 | default:
|
---|
| 370 | {
|
---|
| 371 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 372 |
|
---|
[8e540f4] | 373 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 374 |
|
---|
[8e540f4] | 375 | break;
|
---|
| 376 | }
|
---|
[e3535b3] | 377 | }
|
---|
[da692b9] | 378 |
|
---|
[60017fc] | 379 | cout << "Got to the end of the switch" << endl;
|
---|
| 380 |
|
---|
[da692b9] | 381 | return broadcastResponse;
|
---|
[e3535b3] | 382 | }
|
---|
[da692b9] | 383 |
|
---|
[1106210] | 384 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
[01d0d00] | 385 | {
|
---|
[1106210] | 386 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 387 | id++;
|
---|
[01d0d00] | 388 | }
|
---|