[e084950] | 1 | #include "../common/compiler.h"
|
---|
| 2 |
|
---|
[2488852] | 3 | #include <cstdlib>
|
---|
[371ce29] | 4 | #include <cstdio>
|
---|
[e3535b3] | 5 | #include <unistd.h>
|
---|
[2488852] | 6 | #include <string>
|
---|
[e3535b3] | 7 | #include <iostream>
|
---|
[2488852] | 8 | #include <vector>
|
---|
| 9 | #include <algorithm>
|
---|
[e3535b3] | 10 |
|
---|
[371ce29] | 11 | #include <fcntl.h>
|
---|
| 12 | #include <assert.h>
|
---|
| 13 |
|
---|
[73f75c1] | 14 | #include <sys/socket.h>
|
---|
[371ce29] | 15 | #include <netdb.h>
|
---|
[73f75c1] | 16 | #include <netinet/in.h>
|
---|
| 17 | #include <arpa/inet.h>
|
---|
| 18 |
|
---|
[e3535b3] | 19 | #include <openssl/bio.h>
|
---|
| 20 | #include <openssl/ssl.h>
|
---|
| 21 | #include <openssl/err.h>
|
---|
| 22 |
|
---|
[8e540f4] | 23 | #include "Player.h"
|
---|
[36082e8] | 24 | #include "DataAccess.h"
|
---|
[2488852] | 25 | #include "../common/message.h"
|
---|
[d2b411a] | 26 |
|
---|
| 27 | /*
|
---|
| 28 | Protocol Design
|
---|
| 29 |
|
---|
| 30 | Client sends a login message
|
---|
| 31 | Server replies with client's position in the world and positions of
|
---|
| 32 | oall other logged in players
|
---|
| 33 | Merver sends player ids along with locations
|
---|
| 34 | This means a newly logged in client will need to know which id is
|
---|
| 35 | assigned to it
|
---|
| 36 | So server needs to send an id message, wait for an ack, and then send
|
---|
| 37 | the location messages
|
---|
| 38 | When a client shuts down, it sends a message to indicate this so the
|
---|
| 39 | server can remove it from the list of connected clients
|
---|
| 40 | Eventually, there'll need to be a way to detect timeouts from clients
|
---|
| 41 | (if they crashed or otherwise failed to send the logout message)
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[e3535b3] | 44 | using namespace std;
|
---|
| 45 |
|
---|
[8e540f4] | 46 | void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg);
|
---|
| 47 |
|
---|
[73f75c1] | 48 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 49 | void error(const char *msg)
|
---|
| 50 | {
|
---|
| 51 | perror(msg);
|
---|
| 52 | exit(0);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[8e540f4] | 55 | Player *findPlayerByName(vector<Player> &vec, string name)
|
---|
[2488852] | 56 | {
|
---|
[8e540f4] | 57 | vector<Player>::iterator it;
|
---|
[2488852] | 58 |
|
---|
| 59 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 60 | {
|
---|
| 61 | if ( it->name.compare(name) == 0 )
|
---|
| 62 | return &(*it);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return NULL;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[8e540f4] | 68 | Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
|
---|
[73f75c1] | 69 | {
|
---|
[8e540f4] | 70 | vector<Player>::iterator it;
|
---|
[73f75c1] | 71 |
|
---|
| 72 | for (it = vec.begin(); it != vec.end(); it++)
|
---|
| 73 | {
|
---|
| 74 | if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
| 75 | it->addr.sin_port == addr.sin_port )
|
---|
| 76 | return &(*it);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | return NULL;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[371ce29] | 82 | void set_nonblock(int sock)
|
---|
| 83 | {
|
---|
| 84 | int flags;
|
---|
| 85 | flags = fcntl(sock, F_GETFL,0);
|
---|
| 86 | assert(flags != -1);
|
---|
| 87 | fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[e3535b3] | 90 | int main(int argc, char *argv[])
|
---|
| 91 | {
|
---|
| 92 | int sock, length, n;
|
---|
| 93 | struct sockaddr_in server;
|
---|
[73f75c1] | 94 | struct sockaddr_in from; // holds the info on the connected client
|
---|
[e084950] | 95 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[8e540f4] | 96 | vector<Player> vctPlayers;
|
---|
[e084950] | 97 |
|
---|
| 98 | srand(time(NULL));
|
---|
[371ce29] | 99 | int num = 500;
|
---|
| 100 | //int num = (rand() % 0) + 1;
|
---|
[e084950] | 101 |
|
---|
| 102 | cout << "num: " << num << endl;
|
---|
[e3535b3] | 103 |
|
---|
| 104 | SSL_load_error_strings();
|
---|
| 105 | ERR_load_BIO_strings();
|
---|
| 106 | OpenSSL_add_all_algorithms();
|
---|
| 107 |
|
---|
| 108 | if (argc < 2) {
|
---|
[73f75c1] | 109 | cerr << "ERROR, no port provided" << endl;
|
---|
| 110 | exit(1);
|
---|
[e3535b3] | 111 | }
|
---|
[59061f6] | 112 |
|
---|
[371ce29] | 113 | /*
|
---|
[59061f6] | 114 | DataAccess da;
|
---|
| 115 |
|
---|
| 116 | da.printPlayers();
|
---|
| 117 |
|
---|
| 118 | da.insertPlayer("playerName3", "playerPass");
|
---|
| 119 | cout << endl << "Inserted player" << endl << endl;
|
---|
| 120 |
|
---|
| 121 | Player* p = da.getPlayer("playerName");
|
---|
| 122 | cout << "player name: " << p->name << endl;
|
---|
| 123 | delete(p);
|
---|
| 124 |
|
---|
| 125 | p = da.getPlayer("playerName3");
|
---|
| 126 | cout << "player name: " << p->name << endl;
|
---|
| 127 | delete(p);
|
---|
| 128 |
|
---|
| 129 | da.printPlayers();
|
---|
| 130 | cout << endl;
|
---|
[371ce29] | 131 | */
|
---|
[e3535b3] | 132 |
|
---|
[371ce29] | 133 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[e3535b3] | 134 | if (sock < 0) error("Opening socket");
|
---|
| 135 | length = sizeof(server);
|
---|
| 136 | bzero(&server,length);
|
---|
| 137 | server.sin_family=AF_INET;
|
---|
| 138 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 139 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 140 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 141 | error("binding");
|
---|
[73f75c1] | 142 |
|
---|
[371ce29] | 143 | set_nonblock(sock);
|
---|
| 144 |
|
---|
[cb1f288] | 145 | while (true) {
|
---|
[371ce29] | 146 |
|
---|
| 147 | usleep(5000);
|
---|
| 148 |
|
---|
[e084950] | 149 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 150 |
|
---|
[371ce29] | 151 | if (n >= 0) {
|
---|
| 152 | cout << "Got a message" << endl;
|
---|
[8e540f4] | 153 |
|
---|
[371ce29] | 154 | processMessage(clientMsg, from, vctPlayers, num, serverMsg);
|
---|
| 155 |
|
---|
| 156 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
| 157 |
|
---|
| 158 | n = sendMessage(&serverMsg, sock, &from);
|
---|
| 159 | if (n < 0)
|
---|
| 160 | error("sendMessage");
|
---|
| 161 | }
|
---|
[8e540f4] | 162 |
|
---|
| 163 | }
|
---|
[371ce29] | 164 |
|
---|
[8e540f4] | 165 | return 0;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg)
|
---|
| 169 | {
|
---|
| 170 | cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
|
---|
| 171 | cout << "port: " << from.sin_port << endl;
|
---|
| 172 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 173 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 174 |
|
---|
| 175 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 176 | // receive and display the response. Maybe make a special error msg type
|
---|
| 177 | switch(clientMsg.type)
|
---|
| 178 | {
|
---|
| 179 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 180 | {
|
---|
[8e540f4] | 181 | string username(clientMsg.buffer);
|
---|
| 182 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 183 |
|
---|
[8e540f4] | 184 | cout << "username: " << username << endl;
|
---|
| 185 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 186 |
|
---|
[8e540f4] | 187 | strcpy(serverMsg.buffer, "Registration test");
|
---|
| 188 |
|
---|
| 189 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 190 |
|
---|
[8e540f4] | 191 | break;
|
---|
| 192 | }
|
---|
| 193 | case MSG_TYPE_LOGIN:
|
---|
| 194 | {
|
---|
| 195 | string username(clientMsg.buffer);
|
---|
| 196 | cout << "Player logging in: " << username << endl;
|
---|
| 197 |
|
---|
| 198 | Player *p = findPlayerByName(vctPlayers, username);
|
---|
[d2b411a] | 199 |
|
---|
[8e540f4] | 200 | if (p == NULL)
|
---|
| 201 | {
|
---|
[59061f6] | 202 | Player newP(username, "");
|
---|
| 203 | newP.setAddr(from);
|
---|
| 204 |
|
---|
| 205 | vctPlayers.push_back(newP);
|
---|
[8e540f4] | 206 | strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
|
---|
[07028b9] | 207 | }
|
---|
[8e540f4] | 208 | else
|
---|
[07028b9] | 209 | {
|
---|
[8e540f4] | 210 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 211 | }
|
---|
[07028b9] | 212 |
|
---|
[8e540f4] | 213 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[07028b9] | 214 |
|
---|
[8e540f4] | 215 | break;
|
---|
| 216 | }
|
---|
| 217 | case MSG_TYPE_LOGOUT:
|
---|
| 218 | {
|
---|
| 219 | string name(clientMsg.buffer);
|
---|
| 220 | cout << "Player logging out: " << name << endl;
|
---|
| 221 |
|
---|
| 222 | Player *p = findPlayerByName(vctPlayers, name);
|
---|
[633f42a] | 223 |
|
---|
[8e540f4] | 224 | if (p == NULL)
|
---|
| 225 | {
|
---|
| 226 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[07028b9] | 227 | }
|
---|
[8e540f4] | 228 | else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 229 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 230 | {
|
---|
[8e540f4] | 231 | 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.");
|
---|
[2488852] | 232 | }
|
---|
[8e540f4] | 233 | else
|
---|
[2488852] | 234 | {
|
---|
[8e540f4] | 235 | vctPlayers.erase((vector<Player>::iterator)p);
|
---|
| 236 | strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
|
---|
| 237 | }
|
---|
[07028b9] | 238 |
|
---|
[8e540f4] | 239 | break;
|
---|
| 240 | }
|
---|
| 241 | case MSG_TYPE_CHAT:
|
---|
| 242 | {
|
---|
| 243 | Player *p = findPlayerByAddr(vctPlayers, from);
|
---|
[07028b9] | 244 |
|
---|
[8e540f4] | 245 | if (p == NULL)
|
---|
| 246 | {
|
---|
| 247 | 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] | 248 | }
|
---|
[8e540f4] | 249 | else
|
---|
| 250 | {
|
---|
| 251 | int guess = atoi(clientMsg.buffer);
|
---|
| 252 |
|
---|
| 253 | cout << "guess: " << guess << endl;
|
---|
| 254 |
|
---|
| 255 | if (guess < 1 || guess > 1000) {
|
---|
| 256 | strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
|
---|
| 257 | }else if(guess > num)
|
---|
| 258 | strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
|
---|
| 259 | else if(guess < num)
|
---|
| 260 | strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
|
---|
| 261 | else if(guess == num) {
|
---|
| 262 | strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
|
---|
| 263 | num = (rand() % 1000) + 1;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 268 |
|
---|
| 269 | break;
|
---|
[e084950] | 270 | }
|
---|
[8e540f4] | 271 | default:
|
---|
| 272 | {
|
---|
| 273 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 274 |
|
---|
[8e540f4] | 275 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 276 |
|
---|
[8e540f4] | 277 | break;
|
---|
| 278 | }
|
---|
[e3535b3] | 279 | }
|
---|
| 280 | }
|
---|