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