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