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