[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>
|
---|
[371ce29] | 9 |
|
---|
[01d0d00] | 10 | #include <vector>
|
---|
| 11 | #include <map>
|
---|
| 12 |
|
---|
[73f75c1] | 13 | #include <sys/socket.h>
|
---|
[371ce29] | 14 | #include <netdb.h>
|
---|
[73f75c1] | 15 | #include <netinet/in.h>
|
---|
| 16 | #include <arpa/inet.h>
|
---|
| 17 |
|
---|
[b128109] | 18 | #include <crypt.h>
|
---|
| 19 |
|
---|
[edfd1d0] | 20 | /*
|
---|
[e3535b3] | 21 | #include <openssl/bio.h>
|
---|
| 22 | #include <openssl/ssl.h>
|
---|
| 23 | #include <openssl/err.h>
|
---|
[edfd1d0] | 24 | */
|
---|
[e3535b3] | 25 |
|
---|
[b53c6b3] | 26 | #include "../common/Compiler.h"
|
---|
[3b1efcc] | 27 | #include "../common/Common.h"
|
---|
[edfd1d0] | 28 | #include "../common/Message.h"
|
---|
[60017fc] | 29 | #include "../common/WorldMap.h"
|
---|
[edfd1d0] | 30 | #include "../common/Player.h"
|
---|
[b53c6b3] | 31 |
|
---|
[36082e8] | 32 | #include "DataAccess.h"
|
---|
[d2b411a] | 33 |
|
---|
[e3535b3] | 34 | using namespace std;
|
---|
| 35 |
|
---|
[60017fc] | 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);
|
---|
[01d0d00] | 37 |
|
---|
[1106210] | 38 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
---|
[8e540f4] | 39 |
|
---|
[73f75c1] | 40 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 41 | void error(const char *msg)
|
---|
| 42 | {
|
---|
| 43 | perror(msg);
|
---|
| 44 | exit(0);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[01d0d00] | 47 | Player *findPlayerByName(map<unsigned int, Player> &m, string name)
|
---|
[2488852] | 48 | {
|
---|
[01d0d00] | 49 | map<unsigned int, Player>::iterator it;
|
---|
[2488852] | 50 |
|
---|
[01d0d00] | 51 | for (it = m.begin(); it != m.end(); it++)
|
---|
[2488852] | 52 | {
|
---|
[01d0d00] | 53 | if ( it->second.name.compare(name) == 0 )
|
---|
| 54 | return &(it->second);
|
---|
[2488852] | 55 | }
|
---|
| 56 |
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[01d0d00] | 60 | Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
|
---|
[73f75c1] | 61 | {
|
---|
[01d0d00] | 62 | map<unsigned int, Player>::iterator it;
|
---|
[73f75c1] | 63 |
|
---|
[01d0d00] | 64 | for (it = m.begin(); it != m.end(); it++)
|
---|
[73f75c1] | 65 | {
|
---|
[01d0d00] | 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);
|
---|
[73f75c1] | 69 | }
|
---|
| 70 |
|
---|
| 71 | return NULL;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[01d0d00] | 74 | void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock)
|
---|
[edfd1d0] | 75 | {
|
---|
[01d0d00] | 76 | map<unsigned int, Player>::iterator it, it2;
|
---|
[edfd1d0] | 77 | NETWORK_MSG serverMsg;
|
---|
| 78 |
|
---|
| 79 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 80 |
|
---|
[01d0d00] | 81 | for (it = m.begin(); it != m.end(); it++)
|
---|
[edfd1d0] | 82 | {
|
---|
[01d0d00] | 83 | it->second.serialize(serverMsg.buffer);
|
---|
[edfd1d0] | 84 |
|
---|
[01d0d00] | 85 | for (it2 = m.begin(); it2 != m.end(); it2++)
|
---|
[edfd1d0] | 86 | {
|
---|
[01d0d00] | 87 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
[edfd1d0] | 88 | error("sendMessage");
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[e3535b3] | 93 | int main(int argc, char *argv[])
|
---|
| 94 | {
|
---|
| 95 | int sock, length, n;
|
---|
| 96 | struct sockaddr_in server;
|
---|
[3b1efcc] | 97 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 98 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[01d0d00] | 99 | map<unsigned int, Player> mapPlayers;
|
---|
[1106210] | 100 | unsigned int unusedId = 1;
|
---|
[e084950] | 101 |
|
---|
[edfd1d0] | 102 | //SSL_load_error_strings();
|
---|
| 103 | //ERR_load_BIO_strings();
|
---|
| 104 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 105 |
|
---|
| 106 | if (argc < 2) {
|
---|
[73f75c1] | 107 | cerr << "ERROR, no port provided" << endl;
|
---|
| 108 | exit(1);
|
---|
[e3535b3] | 109 | }
|
---|
[60017fc] | 110 |
|
---|
[f401cac] | 111 | WorldMap* gameMap = NULL; //WorldMap::createDefaultMap();
|
---|
[41ad8ed] | 112 |
|
---|
[371ce29] | 113 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[e3535b3] | 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]));
|
---|
[2488852] | 119 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 120 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 121 | error("binding");
|
---|
[73f75c1] | 122 |
|
---|
[371ce29] | 123 | set_nonblock(sock);
|
---|
| 124 |
|
---|
[da692b9] | 125 | bool broadcastResponse;
|
---|
[cb1f288] | 126 | while (true) {
|
---|
[371ce29] | 127 |
|
---|
| 128 | usleep(5000);
|
---|
| 129 |
|
---|
[e084950] | 130 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 131 |
|
---|
[371ce29] | 132 | if (n >= 0) {
|
---|
| 133 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 134 |
|
---|
[60017fc] | 135 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
|
---|
[371ce29] | 136 |
|
---|
[b128109] | 137 | // probably replace this with a function that prints based on the
|
---|
| 138 | // message type
|
---|
[371ce29] | 139 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
[b128109] | 140 | cout << "broadcastResponse: " << broadcastResponse << endl;
|
---|
[da692b9] | 141 | if (broadcastResponse)
|
---|
[3b1efcc] | 142 | {
|
---|
[da692b9] | 143 | cout << "Should be broadcasting the message" << endl;
|
---|
| 144 |
|
---|
[01d0d00] | 145 | map<unsigned int, Player>::iterator it;
|
---|
| 146 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
[3b1efcc] | 147 | {
|
---|
[01d0d00] | 148 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[3b1efcc] | 149 | error("sendMessage");
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | else
|
---|
| 153 | {
|
---|
[da692b9] | 154 | cout << "Should be sending back the message" << endl;
|
---|
| 155 |
|
---|
[3b1efcc] | 156 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 157 | error("sendMessage");
|
---|
| 158 | }
|
---|
[8e540f4] | 159 |
|
---|
[66906aa] | 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 |
|
---|
[01d0d00] | 167 | broadcastPlayerPositions(mapPlayers, sock);
|
---|
[edfd1d0] | 168 | }
|
---|
[8e540f4] | 169 | }
|
---|
[371ce29] | 170 |
|
---|
[8e540f4] | 171 | return 0;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[60017fc] | 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)
|
---|
[8e540f4] | 175 | {
|
---|
[41ad8ed] | 176 | DataAccess da;
|
---|
| 177 |
|
---|
[8e540f4] | 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 |
|
---|
[da692b9] | 183 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 184 | bool broadcastResponse = false;
|
---|
| 185 |
|
---|
[8e540f4] | 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:
|
---|
[d2b411a] | 191 | {
|
---|
[8e540f4] | 192 | string username(clientMsg.buffer);
|
---|
| 193 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 194 |
|
---|
[8e540f4] | 195 | cout << "username: " << username << endl;
|
---|
| 196 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 197 |
|
---|
[3b1efcc] | 198 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 199 |
|
---|
[3b1efcc] | 200 | if (!error)
|
---|
| 201 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 202 | else
|
---|
| 203 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 204 |
|
---|
| 205 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 206 |
|
---|
[8e540f4] | 207 | break;
|
---|
| 208 | }
|
---|
| 209 | case MSG_TYPE_LOGIN:
|
---|
| 210 | {
|
---|
[60017fc] | 211 | cout << "Got login message" << endl;
|
---|
| 212 |
|
---|
[8e540f4] | 213 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 214 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 215 |
|
---|
[41ad8ed] | 216 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 217 |
|
---|
[b128109] | 218 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 219 | {
|
---|
| 220 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 221 | }
|
---|
[01d0d00] | 222 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 223 | {
|
---|
| 224 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 225 | }
|
---|
| 226 | else
|
---|
[8e540f4] | 227 | {
|
---|
[01d0d00] | 228 | p->setAddr(from);
|
---|
[1106210] | 229 | updateUnusedId(unusedId, mapPlayers);
|
---|
[01d0d00] | 230 | p->id = unusedId;
|
---|
| 231 | mapPlayers[unusedId] = *p;
|
---|
[59061f6] | 232 |
|
---|
[594d2e9] | 233 | // sendd back the new player info to the user
|
---|
| 234 | p->serialize(serverMsg.buffer);
|
---|
[07028b9] | 235 | }
|
---|
| 236 |
|
---|
[8e540f4] | 237 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[41ad8ed] | 238 |
|
---|
| 239 | delete(p);
|
---|
[07028b9] | 240 |
|
---|
[8e540f4] | 241 | break;
|
---|
| 242 | }
|
---|
| 243 | case MSG_TYPE_LOGOUT:
|
---|
| 244 | {
|
---|
| 245 | string name(clientMsg.buffer);
|
---|
| 246 | cout << "Player logging out: " << name << endl;
|
---|
| 247 |
|
---|
[01d0d00] | 248 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 249 |
|
---|
[8e540f4] | 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.");
|
---|
[8a3ef42] | 253 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 254 | }
|
---|
[01d0d00] | 255 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 256 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 257 | {
|
---|
[8e540f4] | 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.");
|
---|
[8a3ef42] | 259 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 260 | }
|
---|
[8e540f4] | 261 | else
|
---|
[2488852] | 262 | {
|
---|
[01d0d00] | 263 | if (p->id < unusedId)
|
---|
| 264 | unusedId = p->id;
|
---|
| 265 | mapPlayers.erase(p->id);
|
---|
[41ad8ed] | 266 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8a3ef42] | 267 | cout << "Player logged out successfuly" << endl;
|
---|
[8e540f4] | 268 | }
|
---|
[07028b9] | 269 |
|
---|
[8a3ef42] | 270 | // should really be serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
| 271 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 272 |
|
---|
[8e540f4] | 273 | break;
|
---|
| 274 | }
|
---|
| 275 | case MSG_TYPE_CHAT:
|
---|
| 276 | {
|
---|
[da692b9] | 277 | cout << "Got a chat message" << endl;
|
---|
| 278 |
|
---|
[01d0d00] | 279 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 280 |
|
---|
[8e540f4] | 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.");
|
---|
[2488852] | 284 | }
|
---|
[8e540f4] | 285 | else
|
---|
| 286 | {
|
---|
[3b1efcc] | 287 | broadcastResponse = true;
|
---|
| 288 |
|
---|
[b128109] | 289 | ostringstream oss;
|
---|
| 290 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 291 |
|
---|
[b128109] | 292 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 293 | }
|
---|
| 294 |
|
---|
| 295 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 296 |
|
---|
| 297 | break;
|
---|
[e084950] | 298 | }
|
---|
[b128109] | 299 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 300 | {
|
---|
[60017fc] | 301 | cout << "Got a move message" << endl;
|
---|
| 302 |
|
---|
[b128109] | 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 | {
|
---|
[60017fc] | 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;
|
---|
[b128109] | 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 | }
|
---|
[8e540f4] | 362 | default:
|
---|
| 363 | {
|
---|
| 364 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 365 |
|
---|
[8e540f4] | 366 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 367 |
|
---|
[8e540f4] | 368 | break;
|
---|
| 369 | }
|
---|
[e3535b3] | 370 | }
|
---|
[da692b9] | 371 |
|
---|
[60017fc] | 372 | cout << "Got to the end of the switch" << endl;
|
---|
| 373 |
|
---|
[da692b9] | 374 | return broadcastResponse;
|
---|
[e3535b3] | 375 | }
|
---|
[da692b9] | 376 |
|
---|
[1106210] | 377 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
[01d0d00] | 378 | {
|
---|
[1106210] | 379 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 380 | id++;
|
---|
[01d0d00] | 381 | }
|
---|