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